@futurelastic/colab-handbook 1.13.0-rc.9 → 1.13.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.
@@ -26,6 +26,19 @@
26
26
  * ship-ignore-workflows: [Deploy] set these aside too, even when push-triggered (a deploy
27
27
  * workflow that fires on a push to trunk). Wins over the gate
28
28
  * list for a name in both.
29
+ *
30
+ * A LOST PUSH (#567). The forge can drop a trunk commit's `push` event (measured: a ship's push landed
31
+ * during a window of HTTP 500s and CI never started for that sha). Re-running is impossible (there is
32
+ * no run), and dispatching the same CI workflow at that sha produced a `workflow_dispatch` run the
33
+ * rule above set aside — so trunk stayed unmeasured until a human wrote an empty commit. Now: a
34
+ * `workflow_dispatch` run COUNTS when (a) its workflow's own definition at that sha is triggered by a
35
+ * branch push or a pull request — i.e. it is the workflow that verifies the code, not a release lane
36
+ * that merely accepts a manual trigger — and (b) no push / pull-request run of that same workflow is
37
+ * at the sha. (b) keeps the rescue narrow: where the push run exists, it is the verdict, as before.
38
+ * The caller supplies (a) as `policy.dispatchVerifies(sha) → string[] | null` (the workflow names, read
39
+ * from the tree at that sha — this module does no I/O); null / absent / unreadable → nothing is
40
+ * rescued, the run stays set aside (fail closed). With `ship-gate-workflows` declared this rescue is
41
+ * moot: the gate list already counts its workflows whatever their trigger.
29
42
  */
30
43
 
31
44
  const VERIFYING_EVENTS = Object.freeze(['push', 'pull_request', 'pull_request_target', 'merge_group']);
@@ -77,17 +90,117 @@ function setAsideReason(row, policy) {
77
90
  return VERIFYING_EVENTS.includes(ev) ? null : `event: ${ev}`;
78
91
  }
79
92
 
80
- /** Split rows into `{ counted, setAside }`; each set-aside row carries `setAsideWhy`. Null in → null out. */
93
+ /**
94
+ * Split rows into `{ counted, setAside, dispatchCounted }`; each set-aside row carries `setAsideWhy`,
95
+ * and `dispatchCounted` lists the `workflow_dispatch` rows the lost-push rescue (#567) moved into
96
+ * `counted`. Null in → null out.
97
+ */
81
98
  function splitVerifying(rows, policy) {
82
99
  if (!Array.isArray(rows)) return null;
83
100
  const counted = [];
84
- const setAside = [];
101
+ let setAside = [];
85
102
  for (const r of rows) {
86
103
  const why = setAsideReason(r, policy);
87
104
  if (why) setAside.push({ ...r, setAsideWhy: why });
88
105
  else counted.push(r);
89
106
  }
90
- return { counted, setAside };
107
+ const dispatchCounted = [];
108
+ const resolver = policy && typeof policy.dispatchVerifies === 'function' ? policy.dispatchVerifies : null;
109
+ const candidates = setAside.filter((x) => x.setAsideWhy === 'event: workflow_dispatch' && x.workflowName
110
+ && !counted.some((c) => c.workflowName === x.workflowName));
111
+ if (resolver && candidates.length) {
112
+ const byShas = new Map();
113
+ const namesAt = (sha) => {
114
+ if (!byShas.has(sha)) {
115
+ let v = null;
116
+ try { v = resolver(sha); } catch (_) { v = null; }
117
+ byShas.set(sha, Array.isArray(v) ? v : null);
118
+ }
119
+ return byShas.get(sha);
120
+ };
121
+ const keep = [];
122
+ for (const x of setAside) {
123
+ const names = candidates.includes(x) && x.headSha ? namesAt(x.headSha) : null;
124
+ if (names && names.includes(x.workflowName)) {
125
+ const { setAsideWhy, ...row } = x;
126
+ counted.push(row);
127
+ dispatchCounted.push(row);
128
+ } else keep.push(x);
129
+ }
130
+ setAside = keep;
131
+ }
132
+ return { counted, setAside, dispatchCounted };
133
+ }
134
+
135
+ /** Strip a trailing ` # comment` and matching quotes from a one-line YAML scalar. */
136
+ function yamlScalar(raw) {
137
+ let v = String(raw).replace(/\s+#.*$/, '').trim();
138
+ if (v.length > 1 && ((v[0] === '"' && v.endsWith('"')) || (v[0] === "'" && v.endsWith("'")))) v = v.slice(1, -1);
139
+ return v;
140
+ }
141
+
142
+ const BRANCH_TRIGGERS = Object.freeze(['push', 'pull_request', 'pull_request_target', 'merge_group']);
143
+
144
+ /**
145
+ * What one workflow file says about itself (#567): `{ name, verifiesBranches }`, or null when the text
146
+ * has no top-level `on:` at all. `name` is the top-level `name:` (null when absent — GitHub then names
147
+ * the workflow by its path, which the caller knows). `verifiesBranches` is true when the triggers
148
+ * include a pull request, or a push that is not tags-only (`push: { tags: [...] }` with no
149
+ * `branches`/`branches-ignore` is a release lane, never a code verifier). A deliberately small reader
150
+ * of the three `on:` shapes (scalar, flow list, block map / block list) — not a YAML parser.
151
+ */
152
+ function workflowTriggers(text) {
153
+ const lines = String(text || '').split(/\r?\n/);
154
+ let name = null;
155
+ let onIdx = -1;
156
+ let onInline = null;
157
+ for (let i = 0; i < lines.length; i++) {
158
+ const l = lines[i];
159
+ let m = /^name:\s*(.*)$/.exec(l);
160
+ if (m && name === null) { name = yamlScalar(m[1]) || null; continue; }
161
+ m = /^(?:on|"on"|'on'|true):\s*(.*)$/.exec(l);
162
+ if (m && onIdx < 0) { onIdx = i; onInline = yamlScalar(m[1]); }
163
+ }
164
+ if (onIdx < 0) return null;
165
+ const events = new Map(); // event -> its child lines (block map form only)
166
+ if (onInline) {
167
+ const list = onInline.startsWith('[') ? onInline.replace(/^\[|\]$/g, '').split(',') : [onInline];
168
+ for (const e of list) { const v = yamlScalar(e); if (v) events.set(v, []); }
169
+ } else {
170
+ let evIndent = null;
171
+ let cur = null;
172
+ for (let i = onIdx + 1; i < lines.length; i++) {
173
+ const l = lines[i];
174
+ if (!l.trim() || /^\s*#/.test(l)) continue;
175
+ const indent = l.length - l.trimStart().length;
176
+ if (indent === 0) break;
177
+ if (evIndent === null) evIndent = indent;
178
+ if (indent < evIndent) break;
179
+ if (indent === evIndent) {
180
+ const t = l.trim();
181
+ const m = /^-\s*(.+)$/.exec(t) || /^([A-Za-z_]+)\s*:/.exec(t);
182
+ cur = m ? yamlScalar(m[1].replace(/:.*$/, '')) : null;
183
+ if (cur) events.set(cur, []);
184
+ } else if (cur) events.get(cur).push(l.trim());
185
+ }
186
+ }
187
+ let verifies = false;
188
+ for (const [ev, body] of events) {
189
+ if (!BRANCH_TRIGGERS.includes(ev)) continue;
190
+ if (ev !== 'push') { verifies = true; break; }
191
+ const keys = body.map((t) => (/^([A-Za-z_-]+)\s*:/.exec(t) || [])[1]).filter(Boolean);
192
+ const tagsOnly = (keys.includes('tags') || keys.includes('tags-ignore')) && !keys.includes('branches') && !keys.includes('branches-ignore');
193
+ if (!tagsOnly) { verifies = true; break; }
194
+ }
195
+ return { name, verifiesBranches: verifies };
196
+ }
197
+
198
+ /** The compact verdict-detail note for rows the lost-push rescue counted — '' when none. */
199
+ function dispatchCountedNote(rows) {
200
+ const list = Array.isArray(rows) ? rows : [];
201
+ if (!list.length) return '';
202
+ const names = list.map((x) => `${x.workflowName || 'unnamed'}${x.databaseId ? ` (run ${x.databaseId})` : ''}`);
203
+ return ` — counted ${list.length} workflow_dispatch run${list.length === 1 ? '' : 's'} of a push-verifying workflow with no push run at this sha (lost push event, #567): ${names.join(', ')}`;
91
204
  }
92
205
 
93
206
  /**
@@ -101,4 +214,4 @@ function setAsideNote(rows) {
101
214
  return ` — set aside ${list.length} run${list.length === 1 ? '' : 's'} that do${list.length === 1 ? 'es' : ''} not verify the code (post-CI release/deploy lane, #503): ${names.join(', ')}`;
102
215
  }
103
216
 
104
- module.exports = { VERIFYING_EVENTS, GATE_KEY, IGNORE_KEY, parsePolicy, setAsideReason, splitVerifying, setAsideNote };
217
+ module.exports = { VERIFYING_EVENTS, GATE_KEY, IGNORE_KEY, parsePolicy, setAsideReason, splitVerifying, setAsideNote, workflowTriggers, dispatchCountedNote };