name: benchmark PR comment "on": workflow_run: workflows: - benchmark types: - completed jobs: comment: if: github.event.workflow_run.event == 'pull_request' runs-on: ubuntu-24.04 permissions: actions: read contents: read issues: write pull-requests: write steps: - name: download benchmark PR comment artifact env: FALLBACK_PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number || '' }} GH_TOKEN: ${{ github.token }} REPOSITORY: ${{ github.repository }} RUN_ID: ${{ github.event.workflow_run.id }} RUN_URL: ${{ github.event.workflow_run.html_url }} run: | set -euo pipefail mkdir -p artifact gh api "repos/${REPOSITORY}/actions/runs/${RUN_ID}/artifacts" > artifacts.json artifact_url=$( python -c "import json; from pathlib import Path; artifacts = json.loads(Path('artifacts.json').read_text())['artifacts']; print(next((artifact['archive_download_url'] for artifact in artifacts if artifact['name'] == 'benchmark-pr-comment'), ''))" ) if [ -n "$artifact_url" ]; then curl -LfsS \ -H "Authorization: Bearer ${GH_TOKEN}" \ "$artifact_url" > artifact.zip python -c "from zipfile import ZipFile; ZipFile('artifact.zip').extractall('artifact')" else python -c "import json, os; from pathlib import Path; Path('artifact/comment.md').write_text('\\n## Benchmark report\\n\\n:warning: Benchmark comment artifact not found.\\n\\n- Run: [benchmark workflow run]({})\\n- Result: the benchmark workflow completed, but it did not upload the PR comment artifact.\\n\\n_This benchmark check is informational and does not block merging._\\n'.format(os.environ['RUN_URL'])); Path('artifact/result.json').write_text(json.dumps({'pr_number': os.environ['FALLBACK_PR_NUMBER']}) + '\\n')" fi - name: create or update sticky benchmark comment env: GH_TOKEN: ${{ github.token }} FALLBACK_PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number || '' }} REPOSITORY: ${{ github.repository }} run: | set -euo pipefail PR_NUMBER=$( python -c "import json, os; from pathlib import Path; result_path = Path('artifact/result.json'); pr_number = str(json.loads(result_path.read_text()).get('pr_number', '')) if result_path.exists() else ''; print(pr_number or os.environ.get('FALLBACK_PR_NUMBER', ''))" ) [ -n "$PR_NUMBER" ] gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" > comments.json comment_id=$( python -c "import json; marker = ''; comments = json.load(open('comments.json')); print(next((str(comment['id']) for comment in comments if marker in comment['body']), ''))" ) python -c "import json; from pathlib import Path; body = Path('artifact/comment.md').read_text(); Path('comment-payload.json').write_text(json.dumps({'body': body}))" if [ -n "$comment_id" ]; then gh api \ --method PATCH \ "repos/${REPOSITORY}/issues/comments/${comment_id}" \ --input comment-payload.json else gh api \ --method POST \ "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \ --input comment-payload.json fi