| 1 |
name: benchmark PR comment |
| 2 |
|
| 3 |
"on": |
| 4 |
workflow_run: |
| 5 |
workflows: |
| 6 |
- benchmark |
| 7 |
types: |
| 8 |
- completed |
| 9 |
|
| 10 |
jobs: |
| 11 |
comment: |
| 12 |
if: github.event.workflow_run.event == 'pull_request' |
| 13 |
runs-on: ubuntu-24.04 |
| 14 |
permissions: |
| 15 |
actions: read |
| 16 |
contents: read |
| 17 |
issues: write |
| 18 |
pull-requests: write |
| 19 |
steps: |
| 20 |
- name: download benchmark PR comment artifact |
| 21 |
env: |
| 22 |
FALLBACK_PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number || '' }} |
| 23 |
GH_TOKEN: ${{ github.token }} |
| 24 |
REPOSITORY: ${{ github.repository }} |
| 25 |
RUN_ID: ${{ github.event.workflow_run.id }} |
| 26 |
RUN_URL: ${{ github.event.workflow_run.html_url }} |
| 27 |
run: | |
| 28 |
set -euo pipefail |
| 29 |
mkdir -p artifact |
| 30 |
gh api "repos/${REPOSITORY}/actions/runs/${RUN_ID}/artifacts" > artifacts.json |
| 31 |
artifact_url=$( |
| 32 |
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'), ''))" |
| 33 |
) |
| 34 |
if [ -n "$artifact_url" ]; then |
| 35 |
curl -LfsS \ |
| 36 |
-H "Authorization: Bearer ${GH_TOKEN}" \ |
| 37 |
"$artifact_url" > artifact.zip |
| 38 |
python -c "from zipfile import ZipFile; ZipFile('artifact.zip').extractall('artifact')" |
| 39 |
else |
| 40 |
python -c "import json, os; from pathlib import Path; Path('artifact/comment.md').write_text('<!-- bidict-benchmark-comment -->\\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')" |
| 41 |
fi |
| 42 |
- name: create or update sticky benchmark comment |
| 43 |
env: |
| 44 |
GH_TOKEN: ${{ github.token }} |
| 45 |
FALLBACK_PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number || '' }} |
| 46 |
REPOSITORY: ${{ github.repository }} |
| 47 |
run: | |
| 48 |
set -euo pipefail |
| 49 |
PR_NUMBER=$( |
| 50 |
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', ''))" |
| 51 |
) |
| 52 |
[ -n "$PR_NUMBER" ] |
| 53 |
gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" > comments.json |
| 54 |
comment_id=$( |
| 55 |
python -c "import json; marker = '<!-- bidict-benchmark-comment -->'; comments = json.load(open('comments.json')); print(next((str(comment['id']) for comment in comments if marker in comment['body']), ''))" |
| 56 |
) |
| 57 |
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}))" |
| 58 |
if [ -n "$comment_id" ]; then |
| 59 |
gh api \ |
| 60 |
--method PATCH \ |
| 61 |
"repos/${REPOSITORY}/issues/comments/${comment_id}" \ |
| 62 |
--input comment-payload.json |
| 63 |
else |
| 64 |
gh api \ |
| 65 |
--method POST \ |
| 66 |
"repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \ |
| 67 |
--input comment-payload.json |
| 68 |
fi |