name: Run Integration Tests

on:
  pull_request:
    types: [opened, synchronize, reopened, edited]

jobs:
  should-run:
    name: Check if integration tests should run
    runs-on: ubuntu-latest
    outputs:
      skip: ${{ steps.test-parameters.outputs.skip }}
      dialects: ${{ steps.test-parameters.outputs.dialects }}

    steps:
      - name: Print debugging info
        run: |
          cat <<'EOF'
            Github event name: ${{ github.event_name }}

            Github event ${{ toJSON(github.event) }}

            Github event comment body: ${{ github.event.comment.body }}

            Github event pr body: ${{ github.event.pull_request.body }}

            Generic Number: ${{ github.event.number }}

            PR number: ${{ github.event.pull_request.number }}

            Issue number: ${{ github.event.issue.number }}

            SHA: ${{ github.sha }}

            Head Ref ${{ github.head_ref }}

            Ref Name: ${{ github.ref_name }}
          EOF

      - name: Checkout Code
        uses: actions/checkout@v5
        with:
          # we need to checkout all refs so we can run `git diff`
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.13'

      - name: Check if integration tests should be run
        id: test-parameters
        run: |
          python .github/scripts/get_integration_test_params.py

  run-integration-tests:
    name: Run Integration Tests
    runs-on: ubuntu-latest
    needs: should-run
    if: needs.should-run.outputs.skip == 'false' && github.event.pull_request.head.repo.full_name == github.repository
    steps:
      - name: Acquire credentials
        id: app-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ vars.INTEGRATION_TEST_CLIENT_ID }}
          private-key: ${{ secrets.INTEGRATION_TEST_PRIVATE_KEY }}
          owner: fivetran
          repositories: sqlglot-integration-tests

      - name: Run integration tests
        id: run-remote
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
        run: |
          set -e

          CORRELATION_ID=$(python -c 'import uuid;print(uuid.uuid4())')
          REMOTE_REPO="fivetran/sqlglot-integration-tests"

          echo "Triggering remote workflow"

          gh workflow run run-tests.yml \
            --repo $REMOTE_REPO \
            --ref main \
            -f sqlglot_ref=${{ github.sha }} \
            -f sqlglot_pr_number=${{ github.event.number || github.event.issue.number }} \
            -f sqlglot_branch_name=${{ github.head_ref || github.ref_name }} \
            -f correlation_id="$CORRELATION_ID" \
            -f dialects="${{ needs.should-run.outputs.dialects }}"

          echo "Triggered workflow using correlation id: $CORRELATION_ID"

          # poll for run id
          RUN_ID=""
          ATTEMPTS=0

          while [ "$RUN_ID" == "" ]; do
            sleep 5
            ATTEMPTS=$((ATTEMPTS + 1))
            if [ $ATTEMPTS -gt 10 ]; then
              echo "Timed out waiting for matching run to start"
              exit 1
            fi

            echo "Checking for run"
            RUN_ID=$(gh run list \
              --repo $REMOTE_REPO \
              --event workflow_dispatch \
              --workflow run-tests.yml \
              --user sqlglot-integration-tests \
              --json displayTitle,databaseId \
              --limit 20 \
              -q '.[] | select(.displayTitle | contains("Correlation ID: '"$CORRELATION_ID"'")) | .databaseId')
          done

          echo "Using Run ID: ${RUN_ID}"
          echo "remote_run_id=$RUN_ID" >> $GITHUB_OUTPUT

          echo "Waiting for completion"
          gh run watch $RUN_ID \
            --repo fivetran/sqlglot-integration-tests \
            --interval 10 \
            --compact \
            --exit-status

          # Fail the workflow on this side if the remote workflow fails
          exit $?

      - name: Fetch outputs
        id: fetch-outputs
        uses: actions/download-artifact@v5
        with:
          github-token: ${{ steps.app-token.outputs.token }}
          repository: fivetran/sqlglot-integration-tests
          run-id: ${{ steps.run-remote.outputs.remote_run_id }}
          name: summary

      - name: Write summary as comment
        uses: actions/github-script@v8
        # only do this when on PR branches, main builds dont have anywhere to write comments
        if: github.event_name == 'pull_request' || github.event.issue.pull_request
        with:
          script: |
            // summary.json is downloaded from the remote workflow in the previous step
            const summary = require("./summary.json");

            // Add a unique identifier to find this comment later
            const commentIdentifier = "<!-- integration-test-summary -->";
            const body = `${commentIdentifier}\n${summary.msg}`;

            // Find existing comment
            const { data: comments } = await github.rest.issues.listComments({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
            });

            const existingComment = comments.find(comment =>
              comment.body.includes(commentIdentifier)
            );

            if (existingComment) {
              // Update existing comment
              await github.rest.issues.updateComment({
                comment_id: existingComment.id,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: body
              });
            } else {
              // Create new comment
              await github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: body
              });
            }
