Repository navigation
282 lines (277 loc) · 12.8 KB
/
Copy pathreports.yml
File metadata and controls
282 lines (277 loc) · 12.8 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
name: Reports
on:
issues:
types: [opened]
# The sweeper re-runs reports whose run was dropped from the concurrency queue.
workflow_dispatch:
inputs:
issue:
description: Issue number of the report
required: true
type: string
permissions: {}
jobs:
decide:
# The bot opens report issues itself for Like bursts; those are left for a maintainer.
if: >-
github.event_name == 'workflow_dispatch' || (
contains(github.event.issue.labels.*.name, 'report') &&
github.event.issue.user.type != 'Bot' &&
github.event.issue.user.login != 'devstreamhq-bot[bot]')
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
outputs:
has: ${{ steps.result.outputs.has }}
hide: ${{ steps.result.outputs.hide }}
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.
# actions: read lists recent health checks, so a dead report doesn't queue a repeat.
# contents: read reads hidden.json on the stats branch, so reports from before an un-hide don't count.
- 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
permission-actions: read
permission-contents: read
# An API failure exits non-zero with no result file, so nothing is acted on.
# It also writes undecided.json, counted below; a rate limit that hasn't reset yet writes nothing.
# A dispatched issue that is already handled, or a bot's issue, exits 0 with no result file.
- 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.reports decide --issue "$ISSUE_INPUT" --out result.json --undecided undecided.json
else
python -m scripts.reports decide --event "$GITHUB_EVENT_PATH" --out result.json --undecided undecided.json
fi
# Its own token, so the one that reads issue text never carries actions: write.
- id: dispatch_token
if: hashFiles('result.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-actions: write
# A failed dispatch fails the run before any comment, so the sweeper tries again.
- if: hashFiles('result.json') != ''
env:
GITHUB_TOKEN: ${{ steps.dispatch_token.outputs.token }}
run: python -m scripts.reports dispatch --result result.json --undecided 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 - <<'PY' >> "$GITHUB_OUTPUT"
import json
r = json.load(open("result.json"))
print("has=" + ("true" if r["actions"] else "false"))
print("hide=" + ("true" if any(a["kind"] == "hide" for a in r["actions"]) else "false"))
PY
# After 3 runs without a decision the report 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.reports undecided --file undecided.json
hide:
needs: decide
if: needs.decide.outputs.hide == 'true'
# Only this job writes entries.json, so only it queues behind submit.yml and rot.yml.
# A report flood then can't fill the one pending slot those share.
concurrency:
group: list-writes
cancel-in-progress: false
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 repo came from entries.json; the regexes here keep the values 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 = next(a["value"] for a in r["actions"] if a["kind"] == "hide")
issue = 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
# Re-checks against fresh main, which may have moved while this job queued: prints the branch
# name, nothing when the entry is already hidden, and fails when it's gone from the list.
- id: apply
env:
RESULT: ${{ runner.temp }}/result/result.json
run: |
branch=$(python -m scripts.reports apply --result "$RESULT")
echo "branch=$branch" >> "$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="chore(list): hide $REPO_NAME pending review"
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.reports pr-body --result "$RESULT" > "$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 "Reported in #$ISSUE."
# Holding the concurrency lock until the merge lands keeps the next writer 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
# Only now is "hidden" true, so the comment waits for the merge.
- env:
GITHUB_TOKEN: ${{ steps.late_issues_token.outputs.token || steps.issues_token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
run: python -m scripts.reports issue --result "$RESULT"
# 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 maintainer:
# needs-review stops the sweeper retrying it. The issue stays open.
- if: (failure() || cancelled()) && steps.issues_token.outputs.token != '' && steps.meta.outputs.issue != ''
env:
GITHUB_TOKEN: ${{ steps.late_issues_token.outputs.token || steps.issues_token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
run: python -m scripts.reports issue --result "$RESULT" --error
respond:
needs: decide
if: needs.decide.outputs.has == 'true' && needs.decide.outputs.hide != 'true'
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:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
run: python -m scripts.reports issue --result "$RESULT"
- if: failure() && steps.token.outputs.token != ''
env:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
RESULT: ${{ runner.temp }}/result/result.json
run: python -m scripts.reports issue --result "$RESULT" --error