Skip to content

Commit e0d2291

Browse files
committed
Bring back pr actions
Signed-off-by: svrnm <[email protected]>
1 parent d7a61cc commit e0d2291

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

.github/workflows/pr-actions.yml

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

0 commit comments

Comments
 (0)