package-registry-manager 0.22.4 → 0.22.6
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/README.md +7 -3
- package/package.json +4 -4
- package/src/bootstrap-reference.mjs +76 -0
- package/src/browser-catalogue.mjs +3 -3
- package/src/cli.mjs +67 -26
- package/src/containers.mjs +8 -1
- package/src/credential-browser.mjs +82 -0
- package/src/credential-cycle.mjs +91 -0
- package/src/credential-setup.mjs +210 -0
- package/src/credential-verification.mjs +96 -0
- package/src/discovery.mjs +113 -8
- package/src/extra-flows.mjs +88 -0
- package/src/flows.mjs +29 -19
- package/src/index.mjs +1 -1
- package/src/model.mjs +15 -0
- package/src/npm-policy.mjs +69 -0
- package/src/package-coverage.mjs +106 -0
- package/src/plan.mjs +51 -30
- package/src/prerequisites.mjs +2 -0
- package/src/publishers.mjs +114 -28
- package/src/python.mjs +149 -0
- package/src/setup-batch.mjs +174 -0
- package/src/setup.mjs +75 -61
- package/src/sign-in-import.mjs +8 -0
- package/src/skips.mjs +3 -0
- package/src/source-code.mjs +61 -0
- package/src/tokens.mjs +8 -0
- package/src/version-guard.mjs +34 -0
- package/src/workflow-proposal.mjs +362 -0
- package/src/workflows.mjs +49 -10
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Exact-version registry check for proposed publishing jobs. Errors fail closed. */
|
|
2
|
+
export function versionGuard(registry) {
|
|
3
|
+
const metadata =
|
|
4
|
+
registry === "npm"
|
|
5
|
+
? 'metadata = json.load(open("package.json"))'
|
|
6
|
+
: 'metadata = tomllib.load(open("Cargo.toml", "rb"))["package"]';
|
|
7
|
+
const base =
|
|
8
|
+
registry === "npm"
|
|
9
|
+
? '"https://registry.npmjs.org/" + urllib.parse.quote(name, safe="") + "/" + version'
|
|
10
|
+
: '"https://crates.io/api/v1/crates/" + urllib.parse.quote(name, safe="") + "/" + version';
|
|
11
|
+
return [
|
|
12
|
+
" - name: Check exact registry version",
|
|
13
|
+
" id: version-check",
|
|
14
|
+
" run: |",
|
|
15
|
+
" python3 - <<'PY'",
|
|
16
|
+
" import json, os, time, tomllib, urllib.request, urllib.error, urllib.parse",
|
|
17
|
+
` ${metadata}`,
|
|
18
|
+
' name, version = metadata["name"], metadata["version"]',
|
|
19
|
+
' expected = os.environ.get("RELEASE_VERSION", "").removeprefix("v")',
|
|
20
|
+
' if expected and version != expected: raise SystemExit("Checkout version does not match release output")',
|
|
21
|
+
` url = ${base} + "?_=" + str(time.time_ns())`,
|
|
22
|
+
' request = urllib.request.Request(url, headers={"Cache-Control": "no-cache, no-store", "User-Agent": "package-registry-manager", "Accept": "application/json"})',
|
|
23
|
+
" try:",
|
|
24
|
+
" with urllib.request.urlopen(request, timeout=30) as response:",
|
|
25
|
+
" json.load(response)",
|
|
26
|
+
" publish = False",
|
|
27
|
+
" except urllib.error.HTTPError as error:",
|
|
28
|
+
" if error.code != 404: raise",
|
|
29
|
+
" publish = True",
|
|
30
|
+
' with open(os.environ["GITHUB_OUTPUT"], "a") as output:',
|
|
31
|
+
' output.write("publish=" + str(publish).lower() + "\\n")',
|
|
32
|
+
" PY",
|
|
33
|
+
];
|
|
34
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mkdir,
|
|
3
|
+
mkdtemp,
|
|
4
|
+
writeFile,
|
|
5
|
+
rm,
|
|
6
|
+
realpath,
|
|
7
|
+
lstat,
|
|
8
|
+
} from "node:fs/promises";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { exec } from "command-stream";
|
|
12
|
+
import { packageDirectory } from "./plan.mjs";
|
|
13
|
+
import { parseWorkflow } from "./publishers.mjs";
|
|
14
|
+
import { versionGuard } from "./version-guard.mjs";
|
|
15
|
+
import { readWorkflows } from "./workflows.mjs";
|
|
16
|
+
|
|
17
|
+
const quote = (text) => `'${text.replaceAll("'", "''")}'`;
|
|
18
|
+
const RELEASE_IF =
|
|
19
|
+
"github.event_name == 'release' || ((github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == format('refs/heads/{0}', github.event.repository.default_branch))";
|
|
20
|
+
|
|
21
|
+
function job(packageInfo, index, environment, producer) {
|
|
22
|
+
if (
|
|
23
|
+
!/^(?:[\w@.-]+\/)*[\w@.-]+$/.test(packageInfo.manifest) ||
|
|
24
|
+
packageInfo.manifest.split("/").some((part) => ["..", "."].includes(part))
|
|
25
|
+
) {
|
|
26
|
+
throw new Error("unsafe manifest path for publishing job");
|
|
27
|
+
}
|
|
28
|
+
const directory = packageDirectory(packageInfo.manifest);
|
|
29
|
+
const lines = [
|
|
30
|
+
` prm-publish-${packageInfo.registry}-${index + 1}:`,
|
|
31
|
+
` if: (${RELEASE_IF})${producer ? ` && needs.${producer}.outputs.published_version != ''` : ""}`,
|
|
32
|
+
...(producer
|
|
33
|
+
? [
|
|
34
|
+
` needs: ${producer}`,
|
|
35
|
+
" env:",
|
|
36
|
+
` RELEASE_VERSION: \${{ needs.${producer}.outputs.published_version }}`,
|
|
37
|
+
]
|
|
38
|
+
: []),
|
|
39
|
+
" concurrency:",
|
|
40
|
+
` group: prm-publish-${packageInfo.registry}-${index + 1}-\${{ github.ref }}`,
|
|
41
|
+
" cancel-in-progress: false",
|
|
42
|
+
" runs-on: ubuntu-latest",
|
|
43
|
+
" timeout-minutes: 15",
|
|
44
|
+
" permissions:",
|
|
45
|
+
" contents: read",
|
|
46
|
+
" id-token: write",
|
|
47
|
+
...(environment ? [` environment: ${quote(environment)}`] : []),
|
|
48
|
+
" defaults:",
|
|
49
|
+
" run:",
|
|
50
|
+
` working-directory: ${quote(directory)}`,
|
|
51
|
+
" steps:",
|
|
52
|
+
" - uses: actions/checkout@v6",
|
|
53
|
+
" with:",
|
|
54
|
+
" persist-credentials: false",
|
|
55
|
+
...(producer
|
|
56
|
+
? [" ref: ${{ github.event.repository.default_branch }}"]
|
|
57
|
+
: []),
|
|
58
|
+
];
|
|
59
|
+
switch (packageInfo.registry) {
|
|
60
|
+
case "npm":
|
|
61
|
+
lines.push(
|
|
62
|
+
" - uses: actions/setup-node@v6",
|
|
63
|
+
" with:",
|
|
64
|
+
" node-version: '22'",
|
|
65
|
+
" - run: npm install --global npm@^11",
|
|
66
|
+
" - run: npm install",
|
|
67
|
+
" - run: npm run build --if-present",
|
|
68
|
+
...versionGuard("npm"),
|
|
69
|
+
" - run: npm publish --provenance --access public",
|
|
70
|
+
" if: steps.version-check.outputs.publish == 'true'",
|
|
71
|
+
);
|
|
72
|
+
break;
|
|
73
|
+
case "crates-io":
|
|
74
|
+
lines.push(
|
|
75
|
+
" - uses: dtolnay/rust-toolchain@stable",
|
|
76
|
+
...versionGuard("crates-io"),
|
|
77
|
+
" - uses: rust-lang/crates-io-auth-action@v1",
|
|
78
|
+
" if: steps.version-check.outputs.publish == 'true'",
|
|
79
|
+
" id: auth",
|
|
80
|
+
" - run: cargo publish",
|
|
81
|
+
" if: steps.version-check.outputs.publish == 'true'",
|
|
82
|
+
" env:",
|
|
83
|
+
" CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}",
|
|
84
|
+
);
|
|
85
|
+
break;
|
|
86
|
+
case "pypi":
|
|
87
|
+
lines.push(" - uses: actions/setup-python@v6", " with:");
|
|
88
|
+
lines.push(
|
|
89
|
+
packageInfo.manifest.endsWith("pyproject.toml") &&
|
|
90
|
+
packageInfo.requires_python
|
|
91
|
+
? ` python-version-file: ${quote(packageInfo.manifest)}`
|
|
92
|
+
: " python-version: '3.x'",
|
|
93
|
+
);
|
|
94
|
+
lines.push(
|
|
95
|
+
" - run: python -m pip install build",
|
|
96
|
+
" - run: python -m build",
|
|
97
|
+
" - uses: pypa/gh-action-pypi-publish@release/v1",
|
|
98
|
+
" with:",
|
|
99
|
+
" skip-existing: true",
|
|
100
|
+
` packages-dir: ${quote(`${directory === "." ? "" : `${directory}/`}dist/`)}`,
|
|
101
|
+
);
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
throw new Error(`no publishing job template for ${packageInfo.registry}`);
|
|
105
|
+
}
|
|
106
|
+
return `${lines.join("\n")}\n`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Generates reviewable workflow contents without modifying the checkout. */
|
|
110
|
+
export function workflowProposal(
|
|
111
|
+
inspection,
|
|
112
|
+
packages,
|
|
113
|
+
workflows,
|
|
114
|
+
override,
|
|
115
|
+
chosenEnvironment,
|
|
116
|
+
) {
|
|
117
|
+
const known = [
|
|
118
|
+
...new Set(
|
|
119
|
+
inspection.packages
|
|
120
|
+
.filter(
|
|
121
|
+
(item) =>
|
|
122
|
+
item.publishable !== false &&
|
|
123
|
+
["npm", "crates-io", "pypi"].includes(item.registry),
|
|
124
|
+
)
|
|
125
|
+
.map((item) => item.workflow)
|
|
126
|
+
.filter(Boolean),
|
|
127
|
+
),
|
|
128
|
+
];
|
|
129
|
+
if (!override && known.length > 1) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
"publishing uses several files; pass --add-publish-job --workflow <file> to choose the shared workflow",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const workflow =
|
|
135
|
+
override ??
|
|
136
|
+
known[0] ??
|
|
137
|
+
inspection.repository.release_workflow ??
|
|
138
|
+
"release.yml";
|
|
139
|
+
if (!/^[\w.-]+\.ya?ml$/.test(workflow)) {
|
|
140
|
+
throw new Error("invalid publishing workflow name");
|
|
141
|
+
}
|
|
142
|
+
let contents =
|
|
143
|
+
workflows.find((item) => item.name === workflow)?.contents ??
|
|
144
|
+
"name: Package release\non:\n workflow_dispatch:\n release:\n types: [published]\npermissions:\n contents: read\njobs:\n";
|
|
145
|
+
const parsed = parseWorkflow(contents);
|
|
146
|
+
const producers = parsed.jobs.filter((item) =>
|
|
147
|
+
item.lines.some((line) => /^\s+published_version\s*:/.test(line)),
|
|
148
|
+
);
|
|
149
|
+
if (producers.length > 1) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
"several jobs expose published_version; choose the release dependency manually",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const producer = producers[0]?.name;
|
|
155
|
+
const environments = [
|
|
156
|
+
...new Set(
|
|
157
|
+
inspection.packages
|
|
158
|
+
.filter((item) => item.workflow === workflow)
|
|
159
|
+
.map((item) => item.environment ?? null),
|
|
160
|
+
),
|
|
161
|
+
];
|
|
162
|
+
if (chosenEnvironment === undefined && environments.length > 1) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
"publishing jobs use different environments; choose a shared environment with --environment",
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
let addition = packages
|
|
168
|
+
.map((item, index) =>
|
|
169
|
+
job(item, index, chosenEnvironment ?? environments[0], producer),
|
|
170
|
+
)
|
|
171
|
+
.join("\n");
|
|
172
|
+
for (const item of parseWorkflow(`jobs:\n${addition}`).jobs) {
|
|
173
|
+
if (parsed.jobs.some((existing) => existing.name === item.name)) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`publishing job ${item.name} already exists; inspect the workflow before adding it again`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const lines = contents.split(/\r?\n/);
|
|
180
|
+
const start = lines.findIndex((line) => /^jobs\s*:\s*$/.test(line));
|
|
181
|
+
if (start === -1) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
"workflow has no editable jobs block; add a publishing job manually",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
let end = lines.findIndex((line, index) => index > start && /^\S/.test(line));
|
|
187
|
+
end = end === -1 ? lines.length : end;
|
|
188
|
+
// Match the existing job indentation without reformatting unrelated YAML.
|
|
189
|
+
const indent = parsed.jobs.length
|
|
190
|
+
? (lines
|
|
191
|
+
.slice(start + 1, end)
|
|
192
|
+
.find((line) => /^\s+\S/.test(line) && !/^\s*#/.test(line))
|
|
193
|
+
?.search(/\S/) ?? 2)
|
|
194
|
+
: 2;
|
|
195
|
+
if (indent !== 2) {
|
|
196
|
+
if (indent < 2) {
|
|
197
|
+
throw new Error("unsupported jobs indentation");
|
|
198
|
+
}
|
|
199
|
+
addition = addition
|
|
200
|
+
.split("\n")
|
|
201
|
+
.map((line) => (line ? `${" ".repeat(indent - 2)}${line}` : line))
|
|
202
|
+
.join("\n");
|
|
203
|
+
}
|
|
204
|
+
contents = `${lines.slice(0, end).join("\n").trimEnd()}\n\n${addition}${lines.slice(end).join("\n")}`;
|
|
205
|
+
return { workflow, contents };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Creates a branch and draft PR; never writes or pushes the default branch. */
|
|
209
|
+
export async function offerWorkflow(plans, options) {
|
|
210
|
+
const inspection = options.inspection ?? {
|
|
211
|
+
repository: plans[0].repository,
|
|
212
|
+
packages: plans.map((plan) => plan.package),
|
|
213
|
+
};
|
|
214
|
+
const proposal = workflowProposal(
|
|
215
|
+
inspection,
|
|
216
|
+
plans.map((plan) => plan.package),
|
|
217
|
+
await readWorkflows(options.repository),
|
|
218
|
+
options.workflow,
|
|
219
|
+
options.publisherEnvironment,
|
|
220
|
+
);
|
|
221
|
+
console.log(
|
|
222
|
+
`Proposed .github/workflows/${proposal.workflow}:\n${proposal.contents}`,
|
|
223
|
+
);
|
|
224
|
+
if (
|
|
225
|
+
!options.yes &&
|
|
226
|
+
!/^y(?:es)?$/i.test(
|
|
227
|
+
(
|
|
228
|
+
await options.prompt(
|
|
229
|
+
"Create a branch and draft pull request with these publishing jobs? [y/N] ",
|
|
230
|
+
)
|
|
231
|
+
).trim(),
|
|
232
|
+
)
|
|
233
|
+
) {
|
|
234
|
+
return { status: "blocked" };
|
|
235
|
+
}
|
|
236
|
+
const run =
|
|
237
|
+
options.run ??
|
|
238
|
+
(async (program, args, cwd = options.repository) => {
|
|
239
|
+
if (options.verbose) {
|
|
240
|
+
console.error(`+ ${program} ${args.join(" ")}`);
|
|
241
|
+
}
|
|
242
|
+
const result = await exec(program, args, {
|
|
243
|
+
cwd,
|
|
244
|
+
capture: true,
|
|
245
|
+
mirror: false,
|
|
246
|
+
stdin: "ignore",
|
|
247
|
+
});
|
|
248
|
+
if (result.code !== 0) {
|
|
249
|
+
throw new Error(
|
|
250
|
+
`${program} failed: ${String(result.stderr ?? result.stdout ?? "")}`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
return String(result.stdout ?? "").trim();
|
|
254
|
+
});
|
|
255
|
+
const remote = await run("git", ["remote", "get-url", "origin"]);
|
|
256
|
+
const slug = `${inspection.repository.github_owner}/${inspection.repository.github_repository}`;
|
|
257
|
+
if (
|
|
258
|
+
!inspection.repository.github_owner ||
|
|
259
|
+
!inspection.repository.github_repository ||
|
|
260
|
+
!["https://github.com/", "git@github.com:", "ssh://git@github.com/"].some(
|
|
261
|
+
(prefix) =>
|
|
262
|
+
remote === `${prefix}${slug}` || remote === `${prefix}${slug}.git`,
|
|
263
|
+
)
|
|
264
|
+
) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
"origin must match the inspected GitHub repository before proposing a workflow",
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
const base = await run("gh", [
|
|
270
|
+
"repo",
|
|
271
|
+
"view",
|
|
272
|
+
slug,
|
|
273
|
+
"--json",
|
|
274
|
+
"defaultBranchRef",
|
|
275
|
+
"--jq",
|
|
276
|
+
".defaultBranchRef.name",
|
|
277
|
+
]);
|
|
278
|
+
await run("git", ["fetch", "origin", base]);
|
|
279
|
+
const branch = `prm/publish-jobs-${Date.now()}`;
|
|
280
|
+
const temporary = await mkdtemp(path.join(os.tmpdir(), "prm-workflow-"));
|
|
281
|
+
const checkout = path.join(temporary, "checkout");
|
|
282
|
+
let created = false;
|
|
283
|
+
try {
|
|
284
|
+
await run("git", [
|
|
285
|
+
"worktree",
|
|
286
|
+
"add",
|
|
287
|
+
"-b",
|
|
288
|
+
branch,
|
|
289
|
+
checkout,
|
|
290
|
+
`origin/${base}`,
|
|
291
|
+
]);
|
|
292
|
+
created = true;
|
|
293
|
+
// Recompute from the default branch to preserve edits made since inspection.
|
|
294
|
+
const current = workflowProposal(
|
|
295
|
+
inspection,
|
|
296
|
+
plans.map((plan) => plan.package),
|
|
297
|
+
await readWorkflows(checkout),
|
|
298
|
+
proposal.workflow,
|
|
299
|
+
options.publisherEnvironment,
|
|
300
|
+
);
|
|
301
|
+
if (current.contents !== proposal.contents) {
|
|
302
|
+
console.log(`Updated proposal from ${base}:\n${current.contents}`);
|
|
303
|
+
if (
|
|
304
|
+
!options.yes &&
|
|
305
|
+
!/^y(?:es)?$/i.test(
|
|
306
|
+
(await options.prompt("Use this updated proposal? [y/N] ")).trim(),
|
|
307
|
+
)
|
|
308
|
+
) {
|
|
309
|
+
return { status: "blocked" };
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const file = `.github/workflows/${current.workflow}`;
|
|
313
|
+
await mkdir(path.dirname(path.join(checkout, file)), { recursive: true });
|
|
314
|
+
const target = path.join(checkout, file);
|
|
315
|
+
const physicalParent = await realpath(path.dirname(target));
|
|
316
|
+
if (
|
|
317
|
+
!physicalParent.startsWith(`${await realpath(checkout)}${path.sep}`) ||
|
|
318
|
+
(await lstat(target).catch(() => null))?.isSymbolicLink()
|
|
319
|
+
) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
"workflow must stay inside the proposal worktree without symlinks",
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
await writeFile(target, current.contents);
|
|
325
|
+
await run("git", ["add", "--", file], checkout);
|
|
326
|
+
await run(
|
|
327
|
+
"git",
|
|
328
|
+
["commit", "-m", "Add missing trusted publishing jobs"],
|
|
329
|
+
checkout,
|
|
330
|
+
);
|
|
331
|
+
await run("git", ["push", "origin", `HEAD:refs/heads/${branch}`], checkout);
|
|
332
|
+
const body = `Add missing publishing jobs for ${plans.map((plan) => `${plan.registry}: ${plan.package.name}`).join(", ")} in ${current.workflow}. Review the build, versioning, triggers and job dependencies before merging. Then re-run setup to attach the trusted publishers to this file.`;
|
|
333
|
+
const url = await run(
|
|
334
|
+
"gh",
|
|
335
|
+
[
|
|
336
|
+
"pr",
|
|
337
|
+
"create",
|
|
338
|
+
"--repo",
|
|
339
|
+
slug,
|
|
340
|
+
"--base",
|
|
341
|
+
base,
|
|
342
|
+
"--head",
|
|
343
|
+
branch,
|
|
344
|
+
"--draft",
|
|
345
|
+
"--title",
|
|
346
|
+
"Add missing trusted publishing jobs",
|
|
347
|
+
"--body",
|
|
348
|
+
body,
|
|
349
|
+
],
|
|
350
|
+
checkout,
|
|
351
|
+
);
|
|
352
|
+
console.log(
|
|
353
|
+
`Review ${url}, merge it, then re-run setup. Trusted publishers have not been attached.`,
|
|
354
|
+
);
|
|
355
|
+
return { status: "workflow-pr", url };
|
|
356
|
+
} finally {
|
|
357
|
+
if (created) {
|
|
358
|
+
await run("git", ["worktree", "remove", "--force", checkout]);
|
|
359
|
+
}
|
|
360
|
+
await rm(temporary, { recursive: true, force: true });
|
|
361
|
+
}
|
|
362
|
+
}
|
package/src/workflows.mjs
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
import { readFile, readdir } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { parseWorkflow, executableLines } from "./publishers.mjs";
|
|
4
|
+
import { stripComments, commandPosition } from "./source-code.mjs";
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
]);
|
|
6
|
+
function buildPush(lines) {
|
|
7
|
+
let building = false;
|
|
8
|
+
const start = lines.findIndex((line) => /^\s*steps\s*:/.test(line));
|
|
9
|
+
const first = lines
|
|
10
|
+
.slice(start + 1)
|
|
11
|
+
.find((line) => /^\s*-\s+[\w-]+\s*:/.test(line));
|
|
12
|
+
const stepIndent = first?.search(/\S/) ?? -1;
|
|
13
|
+
for (const line of lines) {
|
|
14
|
+
if (line.search(/\S/) === stepIndent && /^\s*-\s+[\w-]+\s*:/.test(line)) {
|
|
15
|
+
building = false;
|
|
16
|
+
}
|
|
17
|
+
if (/^\s*-?\s*uses\s*:\s*['"]?docker\/build-push-action@/.test(line)) {
|
|
18
|
+
building = true;
|
|
19
|
+
}
|
|
20
|
+
if (building && /^\s*push\s*:\s*true\b/.test(line)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
10
26
|
|
|
11
27
|
/** Reads GitHub Actions workflow files sorted by filename. */
|
|
12
28
|
export async function readWorkflows(root) {
|
|
@@ -31,14 +47,37 @@ export async function readWorkflows(root) {
|
|
|
31
47
|
|
|
32
48
|
/** Returns the first workflow that publishes to the registry, if any. */
|
|
33
49
|
export function publishingWorkflow(workflows, registry) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
if (!["docker-hub", "ghcr"].includes(registry)) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return (
|
|
54
|
+
workflows.find((workflow) =>
|
|
55
|
+
parseWorkflow(stripComments(workflow.contents)).jobs.some((job) => {
|
|
56
|
+
const text = job.lines.join("\n");
|
|
57
|
+
const commands = executableLines(job.lines);
|
|
58
|
+
const pushes =
|
|
59
|
+
commands.some(
|
|
60
|
+
(line) =>
|
|
61
|
+
/\bdocker\s+push\b/.test(line) &&
|
|
62
|
+
commandPosition(line.slice(0, line.indexOf("docker"))),
|
|
63
|
+
) || buildPush(job.lines);
|
|
64
|
+
if (!pushes) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const ghcr = /\bghcr\.io\b/.test(text);
|
|
68
|
+
return registry === "ghcr"
|
|
69
|
+
? ghcr
|
|
70
|
+
: /\bDOCKER_?HUB_|\bdocker\.io\/|hub\.docker\.com/i.test(text) ||
|
|
71
|
+
(/^\s*-?\s*uses\s*:\s*['"]?docker\/login-action@/m.test(text) &&
|
|
72
|
+
!ghcr);
|
|
73
|
+
}),
|
|
74
|
+
) ?? null
|
|
75
|
+
);
|
|
38
76
|
}
|
|
39
77
|
|
|
40
78
|
/** Reports whether a workflow grants `packages: write` to its token. */
|
|
41
79
|
export function grantsPackagesWrite(contents) {
|
|
80
|
+
contents = stripComments(contents);
|
|
42
81
|
return (
|
|
43
82
|
/^\s*packages\s*:\s*['"]?write\b/m.test(contents) ||
|
|
44
83
|
/^\s*permissions\s*:\s*['"]?write-all\b/m.test(contents)
|