blueprint-shared-templates 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/.github/ISSUE_TEMPLATE/bug.yml +2 -2
- package/base/.github/ISSUE_TEMPLATE/hotfix.yml +1 -1
- package/base/.github/actions/setup-workspace/action.yml +2 -2
- package/base/.github/workflows/auto-merge.yml +4 -0
- package/base/.github/workflows/ci.yml +17 -1
- package/base/scripts/ci/run-affected.sh +6 -0
- package/base/scripts/lib/common.sh +1 -1
- package/base/scripts/lib/retry.mjs +36 -6
- package/base/scripts/merge/decide.mjs +32 -17
- package/base/scripts/setup/github-repo-settings.sh +1 -1
- package/deploy/.github/workflows/deploy-prerelease.yml +6 -50
- package/deploy/.github/workflows/deploy-prod.yml +103 -0
- package/deploy/scripts/deploy/plan-prod.sh +57 -0
- package/deploy/scripts/deploy/promote.sh +23 -7
- package/deploy/scripts/deploy/publish-bundle.sh +10 -1
- package/deploy/scripts/deploy/route-tag.sh +10 -12
- package/deploy/scripts/deploy/run-prerelease.sh +4 -7
- package/deploy/scripts/deploy/{run-production.sh → run-prod.sh} +15 -17
- package/deploy/scripts/deploy/writeback-ops.sh +71 -40
- package/facts-skeleton.mjs +1 -1
- package/hub/scripts/release/dispatch-release-lines.sh +25 -2
- package/hub/scripts/release/snapshot-members.sh +1 -1
- package/ops/.github/workflows/ops-deploy.yml +92 -11
- package/ops/schemas/targets.schema.json +209 -0
- package/ops/scripts/ci/check-targets.mjs +291 -0
- package/ops/scripts/deploy/apply-iac.sh +89 -0
- package/ops/scripts/deploy/apply-ops.sh +101 -48
- package/ops/scripts/deploy/ops-run.sh +50 -12
- package/ops/scripts/deploy/smoke.sh +35 -16
- package/ops/scripts/lib/ops-steps.sh +75 -0
- package/org/scripts/ci/check-org-values.mjs +91 -0
- package/package.json +2 -2
- package/publish/.github/workflows/publish.yml +12 -3
- package/publish/scripts/release/cleanup-private-packages.mjs +1 -1
- package/publish/scripts/release/mirror-units.sh +45 -5
- package/publish/scripts/release/publish-units.sh +28 -4
- package/release/.github/workflows/advance-release.yml +107 -0
- package/release/.github/workflows/close-release.yml +133 -0
- package/release/.github/workflows/merge-back.yml +82 -0
- package/release/.github/workflows/merge-release-pr.yml +82 -0
- package/release/.github/workflows/open-release-line.yml +14 -5
- package/release/.github/workflows/release-line-finalize.yml +5 -3
- package/release/.github/workflows/release-please.yml +99 -15
- package/release/.github/workflows/sync-release-tools.yml +91 -0
- package/release/release-policy.json +1 -1
- package/release/schemas/release-policy.schema.json +13 -3
- package/release/scripts/ci/branch-exists.sh +27 -0
- package/release/scripts/ci/check-release-policy.sh +13 -2
- package/release/scripts/ci/pin-train.sh +5 -3
- package/release/scripts/ci/release-train.sh +22 -3
- package/release/scripts/lib/release-plan.sh +3 -1
- package/release/scripts/lib/release-pr.sh +275 -11
- package/release/scripts/lib/release-stages.sh +265 -36
- package/release/scripts/release/check-trigger-actor.sh +32 -0
- package/release/scripts/release/close-ready.mjs +72 -0
- package/release/scripts/release/close-ready.sh +70 -0
- package/release/scripts/release/cut-release.sh +35 -4
- package/release/scripts/release/ensure-stage-deploys.sh +3 -3
- package/release/scripts/release/finalize-release.sh +47 -15
- package/release/scripts/release/merge-back-ready.sh +27 -0
- package/release/scripts/release/merge-release-pr.sh +34 -107
- package/release/scripts/release/open-release-line.sh +28 -8
- package/release/scripts/release/pin-release-pr.sh +53 -0
- package/release/scripts/release/product-release-notes.sh +28 -0
- package/release/scripts/release/sync-release-tools.sh +137 -0
- package/release/scripts/release/{verify-production.sh → verify-prod.sh} +11 -10
- package/transform.mjs +3 -4
- package/verify/.github/workflows/{verify-production.yml → verify-prod.yml} +7 -7
- package/verify/scripts/release/verify-ops.sh +4 -4
- package/deploy/.github/workflows/deploy-production.yml +0 -173
- package/deploy/scripts/deploy/plan-production.sh +0 -43
- package/infra/.github/workflows/deploy-prerelease.yml +0 -157
- package/infra/.github/workflows/deploy-production.yml +0 -173
- package/infra/scripts/deploy/apply-infra.sh +0 -46
- package/infra/scripts/deploy/apply-prerelease.sh +0 -22
- package/infra/scripts/deploy/apply-production.sh +0 -33
- package/infra/scripts/deploy/route-tag.sh +0 -65
|
@@ -26,6 +26,15 @@ policy_require() {
|
|
|
26
26
|
[[ -f "$(policy_file)" ]] || die "找不到政策檔 $(policy_file)(格式見 docs/release-policy.md)"
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
# policy_require_product <script> → stops with a message that names the organization layer when the policy is the one of a
|
|
30
|
+
# repository of that layer. Such a repository belongs to no product, so a script that reads product.members has nothing to read.
|
|
31
|
+
policy_require_product() {
|
|
32
|
+
policy_require
|
|
33
|
+
if jq -e '.layer == "organization"' "$(policy_file)" >/dev/null 2>&1; then
|
|
34
|
+
die "$1 只用在產品的 hub:$(policy_file) 是組織層的倉(layer: organization),不屬於任何產品,沒有 product.members"
|
|
35
|
+
fi
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
policy_unit_exists() { policy_require; jq -e --arg u "$1" '(.units // {}) | has($u)' "$(policy_file)" >/dev/null; }
|
|
30
39
|
|
|
31
40
|
# policy_unit_stages <unit> → the stages the unit walks on its own, one per line. Stops when the policy
|
|
@@ -35,6 +44,24 @@ policy_unit_stages() {
|
|
|
35
44
|
jq -r --arg u "$1" '.units[$u].stages[]' "$(policy_file)"
|
|
36
45
|
}
|
|
37
46
|
|
|
47
|
+
# stages_override_check <json> → stops with the reason when <json> is not a valid stage override of this release. The format is a
|
|
48
|
+
# JSON object { "<unit>": ["rc", "ga"], ... }: every key a unit the policy lists, every value a non-empty list of stages
|
|
49
|
+
# (RELEASE_STAGES) that ends at ga. A workflow input is a string, so the override travels as this JSON text.
|
|
50
|
+
stages_override_check() {
|
|
51
|
+
local json="$1" bad
|
|
52
|
+
jq -e . <<<"$json" >/dev/null 2>&1 || die "stages_override 不是 JSON:${json}(格式 {\"<單元>\":[\"rc\",\"ga\"]})"
|
|
53
|
+
jq -e 'type == "object" and length > 0' <<<"$json" >/dev/null || die "stages_override 需為非空的 JSON 物件 {\"<單元>\":[\"rc\",\"ga\"]}:${json}"
|
|
54
|
+
bad="$(jq -r '. as $o | keys[] | select(($o[.] | type == "array" and length > 0 and all(.[]; IN("alpha","beta","rc","ga")) and .[-1] == "ga") | not)' <<<"$json" | paste -sd' ' -)"
|
|
55
|
+
[[ -z "$bad" ]] || die "stages_override 的 ${bad} 需為 alpha、beta、rc、ga 組成、以 ga 結尾的非空陣列"
|
|
56
|
+
bad="$(jq -r --slurpfile p "$(policy_file)" 'keys[] as $k | select(($p[0].units // {}) | has($k) | not) | $k' <<<"$json" | paste -sd' ' -)"
|
|
57
|
+
[[ -z "$bad" ]] || die "stages_override 指到政策檔沒有的單元:${bad}"
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
# stages_override_policy <json> → prints the policy file with the stages of the units in <json> replaced
|
|
61
|
+
stages_override_policy() {
|
|
62
|
+
jq --argjson o "$1" 'reduce ($o | to_entries[]) as $e (.; .units[$e.key].stages = $e.value)' "$(policy_file)"
|
|
63
|
+
}
|
|
64
|
+
|
|
38
65
|
policy_unit_kind() { policy_require; jq -r --arg u "$1" '.units[$u].kind // empty' "$(policy_file)"; }
|
|
39
66
|
|
|
40
67
|
# policy_unit_independent <unit> → succeeds when the policy marks the unit "independent": true, which means the
|
|
@@ -85,8 +112,8 @@ unit_registry() {
|
|
|
85
112
|
|
|
86
113
|
# is_aggregate_path <config path> → succeeds when the path is a facts or docs package of the aggregate kind:
|
|
87
114
|
# the aggregate package of the repository (a one-segment path whose component is named <RepoName>-facts or
|
|
88
|
-
# <RepoName>-docs, or plain facts or docs, blueprint.naming.unit-name) or a shared package published on its own from inside that
|
|
89
|
-
# directory (facts/<RepoName>-shared-facts, docs/<RepoName>-shared-docs, blueprint.repo-layout.unit-dirs). The
|
|
115
|
+
# <RepoName>-docs, or plain facts or docs, blueprint-shared.naming.unit-name) or a shared package published on its own from inside that
|
|
116
|
+
# directory (facts/<RepoName>-shared-facts, docs/<RepoName>-shared-docs, blueprint-shared.repo-layout.unit-dirs). The
|
|
90
117
|
# directory is found from the name of the package, so nothing here spells out "facts" or "docs".
|
|
91
118
|
is_aggregate_path() {
|
|
92
119
|
local p="${1%/}" c
|
|
@@ -116,15 +143,22 @@ aggregate_unit() {
|
|
|
116
143
|
# those of <unit>... that have been released (unit_released). A merge-back commit covers the units its
|
|
117
144
|
# Release-Train-Units lists; one with no such trailer covers all of them. The result is the cut that covers the
|
|
118
145
|
# last of them, so a unit left out of the newest releases still counts from the cut of the release that had it.
|
|
146
|
+
# No release lists a unit that has never been released, so a cut with the trailer covers such a unit when it is
|
|
147
|
+
# strictly older than the unit's first commit: every commit of the unit then comes after the cut and counts toward
|
|
148
|
+
# its first version.
|
|
119
149
|
# With nothing to cover it is the newest cut; when the walk ends with a unit uncovered it is the oldest cut;
|
|
120
150
|
# with no cut at all it is empty (no previous release). The walk has no depth limit: the cut is a fact of an
|
|
121
151
|
# earlier release, however far back.
|
|
122
152
|
release_start_point() {
|
|
123
|
-
local base="$1" u c b us row
|
|
124
|
-
local -a left=() listed=() keep=()
|
|
153
|
+
local base="$1" u c b us row first
|
|
154
|
+
local -a left=() firsts=() listed=() keep=()
|
|
125
155
|
shift
|
|
126
156
|
RELEASE_START=""; RELEASE_SKIPPED=()
|
|
127
|
-
for u in "$@"; do
|
|
157
|
+
for u in "$@"; do
|
|
158
|
+
if unit_released "$u"; then left+=("$u"); continue; fi
|
|
159
|
+
first="$(git rev-list --reverse "$base" -- "$(unit_path "$u")" | sed -n 1p)"
|
|
160
|
+
[[ -z "$first" ]] || firsts+=("$first")
|
|
161
|
+
done
|
|
128
162
|
while IFS= read -r row; do
|
|
129
163
|
b="${row%%$'\x1f'*}"; us="${row#*$'\x1f'}"
|
|
130
164
|
[[ "$b" =~ ^[0-9a-f]{7,40}$ ]] || continue
|
|
@@ -132,13 +166,17 @@ release_start_point() {
|
|
|
132
166
|
if ! git merge-base --is-ancestor "$c" "$base" 2>/dev/null; then RELEASE_SKIPPED+=("$c"); continue; fi
|
|
133
167
|
# shellcheck disable=SC2034 # read by cut-release.sh, which sources this file
|
|
134
168
|
RELEASE_START="$c"
|
|
135
|
-
|
|
136
|
-
[[ -n "$us" ]] || { left=(); break; }
|
|
169
|
+
[[ -n "$us" ]] || break
|
|
137
170
|
read -r -a listed <<<"$us"
|
|
138
171
|
keep=()
|
|
139
|
-
for u in "${left[@]}"; do in_array "$u" "${listed[@]}" || keep+=("$u"); done
|
|
172
|
+
for u in "${left[@]+"${left[@]}"}"; do in_array "$u" "${listed[@]}" || keep+=("$u"); done
|
|
140
173
|
left=("${keep[@]+"${keep[@]}"}")
|
|
141
|
-
(
|
|
174
|
+
keep=()
|
|
175
|
+
for first in "${firsts[@]+"${firsts[@]}"}"; do
|
|
176
|
+
if [[ "$c" == "$first" ]] || ! git merge-base --is-ancestor "$c" "$first"; then keep+=("$first"); fi
|
|
177
|
+
done
|
|
178
|
+
firsts=("${keep[@]+"${keep[@]}"}")
|
|
179
|
+
(( ${#left[@]} + ${#firsts[@]} )) || break
|
|
142
180
|
done < <(git log origin/main --format='%(trailers:key=Release-Train-Base,valueonly,unfold,separator=%x20)%x1f%(trailers:key=Release-Train-Units,valueonly,unfold,separator=%x20)' 2>/dev/null || true)
|
|
143
181
|
}
|
|
144
182
|
|
|
@@ -327,15 +365,23 @@ train_assign_stages() {
|
|
|
327
365
|
for u in "$@"; do printf '%s %s\n' "$u" "${stage_of[$u]}"; done
|
|
328
366
|
}
|
|
329
367
|
|
|
368
|
+
# releasable_commits_in <range> [<path>...] → success when <range> has a commit release-please would release: a feat, fix,
|
|
369
|
+
# perf, deps or revert commit or a breaking change that touches one of the paths (any path when none is given). The one rule
|
|
370
|
+
# behind unit_changed, the merge-back wait list (train_units_to_ship) and the pin base check (release_pin_open_pr).
|
|
371
|
+
releasable_commits_in() {
|
|
372
|
+
local range="$1"; shift
|
|
373
|
+
[[ -z "$(git log -1 --format=%H -E \
|
|
374
|
+
--grep='^(feat|fix|perf|deps|revert)(\([^)]*\))?!?:' --grep='^BREAKING[ -]CHANGE:' "$range" -- "$@")" ]] && return 1
|
|
375
|
+
return 0
|
|
376
|
+
}
|
|
377
|
+
|
|
330
378
|
# unit_changed <unit> <anchor> <ref> → whether <ref> has, since <anchor> (empty: the whole history), a
|
|
331
|
-
# commit release-please would release for the unit
|
|
332
|
-
# breaking change that touches the unit's path
|
|
379
|
+
# commit release-please would release for the unit (releasable_commits_in on the unit's path)
|
|
333
380
|
unit_changed() {
|
|
334
381
|
local p range="$3"
|
|
335
382
|
p="$(unit_path "$1")" || return 1
|
|
336
383
|
[[ -z "$2" ]] || range="$2..$3"
|
|
337
|
-
|
|
338
|
-
--grep='^(feat|fix|perf|deps|revert)(\([^)]*\))?!?:' --grep='^BREAKING[ -]CHANGE:' "$range" -- "$p")" ]]
|
|
384
|
+
releasable_commits_in "$range" "$p"
|
|
339
385
|
}
|
|
340
386
|
|
|
341
387
|
# stage_version <version> <stage> → the version a unit enters <stage> with, computed by the semver
|
|
@@ -372,6 +418,66 @@ train_stage() {
|
|
|
372
418
|
printf '%s\n' "$stage"
|
|
373
419
|
}
|
|
374
420
|
|
|
421
|
+
# train_units_to_ship [<ref>] → one component per line: the units the line opened with (the Release-Train-Units trailer of the opening
|
|
422
|
+
# commit on <ref>, default HEAD) minus those with no releasable commit since the start point of the line (unit_changed's rule, anchored
|
|
423
|
+
# where release-please counts from: last-release-sha of the config, the whole history when the config has none). Such a unit gets no
|
|
424
|
+
# Release PR entry and no tag, so waiting for its tag would never end. The list does not depend on the manifest: right after the
|
|
425
|
+
# line is opened no unit has been bumped yet, and a manifest-based list is empty and looks complete. Without a Release-Train-Units
|
|
426
|
+
# trailer the units of the config count.
|
|
427
|
+
train_units_to_ship() {
|
|
428
|
+
local ref="${1:-HEAD}" anchor u list
|
|
429
|
+
list="$(train_units_of "$ref")"
|
|
430
|
+
if [[ -z "$list" ]]; then list="$(config_units | tr '\n' ' ')"; fi
|
|
431
|
+
[[ -n "$list" ]] || return 0
|
|
432
|
+
anchor="$(git show "$ref:$CONFIG" 2>/dev/null | jq -r '."last-release-sha" // empty' 2>/dev/null || true)"
|
|
433
|
+
for u in $list; do
|
|
434
|
+
unit_changed "$u" "$anchor" "$ref" || continue
|
|
435
|
+
printf '%s\n' "$u"
|
|
436
|
+
done
|
|
437
|
+
return 0
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
# train_final_tags_missing [<ref>] → one line per unit the train ships (train_units_to_ship) whose final tag is not there yet, nothing
|
|
441
|
+
# when all are. A unit still on a prerelease version has not been through the GA Release PR; a unit whose manifest version is still
|
|
442
|
+
# the one origin/main has was not bumped yet (the line was just opened, and the tag of that old version exists); a unit on a new
|
|
443
|
+
# stable version needs its <component>/v<version> tag, which release-please makes when that PR is merged. Reads the local tags:
|
|
444
|
+
# fetch them first. A train that ships no unit has nothing missing.
|
|
445
|
+
train_final_tags_missing() {
|
|
446
|
+
local u p v mv
|
|
447
|
+
git rev-parse -q --verify origin/main >/dev/null || { printf '%s\n' "找不到 origin/main,無法判斷單元升版了沒"; return 0; }
|
|
448
|
+
while IFS= read -r u; do
|
|
449
|
+
[[ -n "$u" ]] || continue
|
|
450
|
+
p="$(unit_path "$u")" || { printf '%s\n' "$u 在設定裡找不到目錄"; continue; }
|
|
451
|
+
v="$(manifest_version "$p")"
|
|
452
|
+
if [[ "$v" == *-* ]]; then printf '%s\n' "$u 仍是 prerelease $v"; continue; fi
|
|
453
|
+
mv="$(git show "origin/main:$MANIFEST" | jq -r --arg p "$p" '.[$p] // empty')"
|
|
454
|
+
if [[ -n "$mv" && "$mv" == "$v" ]]; then printf '%s\n' "${u} 尚未升版(manifest 仍是 main 的 ${v})"; continue; fi
|
|
455
|
+
git rev-parse -q --verify "refs/tags/$u/v$v" >/dev/null || printf '%s\n' "$u/v$v"
|
|
456
|
+
done < <(train_units_to_ship "${1:-HEAD}")
|
|
457
|
+
return 0
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
# train_merge_back_ready [<ref>] → 0 when the merge-back of the line can be opened: the line stage on <ref> (default HEAD) is ga and
|
|
461
|
+
# train_final_tags_missing lists nothing. Otherwise 1, with TRAIN_MERGE_BACK_WAITING set to what is still missing. The release-please
|
|
462
|
+
# job asks it after every run, so it only looks and never waits. A train with no unit to ship counts as ready at ga.
|
|
463
|
+
TRAIN_MERGE_BACK_WAITING=""
|
|
464
|
+
# shellcheck disable=SC2034 # TRAIN_MERGE_BACK_WAITING is read by merge-back-ready.sh, which sources this file
|
|
465
|
+
train_merge_back_ready() {
|
|
466
|
+
local stage missing
|
|
467
|
+
TRAIN_MERGE_BACK_WAITING=""
|
|
468
|
+
stage="$(train_stage "${1:-HEAD}")"
|
|
469
|
+
if [[ "$stage" != ga ]]; then
|
|
470
|
+
TRAIN_MERGE_BACK_WAITING="發布線的階段是 ${stage},不是 ga"
|
|
471
|
+
return 1
|
|
472
|
+
fi
|
|
473
|
+
missing="$(train_final_tags_missing "${1:-HEAD}")"
|
|
474
|
+
if [[ -n "$missing" ]]; then
|
|
475
|
+
TRAIN_MERGE_BACK_WAITING="GA 尚未完成,缺:$(tr '\n' ' ' <<<"$missing")"
|
|
476
|
+
return 1
|
|
477
|
+
fi
|
|
478
|
+
return 0
|
|
479
|
+
}
|
|
480
|
+
|
|
375
481
|
# train_boarded_units → every unit the train was opened with, deferred ones included, from the newest
|
|
376
482
|
# Release-Train-Units trailer; the units of the config when there is none
|
|
377
483
|
train_boarded_units() {
|
|
@@ -477,6 +583,34 @@ train_commits_not_on_main() {
|
|
|
477
583
|
done < <(git cherry origin/main "$rev" "$open")
|
|
478
584
|
}
|
|
479
585
|
|
|
586
|
+
# train_commits_after_merge_back <rev> <product> → "<short sha> <subject>" for each commit on <rev> that came after the
|
|
587
|
+
# merge-back product commit (chore(<branch>): product v<product>, the newest one) and that origin/main does not have,
|
|
588
|
+
# judged the way train_commits_not_on_main judges: by patch and by content, never by ancestry. Close runs this before it
|
|
589
|
+
# tags: a commit pushed to the release branch after merge-back (a late hotfix) is in the tree the product tag would name,
|
|
590
|
+
# and main never got it. Prints nothing when <rev> has no product commit, then the version check of close decides.
|
|
591
|
+
train_commits_after_merge_back() {
|
|
592
|
+
local rev="$1" product="$2" anchor mark sha
|
|
593
|
+
local -A MAIN_VERSIONS=()
|
|
594
|
+
# No pipe into an early-exiting reader here: under pipefail the writer's SIGPIPE fails the function.
|
|
595
|
+
anchor="$(git log -1 --format=%H -E --grep="^chore\(.+\): product v${product//./\.}\$" "$rev")"
|
|
596
|
+
[[ -n "$anchor" ]] || return 0
|
|
597
|
+
while read -r mark sha; do
|
|
598
|
+
[[ "$mark" == + ]] || continue
|
|
599
|
+
if commit_matches_main "$sha"; then continue; fi
|
|
600
|
+
git log -1 --format='%h %s' "$sha"
|
|
601
|
+
done < <(git cherry origin/main "$rev" "$anchor")
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
# main_has_train_base <base> → success when a commit of origin/main carries "Release-Train-Base: <base>" as a trailer.
|
|
605
|
+
# There is no count limit: main moves on while the merge-back PR waits for its checks, so the commit can be many
|
|
606
|
+
# commits back. --grep narrows the walk to the commits that mention the base, so the cost does not grow with main.
|
|
607
|
+
# The text is read into a variable first: under pipefail, grep -q exiting early would fail the git log feeding it.
|
|
608
|
+
main_has_train_base() {
|
|
609
|
+
local base="$1" trailers
|
|
610
|
+
trailers="$(git log origin/main --grep="Release-Train-Base: ${base}" --format='%(trailers:key=Release-Train-Base,valueonly)')"
|
|
611
|
+
grep -qx "$base" <<<"$trailers"
|
|
612
|
+
}
|
|
613
|
+
|
|
480
614
|
# Bring the local train up to its remote. Fetch even under --dry-run; fetching only updates local refs.
|
|
481
615
|
train_sync() { # train_sync <branch>
|
|
482
616
|
local branch="$1"
|
|
@@ -486,6 +620,49 @@ train_sync() { # train_sync <branch>
|
|
|
486
620
|
run git merge --ff-only "origin/$branch"
|
|
487
621
|
}
|
|
488
622
|
|
|
623
|
+
# Helpers of the deploy gate in train_gates. They read and set the locals of train_gates (bash scopes locals dynamically).
|
|
624
|
+
# dg_need_slug <why>: sets dg_slug, the name of this repository, once; dies with <why> when it cannot be found.
|
|
625
|
+
dg_need_slug() {
|
|
626
|
+
[[ -z "$dg_slug" ]] || return 0
|
|
627
|
+
dg_slug="${GITHUB_REPOSITORY:-}"
|
|
628
|
+
[[ -n "$dg_slug" ]] || dg_slug="$(gh repo view --json nameWithOwner --jq .nameWithOwner 2>/dev/null)" \
|
|
629
|
+
|| die "部署閘無法得知倉名,${1}(設 GITHUB_REPOSITORY,或在有 origin 的倉內執行;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
630
|
+
}
|
|
631
|
+
# dg_ops_gh <gh args>...: gh for the calls that go to the ops repository. With DEPLOY_GATE_OPS_TOKEN set (advance-release.yml gets it from a
|
|
632
|
+
# second GitHub App token limited to the ops repository, read-only: the token of the release branch covers this repository alone, so
|
|
633
|
+
# a private ops repository answers 404 to it) the call uses that token; without it the environment's own GH_TOKEN, as before.
|
|
634
|
+
dg_ops_gh() {
|
|
635
|
+
if [[ -n "${DEPLOY_GATE_OPS_TOKEN:-}" ]]; then GH_TOKEN="$DEPLOY_GATE_OPS_TOKEN" gh "$@"; else gh "$@"; fi
|
|
636
|
+
}
|
|
637
|
+
# dg_variable <slug> <name>: the Actions variable <name> as a repository sees it, the repository's own first and the organization's only
|
|
638
|
+
# when the repository has none. Sets dg_val to "set:<value>" (empty when neither level has it) and dg_where to 倉層 or 組織層.
|
|
639
|
+
# Each list is read at most once per train_gates call, so DEPLOY_ENABLED and OPS_REPO share the queries. A list that cannot be read dies,
|
|
640
|
+
# because it must not be taken for "not set".
|
|
641
|
+
dg_variable() {
|
|
642
|
+
local slug="$1" name="$2" err text
|
|
643
|
+
if (( dg_repo_read == 0 )); then
|
|
644
|
+
err="$(mktemp)"
|
|
645
|
+
if ! dg_repo_vars="$(gh api --paginate "repos/${slug}/actions/variables" --jq '.variables[] | "\(.name)=\(.value)"' 2>"$err")"; then
|
|
646
|
+
text="$(cat "$err")"; rm -f "$err"
|
|
647
|
+
die "部署閘無法讀 ${slug} 的倉層變數 ${name}:${text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
648
|
+
fi
|
|
649
|
+
rm -f "$err"; dg_repo_read=1
|
|
650
|
+
fi
|
|
651
|
+
dg_where=倉層
|
|
652
|
+
dg_val="$(grep -m1 -E "^${name}=" <<<"$dg_repo_vars" | sed "s/^${name}=/set:/" || true)"
|
|
653
|
+
[[ -z "$dg_val" ]] || return 0
|
|
654
|
+
if (( dg_org_read == 0 )); then
|
|
655
|
+
err="$(mktemp)"
|
|
656
|
+
if ! dg_org_vars="$(gh api --paginate "repos/${slug}/actions/organization-variables" --jq '.variables[] | "\(.name)=\(.value)"' 2>"$err")"; then
|
|
657
|
+
text="$(cat "$err")"; rm -f "$err"
|
|
658
|
+
die "部署閘無法讀 ${slug} 可用的組織層變數 ${name}:${text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
659
|
+
fi
|
|
660
|
+
rm -f "$err"; dg_org_read=1
|
|
661
|
+
fi
|
|
662
|
+
dg_where=組織層
|
|
663
|
+
dg_val="$(grep -m1 -E "^${name}=" <<<"$dg_org_vars" | sed "s/^${name}=/set:/" || true)"
|
|
664
|
+
}
|
|
665
|
+
|
|
489
666
|
# Gates a train passes before it moves to <stage>, run after train_sync. The ci gate is called the
|
|
490
667
|
# GA gate when the train goes to ga, which is how the docs know it.
|
|
491
668
|
train_gates() { # train_gates <branch> <line> <stage>
|
|
@@ -589,7 +766,8 @@ train_gates() { # train_gates <branch> <line> <stage>
|
|
|
589
766
|
fi
|
|
590
767
|
|
|
591
768
|
# deploy gate: the deploy of the stage the train leaves must have succeeded before the train moves on (decision
|
|
592
|
-
# deploy-gate-before-advance).
|
|
769
|
+
# deploy-gate-before-advance). A deploy is two runs: deploy-prerelease writes the declaration into ops, and an ops-deploy run that covers the
|
|
770
|
+
# declaration puts it online, so both must have succeeded. The units are those ensure-stage-deploys.sh looks at: every app and service unit of
|
|
593
771
|
# the manifest with a prerelease version, whose tag is the stage before this one. A tag with nothing to deploy
|
|
594
772
|
# (scripts/deploy/route-tag.sh) is left out. Per tag one query for the newest deploy-prerelease run, which passes only
|
|
595
773
|
# when it is completed and successful: a run that is still going, no run, a tag that is not created and a failed query
|
|
@@ -601,11 +779,12 @@ train_gates() { # train_gates <branch> <line> <stage>
|
|
|
601
779
|
# the gate, because it must not be taken for "not enabled". When the file has no such line, or does not exist, the repository removed the
|
|
602
780
|
# condition and deploys whenever the workflow runs: the variable is not read and the gate checks the run as usual.
|
|
603
781
|
if (( DRY_RUN == 0 )) && [[ "${DEPLOY_GATE:-1}" == 1 ]]; then
|
|
604
|
-
local dg_unit dg_ver dg_tag dg_route dg_st dg_err dg_text dg_id dg_rest dg_concl dg_units
|
|
782
|
+
local dg_slug="" dg_repo_vars="" dg_org_vars="" dg_repo_read=0 dg_org_read=0 dg_where dg_unit dg_ver dg_tag dg_route dg_st dg_err dg_text dg_id dg_rest dg_concl dg_units dg_enabled="" dg_val dg_why dg_ops="" dg_sha dg_env dg_list dg_status dg_event dg_title dg_hit dg_vers
|
|
783
|
+
local -A dg_declared=()
|
|
605
784
|
dg_units="$(jq -r --slurpfile cfg "$REPO_ROOT/release-please-config.json" '
|
|
606
785
|
($cfg[0].packages) as $pkgs
|
|
607
786
|
| to_entries[]
|
|
608
|
-
| select(.key | startswith("apps/") or startswith("services/")
|
|
787
|
+
| select(.key | startswith("apps/") or startswith("services/"))
|
|
609
788
|
| select(.value | test("-"))
|
|
610
789
|
| "\($pkgs[.key].component // (.key | split("/")[1])) \(.value)"
|
|
611
790
|
' "$REPO_ROOT/.release-please-manifest.json")"
|
|
@@ -621,26 +800,10 @@ train_gates() { # train_gates <branch> <line> <stage>
|
|
|
621
800
|
fi
|
|
622
801
|
fi
|
|
623
802
|
if [[ -z "$dg_enabled" ]]; then
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
if ! dg_val="$(gh api --paginate "repos/${dg_slug}/actions/variables" \
|
|
629
|
-
--jq '.variables[] | select(.name == "DEPLOY_ENABLED") | "set:" + .value' 2>"$dg_err")"; then
|
|
630
|
-
dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
|
|
631
|
-
die "部署閘無法讀 ${dg_slug} 的倉層變數 DEPLOY_ENABLED:${dg_text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
632
|
-
fi
|
|
633
|
-
dg_why="倉層設為「${dg_val#set:}」"
|
|
634
|
-
if [[ -z "$dg_val" ]]; then
|
|
635
|
-
if ! dg_val="$(gh api --paginate "repos/${dg_slug}/actions/organization-variables" \
|
|
636
|
-
--jq '.variables[] | select(.name == "DEPLOY_ENABLED") | "set:" + .value' 2>"$dg_err")"; then
|
|
637
|
-
dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
|
|
638
|
-
die "部署閘無法讀 ${dg_slug} 可用的組織層變數 DEPLOY_ENABLED:${dg_text:-gh api 失敗}(查 GitHub App 的 Variables 讀取權限;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
639
|
-
fi
|
|
640
|
-
dg_why="組織層設為「${dg_val#set:}」"
|
|
641
|
-
[[ -n "$dg_val" ]] || dg_why="倉層與組織層都沒設"
|
|
642
|
-
fi
|
|
643
|
-
rm -f "$dg_err"
|
|
803
|
+
dg_need_slug "讀不到 DEPLOY_ENABLED"
|
|
804
|
+
dg_variable "$dg_slug" DEPLOY_ENABLED
|
|
805
|
+
dg_why="${dg_where}設為「${dg_val#set:}」"
|
|
806
|
+
[[ -n "$dg_val" ]] || dg_why="倉層與組織層都沒設"
|
|
644
807
|
dg_val="${dg_val#set:}"
|
|
645
808
|
if [[ "$(printf '%s' "$dg_val" | tr 'A-Z' 'a-z')" == true ]]; then dg_enabled=1; else dg_enabled=0; fi
|
|
646
809
|
fi
|
|
@@ -667,6 +830,72 @@ train_gates() { # train_gates <branch> <line> <stage>
|
|
|
667
830
|
[[ "$dg_concl" == success ]] \
|
|
668
831
|
|| die "部署閘未通過:${dg_tag} 最新一筆 deploy-prerelease run ${dg_id} 結論為 ${dg_concl}(查 gh run view ${dg_id})。修好部署後重跑,或以 DEPLOY_GATE=0 略過"
|
|
669
832
|
info "✓ ${dg_tag} 部署 run ${dg_id} 通過"
|
|
833
|
+
# deploy-prerelease only writes the declaration into ops; the unit goes online in an ops-deploy run, so one must have succeeded too.
|
|
834
|
+
# The repository that holds ops is found in this order: the environment variable OPS_REPO; the repository variable OPS_REPO, then the
|
|
835
|
+
# organization variable OPS_REPO (a poly repository sets it there for deploy-prerelease.yml, and a local run has no environment
|
|
836
|
+
# variable); this repository when none is set (a monorepo). The variable lists are the ones DEPLOY_ENABLED was read from, read once.
|
|
837
|
+
# The run that decides is the newest one that COVERS the unit, not the run of the declaration commit, for three reasons.
|
|
838
|
+
# - ops-deploy.yml has one concurrency group that cancels the older pending run, and every run aligns every unit the environment
|
|
839
|
+
# declares (ops-run.sh), so a cancelled run is normal and the next run puts its units online. A cancelled run is passed over.
|
|
840
|
+
# - A skipped run is not passed over. Its jobs carry the condition vars.DEPLOY_ENABLED == 'true' (templates/ops ops-deploy.yml), so a
|
|
841
|
+
# skipped newest run means ops-deploy is not enabled in that repository: nothing is deployed, nothing can be checked, and the gate
|
|
842
|
+
# lets the unit go with a line saying so, as it does for a deploy-prerelease that is not enabled. Skipping it to look at an older
|
|
843
|
+
# success would let a repository that switched ops-deploy off be judged by a run from before.
|
|
844
|
+
# - A monorepo dispatches ops-deploy at the head of main (run-prerelease.sh), so the headSha of the run is not the declaration commit.
|
|
845
|
+
# - When the declaration already exists writeback-ops.sh makes no commit, so there is no commit to look for by its message.
|
|
846
|
+
# A run covers the unit when the ops/versions.json at its headSha declares <unit> at <purpose>/<env> with the version of the tag,
|
|
847
|
+
# and the run aligns that environment: a push run aligns every environment, a workflow_dispatch run only the one in its input env,
|
|
848
|
+
# which ops-deploy.yml writes into the run name ("ops-deploy <env>", "ops-deploy push" on a push), the only place a run list shows it.
|
|
849
|
+
if [[ -z "$dg_ops" ]]; then
|
|
850
|
+
dg_ops="${OPS_REPO:-}"
|
|
851
|
+
if [[ -z "$dg_ops" ]]; then
|
|
852
|
+
dg_need_slug "讀不到 OPS_REPO;設環境變數 OPS_REPO 也可以"
|
|
853
|
+
dg_variable "$dg_slug" OPS_REPO
|
|
854
|
+
dg_ops="${dg_val#set:}"
|
|
855
|
+
[[ -n "$dg_ops" ]] || dg_ops="$dg_slug"
|
|
856
|
+
fi
|
|
857
|
+
fi
|
|
858
|
+
dg_env="$(sed -n 's/^env=//p' <<<"$dg_route")"
|
|
859
|
+
dg_err="$(mktemp)"
|
|
860
|
+
if ! dg_list="$(dg_ops_gh run list --repo "$dg_ops" --workflow ops-deploy.yml --branch "${DEFAULT_BRANCH:-main}" --limit 20 \
|
|
861
|
+
--json status,conclusion,databaseId,headSha,event,displayTitle \
|
|
862
|
+
--jq '.[] | "\(.status)|\(.conclusion // "")|\(.databaseId)|\(.headSha)|\(.event)|\(.displayTitle)"' 2>"$dg_err")"; then
|
|
863
|
+
dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
|
|
864
|
+
die "部署閘無法查詢 ${dg_ops} 的 ops-deploy 執行紀錄:${dg_text:-gh run list 失敗}(查 gh 權限與 token;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
865
|
+
fi
|
|
866
|
+
dg_hit=""
|
|
867
|
+
while IFS='|' read -r dg_status dg_concl dg_id dg_sha dg_event dg_title; do
|
|
868
|
+
[[ -n "$dg_id" ]] || continue
|
|
869
|
+
if [[ "$dg_event" == push ]]; then :
|
|
870
|
+
elif [[ "$dg_event" == workflow_dispatch && "$dg_title" == "ops-deploy ${dg_env}" ]]; then :
|
|
871
|
+
else continue; fi
|
|
872
|
+
if [[ -z "${dg_declared[$dg_sha]+x}" ]]; then
|
|
873
|
+
if ! dg_declared[$dg_sha]="$(dg_ops_gh api "repos/${dg_ops}/contents/ops/versions.json?ref=${dg_sha}" -H "Accept: application/vnd.github.raw+json" 2>"$dg_err")"; then
|
|
874
|
+
dg_text="$(cat "$dg_err")"; rm -f "$dg_err"
|
|
875
|
+
die "部署閘無法讀 ${dg_ops} 在 ${dg_sha:0:7} 的 ops/versions.json:${dg_text:-gh api 失敗}(查 gh 權限與 token;本機演練可用 DEPLOY_GATE=0 略過)"
|
|
876
|
+
fi
|
|
877
|
+
fi
|
|
878
|
+
dg_vers="$(jq -r --arg e "$dg_env" --arg u "$dg_unit" '
|
|
879
|
+
to_entries[] | select((.key | startswith("hosts/") | not) and (.key | endswith("/" + $e)))
|
|
880
|
+
| (.value // {})[$u] | select(. != null) | if type == "object" then .version else . end' <<<"${dg_declared[$dg_sha]}" 2>/dev/null || true)"
|
|
881
|
+
grep -Fxq -- "$dg_ver" <<<"$dg_vers" || continue
|
|
882
|
+
if [[ "$dg_status" == completed && "$dg_concl" == cancelled ]]; then continue; fi
|
|
883
|
+
dg_hit=1; break
|
|
884
|
+
done <<<"$dg_list"
|
|
885
|
+
rm -f "$dg_err"
|
|
886
|
+
if [[ -z "$dg_hit" ]]; then
|
|
887
|
+
die "部署閘未通過:${dg_ops} 最近 20 筆 ops-deploy run 沒有一筆成功套用 ${dg_unit} ${dg_ver} 到 ${dg_env}(被取消的不算)。派送 gh workflow run ops-deploy.yml --repo ${dg_ops} -f env=${dg_env},等它跑完再重跑,或以 DEPLOY_GATE=0 略過"
|
|
888
|
+
fi
|
|
889
|
+
if [[ "$dg_status" != completed ]]; then
|
|
890
|
+
die "部署閘未通過:${dg_tag} 的 ops-deploy run ${dg_id} 還在跑(狀態 ${dg_status})。等它跑完再重跑,或以 DEPLOY_GATE=0 略過"
|
|
891
|
+
fi
|
|
892
|
+
if [[ "$dg_concl" == skipped ]]; then
|
|
893
|
+
info "⊘ ${dg_tag} ops-deploy 沒啟用:${dg_ops} 的 ops-deploy run ${dg_id} 結論為 skipped(job 條件 vars.DEPLOY_ENABLED 不成立),沒有上線可核對,放行"
|
|
894
|
+
continue
|
|
895
|
+
fi
|
|
896
|
+
[[ "$dg_concl" == success ]] \
|
|
897
|
+
|| die "部署閘未通過:${dg_tag} 最新一筆套用它的 ops-deploy run ${dg_id} 結論為 ${dg_concl}(查 gh run view ${dg_id} --repo ${dg_ops})。修好後重跑,或以 DEPLOY_GATE=0 略過"
|
|
898
|
+
info "✓ ${dg_tag} 的 ops-deploy run ${dg_id} 通過"
|
|
670
899
|
done <<<"$dg_units"
|
|
671
900
|
fi
|
|
672
901
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Check who started a manual release workflow. The workflows that advance a stage or merge a Release PR call this before
|
|
3
|
+
# they run anything that writes, because a private repository on the free plan has no environment with required reviewers.
|
|
4
|
+
#
|
|
5
|
+
# usage: check-trigger-actor.sh
|
|
6
|
+
# env: TRIGGERING_ACTOR github.triggering_actor of the run. It is the account that started this run attempt: on a re-run
|
|
7
|
+
# it is the person who re-ran, while github.actor and github.event.sender stay the person who
|
|
8
|
+
# started the first attempt. A name list read against those two would let an account outside the
|
|
9
|
+
# list re-run a run that a listed account dispatched, so never pass them here.
|
|
10
|
+
# ALLOWED_ACTORS the accounts that may start the run, separated by commas, spaces or new lines
|
|
11
|
+
# (the repository variable RELEASE_TRIGGER_ACTORS). A GitHub App appears as <slug>[bot].
|
|
12
|
+
# Empty or unset: nobody is excluded.
|
|
13
|
+
# Exit 0 when the account is listed or the list is empty, 1 otherwise. Logins are compared without regard to case,
|
|
14
|
+
# as GitHub does.
|
|
15
|
+
set -euo pipefail
|
|
16
|
+
actor="${TRIGGERING_ACTOR:-}"
|
|
17
|
+
allowed="${ALLOWED_ACTORS:-}"
|
|
18
|
+
list="$(tr ',[:space:]' '\n\n' <<<"$allowed" | sed '/^$/d' | tr '[:upper:]' '[:lower:]')"
|
|
19
|
+
if [[ -z "$list" ]]; then
|
|
20
|
+
printf '%s\n' "→ RELEASE_TRIGGER_ACTORS 沒有設定,不限制誰可以觸發(觸發者 ${actor:-未知})"
|
|
21
|
+
exit 0
|
|
22
|
+
fi
|
|
23
|
+
if [[ -z "$actor" ]]; then
|
|
24
|
+
printf '%s\n' "✗ 讀不到觸發者(TRIGGERING_ACTOR 是空的),名單有設定時不放行" >&2
|
|
25
|
+
exit 1
|
|
26
|
+
fi
|
|
27
|
+
if grep -qxF -- "$(tr '[:upper:]' '[:lower:]' <<<"$actor")" <<<"$list"; then
|
|
28
|
+
printf '%s\n' "→ 觸發者 ${actor} 在名單內"
|
|
29
|
+
exit 0
|
|
30
|
+
fi
|
|
31
|
+
printf '%s\n' "✗ 觸發者 ${actor} 不在 RELEASE_TRIGGER_ACTORS 的名單內($(tr '\n' ' ' <<<"$list" | sed 's/ $//')),不執行" >&2
|
|
32
|
+
exit 1
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Decides whether the close workflow (close-release.yml) may close a release line now: merge the merge-back PR, tag the product
|
|
2
|
+
// version and delete the branches. It only reads the facts of the PR and of its checks and prints the decision; close-ready.sh
|
|
3
|
+
// gathers the facts with `gh`, so this file stays testable without a network.
|
|
4
|
+
//
|
|
5
|
+
// usage: node scripts/release/close-ready.mjs --input <file|-> [--config scripts/merge/merge-config.json]
|
|
6
|
+
// input JSON { pr, checkedSha, eventHeadSha, checks, changedFiles }
|
|
7
|
+
// pr from `gh pr view --json number,state,isDraft,headRefOid,headRefName,baseRefName,isCrossRepository`, read last
|
|
8
|
+
// checkedSha the head commit of the PR when the checks were read
|
|
9
|
+
// eventHeadSha the head_sha of the workflow_run event that started the run (github.event.workflow_run.head_sha), or null when
|
|
10
|
+
// a person started it. It is the commit the finished ci or pr-message ran for. Never github.sha, which is main.
|
|
11
|
+
// checks from `gh pr checks --json name,bucket`
|
|
12
|
+
// changedFiles the paths the PR changes (for the checks with a path filter)
|
|
13
|
+
// config the one auto-merge reads (scripts/merge/merge-config.json): requiredChecks and pathFiltered
|
|
14
|
+
// Output: one JSON line { action, reason, ... }. action is "close" (pr, sha and line are set) or "skip". A skip is the normal answer of
|
|
15
|
+
// most events, because ci and pr-message finish one after the other and the PR is not ready until both have: the workflow ends
|
|
16
|
+
// successfully and the event of the later one asks again.
|
|
17
|
+
// Exit status: 0 whenever a decision was made, 2 when the input or the config is unusable.
|
|
18
|
+
//
|
|
19
|
+
// Whether the PR can be merged (a conflict with main) is not decided here: finalize-release.sh --phase close looks at it right before
|
|
20
|
+
// it merges and stops with the message to run merge-back again.
|
|
21
|
+
import { readFileSync } from "node:fs";
|
|
22
|
+
import { resolve } from "node:path";
|
|
23
|
+
import { fileURLToPath } from "node:url";
|
|
24
|
+
import { MERGE_BACK_HEAD, requiredCheckProblems } from "../merge/decide.mjs";
|
|
25
|
+
|
|
26
|
+
const skip = (reason, extra = {}) => ({ action: "skip", reason, ...extra });
|
|
27
|
+
|
|
28
|
+
export function decideClose({ pr, checkedSha, eventHeadSha = null, checks = [], changedFiles = [] }, config) {
|
|
29
|
+
const number = pr.number;
|
|
30
|
+
if (pr.state !== "OPEN") return skip(`the pull request is ${pr.state}, not OPEN: nothing to close`, { pr: number });
|
|
31
|
+
if (!MERGE_BACK_HEAD.test(pr.headRefName)) return skip(`${pr.headRefName} is not a merge-back branch (release/vX.Y--to-main)`, { pr: number });
|
|
32
|
+
if (pr.baseRefName !== "main") return skip(`a merge-back PR targets main, this one targets ${pr.baseRefName}`, { pr: number });
|
|
33
|
+
if (pr.isCrossRepository) return skip("the pull request comes from a fork and cannot get the App token", { pr: number });
|
|
34
|
+
if (pr.isDraft) return skip("the pull request is a draft", { pr: number });
|
|
35
|
+
if (eventHeadSha && eventHeadSha !== pr.headRefOid) {
|
|
36
|
+
return skip(`the head moved after the finished run (the run was for ${eventHeadSha}, head now ${pr.headRefOid}): the checks of the new head will start this again`, { pr: number });
|
|
37
|
+
}
|
|
38
|
+
if (checkedSha !== pr.headRefOid) {
|
|
39
|
+
return skip(`a commit was pushed after the checks were read (checked ${checkedSha}, head now ${pr.headRefOid})`, { pr: number });
|
|
40
|
+
}
|
|
41
|
+
const { failed, pending, missing } = requiredCheckProblems(checks, changedFiles, config);
|
|
42
|
+
if (failed.length > 0) return skip(`required check failed: ${failed.join(", ")}`, { pr: number, failed });
|
|
43
|
+
if (pending.length > 0) return skip(`required check still running: ${pending.join(", ")}`, { pr: number });
|
|
44
|
+
if (missing.length > 0) return skip(`required check has not run: ${missing.join(", ")}`, { pr: number });
|
|
45
|
+
return {
|
|
46
|
+
action: "close",
|
|
47
|
+
reason: "every required check passed or was skipped, and the head is the one that was checked",
|
|
48
|
+
pr: number,
|
|
49
|
+
sha: pr.headRefOid,
|
|
50
|
+
line: /^release\/v(\d+\.\d+)--to-main$/.exec(pr.headRefName)[1],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const read = (p) => readFileSync(p === "-" ? 0 : p, "utf8");
|
|
55
|
+
|
|
56
|
+
function main(argv) {
|
|
57
|
+
const opt = (k, d) => (argv.includes(`--${k}`) ? argv[argv.indexOf(`--${k}`) + 1] : d);
|
|
58
|
+
const input = opt("input");
|
|
59
|
+
if (!input) throw new Error("missing --input <file|->");
|
|
60
|
+
const config = JSON.parse(read(opt("config", "scripts/merge/merge-config.json")));
|
|
61
|
+
if (!Array.isArray(config.requiredChecks) || config.requiredChecks.length === 0) throw new Error("the config needs a non-empty requiredChecks");
|
|
62
|
+
console.log(JSON.stringify(decideClose(JSON.parse(read(input)), config)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
66
|
+
try {
|
|
67
|
+
main(process.argv.slice(2));
|
|
68
|
+
} catch (e) {
|
|
69
|
+
console.error(`close-ready.mjs: ${e.message}`);
|
|
70
|
+
process.exit(2);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Whether the merge-back PR of a release line can be closed now (merged, product tag, branches deleted), read from GitHub. The close
|
|
3
|
+
# workflow (close-release.yml) runs it first and goes on only when it says ready=true. It looks once, reads only, and exits 0 for
|
|
4
|
+
# any answer, because a PR that is not ready yet is the normal case for most events (ci and pr-message finish one after the other).
|
|
5
|
+
#
|
|
6
|
+
# usage: close-ready.sh (--pr <number> | --line X.Y) [--head-sha <sha>] [--strict]
|
|
7
|
+
# --pr the PR of the event (github.event.workflow_run.pull_requests[0].number)
|
|
8
|
+
# --line a person started the run: the open PR of release/vX.Y--to-main is the one
|
|
9
|
+
# --head-sha the head_sha of the event (github.event.workflow_run.head_sha), the commit the finished ci or pr-message ran for.
|
|
10
|
+
# A PR whose head is another commit is not closed: the checks of its new head start this again.
|
|
11
|
+
# Leave it out when a person started the run; the head the checks were read for is then the head of the PR.
|
|
12
|
+
# --strict exit 1 when it is not ready (a person started the run and wants to see the failure), instead of 0
|
|
13
|
+
# stdout, to append to $GITHUB_OUTPUT: ready=true|false, reason=<one line>, and when ready: pr=, sha=, line=
|
|
14
|
+
# env: GH_TOKEN (read access to pull requests, checks and statuses), GH_REPO when the checkout has no remote of the repository,
|
|
15
|
+
# MERGE_CONFIG (default scripts/merge/merge-config.json, the file auto-merge reads for the required checks)
|
|
16
|
+
#
|
|
17
|
+
# The decision is scripts/release/close-ready.mjs, over the required checks of scripts/merge/decide.mjs (requiredCheckProblems), the
|
|
18
|
+
# same ones auto-merge reads. The order of the reads is the one of auto-merge: the head first, the checks, the PR last, so a commit
|
|
19
|
+
# pushed in between shows as a different head and the decision skips.
|
|
20
|
+
set -euo pipefail
|
|
21
|
+
source "$(dirname "$0")/../lib/common.sh"
|
|
22
|
+
cd "$REPO_ROOT"
|
|
23
|
+
|
|
24
|
+
pr="" line="" head_sha="" strict=0
|
|
25
|
+
while [[ $# -gt 0 ]]; do
|
|
26
|
+
case "$1" in
|
|
27
|
+
--pr) pr="$2"; shift 2 ;;
|
|
28
|
+
--line) line="$2"; shift 2 ;;
|
|
29
|
+
--head-sha) head_sha="$2"; shift 2 ;;
|
|
30
|
+
--strict) strict=1; shift ;;
|
|
31
|
+
-h|--help) sed -n '2,${/^#/!q; p;}' "$0"; exit 0 ;;
|
|
32
|
+
*) die "unknown argument: $1" ;;
|
|
33
|
+
esac
|
|
34
|
+
done
|
|
35
|
+
config="${MERGE_CONFIG:-scripts/merge/merge-config.json}"
|
|
36
|
+
out="$(mktemp -d)"
|
|
37
|
+
trap 'rm -rf "$out"' EXIT
|
|
38
|
+
|
|
39
|
+
not_ready() { # not_ready <reason>
|
|
40
|
+
warn "$1"
|
|
41
|
+
printf '%s\n' "ready=false" "reason=${1//$'\n'/ }"
|
|
42
|
+
(( strict == 0 )) || exit 1
|
|
43
|
+
exit 0
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if [[ -z "$pr" ]]; then
|
|
47
|
+
[[ "$line" =~ ^[0-9]+\.[0-9]+$ ]] || die "--pr 或 --line X.Y 要給一個"
|
|
48
|
+
pr="$(gh pr list --base main --head "release/v${line}--to-main" --state open --json number --jq '.[0].number // empty')"
|
|
49
|
+
[[ -n "$pr" ]] || not_ready "release/v${line}--to-main 沒有開著的合回 PR:先開合回 PR(merge-back),或它已經合併了"
|
|
50
|
+
fi
|
|
51
|
+
[[ "$pr" =~ ^[0-9]+$ ]] || not_ready "沒有 PR 編號(${pr})"
|
|
52
|
+
|
|
53
|
+
checked_sha="$(gh pr view "$pr" --json headRefOid --jq .headRefOid)"
|
|
54
|
+
# gh pr checks exits 1 for a failed check and 8 for a pending one, and the decision reads both from the output
|
|
55
|
+
rc=0; gh pr checks "$pr" --json name,bucket > "$out/checks.json" 2> "$out/checks.err" || rc=$?
|
|
56
|
+
cat "$out/checks.err" >&2
|
|
57
|
+
# a PR without any check yet: exit 1, nothing on stdout, "no checks reported" on stderr; the decision reads that as "has not run"
|
|
58
|
+
if (( rc == 1 )) && [[ ! -s "$out/checks.json" ]] && grep -q 'no checks reported on the' "$out/checks.err"; then printf '[]\n' > "$out/checks.json"; fi
|
|
59
|
+
(( rc == 0 || rc == 1 || rc == 8 )) || die "gh pr checks 結束碼 $rc"
|
|
60
|
+
# an empty or non-array output means the checks could not be read; it must not reach the decision as "no check ran"
|
|
61
|
+
jq -e 'type == "array"' "$out/checks.json" > /dev/null || die "gh pr checks 結束碼 ${rc},也沒有印出 checks 的 JSON 陣列"
|
|
62
|
+
gh pr diff "$pr" --name-only | jq -R . | jq -s . > "$out/files.json"
|
|
63
|
+
gh pr view "$pr" --json number,state,isDraft,headRefOid,headRefName,baseRefName,isCrossRepository \
|
|
64
|
+
| jq --arg sha "$checked_sha" --arg event "$head_sha" --slurpfile checks "$out/checks.json" --slurpfile files "$out/files.json" \
|
|
65
|
+
'{pr: ., checkedSha: $sha, eventHeadSha: (if $event == "" then null else $event end), checks: $checks[0], changedFiles: $files[0]}' > "$out/input.json"
|
|
66
|
+
node "$(dirname "$0")/close-ready.mjs" --input "$out/input.json" --config "$config" > "$out/decision.json"
|
|
67
|
+
reason="$(jq -r .reason "$out/decision.json")"
|
|
68
|
+
if [[ "$(jq -r .action "$out/decision.json")" != close ]]; then not_ready "$reason"; fi
|
|
69
|
+
info "可以收尾 PR #${pr}:${reason}" >&2
|
|
70
|
+
printf '%s\n' "ready=true" "reason=$reason" "pr=$(jq -r .pr "$out/decision.json")" "sha=$(jq -r .sha "$out/decision.json")" "line=$(jq -r .line "$out/decision.json")"
|