@zgeoff/chex 0.4.2 → 0.4.4
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/chex",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Run each shared docs and repo check as one subcommand, with the lefthook pre-push baseline that calls them",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"checks",
|
|
@@ -83,9 +83,9 @@
|
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"@types/bun": "1.4.4",
|
|
86
|
-
"@zgeoff/rig": "0.3.
|
|
86
|
+
"@zgeoff/rig": "0.3.1",
|
|
87
87
|
"@zgeoff/std": "0.4.5",
|
|
88
|
-
"@zgeoff/testing": "0.4.
|
|
88
|
+
"@zgeoff/testing": "0.4.1",
|
|
89
89
|
"@zgeoff/tools-scripts": "0.0.0",
|
|
90
90
|
"msw": "3.0.4",
|
|
91
91
|
"tiny-invariant": "1.3.3",
|
|
@@ -129,7 +129,8 @@ function isChecksGroup(context: WorkflowContext, concurrency: unknown): boolean
|
|
|
129
129
|
return (
|
|
130
130
|
isCancelling(concurrency) ||
|
|
131
131
|
isGateGroup(context, concurrency) ||
|
|
132
|
-
isDispatchGroup(context, concurrency)
|
|
132
|
+
isDispatchGroup(context, concurrency) ||
|
|
133
|
+
findPushSplitGroup(context, concurrency) !== undefined
|
|
133
134
|
);
|
|
134
135
|
}
|
|
135
136
|
|
|
@@ -147,13 +148,27 @@ function isGateGroup(context: WorkflowContext, concurrency: unknown): boolean {
|
|
|
147
148
|
|
|
148
149
|
// A gate on main runs once for each commit, so a newer commit must not cancel the older run.
|
|
149
150
|
function isMainGate(context: WorkflowContext): boolean {
|
|
151
|
+
return (
|
|
152
|
+
!pullRequestTriggers.some((name) => context.triggers.has(name)) &&
|
|
153
|
+
hasPushBranches(context) &&
|
|
154
|
+
isPushLimitedToMain(context)
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function hasPushBranches(context: WorkflowContext): boolean {
|
|
159
|
+
const push = findMember(findMember(context.workflow, 'on'), 'push');
|
|
160
|
+
|
|
161
|
+
return [findMember(push, 'branches')].flat().length > 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// A workflow with no push trigger has no push run to limit.
|
|
165
|
+
function isPushLimitedToMain(context: WorkflowContext): boolean {
|
|
150
166
|
const push = findMember(findMember(context.workflow, 'on'), 'push');
|
|
151
167
|
const branches = [findMember(push, 'branches')].flat();
|
|
152
168
|
|
|
153
169
|
return (
|
|
154
|
-
!
|
|
155
|
-
branches.length > 0 &&
|
|
156
|
-
branches.every((branch) => branch === 'main')
|
|
170
|
+
!context.triggers.has('push') ||
|
|
171
|
+
(branches.length > 0 && branches.every((branch) => branch === 'main'))
|
|
157
172
|
);
|
|
158
173
|
}
|
|
159
174
|
|
|
@@ -217,6 +232,91 @@ function isEventExpressionTrue(text: string, event: string): boolean {
|
|
|
217
232
|
);
|
|
218
233
|
}
|
|
219
234
|
|
|
235
|
+
// A pull-request run takes its group from the first value, so a newer run of its ref cancels it.
|
|
236
|
+
// A push run to main takes the second, which may hold github.sha, so no push run cancels another.
|
|
237
|
+
// Only those two events may trigger it, because any other event takes the second group.
|
|
238
|
+
const pushSplitGroupPattern = /^(?<prefix>[^$]*)\$\{\{(?<body>.+)\}\}(?<suffix>[^$]*)$/su;
|
|
239
|
+
const pullRequestTestPattern = /^github\.event_name\s*==\s*'pull_request'$/u;
|
|
240
|
+
const pushSplitCancelPattern = /^\s*\$\{\{\s*github\.event_name\s*==\s*'pull_request'\s*\}\}\s*$/u;
|
|
241
|
+
|
|
242
|
+
interface PushSplitGroup {
|
|
243
|
+
pullRequest: string;
|
|
244
|
+
push: string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function findPushSplitGroup(
|
|
248
|
+
context: WorkflowContext,
|
|
249
|
+
concurrency: unknown,
|
|
250
|
+
): PushSplitGroup | undefined {
|
|
251
|
+
const group = findMember(concurrency, 'group');
|
|
252
|
+
const cancel = findMember(concurrency, 'cancel-in-progress');
|
|
253
|
+
const parts = typeof group === 'string' ? pushSplitGroupPattern.exec(group)?.groups : undefined;
|
|
254
|
+
const split = splitTopLevel(parts?.['body'] ?? '');
|
|
255
|
+
const [test = '', pullRequest = '', push = ''] = split?.operands ?? [];
|
|
256
|
+
const prefix = parts?.['prefix'] ?? '';
|
|
257
|
+
const suffix = parts?.['suffix'] ?? '';
|
|
258
|
+
|
|
259
|
+
return typeof cancel === 'string' &&
|
|
260
|
+
pushSplitCancelPattern.test(cancel) &&
|
|
261
|
+
split?.operators.join('') === '&&||' &&
|
|
262
|
+
pullRequestTestPattern.test(test.trim()) &&
|
|
263
|
+
isPullRequestGroup(`\${{ ${pullRequest} }}`) &&
|
|
264
|
+
context.triggers.size === 2 &&
|
|
265
|
+
context.triggers.has('pull_request') &&
|
|
266
|
+
context.triggers.has('push') &&
|
|
267
|
+
isPushLimitedToMain(context)
|
|
268
|
+
? {
|
|
269
|
+
pullRequest: `${prefix}\${{ ${pullRequest} }}${suffix}`,
|
|
270
|
+
push: `${prefix}\${{ ${push} }}${suffix}`,
|
|
271
|
+
}
|
|
272
|
+
: undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
interface TopLevelSplit {
|
|
276
|
+
operands: string[];
|
|
277
|
+
operators: string[];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Splits an expression at the `&&` and `||` outside string literals and brackets; returns undefined
|
|
281
|
+
// when a quote or bracket does not close.
|
|
282
|
+
function splitTopLevel(body: string): TopLevelSplit | undefined {
|
|
283
|
+
const operands: string[] = [];
|
|
284
|
+
const operators: string[] = [];
|
|
285
|
+
let depth = 0;
|
|
286
|
+
let inString = false;
|
|
287
|
+
let start = 0;
|
|
288
|
+
|
|
289
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
290
|
+
const char = body.charAt(index);
|
|
291
|
+
const pair = body.slice(index, index + 2);
|
|
292
|
+
|
|
293
|
+
if (char === "'") {
|
|
294
|
+
inString = !inString;
|
|
295
|
+
} else if (!inString && (char === '(' || char === '[')) {
|
|
296
|
+
depth += 1;
|
|
297
|
+
} else if (!inString && (char === ')' || char === ']')) {
|
|
298
|
+
depth -= 1;
|
|
299
|
+
} else if (!inString && depth === 0 && (pair === '&&' || pair === '||')) {
|
|
300
|
+
operands.push(body.slice(start, index));
|
|
301
|
+
operators.push(pair);
|
|
302
|
+
|
|
303
|
+
start = index + 2;
|
|
304
|
+
index += 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
operands.push(body.slice(start));
|
|
309
|
+
|
|
310
|
+
return inString || depth !== 0 ? undefined : { operands, operators };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// A group that can be empty makes GitHub fall through to the push group, so it must name the ref.
|
|
314
|
+
function isPullRequestGroup(expression: string): boolean {
|
|
315
|
+
const references = collectExpressionReferences(expression);
|
|
316
|
+
|
|
317
|
+
return references.includes('github.ref') || references.includes('github.head_ref');
|
|
318
|
+
}
|
|
319
|
+
|
|
220
320
|
// A value that differs between two runs on the same branch gives each run its own group, which no
|
|
221
321
|
// later run cancels.
|
|
222
322
|
const perRunValues = ['github.sha', 'github.run_id', 'github.run_number', 'github.run_attempt'];
|
|
@@ -227,10 +327,17 @@ function collectPerRunGroupFindings(
|
|
|
227
327
|
line: number,
|
|
228
328
|
): WorkflowFinding[] {
|
|
229
329
|
const group = findMember(concurrency, 'group');
|
|
330
|
+
const pushSplit = findPushSplitGroup(context, concurrency);
|
|
230
331
|
const checked = typeof group === 'string' ? (findNonDispatchGroup(group) ?? group) : '';
|
|
231
|
-
const references = collectExpressionReferences(checked);
|
|
332
|
+
const references = collectExpressionReferences(pushSplit?.pullRequest ?? checked);
|
|
333
|
+
const pushReferences = collectExpressionReferences(pushSplit?.push ?? '');
|
|
232
334
|
const allowed = isGateGroup(context, concurrency) ? 'github.sha' : undefined;
|
|
233
|
-
|
|
335
|
+
|
|
336
|
+
const perRun = perRunValues.find(
|
|
337
|
+
(value) =>
|
|
338
|
+
(value !== allowed && references.includes(value)) ||
|
|
339
|
+
(value !== 'github.sha' && pushReferences.includes(value)),
|
|
340
|
+
);
|
|
234
341
|
|
|
235
342
|
return perRun === undefined
|
|
236
343
|
? []
|