|
| 1 | +name: Post Perf Results to PR |
| 2 | + |
| 3 | +# Runs after the Perf Tests workflow completes. Uses workflow_run so |
| 4 | +# the GITHUB_TOKEN is scoped to the base repo and can write PR comments |
| 5 | +# even when the PR comes from a fork. |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ['Perf Tests'] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +jobs: |
| 12 | + comment: |
| 13 | + name: Post PR Comment |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: > |
| 16 | + github.event.workflow_run.event == 'pull_request' && |
| 17 | + github.event.workflow_run.conclusion != 'cancelled' |
| 18 | +
|
| 19 | + permissions: |
| 20 | + pull-requests: write |
| 21 | + actions: read |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Download perf results artifact |
| 25 | + uses: actions/download-artifact@v4 |
| 26 | + with: |
| 27 | + name: perf-results |
| 28 | + path: perf-results |
| 29 | + run-id: ${{ github.event.workflow_run.id }} |
| 30 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 31 | + |
| 32 | + - name: Read PR number |
| 33 | + id: pr |
| 34 | + run: | |
| 35 | + if [ ! -f perf-results/pr-number.txt ]; then |
| 36 | + echo "No PR number file found — skipping." |
| 37 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | + PR_NUMBER=$(cat perf-results/pr-number.txt | tr -d '[:space:]') |
| 41 | + echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT" |
| 42 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 43 | +
|
| 44 | + - name: Read markdown report |
| 45 | + if: steps.pr.outputs.skip != 'true' |
| 46 | + id: report |
| 47 | + run: | |
| 48 | + if [ ! -f perf-results/report.md ]; then |
| 49 | + echo "No report.md found — skipping." |
| 50 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 51 | + exit 0 |
| 52 | + fi |
| 53 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 54 | +
|
| 55 | + - name: Post or update PR comment |
| 56 | + if: steps.pr.outputs.skip != 'true' && steps.report.outputs.skip != 'true' |
| 57 | + uses: actions/github-script@v7 |
| 58 | + with: |
| 59 | + script: | |
| 60 | + const fs = require('fs'); |
| 61 | + const marker = '<!-- rnw-perf-results -->'; |
| 62 | + const reportPath = 'perf-results/report.md'; |
| 63 | + const prNumber = parseInt('${{ steps.pr.outputs.number }}', 10); |
| 64 | +
|
| 65 | + const markdown = fs.readFileSync(reportPath, 'utf-8'); |
| 66 | + const body = `${marker}\n${markdown}`; |
| 67 | +
|
| 68 | + // Find existing comment |
| 69 | + const { data: comments } = await github.rest.issues.listComments({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + issue_number: prNumber, |
| 73 | + per_page: 100, |
| 74 | + }); |
| 75 | +
|
| 76 | + const existing = comments.find(c => c.body && c.body.includes(marker)); |
| 77 | +
|
| 78 | + if (existing) { |
| 79 | + await github.rest.issues.updateComment({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + comment_id: existing.id, |
| 83 | + body, |
| 84 | + }); |
| 85 | + core.info(`Updated existing comment (id: ${existing.id})`); |
| 86 | + } else { |
| 87 | + await github.rest.issues.createComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + issue_number: prNumber, |
| 91 | + body, |
| 92 | + }); |
| 93 | + core.info('Created new comment'); |
| 94 | + } |
0 commit comments