-
Notifications
You must be signed in to change notification settings - Fork 39
295 lines (262 loc) · 12.1 KB
/
Copy pathpr-artifacts.yml
File metadata and controls
295 lines (262 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
name: PR Artifacts
on:
workflow_dispatch: # Manual trigger for testing
pull_request_target:
types: [opened, synchronize, reopened, closed]
branches: [main]
pull_request_review:
types: [submitted]
jobs:
# Auto-remove .pr/ directory from same-repository PRs when approved.
cleanup-on-approval:
concurrency:
group: cleanup-pr-artifacts-${{ github.event.pull_request.number }}
cancel-in-progress: false
if: >-
github.event_name == 'pull_request_review' &&
github.event.review.state == 'approved' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
# Use PAT so the push triggers CI workflows that will complete and
# satisfy branch protection. We can't use [skip ci] because the Vercel
# GitHub App creates stuck checks that block merging.
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ secrets.OPENHANDS_BOT_GITHUB_PAT_PUBLIC }}
- name: Remove .pr/ directory
id: remove
run: |
if [ -d ".pr" ]; then
git config user.name "allhands-bot"
git config user.email "allhands-bot@users.noreply.github.com"
git rm -rf .pr/
git commit -m "chore: Remove PR-only artifacts [automated]"
git push || {
echo "::error::Failed to push cleanup commit. Check branch protection rules."
exit 1
}
echo "removed=true" >> "$GITHUB_OUTPUT"
echo "::notice::Removed .pr/ directory"
else
echo "removed=false" >> "$GITHUB_OUTPUT"
echo "::notice::No .pr/ directory to remove"
fi
- name: Update PR comment after cleanup
if: steps.remove.outputs.removed == 'true'
uses: actions/github-script@v9
with:
script: |
const marker = '<!-- pr-artifacts-notice -->';
const body = `${marker}
✅ **PR Artifacts Cleaned Up**
The \`.pr/\` directory has been automatically removed.
`;
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
},
);
const existing = comments.find(c => c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
}
# Inspect the fork through the API; never check out untrusted code with a write token.
check-pr-artifacts:
if: >-
github.event_name == 'pull_request_target' &&
github.event.action != 'closed'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Post or update PR comment
uses: actions/github-script@v9
with:
script: |
const marker = '<!-- pr-artifacts-notice -->';
const pullRequest = context.payload.pull_request;
const headRepository = pullRequest.head.repo;
let exists = true;
try {
await github.rest.repos.getContent({
owner: headRepository.owner.login,
repo: headRepository.name,
path: '.pr',
ref: pullRequest.head.sha,
});
} catch (error) {
if (error.status === 404) {
exists = false;
} else {
throw error;
}
}
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
},
);
const existing = comments.find(c => c.body.includes(marker));
if (!exists) {
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: `${marker}
✅ **PR Artifacts Cleaned Up**
The \`.pr/\` directory is no longer present.
`,
});
}
return;
}
const isFork = headRepository.full_name !== context.payload.repository.full_name;
const cleanup = isFork
? 'Because this is a fork PR, the workflow will **open or update a cleanup PR against `main` after merge**.'
: 'The directory will be **automatically removed when the PR is approved**.';
const body = `${marker}
📁 **PR Artifacts Notice**
This PR contains a \`.pr/\` directory with temporary PR-specific documents. ${cleanup}
`;
core.warning('.pr/ directory contains temporary PR-only artifacts');
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}
# Fork branches cannot be modified reliably. If artifacts reach the trusted base,
# remove them through a pull request so branch protections remain enforced.
cleanup-after-merge:
concurrency:
group: cleanup-pr-artifacts-${{ github.event.pull_request.base.ref }}
cancel-in-progress: false
if: >-
github.event_name == 'pull_request_target' &&
github.event.action == 'closed' &&
github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.base.ref }}
token: ${{ secrets.OPENHANDS_BOT_GITHUB_PAT_PUBLIC }}
- name: Create or update cleanup PR
id: cleanup
env:
BASE_REF: ${{ github.event.pull_request.base.ref }}
CLEANUP_BRANCH: automation/remove-pr-artifacts
GH_TOKEN: ${{ secrets.OPENHANDS_BOT_GITHUB_PAT_PUBLIC }}
run: |
if [ ! -d ".pr" ]; then
echo "created=false" >> "$GITHUB_OUTPUT"
echo "::notice::No .pr/ directory to remove from $BASE_REF"
exit 0
fi
git config user.name "allhands-bot"
git config user.email "allhands-bot@users.noreply.github.com"
git fetch origin \
"+refs/heads/$CLEANUP_BRANCH:refs/remotes/origin/$CLEANUP_BRANCH" || true
git checkout -B "$CLEANUP_BRANCH"
git rm -rf .pr/
git commit \
-m "chore: remove merged PR artifacts" \
-m "Co-authored-by: openhands <openhands@all-hands.dev>"
git push --force-with-lease origin "HEAD:$CLEANUP_BRANCH"
cleanup_pr_url=$(gh pr list \
--base "$BASE_REF" \
--head "$CLEANUP_BRANCH" \
--state open \
--json url \
--jq '.[0].url')
if [ -z "$cleanup_pr_url" ]; then
cleanup_pr_url=$(gh pr create \
--base "$BASE_REF" \
--head "$CLEANUP_BRANCH" \
--title "chore: remove merged PR artifacts" \
--body-file - <<EOF
## Why
Temporary \`.pr/\` artifacts reached \`$BASE_REF\` through one or more merged PRs.
## Summary
- Remove the temporary \`.pr/\` directory from \`$BASE_REF\`.
## How to Test
- Confirm this PR only deletes files under \`.pr/\`.
- Let the required checks validate the cleanup commit.
---
_This PR was automatically created by the PR Artifacts workflow._
EOF
)
fi
echo "created=true" >> "$GITHUB_OUTPUT"
echo "pr_url=$cleanup_pr_url" >> "$GITHUB_OUTPUT"
echo "::notice::Cleanup PR ready: $cleanup_pr_url"
- name: Update source PR comment
if: steps.cleanup.outputs.created == 'true'
uses: actions/github-script@v9
env:
CLEANUP_PR_URL: ${{ steps.cleanup.outputs.pr_url }}
with:
script: |
const marker = '<!-- pr-artifacts-notice -->';
const body = `${marker}
🧹 **PR Artifact Cleanup Queued**
The \`.pr/\` directory reached \`${context.payload.pull_request.base.ref}\` after merge. A cleanup PR has been opened or updated: ${process.env.CLEANUP_PR_URL}
`;
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
},
);
const existing = comments.find(c => c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}