blueprint-shared-templates 0.3.1 → 0.4.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.
@@ -18,6 +18,10 @@ name: auto-merge
18
18
  # pull_request_target runs this file and the scripts from the base branch, never from the pull request, so the
19
19
  # App token is not handed to code of the pull request. A pull request from a fork is never merged.
20
20
  # The names of the GitHub App variable and secret are written in when the repository is composed.
21
+ # run-name: a run of the workflow_run event has the head commit and branch of main, so its head sha says nothing about the pull request.
22
+ # The name carries the head sha of the pull request for both events, and the scenario finds the auto-merge runs of a pull request by it.
23
+ run-name: "auto-merge ${{ github.event.pull_request.head.sha || github.event.workflow_run.head_sha }}"
24
+
21
25
  on:
22
26
  pull_request_target:
23
27
  types: [labeled]
@@ -76,15 +80,21 @@ jobs:
76
80
  GH_TOKEN: ${{ github.token }}
77
81
  GH_REPO: ${{ github.repository }}
78
82
  PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
83
+ # the token of this job was made when the job started, so a repository that is not found is asked again (scripts/lib/retry.mjs)
84
+ RETRY_FRESH_TOKEN: "1"
79
85
  run: |
80
86
  if [[ -z "$PR_NUMBER" ]]; then
81
87
  echo "action=skip" >> "$GITHUB_OUTPUT"; echo "no pull request belongs to this event"; exit 0
82
88
  fi
89
+ # Every gh call below is a read, so scripts/lib/retry.mjs read asks again after a transient error of GitHub (HTTP 500, 502, 503,
90
+ # 504, a connect timeout such as `dial tcp ...: i/o timeout`), up to 5 attempts RETRY_SLEEP seconds apart (decision
91
+ # transient-error-shared-retry). Any other failure comes back as it was, with the exit status of gh, so the exit status 1 and 8
92
+ # of gh pr checks reach the judgement below unchanged.
83
93
  fields=number,state,isDraft,headRefOid,headRefName,baseRefName,isCrossRepository,labels,author,latestReviews
84
94
  mkdir -p "$RUNNER_TEMP/merge"
85
- checked_sha="$(gh pr view "$PR_NUMBER" --json headRefOid --jq .headRefOid)"
95
+ checked_sha="$(node scripts/lib/retry.mjs read -- gh pr view "$PR_NUMBER" --json headRefOid --jq .headRefOid)"
86
96
  # gh pr checks exits 1 for a failed check and 8 for a pending one; the decision reads both from the output
87
- rc=0; gh pr checks "$PR_NUMBER" --json name,bucket > "$RUNNER_TEMP/merge/checks.json" 2> "$RUNNER_TEMP/merge/checks.err" || rc=$?
97
+ rc=0; node scripts/lib/retry.mjs read -- gh pr checks "$PR_NUMBER" --json name,bucket > "$RUNNER_TEMP/merge/checks.json" 2> "$RUNNER_TEMP/merge/checks.err" || rc=$?
88
98
  cat "$RUNNER_TEMP/merge/checks.err" >&2
89
99
  # a pull request without any check yet: exit 1, nothing on stdout, "no checks reported" on stderr; the decision reads that as "has not run"
90
100
  if (( rc == 1 )) && [[ ! -s "$RUNNER_TEMP/merge/checks.json" ]] && grep -q 'no checks reported on the' "$RUNNER_TEMP/merge/checks.err"; then
@@ -93,8 +103,8 @@ jobs:
93
103
  (( rc == 0 || rc == 1 || rc == 8 )) || { echo "::error::gh pr checks exited $rc"; exit 1; }
94
104
  # an empty or non-array output means the checks could not be read; it must not reach the decision as "no check ran"
95
105
  jq -e 'type == "array"' "$RUNNER_TEMP/merge/checks.json" > /dev/null || { echo "::error::gh pr checks exited $rc and printed no JSON array of checks"; exit 1; }
96
- gh pr diff "$PR_NUMBER" --name-only | jq -R . | jq -s . > "$RUNNER_TEMP/merge/files.json"
97
- gh pr view "$PR_NUMBER" --json "$fields" \
106
+ node scripts/lib/retry.mjs read -- gh pr diff "$PR_NUMBER" --name-only | jq -R . | jq -s . > "$RUNNER_TEMP/merge/files.json"
107
+ node scripts/lib/retry.mjs read -- gh pr view "$PR_NUMBER" --json "$fields" \
98
108
  | jq --arg sha "$checked_sha" --slurpfile checks "$RUNNER_TEMP/merge/checks.json" --slurpfile files "$RUNNER_TEMP/merge/files.json" \
99
109
  '{pr: ., checkedSha: $sha, checks: $checks[0], changedFiles: $files[0]}' > "$RUNNER_TEMP/merge/input.json"
100
110
  node scripts/merge/decide.mjs --input "$RUNNER_TEMP/merge/input.json" | tee "$RUNNER_TEMP/merge/decision.json"
@@ -1,7 +1,8 @@
1
1
  // The one place that asks again after a transient error of GitHub (decision transient-error-shared-retry).
2
- // A transient error is HTTP 500, 502, 503 or 504 (gh and git word it differently), a connect timeout (Linux, macOS and Go wordings), a
2
+ // A transient error is HTTP 500, 502, 503 or 504 (gh and git word it differently; gh also words a bad answer of a request it makes itself
3
+ // as `non-200 OK status code: 503 Service Unavailable`, with no `HTTP` before the code), a connect timeout (Linux, macOS and Go wordings), a
3
4
  // connection that could not be made at all (gh's `error connecting to <host>`, git's `Could not resolve host`) and a break of the connection itself:
4
- // a TLS handshake timeout, a connection reset by the peer, and a Go net/http read that ends in EOF (`Get "<url>": EOF`, `unexpected EOF`).
5
+ // a TLS handshake timeout, a socket the host could not open (no local address, no network, no route), a connection reset by the peer, and a Go net/http read that ends in EOF (`Get "<url>": EOF`, `unexpected EOF`).
5
6
  // Nothing else counts: another 5xx, a 4xx, a refused write, a dropped credential and a text that only has the letters EOF in it (a
6
7
  // heredoc marker, EOFError) do not get better by asking again.
7
8
  //
@@ -11,14 +12,22 @@
11
12
  // CLI node retry.mjs <read|push|merge> -- <cmd> [args...]
12
13
  //
13
14
  // What each kind retries (a failure of any other text stops at once, with the error as it was):
14
- // read a transient error, what the caller adds through `also` (local.mjs adds the HTTP 404 of a run that was just dispatched), and
15
+ // read a transient error, the GraphQL timeout of GitHub (`Something went wrong while executing your query`), a command of the CLI
16
+ // that ran past its timeout (`Command Timeout`), in the CLI with RETRY_FRESH_TOKEN=1 a repository that is not found
17
+ // (FRESH_NOT_FOUND: the token was just made and not every GitHub server knows it yet; with a token that is not new it is the answer),
18
+ // what the caller adds through `also` (local.mjs adds the HTTP 404 of a run that was just dispatched), and
15
19
  // in the CLI an answer of a command with --json (and no --jq or --template) that exits 0 but is not whole JSON: gh has cut such
16
20
  // an answer short now and then, and asking again gets the whole of it
17
- // push a transient error, and never a force or a delete (--force, -f, --force-with-lease, --force-if-includes, --delete, -d, +refspec,
21
+ // push a transient error (not `unexpected end of JSON input`: a write may have happened), and never a force or a delete (--force, -f, --force-with-lease, --force-if-includes, --delete, -d, +refspec,
18
22
  // :ref, --mirror, --prune): the retry of a write that rewrites or removes history is a decision, not a reflex
19
- // merge a transient error and "Base branch was modified" (the base moved between the pull request being opened and merged)
23
+ // merge a transient error (not `unexpected end of JSON input`) and "Base branch was modified" (the base moved between the pull request being opened and merged)
20
24
  // dispatch a transient error, but first `exists()` asks whether the dispatch made its run; a run that is there is returned and the
21
25
  // dispatch is not made again. Without a nonce `exists` cannot be written, so such a dispatch is not retried at all.
26
+ // In the CLI every attempt of a read has a timeout of RETRY_TIMEOUT seconds (120 by default); a push or a merge has one only when
27
+ // RETRY_TIMEOUT is set, because a push runs the pre-push hook of the repository, which may build and check for longer than any default
28
+ // (attemptTimeoutOf). A command that runs longer is killed with SIGKILL
29
+ // (a gh that hangs may ignore SIGTERM, and then the sequence would still wait), one line on stderr says so, and the failure text is
30
+ // `Command Timeout`. Only a read asks again after it; a push or a merge stops, because the write may have happened.
22
31
  // The pause between rounds is the caller's (`pause`): this file sleeps only in the CLI, whose interval is RETRY_SLEEP seconds.
23
32
  import { spawnSync } from "node:child_process";
24
33
  import { resolve } from "node:path";
@@ -33,6 +42,8 @@ const CODES = ["500", "502", "503", "504"];
33
42
  export function transientOf(text) {
34
43
  const s = String(text ?? "");
35
44
  const codes = [...s.matchAll(/\bHTTP\/?\s*(\d{3})\b/gi), ...s.matchAll(/requested URL returned error:\s*(\d{3})\b/gi)].map((m) => m[1]);
45
+ // gh's `error fetching labels: non-200 OK status code: 503 Service Unavailable`: the same error as `HTTP 503`
46
+ codes.push(...[...s.matchAll(/non-200 OK status code:\s*(\d{3})\b/gi)].map((m) => m[1]));
36
47
  const code = codes.find((c) => CODES.includes(c));
37
48
  if (code) return `HTTP ${code}`;
38
49
  // the connect timeout in its three wordings: Linux `connect: connection timed out`, macOS `connect: operation timed out`, and Go's
@@ -41,6 +52,9 @@ export function transientOf(text) {
41
52
  // a connection that was not made: gh prints `error connecting to api.github.com` for a failed dial or lookup, git and curl print
42
53
  // `Could not resolve host: github.com`
43
54
  if (/\berror connecting to [\w-]+(?:\.[\w-]+)+|Could not resolve host:/i.test(s)) return "Connect Failure";
55
+ // a socket the host could not open, only after the `connect:`, `read:` or `write:` of a Go socket error: no free local address (macOS
56
+ // `can't assign requested address`, Linux `cannot assign requested address`), no network and no route
57
+ if (/\b(?:connect|read|write): (?:can't|cannot) assign requested address|\bconnect: (?:network is unreachable|no route to host)/i.test(s)) return "Connect Failure";
44
58
  if (/TLS handshake timeout/i.test(s)) return "TLS Handshake Timeout";
45
59
  // EOF only in the shape of a dropped connection: after the quoted URL of a Go request, after `read tcp ...:`, or as `unexpected EOF`
46
60
  if (/connection reset by peer|"\s*:\s*(?:unexpected\s+)?EOF\b|\b(?:read|write) tcp [^\n]*:\s*EOF\b|\bunexpected EOF\b/i.test(s)) return "Connection Reset";
@@ -49,6 +63,22 @@ export function transientOf(text) {
49
63
 
50
64
  export const BASE_MODIFIED = "Base branch was modified";
51
65
  export const INCOMPLETE_JSON = "Incomplete JSON";
66
+ // what gh prints when it cannot parse the answer of a request (exit status non-zero); only a read asks again after it
67
+ export const GH_JSON_END = "unexpected end of JSON input";
68
+ // the GraphQL errors of GitHub that go away when asked again (a read only: a write may have happened)
69
+ export const GRAPHQL_TRANSIENT = ["Something went wrong while executing your query"];
70
+ // what git and gh answer for a repository the token cannot see yet: asked again only for a token that was just made (RETRY_FRESH_TOKEN=1 in
71
+ // the CLI, notFoundOfFreshToken in scripts/sync/sync.mjs), because a 404 of a token that is not new is the answer
72
+ export const FRESH_NOT_FOUND = /Repository not found|Could not resolve to a Repository|HTTP 404/i;
73
+ export const FRESH_NOT_FOUND_REASON = "Repository not found (fresh token)";
74
+ // the failure text of an attempt of the CLI that ran past its timeout
75
+ export const COMMAND_TIMEOUT = "Command Timeout";
76
+ export const TIMEOUT_SECONDS = 120;
77
+ // The timeout of one attempt of the CLI in seconds, or 0 for none: RETRY_TIMEOUT when it is set, else TIMEOUT_SECONDS for a read and none for a write
78
+ export function attemptTimeoutOf(kind, env = {}) {
79
+ if (env.RETRY_TIMEOUT !== undefined && env.RETRY_TIMEOUT !== "") return Number(env.RETRY_TIMEOUT);
80
+ return kind === "read" ? TIMEOUT_SECONDS : 0;
81
+ }
52
82
 
53
83
  // For a read whose command asks for JSON (--json) and prints it as it is (no --jq, -q, --template, -t): the parse error of what it printed,
54
84
  // or null when it is whole JSON or the command does not ask for JSON
@@ -84,6 +114,9 @@ export function retryMove({ kind, text, round, rounds = ROUNDS, argv = [], also
84
114
  let reason = transientOf(text);
85
115
  if (reason === null && kind === "merge" && new RegExp(BASE_MODIFIED, "i").test(String(text ?? ""))) reason = BASE_MODIFIED;
86
116
  if (reason === null && kind === "read" && String(text ?? "").includes(INCOMPLETE_JSON)) reason = INCOMPLETE_JSON;
117
+ if (reason === null && kind === "read" && String(text ?? "").includes(GH_JSON_END)) reason = GH_JSON_END;
118
+ if (reason === null && kind === "read") reason = GRAPHQL_TRANSIENT.find((g) => String(text ?? "").includes(g)) ?? null;
119
+ if (reason === null && kind === "read" && String(text ?? "").includes(COMMAND_TIMEOUT)) reason = COMMAND_TIMEOUT;
87
120
  if (reason === null && kind === "read" && also) reason = also(String(text ?? "")) ?? null;
88
121
  if (reason === null) return { move: "stop", reason: null, why: "not-transient" };
89
122
  if (kind === "push" && rewritesRemote(argv)) return { move: "stop", reason, why: "force" };
@@ -173,8 +206,14 @@ export async function main(args, { env = process.env, out = process.stdout, err
173
206
  return 2;
174
207
  }
175
208
  const seconds = Number(env.RETRY_SLEEP ?? SLEEP_SECONDS);
209
+ const limit = attemptTimeoutOf(kind, env);
176
210
  const attempt = async () => {
177
- const r = spawnSync(cmd[0], cmd.slice(1), { encoding: "utf8", stdio: ["inherit", "pipe", "pipe"], maxBuffer: 256 * 1024 * 1024, env });
211
+ const r = spawnSync(cmd[0], cmd.slice(1), { encoding: "utf8", stdio: ["inherit", "pipe", "pipe"], maxBuffer: 256 * 1024 * 1024, env, ...(limit > 0 ? { timeout: limit * 1000, killSignal: "SIGKILL" } : {}) });
212
+ if (r.error?.code === "ETIMEDOUT") {
213
+ const stderr = `${r.stderr ?? ""}${cmd[0]} 超過 ${limit} 秒沒有結束,已終止\n${COMMAND_TIMEOUT}\n`;
214
+ err.write(`${cmd[0]} 超過 ${limit} 秒沒有結束,已終止\n`);
215
+ throw new CommandFailed({ status: 124, stderr });
216
+ }
178
217
  if (r.error) throw new CommandFailed({ status: 127, stderr: `${cmd[0]}: ${r.error.message}\n` });
179
218
  const cut = kind === "read" && r.status === 0 ? jsonProblem(cmd, r.stdout ?? "") : null;
180
219
  if (cut !== null) {
@@ -190,6 +229,7 @@ export async function main(args, { env = process.env, out = process.stdout, err
190
229
  try {
191
230
  await retryTransient({
192
231
  kind, attempt, argv: cmd,
232
+ also: kind === "read" && env.RETRY_FRESH_TOKEN === "1" ? (text) => (FRESH_NOT_FOUND.test(text) ? FRESH_NOT_FOUND_REASON : null) : null,
193
233
  pause: async (round, reason) => {
194
234
  err.write(`! 第 ${round} 次失敗(${reason}),${seconds} 秒後重試\n`);
195
235
  await sleep(seconds);
@@ -1,5 +1,5 @@
1
1
  {
2
- "description": "`ops/targets.json` 的格式,組織層與產品層共用。`hosts` 宣告位置與它的能力,`instances` 宣告實例。實例的位置二選一:自己寫 `host`(放在母叢集或租戶裡時加 `cluster`),或用 `ref` 指向別的倉宣告的實例。`host` 指向別的倉宣告的位置時加 `<RepoName>:` 前綴。只有 `shared` 實例必寫 `serves`。這份 schema 驗證鍵的格式、`host` 與 `ref` 二選一、`serves` 只在 `shared` 實例、`provider` 是 `orbstack` 時只能有一個母叢集。三項由程式檢查:`ref` 指到的實例 `kind` 相同、`host` 與 `cluster` 指到的位置與母叢集存在、帶環境後綴的上層裡實例環境相同。",
2
+ "description": "`ops/targets.json` 的格式,組織層與產品層共用。`hosts` 宣告位置與它的能力,`instances` 宣告實例。實例的位置二選一:自己寫 `host`(放在母叢集或租戶裡時加 `cluster`),或用 `ref` 指向別的倉宣告的實例。`host` 指向別的倉宣告的位置時加 `<RepoName>:` 前綴。這份 schema 驗證鍵的格式、`host` 與 `ref` 二選一、`provider` 是 `orbstack` 時只能有一個母叢集。三項由程式檢查:`ref` 指到的實例 `kind` 相同、`host` 與 `cluster` 指到的位置與母叢集存在、帶環境後綴的上層裡實例環境相同。",
3
3
  "type": "object",
4
4
  "additionalProperties": false,
5
5
  "properties": {
@@ -99,31 +99,9 @@
99
99
  "pattern": "^(hosts|clusters)/"
100
100
  }
101
101
  },
102
- "patternProperties": {
103
- "^[a-z0-9][a-z0-9-]*/shared$": {
104
- "allOf": [
105
- {
106
- "$ref": "#/$defs/instance"
107
- }
108
- ],
109
- "required": [
110
- "serves"
111
- ]
112
- },
113
- "^[a-z0-9][a-z0-9-]*/(dev|stg|prod)$": {
114
- "allOf": [
115
- {
116
- "$ref": "#/$defs/instance"
117
- }
118
- ],
119
- "not": {
120
- "required": [
121
- "serves"
122
- ]
123
- }
124
- }
125
- },
126
- "additionalProperties": false
102
+ "additionalProperties": {
103
+ "$ref": "#/$defs/instance"
104
+ }
127
105
  }
128
106
  },
129
107
  "$defs": {
@@ -158,19 +136,6 @@
158
136
  "type": "string",
159
137
  "pattern": "^[A-Za-z0-9_.-]+:[a-z0-9][a-z0-9-]*/(dev|stg|prod|shared)$",
160
138
  "description": "別的倉宣告的實例 `<RepoName>:<用途>/<環境>`"
161
- },
162
- "serves": {
163
- "type": "array",
164
- "minItems": 1,
165
- "uniqueItems": true,
166
- "items": {
167
- "enum": [
168
- "dev",
169
- "stg",
170
- "prod"
171
- ]
172
- },
173
- "description": "`shared` 實例服務的環境"
174
139
  }
175
140
  },
176
141
  "oneOf": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blueprint-shared-templates",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Templates shared by several products, published on its own",
5
5
  "license": "MIT",
6
6
  "author": "Carlos Space",
@@ -141,6 +141,7 @@ jobs:
141
141
  gh workflow run deploy-prerelease.yml --ref "$tag" -f tag="$tag"
142
142
  done
143
143
 
144
+ # @if publish
144
145
  # packages/*、libs/*、facts 與 docs 匯總套件 → 每個階段都發布(publish.yml)。哪些路徑真的是要發布的單元,
145
146
  # 由 publish.yml 的 plan job 判斷(libs/* 有的不發布)。alpha、beta、rc 掛在階段名的 npm dist-tag,GA 才進
146
147
  # latest,所以使用者要明確指定才會裝到預發布版。同一條發布線的單元可以停在不同階段(docs/versioning.md),
@@ -152,6 +153,7 @@ jobs:
152
153
  GH_TOKEN: ${{ github.token }}
153
154
  PATHS: ${{ steps.rp.outputs.paths_released }}
154
155
  run: bash scripts/release/dispatch-publish.sh
156
+ # @end
155
157
 
156
158
  # 釘版:這次 run 建立或更新了 Release PR(prs_created 在建立與只更新時都是 'true',沒有變動時是 'false'),而且沒有建 tag。
157
159
  # 釘版中途失敗後重跑同一個 job 時,release-please 沒有東西可更新,prs_created 是 'false',所以 run_attempt 大於 1
@@ -8,15 +8,34 @@
8
8
  # 好幾個迴圈,每個各自有這個上限。
9
9
  POLL_TRIES="${POLL_TRIES:-180}" POLL_SLEEP="${POLL_SLEEP:-3}"
10
10
 
11
- # wait_release_pr <base-branch> → prints PR number.
11
+ # gh_read <gh args...>: a read of gh through retry.mjs (a transient error is asked again). Sets GH_READ_OUT (stdout) and GH_READ_ERR
12
+ # (stderr), returns the status. Call it in the shell itself, not in $(...), to keep the variables. A caller that gets a non-zero status
13
+ # must stop: a failed read is not "nothing there".
14
+ gh_read() {
15
+ local errf status=0
16
+ errf="$(mktemp)"
17
+ GH_READ_OUT="$(node "$RETRY_MJS" read -- gh "$@" 2>"$errf")" || status=$?
18
+ GH_READ_ERR="$(cat "$errf")"
19
+ rm -f "$errf"
20
+ return "$status"
21
+ }
22
+
23
+ # wait_release_pr <base-branch> → prints PR number. Returns 1 when none appeared in time, and 2 when gh could not be read
24
+ # (it then prints the reason instead of a number).
12
25
  # Identify the Release PR by its head branch, not by the "autorelease: pending" label: release-please
13
26
  # opens the PR first and labels it afterwards, so a run that fails in between leaves a real, usable
14
27
  # Release PR with no label at all (observed 2026-09-07).
15
28
  wait_release_pr() {
16
29
  local base="$1" n="" i
17
30
  for ((i=1; i<=POLL_TRIES; i++)); do
18
- n="$(gh pr list --base "$base" --head "release-please--branches--$base" --state open --json number --jq '.[0].number // empty' 2>/dev/null || true)"
19
- [[ -z "$n" ]] && n="$(gh pr list --base "$base" --label 'autorelease: pending' --state open --json number --jq '.[0].number // empty' 2>/dev/null || true)"
31
+ gh_read pr list --base "$base" --head "release-please--branches--$base" --state open --json number --jq '.[0].number // empty' \
32
+ || { printf '%s\n' "讀不到 ${base} 的 Release PR(gh pr list 失敗):${GH_READ_ERR}"; return 2; }
33
+ n="$GH_READ_OUT"
34
+ if [[ -z "$n" ]]; then
35
+ gh_read pr list --base "$base" --label 'autorelease: pending' --state open --json number --jq '.[0].number // empty' \
36
+ || { printf '%s\n' "讀不到 ${base} 的 Release PR(gh pr list 失敗):${GH_READ_ERR}"; return 2; }
37
+ n="$GH_READ_OUT"
38
+ fi
20
39
  [[ -n "$n" ]] && { printf '%s\n' "$n"; return 0; }
21
40
  sleep "$POLL_SLEEP"
22
41
  done
@@ -155,13 +174,17 @@ release_pr_settle() {
155
174
  log "等待 $base 上的 release-please 執行完畢(避免合併過期的 Release PR)…"
156
175
  wait_release_please_idle "$base" "$pushed" || die "release-please 還沒處理完 ${base},不繼續。等它處理完再重跑"
157
176
  log "等待 release-please 在 $base 開 Release PR…"
158
- if ! n="$(wait_release_pr "$base")"; then
177
+ local wait_status=0
178
+ n="$(wait_release_pr "$base")" || wait_status=$?
179
+ if (( wait_status == 2 )); then die "$n"; fi
180
+ if (( wait_status != 0 )); then
159
181
  (( optional )) && { info "$base 目前沒有可發版的變更,略過"; return 1; }
160
182
  die "逾時:$base 上沒有 Release PR(查 gh run list --workflow release-please.yml)"
161
183
  fi
162
184
  for ((i=1; i<=POLL_TRIES; i++)); do
163
185
  wait_release_please_idle "$base" "$pushed" || die "release-please 還沒處理完 ${base},不繼續。等它處理完再重跑"
164
- sha="$(gh pr view "$n" --json headRefOid --jq .headRefOid 2>/dev/null || true)"
186
+ gh_read pr view "$n" --json headRefOid --jq .headRefOid || die "讀不到 Release PR #${n} 的 head(gh pr view 失敗):${GH_READ_ERR}"
187
+ sha="$GH_READ_OUT"
165
188
  if [[ -n "$sha" && "$sha" == "$prev" ]]; then
166
189
  (( expect_stable )) || break
167
190
  mf="$(release_pr_manifest "$base" || true)"
@@ -266,7 +266,14 @@ BODY
266
266
 
267
267
  # 1. 合併 release PR。合併訊息由這裡指定:GitHub 預設的 squash 訊息會在 PR 內文後面接上各 commit 的
268
268
  # 標題,Release-Train-Base 就不在最後一段,git 讀不成 trailer(v0.5.0 實際發生,見待辦 i030)。
269
- pr="$(gh pr list --base main --head "$sync_branch" --state open --json number --jq '.[0].number // empty' 2>/dev/null || true)"
269
+ # 讀開著的合回 PR 失敗要停下:把 gh 的錯誤當成「沒有 PR」,PR 明明開著卻不合併,後面會誤報成找不到合併後的 commit。
270
+ # 讀取走 retry.mjs(暫時性錯誤會重試)。空值只在 gh 成功、真的沒有開著的 PR 時出現(例如 PR 已被另一次 close 合併)。
271
+ pr_err="$(mktemp)"
272
+ pr="$(node "$RETRY_MJS" read -- gh pr list --base main --head "$sync_branch" --state open --json number --jq '.[0].number // empty' 2>"$pr_err")" || {
273
+ pr_msg="$(cat "$pr_err")"; rm -f "$pr_err"
274
+ die "讀不到開著的 release PR(gh 失敗),沒有合併、打 tag 或刪分支:${pr_msg}"
275
+ }
276
+ rm -f "$pr_err"
270
277
  if [[ -n "$pr" ]]; then
271
278
  pr_json="$(gh pr view "$pr" --json body,headRefOid,mergeable)"
272
279
  body="$(jq -r .body <<<"$pr_json")"