# verified is advisory: the tests passed in a separate verify job.
# A reviewer hint, not proof. No automation may use it to merge, approve,
# enable auto-merge or skip human review. Every PR is titled "[needs review] ...".
name: Autofix (human review required)
on:
  workflow_run:
    workflows:
      - REPLACE_WITH_TEST_WORKFLOW_NAME
    types:
      - completed
permissions: {}
env:
  SHIPLIGHT_API_URL: ${{ vars.SHIPLIGHT_API_URL }}
  AUTOFIX_RUNNER_VERSION: REPLACE_WITH_CURRENT_VERSION
  AUTOFIX_RUNNER_SHA256: REPLACE_WITH_CURRENT_SHA256
  AUTOFIX_WORKING_DIRECTORY: .
  AUTOFIX_ALLOWED_PATHS: tests templates
jobs:
  prepare:
    if: ${{ github.event.workflow_run.head_repository.full_name == github.repository && (github.event.workflow_run.conclusion == 'failure' || github.event.workflow_run.conclusion == 'timed_out') }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    outputs:
      tasks: ${{ steps.prepare.outputs.tasks }}
      base_sha: ${{ steps.prepare.outputs.base_sha }}
    env:
      AUTOFIX_API_TOKEN: ${{ secrets.AUTOFIX_API_TOKEN }}
      AUTOFIX_GITHUB_READ_TOKEN: ${{ github.token }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.repository.default_branch }}
          persist-credentials: false
      - name: Download pinned runner and prepare
        id: prepare
        shell: bash
        run: |
          set -euo pipefail
          : "${SHIPLIGHT_API_URL:?Set the repository variable SHIPLIGHT_API_URL}"
          runner="$RUNNER_TEMP/autofix/autofix-runner.sh"
          mkdir -p "${runner%/*}"
          code=$(curl -q -sS --proto '=https' -H "Authorization: Bearer $AUTOFIX_API_TOKEN" \
            -o "$runner" -w '%{http_code}' "$SHIPLIGHT_API_URL/v1/autofix/runner/$AUTOFIX_RUNNER_VERSION")
          if [ "$code" = 403 ] && jq -e '.error == "autofix_disabled"' "$runner" >/dev/null; then
            echo "::notice::Autofix is off for this org; nothing to do."
            echo 'tasks=[]' >> "$GITHUB_OUTPUT"
            exit 0
          fi
          [ "$code" = 200 ] || { echo "Runner download failed (HTTP $code)" >&2; exit 1; }
          echo "$AUTOFIX_RUNNER_SHA256  $runner" | sha256sum -c -
          bash "$runner" prepare
      - uses: actions/upload-artifact@v4
        with:
          name: autofix-prepare
          path: ${{ runner.temp }}/autofix/prepare
          if-no-files-found: ignore
          retention-days: 1
  fix:
    needs: prepare
    if: ${{ github.event.workflow_run.head_repository.full_name == github.repository && needs.prepare.outputs.tasks != '[]' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        task: ${{ fromJSON(needs.prepare.outputs.tasks) }}
    env:
      CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.prepare.outputs.base_sha }}
          persist-credentials: false
      - uses: actions/download-artifact@v4
        with:
          name: autofix-prepare
          path: ${{ runner.temp }}/autofix/prepare
      - name: Check pinned runner and fix
        shell: bash
        run: |
          set -euo pipefail
          runner="$RUNNER_TEMP/autofix/prepare/autofix-runner.sh"
          echo "$AUTOFIX_RUNNER_SHA256  $runner" | sha256sum -c -
          bash "$runner" fix
        env:
          AUTOFIX_TASK_ID: ${{ matrix.task.taskId }}
      - uses: actions/upload-artifact@v4
        with:
          name: autofix-candidate-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/candidate
          include-hidden-files: true
          if-no-files-found: ignore
          retention-days: 1
        if: ${{ !cancelled() }}
  verify:
    needs:
      - prepare
      - fix
    if: ${{ !cancelled() && needs.prepare.result == 'success' && github.event.workflow_run.head_repository.full_name == github.repository && needs.prepare.outputs.tasks != '[]' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        task: ${{ fromJSON(needs.prepare.outputs.tasks) }}
    env:
      SHIPLIGHT_API_TOKEN: ${{ secrets.SHIPLIGHT_API_TOKEN }}
      AUTOFIX_TEST_COMMAND: npx shiplight test
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.prepare.outputs.base_sha }}
          persist-credentials: false
      - uses: actions/setup-node@v4
        with:
          node-version: '24'
      - name: Install project dependencies and browsers (adapt to the lockfile)
        working-directory: ${{ env.AUTOFIX_WORKING_DIRECTORY }}
        run: |
          npm ci
          npx playwright install --with-deps chromium
      - uses: actions/download-artifact@v4
        with:
          name: autofix-prepare
          path: ${{ runner.temp }}/autofix/prepare
      - uses: actions/download-artifact@v4
        with:
          name: autofix-candidate-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/candidate
        continue-on-error: true
      - name: Check pinned runner and verify
        shell: bash
        run: |
          set -euo pipefail
          runner="$RUNNER_TEMP/autofix/prepare/autofix-runner.sh"
          echo "$AUTOFIX_RUNNER_SHA256  $runner" | sha256sum -c -
          bash "$runner" verify
        env:
          AUTOFIX_TASK_ID: ${{ matrix.task.taskId }}
      - uses: actions/upload-artifact@v4
        with:
          name: autofix-verification-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/verification
          if-no-files-found: ignore
          retention-days: 1
        if: ${{ !cancelled() }}
  publish:
    needs:
      - prepare
      - fix
      - verify
    if: ${{ !cancelled() && needs.prepare.result == 'success' && github.event.workflow_run.head_repository.full_name == github.repository && needs.prepare.outputs.tasks != '[]' }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    strategy:
      fail-fast: false
      matrix:
        task: ${{ fromJSON(needs.prepare.outputs.tasks) }}
    env:
      AUTOFIX_API_TOKEN: ${{ secrets.AUTOFIX_API_TOKEN }}
      AUTOFIX_PUBLISH_TOKEN: ${{ github.token }}
    concurrency:
      group: autofix-publish-${{ github.repository }}-${{ matrix.task.slug }}
      cancel-in-progress: false
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: autofix-prepare
          path: ${{ runner.temp }}/autofix/prepare
      - uses: actions/download-artifact@v4
        with:
          name: autofix-candidate-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/candidate
        continue-on-error: true
      - uses: actions/download-artifact@v4
        with:
          name: autofix-verification-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/verification
        continue-on-error: true
      - name: Check pinned runner and publish
        shell: bash
        run: |
          set -euo pipefail
          runner="$RUNNER_TEMP/autofix/prepare/autofix-runner.sh"
          echo "$AUTOFIX_RUNNER_SHA256  $runner" | sha256sum -c -
          bash "$runner" publish
        env:
          AUTOFIX_TASK_ID: ${{ matrix.task.taskId }}
      - uses: actions/upload-artifact@v4
        with:
          name: autofix-result-${{ matrix.task.taskId }}
          path: ${{ runner.temp }}/autofix/*.json
          if-no-files-found: ignore
          retention-days: 1
        if: ${{ !cancelled() }}
