wads.npm_trust¶
wads npm-trust: find a repo’s npm publishers and attach Trusted Publishers.
npm trusted publishing (OIDC, see the npm-publishing skill) needs, per package,
one npm trust github <pkg> --file <caller workflow> --repo <owner>/<repo> [--env E]
--allow-publish -y (npm >= 11.15, an owner login with 2FA) and the package must already
exist on npm. Doing that by hand across a fleet means finding, for every package, the
workflow that publishes it. This module does the finding, reports it, and optionally
runs the commands:
Discover (
discover()): the workflows under.github/workflowsthat publish (npm publish,pnpm publish,pnpm -r publish,changeset publish,changesets/actionwith apublish:command, a script such aspnpm releasethat runs one of those, or a caller of a wadsnpm-ci*.ymlreusable workflow), and the non-private packages each publishes (the package in the working directory, every workspace member for-r/--workspaces/ changesets, or the--filter``ed ones), with the job's ``environment. A--dry-run, a commented orecho``ed line, or ``npm run publish:docsis not a publish. For a reusable workflow npm checks the caller’s filename, which is the file found here.Report (
npm_trust(), the default): one row per (package, workflow, environment), saying whether the package is on npm or needs a first manual publish; a row whose name or environment cannot be resolved (${{ ... }}) or is unsafe to pass to npm says so and is never run.Apply (
apply=True): runnpm trust githubfor each package that is on npm, continue past per-package failures, and verify withnpm trust list.
Everything that touches the world (npm, git) goes through one injectable
run callable, so the discovery and the plan are testable without a network.
>>> trust_command(Publisher("@x/a", "o/r", "release.yml", "npm"))
['npm', 'trust', 'github', '@x/a', '--file', 'release.yml', '--repo', 'o/r', '--env', 'npm', '--allow-publish', '-y']
Module Attributes
Seconds one npm/git call may take (npm may wait on an OTP or a web login). |
|
What wads' reusable npm workflows publish when the caller passes no |
Functions
|
Attach each row's publisher; a failure on one package never stops the others. |
|
|
|
Every (package, workflow, environment) the repo's workflows publish to npm. |
|
|
|
Directories under |
|
Whether an |
|
Whether a wads reusable workflow is the pnpm + turbo monorepo one. |
|
Whether |
|
The publish calls of a workflow job: its |
|
Find the npm packages |
|
Names of the non-private packages |
|
Problems that would make every attach fail: no npm, not logged in. |
|
The publish commands in a shell script text, following |
|
Rows as an aligned |
|
|
|
|
|
Items picked by |
|
Packages that more than one workflow publishes, with those workflows. |
|
The default |
|
|
|
The |
|
The text of |
|
|
|
The workspace patterns of the package at |
|
|
|
The nearest directory from |
Classes
|
The flags of one package manager that change what a publish call covers. |
|
What |
|
One publish command: where it runs and which workspace members it covers. |
|
One package that one workflow job publishes: what |
|
A |
- wads.npm_trust.CALL_TIMEOUT = 60¶
Seconds one npm/git call may take (npm may wait on an OTP or a web login).
- class wads.npm_trust.ManagerFlags(filter=frozenset({}), directory=frozenset({}), recursive=frozenset({}), root=frozenset({}), with_root=frozenset({}))[source]¶
Bases:
objectThe flags of one package manager that change what a publish call covers.
Each manager reads the same spelling differently (pnpm’s
-wis the workspace root and takes no value, npm’s-wnames a member and takes one), so the flags are per manager, never shared sets. Onlyfilteranddirectoryflags take a value; every other flag is read as a switch.
- class wads.npm_trust.Outcome(row, action, detail='', verified=None)[source]¶
Bases:
objectWhat
apply_trust()did for one row.
- class wads.npm_trust.PublishCall(directory='.', everything=False, filters=(), workspace_root=False, include_root=False, script='', wads_caller=False, problem='')[source]¶
Bases:
objectOne publish command: where it runs and which workspace members it covers.
- class wads.npm_trust.Publisher(package, repo, workflow, environment='', problem='', publishing_disabled=False)[source]¶
Bases:
objectOne package that one workflow job publishes: what
npm trust githubneeds.problemis set when the row cannot be acted on (unresolved expression, name npm would read as an option); such rows are reported, never run.
- class wads.npm_trust.Row(publisher, on_npm=None, trusted=None)[source]¶
Bases:
objectA
Publisherplus what npm says about it (on_npmNone = unknown).
- wads.npm_trust.WADS_REUSABLE_DEFAULT_DIR = {'npm-ci-monorepo.yml': '.', 'npm-ci.yml': 'js'}¶
What wads’ reusable npm workflows publish when the caller passes no
package-dir.
- wads.npm_trust.apply_trust(rows, *, run=<function subprocess_run>)[source]¶
Attach each row’s publisher; a failure on one package never stops the others.
Rows with a problem, not on npm, or of unknown state are skipped with the reason, rows that already carry the trust are left alone, and a successful attach is verified with
npm trust list.
- wads.npm_trust.check_publisher(publisher)[source]¶
publisherwithproblemset if it must not be passed tonpm trust.- Return type:
- wads.npm_trust.discover(repo_dir='.', *, repo=None, run=<function subprocess_run>)[source]¶
Every (package, workflow, environment) the repo’s workflows publish to npm.
repois theowner/nameslug;Nonereads it from theoriginremote (seegithub_slug()), falling back to'?/?'.
- wads.npm_trust.github_slug(repo_dir='.', *, run=<function subprocess_run>)[source]¶
owner/nameof theoriginremote ofrepo_dir, if it is a GitHub one.>>> fake = lambda argv: subprocess.CompletedProcess(argv, 0, "git@github.com:i2mint/wads.git\n", "") >>> github_slug(".", run=fake) 'i2mint/wads'
- wads.npm_trust.glob_dirs(root, pattern, *, directories=None)[source]¶
Directories under
rootmatching a workspace glob, never enteringnode_modules.directories(from a previous walk) spares walking the tree again for each pattern.
- wads.npm_trust.has_trust(listing, publisher)[source]¶
Whether an
npm trust listtext has an entry naming exactly this repo and workflow.Whole tokens only, so
o/rrorrelease.yml.baknever stand in foro/rorrelease.yml.>>> p = Publisher("pkg", "o/r", "release.yml") >>> has_trust("github o/r release.yml", p) True >>> has_trust("github o/rr release.yml.bak", p) False
- Return type:
- wads.npm_trust.is_monorepo_reusable(workflow)[source]¶
Whether a wads reusable workflow is the pnpm + turbo monorepo one.
- Return type:
- wads.npm_trust.is_on_npm(package, *, run=<function subprocess_run>)[source]¶
Whether
packageis published (None: could not tell, e.g. offline).
- wads.npm_trust.job_calls(job, scripts)[source]¶
The publish calls of a workflow job: its
runsteps, changesets, a wads caller.- Return type:
- wads.npm_trust.npm_trust(repo='.', *, apply=False, dry_run=False, github_repo=None, offline=False, fmt='table')[source]¶
Find the npm packages
repopublishes and attach Trusted Publishers (dry run by default).Prints one row per (package, workflow, environment) with whether the package is on npm or needs a first manual publish, and the
npm trust githubcall for each package--applywould act on. Nothing changes unless--apply(--dry-runis the explicit spelling of the default): then each package that is on npm gets its Trusted Publisher (needs npm >= 11.15 and an owner login with 2FA), failures are reported per package, and every attach is verified withnpm trust list.--offlineskips the npm lookups;--github-repo owner/nameoverrides the origin remote.
- wads.npm_trust.packages_of(root, call)[source]¶
Names of the non-private packages
callpublishes in the repo atroot.Filters,
-rand-ware evaluated from the workspace root abovecall.directory(as the package managers do), not fromcall.directory.>>> import tempfile >>> with tempfile.TemporaryDirectory() as d: ... _ = Path(d, "package.json").write_text('{"name": "solo"}') ... packages_of(Path(d), PublishCall()) ['solo']
- wads.npm_trust.preflight(*, run=<function subprocess_run>)[source]¶
Problems that would make every attach fail: no npm, not logged in.
- wads.npm_trust.publish_calls(text, scripts=None, *, directory='.', _depth=0)[source]¶
The publish commands in a shell script text, following
cdand package scripts.>>> publish_calls("pnpm -r publish --access public")[0].everything True >>> publish_calls("cd packages/a && npm publish")[0].directory 'packages/a' >>> publish_calls("npm publish --dry-run\n# npm publish\necho npm publish") [] >>> publish_calls("pnpm release", {"release": "changeset publish"})[0].everything True
- Return type:
- wads.npm_trust.render_rows(rows, fmt='table')[source]¶
Rows as an aligned
table,tsv(the fleet rollout’s format) orjson.- Return type:
- wads.npm_trust.run_npm_trust(repo='.', *, apply=False, dry_run=False, github_repo=None, offline=False, fmt='table', run=<function subprocess_run>, out=<built-in function print>, err=None)[source]¶
npm_trust()with injectablerun(npm/git) andout(printing).
- wads.npm_trust.select(items, patterns, matches, *, default_all)[source]¶
Items picked by
patterns, where a!prefix subtracts its matches.With no inclusive pattern the result is every item (
default_all) or none, minus the exclusions:pnpm --filter '!b'means “all but b”, a workspace list of only!xglobs means “no members”.>>> select("abc", ["a", "!b"], str.__eq__, default_all=True) ['a'] >>> select("abc", ["!b"], str.__eq__, default_all=True) ['a', 'c'] >>> select("abc", ["!b"], str.__eq__, default_all=False) []
Packages that more than one workflow publishes, with those workflows.
- wads.npm_trust.subprocess_run(argv)[source]¶
The default
run: captured, no stdin, time-limited; a missing tool is rc 127.- Return type:
- wads.npm_trust.survey(publishers, *, run=<function subprocess_run>, check_npm=True)[source]¶
Rowper publisher, asking npm once per package unlesscheck_npm=False.
- wads.npm_trust.trust_command(publisher)[source]¶
The
npm trust githubcall that attaches this publisher.
- wads.npm_trust.trust_listing(package, *, run=<function subprocess_run>)[source]¶
The text of
npm trust list <package>, orNonewhen it cannot be read.
- wads.npm_trust.wads_reusable_call(job)[source]¶
(reusable workflow, package dir)whenjobcalls a wadsnpm-ci*.yml.The dir is the job’s
package-dirinput, else the reusable workflow’s default.>>> wads_reusable_call({"uses": "i2mint/wads/.github/workflows/npm-ci.yml@master"}) ('npm-ci.yml', 'js') >>> wads_reusable_call({"runs-on": "ubuntu-latest"}) is None True
- wads.npm_trust.workspace_globs(root)[source]¶
The workspace patterns of the package at
root(npm/yarnworkspacesor pnpm).