vouchington-tooling 0.0.8 → 0.0.10

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.
Files changed (41) hide show
  1. package/README.md +16 -1
  2. package/dist/cli/commands/gha-artifacts-cleanup.d.mts +6 -0
  3. package/dist/cli/commands/gha-artifacts-cleanup.mjs +50 -0
  4. package/dist/cli/commands/http-origin.d.mts +2 -0
  5. package/dist/cli/commands/http-origin.mjs +14 -0
  6. package/dist/cli/index.mjs +11 -0
  7. package/dist/cli/parse-gha-artifacts-cleanup.d.mts +15 -0
  8. package/dist/cli/parse-gha-artifacts-cleanup.mjs +74 -0
  9. package/dist/cli/parse.d.mts +7 -2
  10. package/dist/cli/parse.mjs +36 -0
  11. package/dist/cli/usage.d.mts +1 -1
  12. package/dist/cli/usage.mjs +9 -0
  13. package/dist/gha-artifacts-cleanup/classify.d.mts +14 -0
  14. package/dist/gha-artifacts-cleanup/classify.mjs +32 -0
  15. package/dist/gha-artifacts-cleanup/commands.d.mts +23 -0
  16. package/dist/gha-artifacts-cleanup/commands.mjs +55 -0
  17. package/dist/gha-artifacts-cleanup/github.d.mts +16 -0
  18. package/dist/gha-artifacts-cleanup/github.mjs +100 -0
  19. package/dist/gha-artifacts-cleanup/index.d.mts +8 -0
  20. package/dist/gha-artifacts-cleanup/index.mjs +4 -0
  21. package/dist/gha-artifacts-cleanup/plan.d.mts +17 -0
  22. package/dist/gha-artifacts-cleanup/plan.mjs +40 -0
  23. package/dist/gha-selected-files/index.d.mts +6 -0
  24. package/dist/gha-selected-files/index.mjs +38 -0
  25. package/dist/http-origin/index.d.mts +1 -0
  26. package/dist/http-origin/index.mjs +23 -0
  27. package/dist/index.d.mts +7 -2
  28. package/dist/index.mjs +5 -1
  29. package/dist/pnpm-install/native-health.d.mts +4 -0
  30. package/dist/pnpm-install/native-health.mjs +84 -0
  31. package/dist/pnpm-install/runner.mjs +8 -3
  32. package/dist/process-line-buffer/index.d.mts +7 -0
  33. package/dist/process-line-buffer/index.mjs +28 -0
  34. package/dist/shared-context/{fake-git.test-helpers.d.mts → fake-git.d.mts} +1 -0
  35. package/dist/shared-context/{fake-git.test-helpers.mjs → fake-git.mjs} +4 -1
  36. package/dist/shared-context/index.d.mts +2 -0
  37. package/dist/shared-context/index.mjs +1 -0
  38. package/package.json +27 -1
  39. package/scripts/allocate-browser-safe-ports.py +113 -1
  40. package/scripts/gha/diagnose-port-collision.sh +232 -0
  41. package/scripts/gha/prepare-trivy-db.sh +32 -0
package/README.md CHANGED
@@ -22,6 +22,10 @@ vouchington gha-needs-results
22
22
  vouchington download-with-diagnostics <url> <destination>
23
23
  vouchington host-pressure-diagnostics
24
24
  vouchington allocate-browser-safe-ports 2 --policy ./policy.json --forbidden-ports ./ports.json
25
+ vouchington diagnose-port-collision --ports "2200 2216"
26
+ vouchington prepare-trivy-db
27
+ vouchington gha-artifacts-cleanup run --run-id 123 --keep-pattern 'plan-*' --delete-pattern 'coverage-*'
28
+ vouchington http-origin --field cdn_origin https://images.example.com
25
29
  vouchington vitest-blob-manifest <suite> [reports-directory]
26
30
  vouchington pnpm-install --runner-lifecycle persistent --install-scripts true
27
31
  ```
@@ -48,5 +52,16 @@ import { splitSqlStatements, stripSqlComments } from 'vouchington-tooling/sql-sc
48
52
  import { auditCiJobRuntime } from 'vouchington-tooling/gha-runtime-audit'
49
53
  import { writeVitestBlobManifest } from 'vouchington-tooling/vitest-blob-manifest'
50
54
  import { runInstallLifecycle } from 'vouchington-tooling/pnpm-install'
51
- import { buildSharedContext, runNamedChecks } from 'vouchington-tooling/shared-context'
55
+ import {
56
+ buildSharedContext,
57
+ installFakeGit,
58
+ runNamedChecks,
59
+ } from 'vouchington-tooling/shared-context'
60
+ import {
61
+ decodeSelectedFiles,
62
+ writeSelectedFilesOutput,
63
+ } from 'vouchington-tooling/gha-selected-files'
64
+ import { createArtifactClassifier, runCleanup } from 'vouchington-tooling/gha-artifacts-cleanup'
65
+ import { validateOptionalHttpOrigin } from 'vouchington-tooling/http-origin'
66
+ import { boundPendingLine, splitCompleteLines } from 'vouchington-tooling/process-line-buffer'
52
67
  ```
@@ -0,0 +1,6 @@
1
+ import type { ParsedGhaArtifactsCleanup } from '../parse-gha-artifacts-cleanup.mts';
2
+ export declare function loadCleanupPatterns(parsed: ParsedGhaArtifactsCleanup): {
3
+ keepPatterns: string[];
4
+ deletePatterns: string[];
5
+ };
6
+ export declare function runGhaArtifactsCleanup(parsed: ParsedGhaArtifactsCleanup, env?: Record<string, string | undefined>): Promise<number>;
@@ -0,0 +1,50 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { createArtifactClassifier, parseArtifactPatternsJson, runCleanup, sweepCleanup, } from '../../gha-artifacts-cleanup/index.mjs';
3
+ function usage() {
4
+ return [
5
+ 'Usage: vouchington gha-artifacts-cleanup run --run-id <id> [pattern options]',
6
+ ' vouchington gha-artifacts-cleanup sweep --older-than-hours <n> [pattern options]',
7
+ '',
8
+ 'Pattern options: --keep-pattern <glob> --delete-pattern <glob> --patterns-file <json>',
9
+ 'Requires GITHUB_TOKEN or GH_TOKEN and GITHUB_REPOSITORY (owner/repo) in the env.',
10
+ ].join('\n');
11
+ }
12
+ function logSummary(subcommand, summary) {
13
+ const mb = (summary.bytesFreed / (1024 * 1024)).toFixed(1);
14
+ console.log(`[gha-artifacts-cleanup] ${subcommand}: deleted ${summary.deletedCount} artifact(s), freed ~${mb} MB`);
15
+ }
16
+ export function loadCleanupPatterns(parsed) {
17
+ const fromFile = parsed.patternsFile === undefined
18
+ ? { keepPatterns: [], deletePatterns: [] }
19
+ : parseArtifactPatternsJson(JSON.parse(readFileSync(parsed.patternsFile, 'utf8')));
20
+ return {
21
+ keepPatterns: [...fromFile.keepPatterns, ...parsed.keepPatterns],
22
+ deletePatterns: [...fromFile.deletePatterns, ...parsed.deletePatterns],
23
+ };
24
+ }
25
+ export async function runGhaArtifactsCleanup(parsed, env = process.env) {
26
+ const token = env.GITHUB_TOKEN ?? env.GH_TOKEN;
27
+ const repo = env.GITHUB_REPOSITORY;
28
+ if (!token || !repo) {
29
+ console.error('[gha-artifacts-cleanup] missing GITHUB_TOKEN/GH_TOKEN or GITHUB_REPOSITORY');
30
+ return 0;
31
+ }
32
+ const patterns = loadCleanupPatterns(parsed);
33
+ const { classify } = createArtifactClassifier(patterns);
34
+ if (parsed.subcommand === 'run') {
35
+ const runId = parsed.runId;
36
+ if (runId === undefined) {
37
+ console.error(usage());
38
+ return 2;
39
+ }
40
+ logSummary('run', await runCleanup({ repo, token, classify, runId }));
41
+ return 0;
42
+ }
43
+ const olderThanHours = parsed.olderThanHours;
44
+ if (olderThanHours === undefined) {
45
+ console.error(usage());
46
+ return 2;
47
+ }
48
+ logSummary('sweep', await sweepCleanup({ repo, token, classify, olderThanHours }));
49
+ return 0;
50
+ }
@@ -0,0 +1,2 @@
1
+ export declare function formatCliError(error: unknown): string;
2
+ export declare function runHttpOrigin(field: string, value: string): number;
@@ -0,0 +1,14 @@
1
+ import { validateOptionalHttpOrigin } from '../../http-origin/index.mjs';
2
+ export function formatCliError(error) {
3
+ return error instanceof Error ? error.message : String(error);
4
+ }
5
+ export function runHttpOrigin(field, value) {
6
+ try {
7
+ validateOptionalHttpOrigin(value, field);
8
+ return 0;
9
+ }
10
+ catch (error) {
11
+ process.stderr.write(`${formatCliError(error)}\n`);
12
+ return 1;
13
+ }
14
+ }
@@ -3,7 +3,9 @@ import { readFileSync, realpathSync } from 'node:fs';
3
3
  import { resolve } from 'node:path';
4
4
  import { pathToFileURL } from 'node:url';
5
5
  import { readPackageVersion } from '../package-version.mjs';
6
+ import { runGhaArtifactsCleanup } from './commands/gha-artifacts-cleanup.mjs';
6
7
  import { runGhaRuntimeAudit } from './commands/gha-runtime-audit.mjs';
8
+ import { runHttpOrigin } from './commands/http-origin.mjs';
7
9
  import { runPnpmInstallCli } from './commands/pnpm-install.mjs';
8
10
  import { runRunnerPortPolicy } from './commands/runner-port-policy.mjs';
9
11
  import { runScript } from './commands/spawn-script.mjs';
@@ -27,6 +29,11 @@ const SCRIPT_PATHS = {
27
29
  command: 'python3',
28
30
  path: 'scripts/allocate-browser-safe-ports.py',
29
31
  },
32
+ 'diagnose-port-collision': {
33
+ command: 'bash',
34
+ path: 'scripts/gha/diagnose-port-collision.sh',
35
+ },
36
+ 'prepare-trivy-db': { command: 'bash', path: 'scripts/gha/prepare-trivy-db.sh' },
30
37
  };
31
38
  export function runCli(argv = process.argv) {
32
39
  const parsed = parseCli(argv);
@@ -55,6 +62,10 @@ export function runCli(argv = process.argv) {
55
62
  return runPnpmInstallCli(parsed.args);
56
63
  case 'vitest-blob-manifest':
57
64
  return runVitestBlobManifestCommand(parsed.args);
65
+ case 'http-origin':
66
+ return runHttpOrigin(parsed.field, parsed.value);
67
+ case 'gha-artifacts-cleanup':
68
+ return runGhaArtifactsCleanup(parsed);
58
69
  }
59
70
  }
60
71
  function readInstalledVersion() {
@@ -0,0 +1,15 @@
1
+ export type ParsedGhaArtifactsCleanup = {
2
+ kind: 'gha-artifacts-cleanup';
3
+ subcommand: 'run' | 'sweep';
4
+ runId?: string;
5
+ olderThanHours?: number;
6
+ keepPatterns: string[];
7
+ deletePatterns: string[];
8
+ patternsFile?: string;
9
+ };
10
+ export declare function parseGhaArtifactsCleanup(args: readonly string[]): ParsedGhaArtifactsCleanup | {
11
+ kind: 'help';
12
+ } | {
13
+ kind: 'error';
14
+ message: string;
15
+ };
@@ -0,0 +1,74 @@
1
+ export function parseGhaArtifactsCleanup(args) {
2
+ const [subcommand, ...rest] = args;
3
+ if (subcommand === '--help' || subcommand === '-h' || subcommand === undefined) {
4
+ return subcommand === undefined
5
+ ? { kind: 'error', message: 'gha-artifacts-cleanup requires run or sweep' }
6
+ : { kind: 'help' };
7
+ }
8
+ if (subcommand !== 'run' && subcommand !== 'sweep') {
9
+ return { kind: 'error', message: `unknown gha-artifacts-cleanup subcommand: ${subcommand}` };
10
+ }
11
+ let runId;
12
+ let olderThanHours;
13
+ let patternsFile;
14
+ const keepPatterns = [];
15
+ const deletePatterns = [];
16
+ for (let index = 0; index < rest.length; index += 1) {
17
+ const flag = rest[index];
18
+ if (flag === '--help' || flag === '-h')
19
+ return { kind: 'help' };
20
+ if (flag === '--run-id' || flag === '--older-than-hours' || flag === '--patterns-file') {
21
+ const value = rest[index + 1];
22
+ if (value === undefined)
23
+ return { kind: 'error', message: `${flag} requires a value` };
24
+ index += 1;
25
+ if (flag === '--run-id')
26
+ runId = value;
27
+ else if (flag === '--patterns-file')
28
+ patternsFile = value;
29
+ else {
30
+ const parsed = Number(value.trim());
31
+ if (!value.trim() || !Number.isFinite(parsed) || parsed < 0) {
32
+ return { kind: 'error', message: '--older-than-hours must be a non-negative number' };
33
+ }
34
+ olderThanHours = parsed;
35
+ }
36
+ continue;
37
+ }
38
+ if (flag === '--keep-pattern' || flag === '--delete-pattern') {
39
+ const value = rest[index + 1];
40
+ if (value === undefined)
41
+ return { kind: 'error', message: `${flag} requires a value` };
42
+ index += 1;
43
+ if (flag === '--keep-pattern')
44
+ keepPatterns.push(value);
45
+ else
46
+ deletePatterns.push(value);
47
+ continue;
48
+ }
49
+ return { kind: 'error', message: `unknown gha-artifacts-cleanup option: ${flag}` };
50
+ }
51
+ if (subcommand === 'run') {
52
+ if (!runId)
53
+ return { kind: 'error', message: 'gha-artifacts-cleanup run requires --run-id' };
54
+ return {
55
+ kind: 'gha-artifacts-cleanup',
56
+ subcommand: 'run',
57
+ runId,
58
+ keepPatterns,
59
+ deletePatterns,
60
+ ...(patternsFile === undefined ? {} : { patternsFile }),
61
+ };
62
+ }
63
+ if (olderThanHours === undefined) {
64
+ return { kind: 'error', message: 'gha-artifacts-cleanup sweep requires --older-than-hours' };
65
+ }
66
+ return {
67
+ kind: 'gha-artifacts-cleanup',
68
+ subcommand: 'sweep',
69
+ olderThanHours,
70
+ keepPatterns,
71
+ deletePatterns,
72
+ ...(patternsFile === undefined ? {} : { patternsFile }),
73
+ };
74
+ }
@@ -1,3 +1,4 @@
1
+ import { type ParsedGhaArtifactsCleanup } from './parse-gha-artifacts-cleanup.mts';
1
2
  import { type ParsedGhaRuntimeAudit } from './parse-gha-runtime-audit.mts';
2
3
  export type ParsedCli = {
3
4
  kind: 'help';
@@ -23,6 +24,10 @@ export type ParsedCli = {
23
24
  } | {
24
25
  kind: 'vitest-blob-manifest';
25
26
  args: string[];
26
- } | ParsedGhaRuntimeAudit;
27
- export type ScriptCommand = 'gha-output' | 'gha-needs-results' | 'download-with-diagnostics' | 'host-pressure-diagnostics' | 'allocate-browser-safe-ports';
27
+ } | {
28
+ kind: 'http-origin';
29
+ field: string;
30
+ value: string;
31
+ } | ParsedGhaRuntimeAudit | ParsedGhaArtifactsCleanup;
32
+ export type ScriptCommand = 'gha-output' | 'gha-needs-results' | 'download-with-diagnostics' | 'host-pressure-diagnostics' | 'allocate-browser-safe-ports' | 'diagnose-port-collision' | 'prepare-trivy-db';
28
33
  export declare function parseCli(argv: readonly string[]): ParsedCli;
@@ -1,3 +1,4 @@
1
+ import { parseGhaArtifactsCleanup, } from './parse-gha-artifacts-cleanup.mjs';
1
2
  import { parseGhaRuntimeAudit } from './parse-gha-runtime-audit.mjs';
2
3
  const SCRIPT_COMMANDS = new Set([
3
4
  'gha-output',
@@ -5,6 +6,8 @@ const SCRIPT_COMMANDS = new Set([
5
6
  'download-with-diagnostics',
6
7
  'host-pressure-diagnostics',
7
8
  'allocate-browser-safe-ports',
9
+ 'diagnose-port-collision',
10
+ 'prepare-trivy-db',
8
11
  ]);
9
12
  export function parseCli(argv) {
10
13
  const args = argv.slice(2);
@@ -23,6 +26,10 @@ export function parseCli(argv) {
23
26
  return { kind: 'pnpm-install', args: rest };
24
27
  if (command === 'vitest-blob-manifest')
25
28
  return { kind: 'vitest-blob-manifest', args: rest };
29
+ if (command === 'http-origin')
30
+ return parseHttpOrigin(rest);
31
+ if (command === 'gha-artifacts-cleanup')
32
+ return parseGhaArtifactsCleanup(rest);
26
33
  if (command !== undefined && SCRIPT_COMMANDS.has(command)) {
27
34
  return { kind: 'script', command: command, args: rest };
28
35
  }
@@ -62,3 +69,32 @@ function parseRunnerPortPolicy(args) {
62
69
  ...(reserved === undefined ? {} : { reserved }),
63
70
  };
64
71
  }
72
+ function parseHttpOrigin(args) {
73
+ let field = 'origin';
74
+ const values = [];
75
+ let index = 0;
76
+ while (index < args.length) {
77
+ const flag = args[index];
78
+ index += 1;
79
+ if (flag === '--help' || flag === '-h')
80
+ return { kind: 'help' };
81
+ if (flag === '--field') {
82
+ const value = args[index];
83
+ if (value === undefined)
84
+ return { kind: 'error', message: '--field requires a name' };
85
+ field = value;
86
+ index += 1;
87
+ continue;
88
+ }
89
+ if (flag === '--') {
90
+ values.push(...args.slice(index));
91
+ break;
92
+ }
93
+ if (flag.startsWith('-'))
94
+ return { kind: 'error', message: `unknown http-origin option: ${flag}` };
95
+ values.push(flag);
96
+ }
97
+ if (values.length > 1)
98
+ return { kind: 'error', message: 'http-origin accepts at most one value' };
99
+ return { kind: 'http-origin', field, value: values[0] ?? '' };
100
+ }
@@ -1,2 +1,2 @@
1
- export declare const USAGE = "Usage: vouchington <command> [options]\n\nCommands:\n runner-port-policy Print or validate a runner port policy\n with-host-lock Run a command under a host-wide lock\n gha-runtime-audit Audit successful GitHub Actions job runtimes\n gha-output Write a collision-safe multiline GITHUB_OUTPUT record\n gha-needs-results Fail if required GitHub Actions job results failed\n download-with-diagnostics Download a URL and report HTTP status on failure\n host-pressure-diagnostics Print a bounded host memory/OOM/PSI snapshot\n allocate-browser-safe-ports Allocate Fetch-safe localhost ports\n vitest-blob-manifest Stamp a vitest-blob-manifest:v1 identity file\n pnpm-install Install a pnpm workspace with retry and release-age fail-fast\n\nOptions:\n -h, --help Show this help\n -v, --version Print the package version\n\nrunner-port-policy\n (no args) Print the shipped policy as JSON\n --file <path> Validate and print a policy file\n --reserved <port> Print true if the port is reserved\n\nwith-host-lock\n --name <family>\n [--slots <n>]\n --timeout-seconds <n>\n [--command-timeout-seconds <n>]\n [--failure-diagnostics <absolute-script>]\n [--on-acquire-timeout fail|run-unlocked]\n -- <command> [args...]\n\ngha-runtime-audit\n [--repository owner/name] Default GITHUB_REPOSITORY\n [--branch main]\n --pr-workflow <name|/regex/> Repeatable\n --push-workflow <name|/regex/> Repeatable\n\ngha-output <name>\ngha-needs-results [label]\ndownload-with-diagnostics <url> <destination> [-- curl-args...]\nhost-pressure-diagnostics\nallocate-browser-safe-ports [count] [--policy path] [--forbidden-ports path]\nvitest-blob-manifest <suite> [reports-directory]\npnpm-install --runner-lifecycle persistent|ephemeral|ephemeral-full --install-scripts true|false\n";
1
+ export declare const USAGE = "Usage: vouchington <command> [options]\n\nCommands:\n runner-port-policy Print or validate a runner port policy\n with-host-lock Run a command under a host-wide lock\n gha-runtime-audit Audit successful GitHub Actions job runtimes\n gha-output Write a collision-safe multiline GITHUB_OUTPUT record\n gha-needs-results Fail if required GitHub Actions job results failed\n download-with-diagnostics Download a URL and report HTTP status on failure\n host-pressure-diagnostics Print a bounded host memory/OOM/PSI snapshot\n allocate-browser-safe-ports Allocate Fetch-safe localhost ports\n diagnose-port-collision Capture bounded localhost port diagnostics\n prepare-trivy-db Download the Trivy vulnerability database\n gha-artifacts-cleanup Delete classified GitHub Actions artifacts\n http-origin Validate an optional HTTP(S) origin\n vitest-blob-manifest Stamp a vitest-blob-manifest:v1 identity file\n pnpm-install Install a pnpm workspace with retry and release-age fail-fast\n\nOptions:\n -h, --help Show this help\n -v, --version Print the package version\n\nrunner-port-policy\n (no args) Print the shipped policy as JSON\n --file <path> Validate and print a policy file\n --reserved <port> Print true if the port is reserved\n\nwith-host-lock\n --name <family>\n [--slots <n>]\n --timeout-seconds <n>\n [--command-timeout-seconds <n>]\n [--failure-diagnostics <absolute-script>]\n [--on-acquire-timeout fail|run-unlocked]\n -- <command> [args...]\n\ngha-runtime-audit\n [--repository owner/name] Default GITHUB_REPOSITORY\n [--branch main]\n --pr-workflow <name|/regex/> Repeatable\n --push-workflow <name|/regex/> Repeatable\n\ngha-output <name>\ngha-needs-results [label]\ndownload-with-diagnostics <url> <destination> [-- curl-args...]\nhost-pressure-diagnostics\nallocate-browser-safe-ports [count] [--policy path] [--forbidden-ports path]\ndiagnose-port-collision [--ports \"2200 2216\"] [--output-dir PATH]\nprepare-trivy-db\ngha-artifacts-cleanup run --run-id <id> [--keep-pattern glob] [--delete-pattern glob] [--patterns-file json]\ngha-artifacts-cleanup sweep --older-than-hours <n> [--keep-pattern glob] [--delete-pattern glob] [--patterns-file json]\nhttp-origin [--field NAME] [value]\nvitest-blob-manifest <suite> [reports-directory]\npnpm-install --runner-lifecycle persistent|ephemeral|ephemeral-full --install-scripts true|false\n";
2
2
  export declare function printUsage(stream?: NodeJS.WritableStream): void;
@@ -9,6 +9,10 @@ Commands:
9
9
  download-with-diagnostics Download a URL and report HTTP status on failure
10
10
  host-pressure-diagnostics Print a bounded host memory/OOM/PSI snapshot
11
11
  allocate-browser-safe-ports Allocate Fetch-safe localhost ports
12
+ diagnose-port-collision Capture bounded localhost port diagnostics
13
+ prepare-trivy-db Download the Trivy vulnerability database
14
+ gha-artifacts-cleanup Delete classified GitHub Actions artifacts
15
+ http-origin Validate an optional HTTP(S) origin
12
16
  vitest-blob-manifest Stamp a vitest-blob-manifest:v1 identity file
13
17
  pnpm-install Install a pnpm workspace with retry and release-age fail-fast
14
18
 
@@ -41,6 +45,11 @@ gha-needs-results [label]
41
45
  download-with-diagnostics <url> <destination> [-- curl-args...]
42
46
  host-pressure-diagnostics
43
47
  allocate-browser-safe-ports [count] [--policy path] [--forbidden-ports path]
48
+ diagnose-port-collision [--ports "2200 2216"] [--output-dir PATH]
49
+ prepare-trivy-db
50
+ gha-artifacts-cleanup run --run-id <id> [--keep-pattern glob] [--delete-pattern glob] [--patterns-file json]
51
+ gha-artifacts-cleanup sweep --older-than-hours <n> [--keep-pattern glob] [--delete-pattern glob] [--patterns-file json]
52
+ http-origin [--field NAME] [value]
44
53
  vitest-blob-manifest <suite> [reports-directory]
45
54
  pnpm-install --runner-lifecycle persistent|ephemeral|ephemeral-full --install-scripts true|false
46
55
  `;
@@ -0,0 +1,14 @@
1
+ export type ArtifactClassification = 'keep' | 'delete';
2
+ export type ArtifactClassifier = {
3
+ classify: (name: string) => ArtifactClassification;
4
+ isExplicitlyClassified: (name: string) => boolean;
5
+ };
6
+ export type ArtifactPatterns = {
7
+ keepPatterns: readonly string[];
8
+ deletePatterns: readonly string[];
9
+ };
10
+ export declare function createArtifactClassifier(patterns: ArtifactPatterns): ArtifactClassifier;
11
+ export declare function parseArtifactPatternsJson(value: unknown): {
12
+ keepPatterns: string[];
13
+ deletePatterns: string[];
14
+ };
@@ -0,0 +1,32 @@
1
+ import picomatch from 'picomatch';
2
+ function matcher(patterns) {
3
+ if (patterns.length === 0)
4
+ return () => false;
5
+ return picomatch([...patterns]);
6
+ }
7
+ export function createArtifactClassifier(patterns) {
8
+ const isKeep = matcher(patterns.keepPatterns);
9
+ const isDelete = matcher(patterns.deletePatterns);
10
+ return {
11
+ classify: (name) => (isKeep(name) ? 'keep' : 'delete'),
12
+ isExplicitlyClassified: (name) => isKeep(name) || isDelete(name),
13
+ };
14
+ }
15
+ export function parseArtifactPatternsJson(value) {
16
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
17
+ throw new Error('patterns file must be a JSON object with keep and delete arrays');
18
+ }
19
+ const record = value;
20
+ return {
21
+ keepPatterns: stringArray(record.keep, 'keep'),
22
+ deletePatterns: stringArray(record.delete, 'delete'),
23
+ };
24
+ }
25
+ function stringArray(value, field) {
26
+ if (value === undefined)
27
+ return [];
28
+ if (!Array.isArray(value) || value.some((entry) => typeof entry !== 'string')) {
29
+ throw new Error(`patterns file ${field} must be an array of strings`);
30
+ }
31
+ return value;
32
+ }
@@ -0,0 +1,23 @@
1
+ import type { ArtifactClassification } from './classify.mts';
2
+ import { deleteArtifact, getRunConclusion, listArtifactsPage, listRunArtifacts } from './github.mts';
3
+ import { type DeletionSummary } from './plan.mts';
4
+ export interface CleanupDeps {
5
+ listRunArtifacts: typeof listRunArtifacts;
6
+ listArtifactsPage: typeof listArtifactsPage;
7
+ getRunConclusion: typeof getRunConclusion;
8
+ deleteArtifact: typeof deleteArtifact;
9
+ }
10
+ export declare const defaultDeps: CleanupDeps;
11
+ export type CleanupRequest = {
12
+ repo: string;
13
+ token: string;
14
+ classify: (name: string) => ArtifactClassification;
15
+ deps?: CleanupDeps;
16
+ log?: (message: string) => void;
17
+ };
18
+ export declare function runCleanup(request: CleanupRequest & {
19
+ runId: string;
20
+ }): Promise<DeletionSummary>;
21
+ export declare function sweepCleanup(request: CleanupRequest & {
22
+ olderThanHours: number;
23
+ }): Promise<DeletionSummary>;
@@ -0,0 +1,55 @@
1
+ import { deleteArtifact, getRunConclusion, listArtifactsPage, listRunArtifacts, } from './github.mjs';
2
+ import { isSweepCandidate, nextPagingState, planRunDeletions, planSweepDeletions, shouldStopPaging, summarize, } from './plan.mjs';
3
+ export const defaultDeps = {
4
+ listRunArtifacts,
5
+ listArtifactsPage,
6
+ getRunConclusion,
7
+ deleteArtifact,
8
+ };
9
+ async function deleteAll(repo, token, artifacts, deps, log) {
10
+ const deleted = [];
11
+ for (const artifact of artifacts) {
12
+ const outcome = await deps.deleteArtifact(repo, token, artifact.id);
13
+ if (outcome === 'failed') {
14
+ log(`[gha-artifacts-cleanup] failed to delete ${artifact.name} (id ${artifact.id})`);
15
+ continue;
16
+ }
17
+ if (outcome === 'not-found')
18
+ continue;
19
+ deleted.push(artifact);
20
+ }
21
+ return summarize(deleted);
22
+ }
23
+ export async function runCleanup(request) {
24
+ const deps = request.deps ?? defaultDeps;
25
+ const log = request.log ?? console.error;
26
+ const artifacts = await deps.listRunArtifacts(request.repo, request.token, request.runId);
27
+ const toDelete = planRunDeletions(artifacts, request.classify);
28
+ return deleteAll(request.repo, request.token, toDelete, deps, log);
29
+ }
30
+ async function collectSweepCandidates(request, cutoffIso, deps) {
31
+ const candidates = [];
32
+ let state = { page: 1, consecutiveExpiredPages: 0 };
33
+ for (;;) {
34
+ const page = await deps.listArtifactsPage(request.repo, request.token, state.page);
35
+ const pageArtifacts = page ?? [];
36
+ if (page != null) {
37
+ candidates.push(...pageArtifacts.filter((artifact) => isSweepCandidate(artifact, cutoffIso, request.classify)));
38
+ }
39
+ if (page == null)
40
+ break;
41
+ state = nextPagingState(pageArtifacts, state);
42
+ if (shouldStopPaging(pageArtifacts, state))
43
+ break;
44
+ }
45
+ return candidates;
46
+ }
47
+ export async function sweepCleanup(request) {
48
+ const deps = request.deps ?? defaultDeps;
49
+ const log = request.log ?? console.error;
50
+ const cutoffIso = new Date(Date.now() - request.olderThanHours * 60 * 60 * 1000).toISOString();
51
+ const candidates = await collectSweepCandidates(request, cutoffIso, deps);
52
+ const cache = new Map();
53
+ const toDelete = await planSweepDeletions(candidates, (runId) => deps.getRunConclusion(request.repo, request.token, runId, cache));
54
+ return deleteAll(request.repo, request.token, toDelete, deps, log);
55
+ }
@@ -0,0 +1,16 @@
1
+ export interface GithubArtifact {
2
+ id: number;
3
+ name: string;
4
+ size_in_bytes: number;
5
+ expired: boolean;
6
+ created_at: string;
7
+ workflow_run?: {
8
+ id: number;
9
+ };
10
+ }
11
+ export type DeleteOutcome = 'deleted' | 'not-found' | 'failed';
12
+ export declare function githubGet(path: string, token: string, sleepFn?: (ms: number) => Promise<void>): Promise<Response>;
13
+ export declare function deleteArtifact(repo: string, token: string, artifactId: number, sleepFn?: (ms: number) => Promise<void>): Promise<DeleteOutcome>;
14
+ export declare function listRunArtifacts(repo: string, token: string, runId: string): Promise<GithubArtifact[]>;
15
+ export declare function listArtifactsPage(repo: string, token: string, page: number): Promise<GithubArtifact[] | null>;
16
+ export declare function getRunConclusion(repo: string, token: string, runId: number, cache: Map<number, string | null>): Promise<string | null>;
@@ -0,0 +1,100 @@
1
+ const API_BASE = 'https://api.github.com';
2
+ const API_VERSION = '2022-11-28';
3
+ const MAX_RETRIES = 4;
4
+ const PER_PAGE = 100;
5
+ function authHeaders(token) {
6
+ return {
7
+ Authorization: `Bearer ${token}`,
8
+ Accept: 'application/vnd.github+json',
9
+ 'X-GitHub-Api-Version': API_VERSION,
10
+ };
11
+ }
12
+ function sleep(ms) {
13
+ return new Promise((resolve) => setTimeout(resolve, ms));
14
+ }
15
+ function retryDelayMs(response, attempt) {
16
+ const retryAfter = Number(response?.headers.get('retry-after'));
17
+ if (Number.isFinite(retryAfter) && retryAfter > 0)
18
+ return retryAfter * 1000;
19
+ return Math.min(1000 * 2 ** attempt, 30_000);
20
+ }
21
+ export async function githubGet(path, token, sleepFn = sleep) {
22
+ for (let attempt = 0;; attempt += 1) {
23
+ let response;
24
+ try {
25
+ response = await fetch(`${API_BASE}${path}`, { headers: authHeaders(token) });
26
+ }
27
+ catch {
28
+ if (attempt === MAX_RETRIES)
29
+ return new Response(null, { status: 599 });
30
+ await sleepFn(retryDelayMs(null, attempt));
31
+ continue;
32
+ }
33
+ if (response.status !== 403 && response.status !== 429)
34
+ return response;
35
+ if (attempt === MAX_RETRIES)
36
+ return response;
37
+ await sleepFn(retryDelayMs(response, attempt));
38
+ }
39
+ }
40
+ export async function deleteArtifact(repo, token, artifactId, sleepFn = sleep) {
41
+ for (let attempt = 0;; attempt += 1) {
42
+ let response;
43
+ try {
44
+ response = await fetch(`${API_BASE}/repos/${repo}/actions/artifacts/${artifactId}`, {
45
+ method: 'DELETE',
46
+ headers: authHeaders(token),
47
+ });
48
+ }
49
+ catch {
50
+ if (attempt === MAX_RETRIES)
51
+ return 'failed';
52
+ await sleepFn(retryDelayMs(null, attempt));
53
+ continue;
54
+ }
55
+ if (response.status === 404)
56
+ return 'not-found';
57
+ if (response.ok)
58
+ return 'deleted';
59
+ if (response.status !== 403 && response.status !== 429)
60
+ return 'failed';
61
+ if (attempt === MAX_RETRIES)
62
+ return 'failed';
63
+ await sleepFn(retryDelayMs(response, attempt));
64
+ }
65
+ }
66
+ export async function listRunArtifacts(repo, token, runId) {
67
+ const artifacts = [];
68
+ for (let page = 1;; page += 1) {
69
+ const path = `/repos/${repo}/actions/runs/${runId}/artifacts?per_page=${PER_PAGE}&page=${page}`;
70
+ const response = await githubGet(path, token);
71
+ if (!response.ok)
72
+ break;
73
+ const body = (await response.json());
74
+ artifacts.push(...body.artifacts);
75
+ if (body.artifacts.length < PER_PAGE)
76
+ break;
77
+ }
78
+ return artifacts;
79
+ }
80
+ export async function listArtifactsPage(repo, token, page) {
81
+ const path = `/repos/${repo}/actions/artifacts?per_page=${PER_PAGE}&page=${page}`;
82
+ const response = await githubGet(path, token);
83
+ if (!response.ok)
84
+ return null;
85
+ const body = (await response.json());
86
+ return body.artifacts;
87
+ }
88
+ export async function getRunConclusion(repo, token, runId, cache) {
89
+ if (cache.has(runId))
90
+ return cache.get(runId);
91
+ const response = await githubGet(`/repos/${repo}/actions/runs/${runId}`, token);
92
+ if (!response.ok) {
93
+ cache.set(runId, null);
94
+ return null;
95
+ }
96
+ const body = (await response.json());
97
+ const conclusion = body.status === 'completed' ? body.conclusion : null;
98
+ cache.set(runId, conclusion);
99
+ return conclusion;
100
+ }
@@ -0,0 +1,8 @@
1
+ export { createArtifactClassifier, parseArtifactPatternsJson } from './classify.mts';
2
+ export type { ArtifactClassification, ArtifactClassifier, ArtifactPatterns } from './classify.mts';
3
+ export { defaultDeps, runCleanup, sweepCleanup } from './commands.mts';
4
+ export type { CleanupDeps, CleanupRequest } from './commands.mts';
5
+ export { deleteArtifact, getRunConclusion, githubGet, listArtifactsPage, listRunArtifacts, } from './github.mts';
6
+ export type { DeleteOutcome, GithubArtifact } from './github.mts';
7
+ export { isSweepCandidate, nextPagingState, planRunDeletions, planSweepDeletions, shouldStopPaging, summarize, } from './plan.mts';
8
+ export type { ArtifactLike, DeletionSummary, PagingState } from './plan.mts';
@@ -0,0 +1,4 @@
1
+ export { createArtifactClassifier, parseArtifactPatternsJson } from './classify.mjs';
2
+ export { defaultDeps, runCleanup, sweepCleanup } from './commands.mjs';
3
+ export { deleteArtifact, getRunConclusion, githubGet, listArtifactsPage, listRunArtifacts, } from './github.mjs';
4
+ export { isSweepCandidate, nextPagingState, planRunDeletions, planSweepDeletions, shouldStopPaging, summarize, } from './plan.mjs';
@@ -0,0 +1,17 @@
1
+ import type { ArtifactClassification } from './classify.mts';
2
+ import type { GithubArtifact as ArtifactLike } from './github.mts';
3
+ export type { ArtifactLike };
4
+ export interface DeletionSummary {
5
+ deletedCount: number;
6
+ bytesFreed: number;
7
+ }
8
+ export declare function summarize(deleted: Array<Pick<ArtifactLike, 'size_in_bytes'>>): DeletionSummary;
9
+ export declare function planRunDeletions(artifacts: ArtifactLike[], classify: (name: string) => ArtifactClassification): ArtifactLike[];
10
+ export declare function isSweepCandidate(artifact: ArtifactLike, cutoffIso: string, classify: (name: string) => ArtifactClassification): boolean;
11
+ export interface PagingState {
12
+ page: number;
13
+ consecutiveExpiredPages: number;
14
+ }
15
+ export declare function shouldStopPaging(pageArtifacts: ArtifactLike[], state: PagingState): boolean;
16
+ export declare function nextPagingState(pageArtifacts: ArtifactLike[], state: PagingState): PagingState;
17
+ export declare function planSweepDeletions(candidates: ArtifactLike[], getConclusion: (runId: number) => Promise<string | null>): Promise<ArtifactLike[]>;