Skip to content

Commit 61cd932

Browse files
authored
chore(ci): no more pull_request_target (#8992)
* chore(ci): no more pull_request_target * fixup! * fixup! * fixup! * zizmor justification Signed-off-by: Aviv Keller <me@aviv.sh> * Update leave-comment.yml Signed-off-by: Aviv Keller <me@aviv.sh> * lint Signed-off-by: Aviv Keller <me@aviv.sh> --------- Signed-off-by: Aviv Keller <me@aviv.sh>
1 parent 367f724 commit 61cd932

6 files changed

Lines changed: 244 additions & 146 deletions

File tree

.github/workflows/build.yml

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ jobs:
6565
# this should be a last resort in case by any chances the build memory gets too high
6666
# but in general this should never happen
6767
NODE_OPTIONS: '--max_old_space_size=4096'
68-
# See https://github.com/vercel/next.js/pull/81318
68+
# See https://github.com/vercel/next.js/pull/90949
6969
TURBOPACK_STATS: ${{ matrix.os == 'ubuntu-latest' }}
7070

7171
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
7272
if: matrix.os == 'ubuntu-latest'
7373
with:
7474
name: webpack-stats
75-
path: apps/site/.next/server/webpack-stats.json
75+
path: apps/site/.next/diagnostics/route-bundle-stats.json
7676

7777
- name: Build Next.js (Static Export)
7878
# We want to generate a static build, as it is a requirement of our website.
@@ -85,3 +85,73 @@ jobs:
8585
# We want to ensure that static exports for all locales do not occur on `pull_request` events
8686
# TODO: The output of this is too large, and it crashes the GitHub Runner
8787
NEXT_PUBLIC_STATIC_EXPORT_LOCALE: false # ${{ github.event_name == 'push' }}
88+
89+
compare-bundle-size:
90+
name: Compare Bundle Size
91+
runs-on: ubuntu-latest
92+
needs: build
93+
if: github.event_name == 'pull_request'
94+
95+
steps:
96+
- name: Harden Runner
97+
uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
98+
with:
99+
egress-policy: audit
100+
101+
- name: Git Checkout
102+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
103+
with:
104+
persist-credentials: false
105+
106+
- name: Download Stats (HEAD)
107+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
108+
with:
109+
name: webpack-stats
110+
path: head-stats
111+
112+
- name: Get Run ID from BASE
113+
id: base-run
114+
env:
115+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
116+
GH_TOKEN: ${{ github.token }}
117+
run: |
118+
ID=$(gh run list -c "$BASE_SHA" -w build.yml -s success -L 1 --json databaseId --jq ".[].databaseId")
119+
echo "run_id=$ID" >> "$GITHUB_OUTPUT"
120+
121+
- name: Download Stats (BASE)
122+
id: base-stats
123+
continue-on-error: true
124+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
125+
with:
126+
name: webpack-stats
127+
path: base-stats
128+
run-id: ${{ steps.base-run.outputs.run_id }}
129+
github-token: ${{ secrets.GITHUB_TOKEN }}
130+
131+
- name: Compare Bundle Size
132+
id: compare-bundle-size
133+
if: steps.base-stats.outcome == 'success'
134+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
135+
env:
136+
HEAD_STATS_PATH: ./head-stats/route-bundle-stats.json
137+
BASE_STATS_PATH: ./base-stats/route-bundle-stats.json
138+
with:
139+
script: |
140+
const { compare } = await import('${{github.workspace}}/apps/site/scripts/compare-size/index.mjs')
141+
await compare({core})
142+
143+
- name: Prepare Comment
144+
if: steps.base-stats.outcome == 'success'
145+
env:
146+
COMMENT: ${{ steps.compare-bundle-size.outputs.comment }}
147+
run: |
148+
mkdir -p pr-comment
149+
printf '%s' "$COMMENT" > pr-comment/comment.md
150+
printf '%s' 'compare_bundle_size' > pr-comment/tag.txt
151+
152+
- name: Upload Comment
153+
if: steps.base-stats.outcome == 'success'
154+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
155+
with:
156+
name: pr-comment
157+
path: pr-comment/

.github/workflows/bundle-compare.yml

Lines changed: 0 additions & 77 deletions
This file was deleted.

.github/workflows/chromatic.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ on:
1414
paths:
1515
- packages/ui-components/**
1616
- .github/workflows/chromatic.yml
17-
pull_request_target:
17+
pull_request:
1818
branches:
1919
- main
2020
paths:
2121
- packages/ui-components/**
2222
- .github/workflows/chromatic.yml
23-
types:
24-
- labeled
2523
workflow_dispatch:
2624

2725
defaults:
@@ -42,9 +40,8 @@ jobs:
4240
# We only need to run Storybook Builds and Storybook Visual Regression Tests within Pull Requests that actually
4341
# introduce changes to the Storybook. Hence, we skip running these on Crowdin PRs and Dependabot PRs
4442
if: |
45-
github.event_name != 'pull_request_target' ||
43+
github.event_name != 'pull_request' ||
4644
(
47-
github.event.label.name == 'github_actions:pull-request' &&
4845
github.actor != 'dependabot[bot]' &&
4946
github.event.pull_request.head.ref != 'chore/crowdin'
5047
)
@@ -66,10 +63,10 @@ jobs:
6663
- name: Start Visual Regression Tests (Chromatic)
6764
# This assigns the Environment Deployment for Storybook
6865
id: chromatic-deploy
69-
uses: chromaui/action@c49581ed7e45543eef2b5d727cf97a688a83e7d8 # v17.6.0
66+
uses: chromaui/action@94713c544284a14195de3b50ef24301579f1877e # v18.0.1
7067
with:
7168
workingDir: packages/ui-components
7269
buildScriptName: storybook:build
73-
projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
70+
projectToken: ${{ vars.CHROMATIC_PROJECT_TOKEN }}
7471
exitOnceUploaded: true
7572
onlyChanged: true
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Leave Comment
2+
3+
on:
4+
workflow_run: # zizmor: ignore[dangerous-triggers]
5+
# While it's true (as reported by Zizmor) that many `workflow_run`
6+
# workflows are unsafe, we are using it purely to paste a code comment
7+
# on a pull request. If an attacker were to attempt to modify the _only_
8+
# file of data we collect from their unsafe code, the worst they could do
9+
# is leave a comment on their own PR, which has no harmful impact
10+
# whatsover.
11+
#
12+
# Any Workflow that uploads a `pr-comment` artifact should be listed here
13+
workflows: ['Build', 'Lighthouse']
14+
types: [completed]
15+
16+
permissions:
17+
contents: read
18+
actions: read
19+
20+
concurrency:
21+
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
22+
cancel-in-progress: true
23+
24+
jobs:
25+
leave-comment:
26+
name: Leave Comment
27+
runs-on: ubuntu-latest
28+
permissions:
29+
pull-requests: write
30+
31+
steps:
32+
- name: Harden Runner
33+
uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
34+
with:
35+
egress-policy: audit
36+
37+
- name: Download Comment Artifact
38+
# The Workflow may not have produced a comment (e.g. the comparison was skipped), so this is
39+
# allowed to fail and every subsequent step is gated on it having succeeded.
40+
id: download
41+
continue-on-error: true
42+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
43+
with:
44+
name: pr-comment
45+
path: pr-comment
46+
run-id: ${{ github.event.workflow_run.id }}
47+
github-token: ${{ secrets.GITHUB_TOKEN }}
48+
49+
- name: Resolve Pull Request Number
50+
id: pr
51+
if: steps.download.outcome == 'success'
52+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
53+
with:
54+
script: |
55+
const run = context.payload.workflow_run;
56+
57+
// 1. For same-repo Pull Requests the run is already linked to its PR(s).
58+
if (run.pull_requests && run.pull_requests.length) {
59+
core.setOutput('number', run.pull_requests[0].number);
60+
return;
61+
}
62+
63+
// 2. For forks that list is empty, so find the open Pull Request who has the
64+
// correct branch information
65+
const match = await github.rest.pulls.list({
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
state: 'open',
69+
head: `${context.payload.workflow_run.head_repository.owner.login}:${context.payload.workflow_run.head_branch}`,
70+
sort: 'updated',
71+
direction: 'desc',
72+
per_page: 1,
73+
}).then(r => r.data[0]);
74+
75+
if (!match) {
76+
core.info(`No open pull request found for HEAD ${run.head_sha}`);
77+
return;
78+
}
79+
80+
core.setOutput('number', match.number);
81+
82+
- name: Read Comment Tag
83+
id: meta
84+
if: steps.download.outcome == 'success'
85+
run: |
86+
tag="$(tr -cd 'A-Za-z0-9_-' < pr-comment/tag.txt)"
87+
echo "tag=$tag" >> "$GITHUB_OUTPUT"
88+
89+
- name: Add Comment to PR
90+
# The comment body is untrusted markdown, so it is passed as a file (data) rather than
91+
# interpolated into an expression or shell command.
92+
if: steps.download.outcome == 'success' && steps.pr.outputs.number != ''
93+
uses: thollander/actions-comment-pull-request@e2c37e53a7d2227b61585343765f73a9ca57eda9 # v3.0.0
94+
with:
95+
file-path: pr-comment/comment.md
96+
comment-tag: ${{ steps.meta.outputs.tag }}
97+
pr-number: ${{ steps.pr.outputs.number }}

.github/workflows/lighthouse.yml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Security Notes
2-
# This workflow uses `pull_request_target`, so will run against all PRs automatically (without approval), be careful with allowing any user-provided code to be run here
2+
# This Workflow runs in the untrusted `pull_request` context and therefore must not rely on any
3+
# repository secrets. It does not comment on the Pull Request itself; instead it uploads a
4+
# `pr-comment` artifact which the trusted `Leave Comment` Workflow posts once this Workflow completes.
35
# Only selected Actions are allowed within this repository. Please refer to (https://github.com/nodejs/nodejs.org/settings/actions)
46
# for the full list of available actions. If you want to add a new one, please reach out a maintainer with Admin permissions.
57
# REVIEWERS, please always double-check security practices before merging a PR that contains Workflow changes!!
@@ -9,7 +11,7 @@
911
name: Lighthouse
1012

1113
on:
12-
pull_request_target:
14+
pull_request:
1315
branches:
1416
- main
1517
types:
@@ -36,9 +38,6 @@ jobs:
3638
github.event.label.name == 'github_actions:pull-request'
3739
name: Lighthouse Report
3840
runs-on: ubuntu-latest
39-
permissions:
40-
# Required by `thollander/actions-comment-pull-request`
41-
pull-requests: write
4241

4342
steps:
4443
- name: Harden Runner
@@ -55,21 +54,11 @@ jobs:
5554
check_interval: 10 # check every 10 seconds
5655

5756
- name: Git Checkout
57+
# Only needed for the Lighthouse formatting script; no credentials are persisted.
5858
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
5959
with:
60-
# Provides the Pull Request commit SHA or the GitHub merge group ref
61-
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.ref }}
6260
persist-credentials: false
6361

64-
- name: Add Comment to PR
65-
# Signal that a lighthouse run is about to start
66-
uses: thollander/actions-comment-pull-request@e2c37e53a7d2227b61585343765f73a9ca57eda9 # v3.0.0
67-
with:
68-
message: |
69-
Running Lighthouse audit...
70-
# Used later to edit the existing comment
71-
comment-tag: 'lighthouse_audit'
72-
7362
- name: Audit Preview URL with Lighthouse
7463
# Conduct the lighthouse audit
7564
id: lighthouse_audit
@@ -105,11 +94,16 @@ jobs:
10594
const { formatLighthouseResults } = await import('${{github.workspace}}/apps/site/scripts/lighthouse/index.mjs')
10695
await formatLighthouseResults({core})
10796
108-
- name: Add Comment to PR
109-
# Replace the previous message with our formatted lighthouse results
110-
uses: thollander/actions-comment-pull-request@e2c37e53a7d2227b61585343765f73a9ca57eda9 # v3.0.0
97+
- name: Prepare Comment
98+
env:
99+
COMMENT: ${{ steps.format_lighthouse_score.outputs.comment }}
100+
run: |
101+
mkdir -p pr-comment
102+
printf '%s' "$COMMENT" > pr-comment/comment.md
103+
printf '%s' 'lighthouse_audit' > pr-comment/tag.txt
104+
105+
- name: Upload Comment
106+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
111107
with:
112-
# Reference the previously created comment
113-
comment-tag: 'lighthouse_audit'
114-
message: |
115-
${{ steps.format_lighthouse_score.outputs.comment }}
108+
name: pr-comment
109+
path: pr-comment/

0 commit comments

Comments
 (0)