Repository navigation
421 lines (379 loc) · 19.9 KB
/
Copy pathupstream-config-check.yml
File metadata and controls
421 lines (379 loc) · 19.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
name: upstream-config-check
on:
schedule:
# Weekly on Mondays at 09:15 UTC
- cron: '15 09 * * 1'
workflow_dispatch:
permissions:
contents: write
pull-requests: write
issues: write
actions: write
concurrency:
group: upstream-config-check
cancel-in-progress: false
env:
SYNC_BRANCH: automation/upstream-config-sync
SYNC_LABEL: upstream-sync
jobs:
check-upstream:
runs-on: ubuntu-latest
steps:
# Pinned to a commit rather than a mutable tag, matching maintenance-check.yml.
# This job holds contents/pull-requests/issues/actions write, so a re-pointed
# tag would run with all of it.
#
# ref: always sync the default branch, whatever ref the run was dispatched
# from. Without it, a manual dispatch from a topic branch would build the
# sync branch on top of that branch's commits while opening the pull request
# against the default branch, dragging unrelated work in.
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.repository.default_branch }}
- name: Ensure the drift label exists
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh label create "$SYNC_LABEL" \
--color 0075ca \
--description 'Automated upstream config drift detection' \
2>/dev/null || true
- name: Apply upstream config updates
id: sync
run: |
set +e
output=$(./scripts/sync-upstream-configs.sh --update 2>&1)
exit_code=$?
set -e
echo "$output"
# The output embeds file contents fetched from upstream URLs. Use a
# random heredoc delimiter so upstream content cannot close the block
# early and inject arbitrary variables into the job environment.
delimiter="$(openssl rand -hex 16)"
{
echo "SYNC_OUTPUT<<${delimiter}"
echo "$output"
echo "${delimiter}"
} >> "$GITHUB_ENV"
echo "exit_code=${exit_code}" >> "$GITHUB_OUTPUT"
# With --update the script exits 0 whether or not it wrote anything,
# so the exit code cannot tell us if there was drift. Ask git instead.
if git diff --quiet; then
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
# git alone is not enough. The script reports three kinds of drift it
# never writes to disk, each bumping its "changed" counter without
# touching the working tree:
# - a manifest entry whose local file is missing
# - curated package drift against core, which has no --update path
# - a transform patch conflict, which counts and then skips the file
# Its own summary counters are the only signal, and "changed" minus
# "updated" is the unapplied remainder. Note the third category also
# shows up in `conflicts` below, so the two signals deliberately overlap.
#
# `|| true` on the grep pipelines below: grep exits 1 when it matches
# nothing, which is normal here. Harmless under this job's shell
# (unspecified, so `bash -e` without pipefail, where a pipeline takes
# its last command's status), but adding `shell: bash` anywhere would
# enable pipefail and abort the step mid-way on ordinary runs. The
# values are unaffected: tail prints nothing and awk prints 0 already.
#
# tail -1 is load-bearing: upstream diff content could contain a forged
# "Summary:" line, and the script's real one is always printed last.
summary=$(printf '%s\n' "$output" | grep -E '^Summary:' | tail -1 || true)
drift=$(printf '%s' "$summary" | sed -n 's/.*, \([0-9]\{1,\}\) changed.*/\1/p')
applied=$(printf '%s' "$summary" | sed -n 's/.*, \([0-9]\{1,\}\) updated.*/\1/p')
unapplied=$(( ${drift:-0} - ${applied:-0} ))
[ "$unapplied" -lt 0 ] && unapplied=0
echo "unapplied=${unapplied}" >> "$GITHUB_OUTPUT"
echo "Reported drift: ${drift:-0}, applied: ${applied:-0}, unapplied: ${unapplied}"
# Read this off the summary line only. Scanning the whole output would
# also match the phrase appearing inside a fetched upstream diff.
conflicts=false
if printf '%s' "$summary" | grep -q 'patch conflict'; then
conflicts=true
fi
echo "conflicts=${conflicts}" >> "$GITHUB_OUTPUT"
# One explicit signal for "a human must look at this". Anything that is
# not a clean run has to reach a surface: the worst outcome for a drift
# detector is a green run that reported nothing. In --update mode the
# script exits 0 on a clean pass. Exit 2 is a complete run that hit
# fetch errors - it skips the failed entry and still prints its
# summary - whereas any other nonzero code, or a missing summary line,
# means it aborted before finishing.
attention=false
if [ "$exit_code" -ne 0 ]; then attention=true; fi
if [ "$unapplied" -gt 0 ]; then attention=true; fi
if [ -z "$summary" ]; then attention=true; fi
if [ "$conflicts" = 'true' ]; then attention=true; fi
echo "attention=${attention}" >> "$GITHUB_OUTPUT"
echo "Needs human attention: ${attention}"
# Both report bodies fence this output. Pick a fence longer than the
# longest backtick run in it so upstream content can never close the
# block early; a fixed width can always be beaten. Computed once here
# rather than repeated in each consuming step.
run_len=$(printf '%s\n' "$output" | grep -o '`\{1,\}' | awk '{ if (length($0) > m) m = length($0) } END { print m + 0 }' || true)
fence_len=$(( run_len < 3 ? 3 : run_len + 1 ))
fence=$(printf '%*s' "$fence_len" '' | tr ' ' '`')
echo "fence=${fence}" >> "$GITHUB_OUTPUT"
- name: Push the sync branch
id: push
if: steps.sync.outputs.changed == 'true'
run: |
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git checkout -B "$SYNC_BRANCH"
git add -A
git commit -m "Sync vendored configs with upstream" \
-m "Applied automatically by the upstream config check. The pull request body carries the full comparison report."
# This branch is owned by the automation and is rebuilt from the
# default branch on every run, so replacing it wholesale is intended.
git push --force origin "$SYNC_BRANCH"
- name: Open or update the pull request
id: pr
if: steps.sync.outputs.changed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ATTENTION: ${{ steps.sync.outputs.attention }}
FENCE: ${{ steps.sync.outputs.fence }}
run: |
# Never trust GITHUB_REF_NAME for the base. On workflow_dispatch it is
# whatever ref the invoker picked, which would silently retarget the
# pull request. Ask the repository what its default branch actually is.
#
# Deliberately a live query rather than the github.event.repository
# expression used for the checkout ref. An empty value is survivable
# there (checkout falls back to the triggering ref) but not here, where
# it would break `gh pr create` outright.
base_branch=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')
{
cat <<'MD'
The upstream config check found drift and applied it to the vendored assets on this branch.
> [!IMPORTANT]
> **Press "Approve and run workflows" below before reviewing.**
>
> This pull request was opened by `GITHUB_TOKEN`, so its `pull_request`
> runs are created but held in an `action_required` state rather than
> starting on their own. Approving them starts `maintenance-checker`,
> `tests (stable)` and `tests (HEAD)`. Closing and reopening the pull
> request also works, but approving is one click.
MD
# Warn whenever anything at all needs a human, not just patch
# conflicts: curated package drift, a missing local file, a fetch
# failure and an aborted script all leave work outside this branch,
# and a reviewer reading only the pull request would never know.
# Driven by the single attention signal rather than a re-derived
# copy, because that signal is exactly what decides whether the
# companion issue this warning points at gets filed at all.
if [ "$ATTENTION" = 'true' ]; then
cat <<'MD'
> [!WARNING]
> Some drift could not be applied automatically, so this pull request is
> incomplete. Check the open `upstream-sync` issue for the remainder - it is
> filed by a later step of the same run, so its absence means that step failed.
MD
fi
printf '\n%s\n%s\n%s\n\n' "$FENCE" "$SYNC_OUTPUT" "$FENCE"
cat <<'MD'
## Validation
The test suite is also dispatched against this branch by a later step of this
run, which gives an early signal without waiting for the reopen. Those runs
report in the Actions tab rather than as checks below:
MD
printf '\n<https://github.com/%s/actions/workflows/tests.yml?query=branch%%3A%s>\n\n' \
"$GITHUB_REPOSITORY" "$SYNC_BRANCH"
cat <<'MD'
To validate locally, including the full Drupal install test:
```bash
MD
printf 'git fetch origin %s && git checkout %s\n' "$SYNC_BRANCH" "$SYNC_BRANCH"
cat <<'MD'
DCQ_FULL_TESTS=1 bats --jobs 4 ./tests/test.bats
```
MD
} > pr-body.md
# Match on base as well as head. A pull request left pointing at the
# wrong base would otherwise be found by head alone and only have its
# body edited, since `gh pr edit` cannot retarget an existing base.
existing=$(gh pr list \
--head "$SYNC_BRANCH" \
--base "$base_branch" \
--state open \
--json number,isCrossRepository \
--jq '[.[] | select(.isCrossRepository == false)] | .[0].number // empty')
if [ -n "$existing" ]; then
gh pr edit "$existing" --body-file pr-body.md
echo "Updated pull request #${existing}"
elif gh pr create \
--base "$base_branch" \
--head "$SYNC_BRANCH" \
--title 'Sync vendored configs with upstream' \
--body-file pr-body.md \
--label "$SYNC_LABEL"; then
echo 'Opened a new pull request.'
else
# gh pr create is not an upsert. If a pull request already exists for
# this branch - typically one a maintainer closed while leaving the
# branch behind - creating fails, so try to adopt that one rather
# than losing the report. If nothing adoptable is found, or it
# cannot be reopened, the step fails loudly below.
echo 'Create failed; looking for an existing pull request on this branch.' >&2
# Match base here too. Adopting a pull request that targets a
# different base would rewrite its body while leaving it pointed
# somewhere stale, and `gh pr edit` cannot retarget a base - the
# same trap the primary lookup above avoids.
#
# Exclude merged pull requests: they cannot be reopened, so adopting
# one would leave the run claiming success with nothing open.
prior=$(gh pr list \
--head "$SYNC_BRANCH" \
--base "$base_branch" \
--state all \
--limit 100 \
--json number,state,isCrossRepository \
--jq '[.[] | select(.isCrossRepository == false and .state != "MERGED")]
| .[0].number // empty')
if [ -z "$prior" ]; then
# Surface a wrong-base pull request rather than quietly adopting it.
mismatched=$(gh pr list \
--head "$SYNC_BRANCH" \
--state all \
--limit 100 \
--json number,state,baseRefName,isCrossRepository \
--jq '[.[] | select(.isCrossRepository == false and .state != "MERGED")]
| .[0] | select(. != null) | "#\(.number) -> \(.baseRefName)"')
if [ -n "$mismatched" ]; then
echo "A pull request for this branch exists but targets a different base: ${mismatched}" >&2
echo "Expected base ${base_branch}. Retarget or close it; refusing to rewrite it." >&2
else
echo 'No existing pull request found either; giving up.' >&2
fi
exit 1
fi
gh pr reopen "$prior" 2>/dev/null || true
gh pr edit "$prior" --body-file pr-body.md
# Editing a closed pull request succeeds, so confirm it is actually
# open rather than reporting success over a body nobody will see.
state=$(gh pr view "$prior" --json state --jq '.state')
if [ "$state" != 'OPEN' ]; then
echo "Pull request #${prior} is ${state} and could not be reopened." >&2
echo 'Close it out or delete the branch so a fresh one can be created.' >&2
exit 1
fi
echo "Adopted pull request #${prior}"
fi
# Two halves, both load-bearing:
#
# !cancelled() replaces the implicit success() that a plain `if:` gets, so
# a failed push or pull request step cannot skip this step - the very
# outcome it exists to report.
#
# `|| failure()` covers the rest: attention is computed before push and
# pull request run, so a clean sync whose pull request then fails leaves
# attention false. Naming any status function suppresses the implicit
# success(), so without `|| failure()` the condition would simply evaluate
# false and skip the step - and that drift would reach no surface at all.
#
# Ordered ahead of the test dispatch so a transient dispatch failure
# cannot suppress it either.
- name: Report drift that could not be applied
if: ${{ !cancelled() && (steps.sync.outputs.attention == 'true' || failure()) }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HAS_CONFLICTS: ${{ steps.sync.outputs.conflicts }}
EXIT_CODE: ${{ steps.sync.outputs.exit_code }}
CHANGED: ${{ steps.sync.outputs.changed }}
UNAPPLIED: ${{ steps.sync.outputs.unapplied }}
FENCE: ${{ steps.sync.outputs.fence }}
PUSH_OUTCOME: ${{ steps.push.outcome }}
PR_OUTCOME: ${{ steps.pr.outcome }}
run: |
{
cat <<'MD'
The upstream config check could not complete automatically.
MD
if [ -z "$EXIT_CODE" ]; then
# failure() brings us here even if the job died before the sync
# step ran, in which case every output is an empty string and the
# bullets below would render as nonsense.
echo '- The run failed before the sync step completed, so there is no drift'
echo ' report to show. Check the job log for the step that failed.'
elif [ "$EXIT_CODE" = '2' ]; then
echo '- One or more upstream files could not be fetched.'
elif [ "$EXIT_CODE" != '0' ]; then
printf -- '- The sync script exited %s. In --update mode it should always exit 0,\n' "$EXIT_CODE"
echo ' so it aborted partway through. The log below is likely truncated.'
fi
if [ "$HAS_CONFLICTS" = 'true' ]; then
echo '- One or more transforms hit a patch conflict and were left unapplied.'
fi
if [ "${UNAPPLIED:-0}" -gt 0 ]; then
printf -- '- %s drift item(s) were reported but not written to disk. That covers a\n' "$UNAPPLIED"
echo ' manifest entry whose local file is missing, curated package drift against'
echo ' core, and any patch conflict above - the sync script cannot apply these'
echo ' automatically, so this count overlaps the patch-conflict line when both occur.'
fi
if [ "$PUSH_OUTCOME" = 'failure' ]; then
echo '- Pushing the sync branch failed, so no pull request exists. The applied'
echo ' drift is not published anywhere; re-run once the cause is cleared.'
fi
if [ "$PR_OUTCOME" = 'failure' ]; then
# shellcheck disable=SC2016 # the backticks are literal markdown
printf -- '- The sync branch `%s` was pushed, but opening or\n' "$SYNC_BRANCH"
echo ' updating its pull request failed. The applied drift is on that branch and'
echo ' is currently unreviewed - open a pull request for it by hand, or re-run.'
fi
# Only claim a pull request exists if the step that opens one succeeded.
if [ "$CHANGED" = 'true' ] && [ "$PR_OUTCOME" = 'success' ]; then
echo '- Drift that *could* be applied was opened as a pull request; this issue covers only the remainder.'
fi
printf '\n%s\n%s\n%s\n\n' "$FENCE" "$SYNC_OUTPUT" "$FENCE"
cat <<'MD'
Run `./scripts/sync-upstream-configs.sh --update` locally to investigate, then
`DCQ_FULL_TESTS=1 bats --jobs 4 ./tests/test.bats` to validate.
MD
} > issue-body.md
# Match the title too. The label is not owned by this workflow, so any
# hand-labelled issue would otherwise be found first and overwritten.
# --limit, because the title filter runs client side: with the default
# page size the canonical issue could fall outside the results and a
# duplicate would be created alongside it.
existing=$(gh issue list \
--label "$SYNC_LABEL" \
--state open \
--limit 100 \
--json number,title \
--jq '[.[] | select(.title == "Upstream config check needs attention")] | .[0].number // empty')
if [ -n "$existing" ]; then
gh issue edit "$existing" --body-file issue-body.md
echo "Updated issue #${existing}"
else
gh issue create \
--title 'Upstream config check needs attention' \
--body-file issue-body.md \
--label "$SYNC_LABEL"
fi
- name: Dispatch the test suite against the sync branch
if: steps.sync.outputs.changed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh workflow run tests.yml --ref "$SYNC_BRANCH"
# The reporting step above has already run, so the drift should be
# recorded in an issue - check that step's outcome if not. Redden the run
# regardless: in --update mode the script should always exit 0, so any
# other code is a genuine failure and should look like one in the Actions
# tab rather than passing quietly with an issue off to one side.
- name: Fail the run when the sync script did not complete
if: ${{ !cancelled() && steps.sync.outputs.exit_code != '0' }}
run: |
echo "sync-upstream-configs.sh exited ${EXIT_CODE}." >&2
if [ "$EXIT_CODE" = '2' ]; then
echo 'Exit 2 means one or more upstream files could not be fetched.' >&2
else
echo 'In --update mode it should exit 0, so it aborted partway through.' >&2
fi
exit 1
env:
EXIT_CODE: ${{ steps.sync.outputs.exit_code }}