-
Notifications
You must be signed in to change notification settings - Fork 9
ci(perf): post sticky comment for fork PRs via workflow_run #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JacksonWeber
merged 2 commits into
microsoft:main
from
JacksonWeber:fix-perf-sticky-comment-for-forks
Jun 4, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| name: Performance Comment | ||
|
|
||
| # Posts the perf comparison as a sticky PR comment. | ||
| # | ||
| # This runs in the *base* repository's context via `workflow_run`, which | ||
| # means it has a writable `GITHUB_TOKEN` even for pull requests opened from | ||
| # forks. The Performance workflow itself runs in the (potentially untrusted) | ||
| # PR context and only produces an artifact — no PR write permissions there. | ||
| # | ||
| # Security model: | ||
| # * The artifact from the PR-context workflow is treated as untrusted | ||
| # input. We only consume the JSON benchmark outputs (`base.json` / | ||
| # `pr.json`) and the PR number, and we regenerate `report.md` here | ||
| # using the base repository's checked-out `perf.compare` so the | ||
| # markdown posted under the writable token is never attacker-supplied. | ||
| # * The PR number is validated as a positive integer before being used. | ||
|
|
||
| on: | ||
| workflow_run: | ||
| workflows: ["Performance"] | ||
| types: | ||
| - completed | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| actions: read | ||
|
|
||
| jobs: | ||
| comment: | ||
| name: Post sticky PR comment | ||
| runs-on: ubuntu-latest | ||
| if: github.event.workflow_run.event == 'pull_request' | ||
|
|
||
| steps: | ||
| - name: Check out base repository (trusted) | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Download perf-results artifact | ||
| id: download | ||
| # Don't fail this job if the upstream Performance run was cancelled | ||
| # or failed before producing the artifact — we just skip posting. | ||
| continue-on-error: true | ||
| uses: actions/download-artifact@v4 | ||
| with: | ||
| name: perf-results | ||
| path: perf-results | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Resolve PR number | ||
| id: pr | ||
| if: steps.download.outcome == 'success' | ||
| run: | | ||
| set -euo pipefail | ||
| pr="" | ||
| if [ -f perf-results/pr-number.txt ]; then | ||
| pr="$(tr -d '[:space:]' < perf-results/pr-number.txt)" | ||
| fi | ||
|
|
||
| if ! [[ "$pr" =~ ^[1-9][0-9]*$ ]]; then | ||
| echo "pr-number.txt missing or not a positive integer (got: '${pr}'); skipping comment." >&2 | ||
| echo "number=" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "number=$pr" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Set up Python | ||
| if: steps.pr.outputs.number != '' | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.12" | ||
|
|
||
| - name: Regenerate report from JSON (trusted code) | ||
| id: report | ||
| if: steps.pr.outputs.number != '' | ||
| run: | | ||
| set -euo pipefail | ||
| if [ ! -s perf-results/base.json ] || [ ! -s perf-results/pr.json ]; then | ||
| echo "base.json or pr.json missing from artifact; skipping comment." >&2 | ||
| echo "found=false" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Use the base repo's perf.compare (trusted) to render the markdown | ||
| # from the PR-supplied JSON data, so we never post attacker-supplied | ||
| # markdown under the base repo's writable token. | ||
| python -m perf.compare \ | ||
| --baseline perf-results/base.json \ | ||
| --candidate perf-results/pr.json \ | ||
| --threshold "${PERF_REGRESSION_THRESHOLD:-15}" \ | ||
| --output report.md \ | ||
| || true | ||
|
|
||
| if [ -s report.md ]; then | ||
| echo "found=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "found=false" >> "$GITHUB_OUTPUT" | ||
| echo "report.md was not produced; skipping comment." >&2 | ||
| fi | ||
|
|
||
| - name: Post sticky PR comment | ||
| if: steps.pr.outputs.number != '' && steps.report.outputs.found == 'true' | ||
| uses: marocchino/sticky-pull-request-comment@v2 | ||
| with: | ||
| header: perf-comparison | ||
| number: ${{ steps.pr.outputs.number }} | ||
| path: report.md | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.