Skip to content

Commit 27d3be2

Browse files
authored
fix: perf+size steps don't run on forks outside of facebook repo (#481)
1 parent 07d1623 commit 27d3be2

2 files changed

Lines changed: 84 additions & 32 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: benchmarks-comment
2+
3+
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
4+
5+
on:
6+
workflow_run:
7+
workflows: ['benchmarks']
8+
types: [completed]
9+
10+
permissions:
11+
pull-requests: write
12+
contents: read
13+
actions: read
14+
15+
jobs:
16+
comment:
17+
runs-on: ubuntu-latest
18+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
include:
23+
- artifact: benchmarks-size
24+
marker: '<!-- workflow-benchmarks-size-data -->'
25+
heading: 'workflow: benchmarks/size'
26+
description: 'Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.'
27+
- artifact: benchmarks-perf
28+
marker: '<!-- workflow-benchmarks-perf-data -->'
29+
heading: 'workflow: benchmarks/perf (native)'
30+
description: 'Comparison of performance test results, measured in operations per second. Larger is better.'
31+
steps:
32+
- name: 'Download artifact'
33+
uses: actions/download-artifact@v5
34+
with:
35+
name: ${{ matrix.artifact }}
36+
github-token: ${{ secrets.GITHUB_TOKEN }}
37+
run-id: ${{ github.event.workflow_run.id }}
38+
- name: 'Read PR number and table'
39+
id: read
40+
run: |
41+
# Validate the PR number is a positive integer to avoid any injection
42+
# via the artifact (artifact contents come from a fork-triggered job).
43+
pr_number="$(cat pr-number.txt | tr -d '[:space:]')"
44+
if ! [[ "$pr_number" =~ ^[0-9]+$ ]]; then
45+
echo "Invalid PR number in artifact: $pr_number" >&2
46+
exit 1
47+
fi
48+
echo "pr-number=$pr_number" >> "$GITHUB_OUTPUT"
49+
{
50+
echo 'table<<MARKDOWN_EOF'
51+
cat table.md
52+
echo 'MARKDOWN_EOF'
53+
} >> "$GITHUB_OUTPUT"
54+
- name: 'Post comment'
55+
uses: edumserrano/find-create-or-update-comment@v3
56+
with:
57+
issue-number: ${{ steps.read.outputs.pr-number }}
58+
body-includes: ${{ matrix.marker }}
59+
comment-author: 'github-actions[bot]'
60+
body: |
61+
${{ matrix.marker }}
62+
### ${{ matrix.heading }}
63+
${{ matrix.description }}
64+
${{ steps.read.outputs.table }}
65+
edit-mode: replace

.github/workflows/benchmarks.yml

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: benchmarks
22

33
on: [pull_request]
44

5+
permissions:
6+
contents: read
7+
58
jobs:
69
size:
710
runs-on: ubuntu-latest
@@ -33,24 +36,16 @@ jobs:
3336
npm run size -w benchmarks -- -o ${{ env.PATCH_JSON }}
3437
echo "Ran successfully on patch branch"
3538
- name: 'Collect results'
36-
id: collect
3739
run: |
38-
echo "table<<EOF" >> $GITHUB_OUTPUT
39-
node packages/benchmarks/compare.js ${{ env.BASE_JSON }} ${{ env.PATCH_JSON }} >> markdown
40-
cat markdown >> $GITHUB_OUTPUT
41-
echo "EOF" >> $GITHUB_OUTPUT
42-
- name: 'Post comment'
43-
uses: edumserrano/find-create-or-update-comment@v3
40+
mkdir -p benchmark-output
41+
echo "${{ github.event.pull_request.number }}" > benchmark-output/pr-number.txt
42+
node packages/benchmarks/compare.js ${{ env.BASE_JSON }} ${{ env.PATCH_JSON }} > benchmark-output/table.md
43+
- name: 'Upload results'
44+
uses: actions/upload-artifact@v4
4445
with:
45-
issue-number: ${{ github.event.pull_request.number }}
46-
body-includes: '<!-- workflow-benchmarks-size-data -->'
47-
comment-author: 'github-actions[bot]'
48-
body: |
49-
<!-- workflow-benchmarks-size-data -->
50-
### workflow: benchmarks/size
51-
Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.
52-
${{ steps.collect.outputs.table }}
53-
edit-mode: replace
46+
name: benchmarks-size
47+
path: benchmark-output/
48+
retention-days: 1
5449

5550
perf:
5651
runs-on: ubuntu-latest
@@ -82,21 +77,13 @@ jobs:
8277
npm run perf -w benchmarks -- -o ${{ env.PATCH_JSON }}
8378
echo "Ran successfully on patch branch"
8479
- name: 'Collect results'
85-
id: collect
8680
run: |
87-
echo "table<<EOF" >> $GITHUB_OUTPUT
88-
node packages/benchmarks/compare.js ${{ env.BASE_JSON }} ${{ env.PATCH_JSON }} >> markdown
89-
cat markdown >> $GITHUB_OUTPUT
90-
echo "EOF" >> $GITHUB_OUTPUT
91-
- name: 'Post comment'
92-
uses: edumserrano/find-create-or-update-comment@v3
81+
mkdir -p benchmark-output
82+
echo "${{ github.event.pull_request.number }}" > benchmark-output/pr-number.txt
83+
node packages/benchmarks/compare.js ${{ env.BASE_JSON }} ${{ env.PATCH_JSON }} > benchmark-output/table.md
84+
- name: 'Upload results'
85+
uses: actions/upload-artifact@v4
9386
with:
94-
issue-number: ${{ github.event.pull_request.number }}
95-
body-includes: '<!-- workflow-benchmarks-perf-data -->'
96-
comment-author: 'github-actions[bot]'
97-
body: |
98-
<!-- workflow-benchmarks-perf-data -->
99-
### workflow: benchmarks/perf (native)
100-
Comparison of performance test results, measured in operations per second. Larger is better.
101-
${{ steps.collect.outputs.table }}
102-
edit-mode: replace
87+
name: benchmarks-perf
88+
path: benchmark-output/
89+
retention-days: 1

0 commit comments

Comments
 (0)