name: Performance Benchmark

on:
  pull_request:
    branches: [main]

concurrency:
  group: perf-benchmark-${{ github.event.pull_request.number }}
  cancel-in-progress: false

permissions:
  contents: read

jobs:
  post-pending:
    name: Post Pending Comment
    runs-on: ubuntu-latest
    if: ${{ github.event.pull_request.head.repo.fork == false }}
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          sparse-checkout: .github/scripts/perf-benchmark
          persist-credentials: false

      - name: Post or update pending comment
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
          WORKFLOW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
        run: |
          COMMENT_MARKER="<!-- repomix-perf-benchmark -->"

          # Find existing comment by marker
          COMMENT_ID=$(gh api "repos/${GH_REPO}/issues/${PR_NUMBER}/comments" --paginate --jq ".[] | select(.body | startswith(\"${COMMENT_MARKER}\")) | .id" | head -1)

          # Save existing comment body to file for safe parsing
          if [ -n "$COMMENT_ID" ]; then
            gh api "repos/${GH_REPO}/issues/comments/${COMMENT_ID}" --jq '.body' > "$RUNNER_TEMP/old-comment.txt"
          else
            echo "" > "$RUNNER_TEMP/old-comment.txt"
          fi

          # Fetch commit message in shell (avoids escaping issues in Node)
          COMMIT_MSG=$(gh api "repos/${GH_REPO}/commits/${COMMIT_SHA}" --jq '.commit.message | split("\n") | .[0]' 2>/dev/null || echo "")

          COMMIT_MSG="$COMMIT_MSG" node .github/scripts/perf-benchmark/bench-pending.mjs

          BODY=$(cat "$RUNNER_TEMP/new-comment.md")

          if [ -n "$COMMENT_ID" ]; then
            gh api "repos/${GH_REPO}/issues/comments/${COMMENT_ID}" -X PATCH -f body="$BODY"
          else
            gh pr comment "$PR_NUMBER" --body "$BODY"
          fi

  benchmark:
    name: Benchmark (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            runs: 20
          - os: macos-latest
            runs: 30
          - os: windows-latest
            runs: 20
    steps:
      # Checkout PR branch and main branch into separate directories for isolation
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          path: pr-branch
          persist-credentials: false

      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          ref: main
          path: main-branch
          persist-credentials: false

      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version-file: pr-branch/.tool-versions
          cache: npm
          cache-dependency-path: |
            pr-branch/package-lock.json
            main-branch/package-lock.json

      - name: Install and build (PR branch)
        working-directory: pr-branch
        run: npm ci && node --run build

      - name: Install and build (main branch)
        working-directory: main-branch
        run: npm ci && node --run build

      - name: Run benchmark
        shell: bash
        env:
          BENCH_RUNS: ${{ matrix.runs }}
        run: node pr-branch/.github/scripts/perf-benchmark/bench-run.mjs "$GITHUB_WORKSPACE/pr-branch" "$GITHUB_WORKSPACE/main-branch"

      - name: Upload benchmark result
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: bench-result-${{ matrix.os }}
          path: ${{ runner.temp }}/bench-result.json
          retention-days: 1

  comment:
    name: Comment Results
    needs: [benchmark, post-pending]
    runs-on: ubuntu-latest
    if: ${{ always() && !cancelled() }}
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          sparse-checkout: .github/scripts/perf-benchmark
          persist-credentials: false

      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: results
          pattern: bench-result-*

      - name: Comment on PR
        if: ${{ github.event.pull_request.head.repo.fork == false }}
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: ${{ github.repository }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
          WORKFLOW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
        run: |
          COMMENT_MARKER="<!-- repomix-perf-benchmark -->"

          # Find existing comment and save old body to file
          COMMENT_ID=$(gh api "repos/${GH_REPO}/issues/${PR_NUMBER}/comments" --paginate --jq ".[] | select(.body | startswith(\"${COMMENT_MARKER}\")) | .id" | head -1)

          if [ -n "$COMMENT_ID" ]; then
            gh api "repos/${GH_REPO}/issues/comments/${COMMENT_ID}" --jq '.body' > "$RUNNER_TEMP/old-comment.txt"
          else
            echo "" > "$RUNNER_TEMP/old-comment.txt"
          fi

          # Fetch commit message in shell
          COMMIT_MSG=$(gh api "repos/${GH_REPO}/commits/${COMMIT_SHA}" --jq '.commit.message | split("\n") | .[0]' 2>/dev/null || echo "")

          COMMIT_MSG="$COMMIT_MSG" node .github/scripts/perf-benchmark/bench-comment.mjs

          BODY=$(cat "$RUNNER_TEMP/new-comment.md")

          if [ -n "$COMMENT_ID" ]; then
            gh api "repos/${GH_REPO}/issues/comments/${COMMENT_ID}" -X PATCH -f body="$BODY"
          else
            gh pr comment "$PR_NUMBER" --body "$BODY"
          fi
