Repository navigation
292 lines (286 loc) · 13.7 KB
/
Copy pathsubmit.yml
File metadata and controls
292 lines (286 loc) · 13.7 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
name: Submit
on:
issues:
types: [opened]
# The sweeper re-runs submissions whose run was dropped from the concurrency queue.
workflow_dispatch:
inputs:
issue:
description: Issue number of the submission
required: true
type: string
permissions: {}
# Shared with rot.yml: one run at a time edits entries.json, and each PR merges before the next starts.
concurrency:
group: list-writes
cancel-in-progress: false
jobs:
decide:
if: github.event_name == 'workflow_dispatch' || contains(github.event.issue.labels.*.name, 'submission')
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
outputs:
ok: ${{ steps.result.outputs.ok }}
steps:
- uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
persist-credentials: false
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
# Org block reads need an App token; GITHUB_TOKEN can't see them.
- id: token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-organization-user-blocking: read
permission-issues: read
# An API failure exits non-zero with no result file, so nothing is accepted or rejected.
# Any failure (an undecidable repo, an API error) also writes undecided.json, counted below.
# A rate limit that hasn't reset yet writes nothing, so waiting for it spends no attempt.
# A dispatched issue that is already decided exits 0 with no result file, so nothing runs.
- env:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
EXCLUDED_USERS: ${{ vars.EXCLUDED_USERS }}
ISSUE_INPUT: ${{ inputs.issue }}
run: |
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then
python -m scripts.submit decide --issue "$ISSUE_INPUT" --out result.json --undecided undecided.json
else
python -m scripts.submit decide --event "$GITHUB_EVENT_PATH" --out result.json --undecided undecided.json
fi
# After 3 runs without a decision the issue is parked as needs-review, so the sweeper moves on.
- id: undecided_token
if: failure() && hashFiles('undecided.json') != ''
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- if: failure() && steps.undecided_token.outputs.token != ''
env:
GITHUB_TOKEN: ${{ steps.undecided_token.outputs.token }}
run: python -m scripts.submit undecided --file undecided.json
- if: hashFiles('result.json') != ''
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: result
path: result.json
retention-days: 7
- id: result
if: hashFiles('result.json') != ''
run: python -c 'import json; print("ok=" + ("true" if json.load(open("result.json"))["ok"] is True else "false"))' >> "$GITHUB_OUTPUT"
apply:
needs: decide
if: needs.decide.outputs.ok == 'true'
runs-on: ubuntu-latest
timeout-minutes: 35
permissions:
contents: read
steps:
- uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- id: token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
# Separate token so the one that can push never carries issues: write.
- id: issues_token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
token: ${{ steps.token.outputs.token }}
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: result
path: ${{ runner.temp }}/result
# The values were validated by decide; the regexes here keep them safe to use in shell.
- id: meta
env:
RESULT: ${{ runner.temp }}/result/result.json
run: |
python - <<'PY' >> "$GITHUB_OUTPUT"
import json, os, re
r = json.load(open(os.environ["RESULT"]))
repo, issue = r["entry"]["repo"], r["issue"]
assert re.fullmatch(r"[A-Za-z0-9-]+/[A-Za-z0-9._-]+", repo), repo
assert type(issue) is int and issue > 0, issue
print(f"repo={repo}")
print(f"issue={issue}")
PY
# Prints the branch name, "done" when another run of this issue already added it,
# or nothing when fresh main already has the repo (result.json is then rewritten as a rejection).
- id: apply
env:
RESULT: ${{ runner.temp }}/result/result.json
run: |
out=$(python -m scripts.submit apply --result "$RESULT")
if [ "$out" = done ]; then echo "done=true"; else echo "branch=$out"; fi >> "$GITHUB_OUTPUT"
- id: pr
if: steps.apply.outputs.branch != ''
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
REPO_NAME: ${{ steps.meta.outputs.repo }}
ISSUE: ${{ steps.meta.outputs.issue }}
BRANCH: ${{ steps.apply.outputs.branch }}
run: |
branch="$BRANCH"
subject="feat(list): add $REPO_NAME"
git switch -c "$branch"
git add data/entries.json
git -c user.name='devstreamhq-bot[bot]' \
-c user.email='353074438+devstreamhq-bot[bot]@users.noreply.github.com' \
commit -m "$subject"
git push origin "$branch"
python -m scripts.submit comment --result "$RESULT" --kind pr > "$RUNNER_TEMP/pr-body.md"
url=$(gh pr create --base main --head "$branch" --title "$subject" --body-file "$RUNNER_TEMP/pr-body.md")
echo "url=$url" >> "$GITHUB_OUTPUT"
# An explicit body stops GitHub adding its Co-authored-by trailer to the squash commit.
gh pr merge "$url" --auto --squash --subject "$subject" --body "Submitted in #$ISSUE."
# Holding the concurrency lock until the merge lands keeps the next apply off a stale entries.json.
- if: steps.apply.outputs.branch != ''
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
PR_URL: ${{ steps.pr.outputs.url }}
run: |
for _ in $(seq 45); do
[ "$(gh pr view "$PR_URL" --json state --jq .state)" = MERGED ] && exit 0
sleep 20
done
# Closing also disarms auto-merge, so it can't land later behind our backs.
# It may have merged in the meantime, so look once more.
gh pr close "$PR_URL" || true # fails if it merged meanwhile; the check below decides
[ "$(gh pr view "$PR_URL" --json state --jq .state)" = MERGED ] && exit 0
echo "::error::$PR_URL did not merge within 15 minutes and is now closed"
exit 1
# Minted after the merge wait, so no later step runs on a token that expired during it.
- id: late_token
if: always() && steps.pr.outputs.url != ''
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
- id: late_issues_token
if: always() && steps.issues_token.outputs.token != ''
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
# The entry is live now, so a failure here must not park it as needs-review; the sweeper
# finishes the label and close if the issue is still open. The label goes on first and is
# retried; the comment is a create, so it is sent once and never retried.
- if: steps.apply.outputs.branch != ''
continue-on-error: true
env:
GH_TOKEN: ${{ steps.late_issues_token.outputs.token || steps.issues_token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
ISSUE: ${{ steps.meta.outputs.issue }}
run: |
python -m scripts.submit comment --result "$RESULT" --kind accepted > "$RUNNER_TEMP/comment.md"
retry() { for i in 1 2 3; do "$@" && return; [ "$i" = 3 ] || sleep $((i * 10)); done; return 1; }
retry gh issue edit "$ISSUE" --add-label accepted
gh issue comment "$ISSUE" --body-file "$RUNNER_TEMP/comment.md"
# The new entry's Like thread, made while this run still holds list-writes so a burst of
# submissions can't starve it. A failure is left to the hourly on-merge run, not parked.
- id: discussions_token
if: steps.apply.outputs.branch != ''
continue-on-error: true
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-contents: write
permission-discussions: write
- if: steps.apply.outputs.branch != '' && steps.discussions_token.outputs.token != ''
continue-on-error: true
env:
GITHUB_TOKEN: ${{ steps.discussions_token.outputs.token }}
run: python -m scripts.on_merge
- if: steps.apply.outputs.branch == '' && steps.apply.outputs.done != 'true'
env:
GH_TOKEN: ${{ steps.late_issues_token.outputs.token || steps.issues_token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
ISSUE: ${{ steps.meta.outputs.issue }}
run: |
python -m scripts.submit comment --result "$RESULT" --kind rejected > "$RUNNER_TEMP/comment.md"
gh issue comment "$ISSUE" --body-file "$RUNNER_TEMP/comment.md"
gh issue edit "$ISSUE" --add-label rejected
gh issue close "$ISSUE" --reason "not planned"
# A cancel or job timeout releases the lock, so the armed PR must not merge behind the next writer.
# Timeouts reach this step too: the runner re-evaluates later steps' if: on any job cancellation.
- if: cancelled() && steps.pr.outputs.url != ''
env:
GH_TOKEN: ${{ steps.late_token.outputs.token || steps.token.outputs.token }}
PR_URL: ${{ steps.pr.outputs.url }}
run: gh pr close "$PR_URL" || true # fails if it merged meanwhile
# A genuine failure (apply error, push, PR, merge timeout, cancel) is parked for a human:
# the sweeper skips needs-review, so it can't retry forever. The issue stays open.
- if: (failure() || cancelled()) && steps.issues_token.outputs.token != '' && steps.meta.outputs.issue != ''
env:
GH_TOKEN: ${{ steps.late_issues_token.outputs.token || steps.issues_token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
ISSUE: ${{ steps.meta.outputs.issue }}
run: |
python -m scripts.submit comment --result "$RESULT" --kind error > "$RUNNER_TEMP/error.md"
gh issue edit "$ISSUE" --add-label needs-review
gh issue comment "$ISSUE" --body-file "$RUNNER_TEMP/error.md"
reject:
needs: decide
if: needs.decide.outputs.ok == 'false'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
steps:
- uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
persist-credentials: false
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: result
path: ${{ runner.temp }}/result
- id: token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- env:
GH_TOKEN: ${{ steps.token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
run: |
issue=$(python -c 'import json, os; i = json.load(open(os.environ["RESULT"]))["issue"]; assert type(i) is int; print(i)')
python -m scripts.submit comment --result "$RESULT" --kind rejected > "$RUNNER_TEMP/comment.md"
gh issue comment "$issue" --body-file "$RUNNER_TEMP/comment.md"
gh issue edit "$issue" --add-label rejected
gh issue close "$issue" --reason "not planned"