tslaws 1.2.0 → 1.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/dist/githubCli.js CHANGED
@@ -5,7 +5,8 @@
5
5
  // waits for an accept. Advisory: it runs the pull request's code, so it decides nothing about the lock.
6
6
  // command issue_comment: reads `/tslaws accept|reject ...`, checks the commenter may accept, and records the
7
7
  // fingerprints accepted in a comment of the bot's. Reads the pull request's files as data; runs none of it.
8
- // merge push to the configured branch, or workflow_dispatch by someone with admin or maintain access: runs the
8
+ // merge push to the configured branch, or workflow_dispatch by someone with admin or maintain access (plus
9
+ // merge_group with --results-out to run the laws ahead of the push): runs the
9
10
  // laws on the branch's code, accepts what passes (and, in comment mode, what was accepted for that exact
10
11
  // version) and commits tsc-laws.lock through the API as the bot. The only place the workflow writes the
11
12
  // lock. While a project's "local" is true (the default) a person may also change its lock by hand, through
@@ -31,7 +32,7 @@ import { discoverFromFiles, discoverySignature } from "./projects.js";
31
32
  import { build, readManifest } from "./compile.js";
32
33
  import { acceptedFingerprints, inert, recordComment, decide, parseCommands, isSharedBot, parseGithubConfig, proposalsOf, pullRequestVerdict, readApprovals, renderSticky, renderStickyAll, resolveNames, stickyMarkerFor, } from "./github.js";
33
34
  import { lockLevels } from "./lawKinds.js";
34
- import { acceptChanges, lawKey, lockChanges, readLock, writeLockValue } from "./lock.js";
35
+ import { acceptChanges, lawKey, lockChanges, parseLock, readLock, writeLockValue, } from "./lock.js";
35
36
  import { escapeGithubData, escapeGithubProperty } from "./reporters.js";
36
37
  import { plural } from "./plural.js";
37
38
  /** A project-relative file as the repository names it: the project's prefix, then the path. */
@@ -300,13 +301,58 @@ ${built.diagnostics}`);
300
301
  function lawsToProve(changes) {
301
302
  return changes.filter((c) => c.kind === 'law' && c.reason !== 'removed').map((c) => c.key);
302
303
  }
304
+ /** Reads a project's lock at a git commit (for example `merge_group.base_sha`), when that commit is present locally. */
305
+ function gitLockAt(cwd, lockPath, baseSha) {
306
+ if (typeof baseSha !== 'string' || !/^[0-9a-f]{40}$/.test(baseSha) || /^0+$/.test(baseSha))
307
+ return { found: false };
308
+ try {
309
+ execFileSync('git', ['-C', cwd, 'cat-file', '-e', `${baseSha}^{commit}`], { stdio: 'ignore' });
310
+ }
311
+ catch {
312
+ return { found: false };
313
+ }
314
+ try {
315
+ const bytes = execFileSync('git', ['-C', cwd, 'show', `${baseSha}:${lockPath}`], {
316
+ stdio: ['ignore', 'pipe', 'ignore'],
317
+ });
318
+ return { found: true, lock: parseLock(bytes.toString('utf8')) };
319
+ }
320
+ catch {
321
+ return { found: true, lock: undefined };
322
+ }
323
+ }
303
324
  /** `--results-out <file>`: run the laws of every project and write the reports, doing nothing else. Needs no token. */
304
325
  async function collect(ctx, mode, file) {
305
326
  const { active } = plan(ctx, unitsOf(ctx), (u) => u.cwd);
306
327
  const ev = event(ctx.env);
307
- const sha = String(mode === 'report' ? (ev.pull_request?.head?.sha ?? ctx.env.GITHUB_SHA) : (ev.after ?? ctx.env.GITHUB_SHA));
328
+ const eventName = ctx.env.GITHUB_EVENT_NAME;
329
+ if (mode === 'merge' &&
330
+ eventName !== undefined &&
331
+ eventName !== 'push' &&
332
+ eventName !== 'workflow_dispatch' &&
333
+ eventName !== 'merge_group') {
334
+ ctx.err('tslaws: merge --results-out runs on push, workflow_dispatch and merge_group only\n');
335
+ return 1;
336
+ }
337
+ const mergeGroup = mode === 'merge' && eventName === 'merge_group'
338
+ ? ev.merge_group
339
+ : undefined;
340
+ let targets = active;
341
+ if (mergeGroup !== undefined && typeof mergeGroup.base_ref === 'string' && mergeGroup.base_ref !== '') {
342
+ if (active.length === 0)
343
+ return 0;
344
+ targets = active.filter((a) => mergeGroup.base_ref === `refs/heads/${a.config.branch}`);
345
+ if (targets.length === 0) {
346
+ const branches = [...new Set(active.map((a) => `refs/heads/${a.config.branch}`))];
347
+ ctx.err(`tslaws: ${mergeGroup.base_ref} is not ${branches.join(' or ') || 'a branch any project writes'}; the lock is written only there\n`);
348
+ return 1;
349
+ }
350
+ }
351
+ const sha = String(mode === 'report'
352
+ ? (ev.pull_request?.head?.sha ?? ctx.env.GITHUB_SHA)
353
+ : (mergeGroup?.head_sha ?? ev.after ?? ctx.env.GITHUB_SHA));
308
354
  const projects = {};
309
- for (const { unit } of active) {
355
+ for (const { unit } of targets) {
310
356
  const entry = { lockSha: lockShaOf(unit) };
311
357
  try {
312
358
  const state = projectState(unit.options);
@@ -314,7 +360,19 @@ async function collect(ctx, mode, file) {
314
360
  entry.report = await runIsolated(unit, state);
315
361
  else {
316
362
  // A merge acts only on what the lock does not record, which it works out again from the same lock and files.
317
- const only = lawsToProve(lockChanges(state.lock, state.laws, state.exempt, state.levels));
363
+ // On merge_group, also compare against the queue's base lock when its commit is present locally so any law
364
+ // changed relative to the queue's base runs ahead of the push.
365
+ const base = mergeGroup
366
+ ? gitLockAt(ctx.cwd, joinRepo(unit.path, 'tsc-laws.lock'), mergeGroup.base_sha)
367
+ : { found: false };
368
+ const only = base.found
369
+ ? [
370
+ ...new Set([
371
+ ...lawsToProve(lockChanges(base.lock, state.laws, state.exempt, state.levels)),
372
+ ...lawsToProve(lockChanges(state.lock, state.laws, state.exempt, state.levels)),
373
+ ]),
374
+ ]
375
+ : lawsToProve(lockChanges(state.lock, state.laws, state.exempt, state.levels));
318
376
  if (only.length > 0)
319
377
  entry.report = await runIsolated(unit, state, only);
320
378
  else
@@ -327,7 +385,7 @@ async function collect(ctx, mode, file) {
327
385
  projects[unit.path] = entry;
328
386
  }
329
387
  writeFileSync(file, JSON.stringify({ version: 1, mode, sha, projects }));
330
- ctx.out(`tslaws: ran the laws of ${active.length} project${active.length === 1 ? '' : 's'}; results are in ${file}\n`);
388
+ ctx.out(`tslaws: ran the laws of ${targets.length} project${targets.length === 1 ? '' : 's'}; results are in ${file}\n`);
331
389
  return 0;
332
390
  }
333
391
  async function report(ctx) {
@@ -845,7 +903,9 @@ async function merge(ctx) {
845
903
  }
846
904
  const eventName = ctx.env.GITHUB_EVENT_NAME;
847
905
  if (eventName !== undefined && eventName !== 'push' && eventName !== 'workflow_dispatch') {
848
- ctx.err('tslaws: merge runs on push and workflow_dispatch only\n');
906
+ ctx.err(eventName === 'merge_group'
907
+ ? 'tslaws: merge runs on push and workflow_dispatch only; use --results-out on merge_group to run the laws ahead of the push\n'
908
+ : 'tslaws: merge runs on push and workflow_dispatch only\n');
849
909
  return 1;
850
910
  }
851
911
  // On demand there is no push and no pull request, so whoever started the run is the authority, and only someone
package/dist/lock.d.ts CHANGED
@@ -26,6 +26,7 @@ export interface Lock {
26
26
  export declare function lawKey(file: string, name: string): string;
27
27
  /** The hash of a law: its own text, whether it is `proved`, and its seal (the code it depends on and the run settings). */
28
28
  export declare function lawHash(law: ManifestLaw): string;
29
+ export declare function parseLock(text: string): Lock;
29
30
  export declare function readLock(path: string): Lock | undefined;
30
31
  /** The records that still describe their law: the law is in the lock with the hash the record was made for. */
31
32
  export declare function currentCertificates(laws: LockEntries, records: Record<string, CertificateRecord> | undefined): Record<string, CertificateRecord>;
package/dist/lock.js CHANGED
@@ -20,10 +20,8 @@ export function lawHash(law) {
20
20
  .update(law.seal ?? '')
21
21
  .digest('hex');
22
22
  }
23
- export function readLock(path) {
24
- if (!existsSync(path))
25
- return undefined;
26
- const lock = JSON.parse(readFileSync(path, 'utf8'));
23
+ export function parseLock(text) {
24
+ const lock = JSON.parse(text);
27
25
  return {
28
26
  version: typeof lock.version === 'number' ? Math.min(lockFormat, Math.max(0, lock.version)) : 0,
29
27
  laws: lock.laws,
@@ -32,6 +30,11 @@ export function readLock(path) {
32
30
  ...(lock.certificates === undefined ? {} : { certificates: lock.certificates }),
33
31
  };
34
32
  }
33
+ export function readLock(path) {
34
+ if (!existsSync(path))
35
+ return undefined;
36
+ return parseLock(readFileSync(path, 'utf8'));
37
+ }
35
38
  /** The records that still describe their law: the law is in the lock with the hash the record was made for. */
36
39
  export function currentCertificates(laws, records) {
37
40
  const kept = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tslaws",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Runs and checks `law` declarations compiled by tsc-laws: property tests, SMT proofs, tamper lock.",
5
5
  "bin": {
6
6
  "tslaws": "bin/tslaws.js"
@@ -89,11 +89,11 @@
89
89
  }
90
90
  },
91
91
  "optionalDependencies": {
92
- "tslaws-compiler-darwin-arm64": "1.2.0",
93
- "tslaws-compiler-darwin-x64": "1.2.0",
94
- "tslaws-compiler-linux-arm64-gnu": "1.2.0",
95
- "tslaws-compiler-linux-x64-gnu": "1.2.0",
96
- "tslaws-compiler-win32-x64-msvc": "1.2.0"
92
+ "tslaws-compiler-darwin-arm64": "1.3.0",
93
+ "tslaws-compiler-darwin-x64": "1.3.0",
94
+ "tslaws-compiler-linux-arm64-gnu": "1.3.0",
95
+ "tslaws-compiler-linux-x64-gnu": "1.3.0",
96
+ "tslaws-compiler-win32-x64-msvc": "1.3.0"
97
97
  },
98
98
  "publishConfig": {
99
99
  "access": "public"