Skip to content

Commit 5a5462f

Browse files
svrnmtrask
andauthored
Bring back pr actions (#6894)
Signed-off-by: svrnm <[email protected]> Co-authored-by: Trask Stalnaker <[email protected]>
1 parent 940e1a4 commit 5a5462f

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

.github/workflows/pr-actions.yml

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: PR actions
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
COMMENT: ${{ github.event.comment.body }}
12+
PR_NUM: ${{ github.event.issue.number }}
13+
USER_EMAIL: [email protected]
14+
USER_NAME: opentelemetrybot
15+
MAX_PATCH_SIZE_KB: 1024
16+
17+
jobs:
18+
generate-patch:
19+
name: Run fixer and generate patch (untrusted)
20+
runs-on: ubuntu-latest
21+
if: |
22+
github.event.issue.pull_request &&
23+
startsWith(github.event.comment.body, '/fix:')
24+
25+
outputs:
26+
action_name: ${{ steps.extract.outputs.action_name }}
27+
patch_name: pr-fix-${{ github.run_id }}
28+
patch_skipped: ${{ steps.check_patch.outputs.skipped }}
29+
30+
steps:
31+
- uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
32+
with:
33+
egress-policy: audit
34+
35+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
36+
with:
37+
fetch-depth: 999
38+
39+
- name: Extract action name
40+
id: extract
41+
run: |
42+
PR_ACTION=$(echo "$COMMENT" | grep -oP '/fix:\K[:-_0-9a-z]+')
43+
echo "action_name=$PR_ACTION" >> "$GITHUB_OUTPUT"
44+
45+
- run: gh pr checkout $PR_NUM -b "pr-action-${RANDOM}"
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
49+
- name: Setup Node
50+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
51+
with:
52+
node-version-file: .nvmrc
53+
54+
- name: Install deps & run fixer
55+
run: |
56+
npm install --omit=optional
57+
npm run fix:${{ steps.extract.outputs.action_name }}
58+
59+
- name: Generate and validate patch
60+
id: check_patch
61+
run: |
62+
git diff > pr-fix.patch
63+
64+
if [ ! -s pr-fix.patch ]; then
65+
echo "No changes detected. Skipping patch."
66+
echo "skipped=true" >> "$GITHUB_OUTPUT"
67+
exit 0
68+
fi
69+
70+
actual_size_kb=$(du -k pr-fix.patch | cut -f1)
71+
if (( actual_size_kb > MAX_PATCH_SIZE_KB )); then
72+
echo "Patch too large: ${actual_size_kb} KB (limit: ${MAX_PATCH_SIZE_KB} KB)"
73+
exit 1
74+
fi
75+
76+
echo "skipped=false" >> "$GITHUB_OUTPUT"
77+
78+
- name: Upload patch artifact
79+
if: steps.check_patch.outputs.skipped != 'true'
80+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
81+
with:
82+
name: pr-fix-${{ github.run_id }}
83+
path: pr-fix.patch
84+
retention-days: 1
85+
86+
apply-patch:
87+
name: Apply and push patch (trusted)
88+
runs-on: ubuntu-latest
89+
needs: generate-patch
90+
if: needs.generate-patch.outputs.patch_skipped != 'true'
91+
permissions:
92+
contents: write
93+
pull-requests: write
94+
95+
steps:
96+
- uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
97+
with:
98+
egress-policy: audit
99+
100+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
101+
with:
102+
fetch-depth: 999
103+
104+
- run: gh pr checkout $PR_NUM -b "pr-action-${RANDOM}"
105+
env:
106+
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
107+
108+
- name: Download patch
109+
uses: actions/download-artifact@v4
110+
with:
111+
name: ${{ needs.generate-patch.outputs.patch_name }}
112+
113+
- name: Apply patch
114+
run: |
115+
git apply --check pr-fix.patch && git apply pr-fix.patch || {
116+
echo "Patch failed to apply"
117+
exit 1
118+
}
119+
120+
- name: Commit and push changes, if any
121+
run: |
122+
git config --local user.email "$USER_EMAIL"
123+
git config --local user.name "$USER_NAME"
124+
if [[ $(git status --porcelain) ]]; then
125+
git add -A
126+
current_branch=$(git rev-parse --abbrev-ref HEAD)
127+
echo current_branch=$current_branch
128+
# gh pr checkout sets some git configs that we can use to make sure
129+
# we push to the right repo & to the right branch
130+
remote_repo=$(git config --get branch.${current_branch}.remote)
131+
echo remote_repo=$remote_repo
132+
remote_branch=$(git config --get branch.${current_branch}.merge)
133+
echo remote_branch=$remote_branch
134+
git commit -m "Results from /fix:${PR_ACTION}"
135+
git push ${remote_repo} HEAD:${remote_branch}
136+
else
137+
echo "No changes to commit"
138+
fi
139+
env:
140+
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
141+
142+
- name: Comment success
143+
if: ${{ !failure() && !cancelled() }}
144+
run: |
145+
gh pr comment $PR_NUM --body "✅ \`fix:${{ needs.generate-patch.outputs.action_name }}\` applied successfully in [this run]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)."
146+
env:
147+
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
148+
149+
- name: Comment failure
150+
if: ${{ failure() || cancelled() }}
151+
run: |
152+
gh pr comment $PR_NUM --body "❌ \`fix:${{ needs.generate-patch.outputs.action_name }}\` failed. See logs: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
153+
env:
154+
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
155+
156+
notify-noop:
157+
name: Comment no-op patch
158+
runs-on: ubuntu-latest
159+
if: needs.generate-patch.outputs.patch_skipped == 'true'
160+
needs: generate-patch
161+
steps:
162+
- name: Comment no-op
163+
run: |
164+
gh pr comment $PR_NUM --body "ℹ️ \`fix:${{ needs.generate-patch.outputs.action_name }}\` made no changes – nothing to commit."
165+
env:
166+
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}

0 commit comments

Comments
 (0)