@jonmilner/local-ci 0.3.4 → 0.5.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/README.md +11 -1
- package/dist/cli.js +144 -12
- package/dist/config.d.ts +5 -1
- package/dist/config.js +5 -1
- package/dist/exec.d.ts +2 -0
- package/dist/exec.js +10 -1
- package/dist/github.d.ts +3 -1
- package/dist/github.js +11 -5
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +97 -0
- package/dist/release.d.ts +1 -3
- package/dist/release.js +4 -49
- package/dist/renovate.d.ts +49 -0
- package/dist/renovate.js +191 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,6 +30,14 @@ exec pnpm exec local-ci pre-push "$@"
|
|
|
30
30
|
|
|
31
31
|
A failure after the push closes the PR and deletes `release/<short-sha>`; Ctrl-C (SIGINT), SIGTERM, SIGHUP (closing the terminal) or Ctrl-\\ (SIGQUIT) stops the running step (killing `pnpm release` and the publish it started), does the same cleanup, then exits 128 + the signal number, e.g. 130 for Ctrl-C (a second signal exits at once, killing the publish outright). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. If npm refuses the publish because it already has the version (a rerun while the registry still lags), it says so with the `npm view` command to check, instead of blaming the token; rerun once npm lists it. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
|
|
32
32
|
|
|
33
|
+
- `local-ci renovate`: posts `check` and `pr-title` on Renovate PRs, which no push from this machine ever covers. It finds every open PR the Renovate app opened in the `gh` user's repos (`gh search prs --author app/renovate`) and takes only those on a `renovate/**` branch of the repo itself; any other PR is left to its own push. For each, from the clone at `~/git/<repo>` (`LOCAL_CI_PROJECTS` overrides `~/git`) whose `origin` is that repo, it:
|
|
34
|
+
- skips a repo with no such clone, logging why;
|
|
35
|
+
- skips a head commit that already has a `check` status, posting `pr-title` if that is missing, so a failed check is not rerun on every sweep (rerun it with `local-ci status <sha>` in a checkout of the head);
|
|
36
|
+
- posts without rerunning a head that already passed here (`<repo>.passed`);
|
|
37
|
+
- otherwise fetches the head into a temporary worktree, runs `pnpm install --frozen-lockfile --ignore-scripts` (the head is bot-authored, so no lifecycle scripts run, the root project's included), then `local-ci status <sha>` there, which runs the check command and posts `check` (`success` or `failure`) and `pr-title`. A failed install posts `check` `failure` too. The worktree is removed afterwards, pass or fail; the clone's own checkout is never touched.
|
|
38
|
+
|
|
39
|
+
One sweep runs at a time (`renovate.lock` in the state directory); a second exits with `renovate in progress`. A sweep killed before its cleanup leaves its worktree behind, and the next sweep removes it. It logs one timestamped line per PR and exits non-zero when any PR's check failed or could not run; a PR title that fails lint fails only `pr-title`.
|
|
40
|
+
|
|
33
41
|
## Check command
|
|
34
42
|
|
|
35
43
|
`pnpm check` by default. Override it per repo in `package.json`:
|
|
@@ -42,6 +50,8 @@ A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example in the hook
|
|
|
42
50
|
|
|
43
51
|
## State
|
|
44
52
|
|
|
45
|
-
Logs, the record of passed commits and the release lock live in `~/.local/state/local-ci/<repo>.log`, `<repo>.passed` and `<repo>.release.lock` (`$XDG_STATE_HOME` if set). A lock whose process has died is taken over.
|
|
53
|
+
Logs, the record of passed commits and the release lock live in `~/.local/state/local-ci/<repo>.log`, `<repo>.passed` and `<repo>.release.lock` (`$XDG_STATE_HOME` if set). A lock whose process has died is taken over.
|
|
54
|
+
|
|
55
|
+
One check command runs at a time on the machine, across every repo, so overlapping checks can't starve each other's tests: `pre-push`, `status`, and the `status` runs `renovate` starts each take `check.lock` in the same directory around the check command (not the status posts). A run that finds it held prints `local-ci: waiting for the check lock, held by pid <pid> (<repo> <short-sha>)`, again whenever another run takes it first, and polls every second until the holder exits or dies; Ctrl-C stops the wait. After the wait it skips a commit that passed meanwhile and re-applies the clean-tree rule. The lock goes when the check passes, fails or is killed by a signal. Ctrl-C, SIGTERM or SIGHUP to local-ci passes the signal to the check's whole process group (so a check script's children get it too), waits for the check to exit, then releases the lock and dies of the signal, so a check never outlives its lock. A lock whose holder died (SIGKILL, a crash) or that predates the last boot is taken over by the next run. A local-ci check nested inside a check (one that pushes, say) sees its ancestor holds the lock and runs without waiting. Each check run appends its duration and outcome to the log when it ends, e.g. `[<end time>] abc1234: pnpm check passed in 71.2s` (or `failed in`, or `did not finish after` when the check is killed by a signal or can't start); a commit that already passed is skipped and logs nothing. Ctrl-C stops the check, which logs `did not finish after`.
|
|
46
56
|
|
|
47
57
|
Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
|
package/dist/cli.js
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
3
3
|
import { closeSync, openSync, readFileSync } from "node:fs";
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
5
|
import { basename, dirname, resolve } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, statePaths, } from "./config.js";
|
|
8
|
-
import { exec } from "./exec.js";
|
|
8
|
+
import { exec, run } from "./exec.js";
|
|
9
9
|
import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
|
|
10
|
+
import { LockHeld, takeLock, waitForLock } from "./lock.js";
|
|
10
11
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
11
12
|
import { release } from "./release.js";
|
|
13
|
+
import { projectsDir, renovate } from "./renovate.js";
|
|
12
14
|
import { lintTitle } from "./title.js";
|
|
13
15
|
const USAGE = `Usage:
|
|
14
16
|
local-ci pre-push run the check for each pushed commit (git pre-push hook)
|
|
15
17
|
local-ci post <sha> wait for <sha> on GitHub, then post its check and pr-title statuses
|
|
16
18
|
local-ci pr-title [<pr>] lint the PR title and post the pr-title status
|
|
17
19
|
local-ci status [<sha>] post check for <sha> (default HEAD), running the check if needed
|
|
18
|
-
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
20
|
+
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
21
|
+
local-ci renovate check each open Renovate PR in your repos and post check and pr-title`;
|
|
19
22
|
const git = async (...args) => (await exec("git", args)).trim();
|
|
20
23
|
async function repo() {
|
|
21
24
|
const root = await git("rev-parse", "--show-toplevel");
|
|
22
25
|
// The common dir names the repo even from a linked worktree.
|
|
23
26
|
const common = resolve(root, await git("rev-parse", "--git-common-dir"));
|
|
24
27
|
const name = basename(common) === ".git" ? basename(dirname(common)) : basename(common, ".git");
|
|
25
|
-
return { root, ...statePaths(name) };
|
|
28
|
+
return { root, name, ...statePaths(name) };
|
|
26
29
|
}
|
|
27
30
|
const out = (s) => process.stdout.write(`${s}\n`);
|
|
28
31
|
const err = (s) => process.stderr.write(`${s}\n`);
|
|
@@ -35,34 +38,154 @@ class CheckFailed extends Error {
|
|
|
35
38
|
this.command = command;
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
/** This process's ancestors' pids, or none when `ps` can't list them. */
|
|
42
|
+
function ancestors() {
|
|
43
|
+
const parents = new Map();
|
|
44
|
+
try {
|
|
45
|
+
for (const line of execFileSync("ps", ["-A", "-o", "pid=,ppid="], { encoding: "utf8" }).split("\n")) {
|
|
46
|
+
const [pid, ppid] = line.trim().split(/\s+/).map(Number);
|
|
47
|
+
if (pid && ppid !== undefined)
|
|
48
|
+
parents.set(pid, ppid);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return new Set();
|
|
53
|
+
}
|
|
54
|
+
const found = new Set();
|
|
55
|
+
for (let pid = process.ppid; pid > 1 && !found.has(pid); pid = parents.get(pid) ?? 0)
|
|
56
|
+
found.add(pid);
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
60
|
+
/** The check lock this process holds, the check running under it, and a signal it deferred. */
|
|
61
|
+
let held;
|
|
62
|
+
/** Dies of `signal` as if local-ci had no handler for it. */
|
|
63
|
+
function reraise(signal) {
|
|
64
|
+
for (const name of SIGNALS)
|
|
65
|
+
process.off(name, onSignal);
|
|
66
|
+
process.kill(process.pid, signal);
|
|
67
|
+
// The default action has ended the process by now; this only satisfies the type.
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* While a check runs, passes the signal on and lets the check stop first, so it never outlives
|
|
72
|
+
* the lock; its release then dies of the signal. Otherwise releases the lock and dies of it now.
|
|
73
|
+
*/
|
|
74
|
+
function onSignal(signal) {
|
|
75
|
+
if (held?.child) {
|
|
76
|
+
held.signal ??= signal;
|
|
77
|
+
// The check leads its own process group, so the signal reaches a script's children too.
|
|
78
|
+
try {
|
|
79
|
+
process.kill(-held.child.pid, signal);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
held.child.kill(signal);
|
|
83
|
+
}
|
|
41
84
|
return;
|
|
85
|
+
}
|
|
86
|
+
held?.unlock();
|
|
87
|
+
held = undefined;
|
|
88
|
+
reraise(signal);
|
|
89
|
+
}
|
|
90
|
+
/** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM or SIGHUP too. */
|
|
91
|
+
function releaseOnSignal(unlock) {
|
|
92
|
+
if (!process.listenerCount("SIGINT", onSignal)) {
|
|
93
|
+
for (const name of SIGNALS)
|
|
94
|
+
process.on(name, onSignal);
|
|
95
|
+
}
|
|
96
|
+
const lock = { unlock };
|
|
97
|
+
held = lock;
|
|
98
|
+
return () => {
|
|
99
|
+
if (held === lock)
|
|
100
|
+
held = undefined;
|
|
101
|
+
unlock();
|
|
102
|
+
if (lock.signal)
|
|
103
|
+
reraise(lock.signal);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Throws unless `sha` can be checked in place: it is HEAD and the tree is clean. */
|
|
107
|
+
async function assertCheckable(sha) {
|
|
42
108
|
const reason = cannotCheckInPlace(sha, await git("rev-parse", "HEAD"), await git("status", "--porcelain"));
|
|
43
109
|
if (reason)
|
|
44
110
|
throw new Error(`cannot check ${sha.slice(0, 7)}: ${reason}`);
|
|
111
|
+
}
|
|
112
|
+
/** Takes the machine-wide check lock, waiting while another repo's check holds it. */
|
|
113
|
+
async function lockCheck({ name, checkLock }, sha) {
|
|
114
|
+
const holder = `${name} ${sha.slice(0, 7)}`;
|
|
115
|
+
try {
|
|
116
|
+
return releaseOnSignal(takeLock(checkLock, "check", holder));
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (!(error instanceof LockHeld))
|
|
120
|
+
throw error;
|
|
121
|
+
// A check nested in a check (one that pushes, say) runs under its ancestor's lock rather
|
|
122
|
+
// than waiting forever on it.
|
|
123
|
+
if (ancestors().has(error.pid))
|
|
124
|
+
return () => { };
|
|
125
|
+
}
|
|
126
|
+
return releaseOnSignal(await waitForLock(checkLock, {
|
|
127
|
+
what: "check",
|
|
128
|
+
holder,
|
|
129
|
+
onWait: ({ pid, holder }) => err(`local-ci: waiting for the check lock, held by pid ${pid} (${holder || "unknown"})`),
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Runs the check at `sha` in place, after the clean-tree rule, unless it already passed. One check
|
|
134
|
+
* runs at a time across all repos, so overlapping checks can't starve each other's tests.
|
|
135
|
+
*/
|
|
136
|
+
async function runCheck(current, sha) {
|
|
137
|
+
if (passedCommand(current.passed, sha))
|
|
138
|
+
return;
|
|
139
|
+
await assertCheckable(sha);
|
|
140
|
+
const unlock = await lockCheck(current, sha);
|
|
141
|
+
try {
|
|
142
|
+
// Another run may have passed it, or the tree changed, while this one waited.
|
|
143
|
+
if (passedCommand(current.passed, sha))
|
|
144
|
+
return;
|
|
145
|
+
await assertCheckable(sha);
|
|
146
|
+
await runLocked(current, sha);
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
unlock();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Runs the check command under the check lock, logging its duration and recording a pass. */
|
|
153
|
+
async function runLocked({ root, passed, log: logFile }, sha) {
|
|
45
154
|
const command = resolveCommand(root);
|
|
46
155
|
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
47
156
|
const started = performance.now();
|
|
48
|
-
const
|
|
157
|
+
const child = spawn("sh", ["-c", command], {
|
|
49
158
|
cwd: root,
|
|
159
|
+
// Its own process group, so onSignal can stop everything the check started. A nested check
|
|
160
|
+
// has no handler and stays in its parent check's group, which that check's signal reaches.
|
|
161
|
+
detached: held !== undefined,
|
|
50
162
|
stdio: ["ignore", "inherit", "inherit"],
|
|
51
163
|
});
|
|
164
|
+
if (held)
|
|
165
|
+
held.child = child;
|
|
166
|
+
const result = await new Promise((resolve) => {
|
|
167
|
+
child.on("error", (error) => resolve({ status: null, cause: error.message }));
|
|
168
|
+
child.on("close", (status, signal) => resolve({ status, cause: signal ?? undefined }));
|
|
169
|
+
});
|
|
170
|
+
if (held)
|
|
171
|
+
held.child = undefined;
|
|
172
|
+
// A check local-ci was told to stop never finished, whatever it exited with.
|
|
173
|
+
const interrupted = held?.signal;
|
|
174
|
+
const status = interrupted ? null : result.status;
|
|
52
175
|
// Every run's duration goes in the log, so a faster check can be measured against a slower one.
|
|
53
176
|
// A log that can't be written must not change the check's outcome.
|
|
54
177
|
try {
|
|
55
|
-
appendLog(logFile, checkRunMessage(sha, command,
|
|
178
|
+
appendLog(logFile, checkRunMessage(sha, command, status, performance.now() - started));
|
|
56
179
|
}
|
|
57
180
|
catch (error) {
|
|
58
181
|
err(`local-ci: could not log the run: ${error instanceof Error ? error.message : String(error)}`);
|
|
59
182
|
}
|
|
60
183
|
// A spawn error or a signal means the check never finished, so it is not a failure to post.
|
|
61
|
-
if (
|
|
62
|
-
const cause =
|
|
184
|
+
if (status === null) {
|
|
185
|
+
const cause = interrupted ?? result.cause;
|
|
63
186
|
throw new Error(`${command} did not finish on ${sha.slice(0, 7)}: ${cause}`);
|
|
64
187
|
}
|
|
65
|
-
if (
|
|
188
|
+
if (status !== 0)
|
|
66
189
|
throw new CheckFailed(command, sha);
|
|
67
190
|
recordPass(passed, sha, command);
|
|
68
191
|
}
|
|
@@ -146,11 +269,20 @@ async function runRelease() {
|
|
|
146
269
|
await assertGhAuth(exec);
|
|
147
270
|
out(await release(exec, { root, lock, cli: fileURLToPath(import.meta.url), prTitle, log }));
|
|
148
271
|
}
|
|
272
|
+
async function runRenovate() {
|
|
273
|
+
await assertGhAuth(exec);
|
|
274
|
+
const cli = fileURLToPath(import.meta.url);
|
|
275
|
+
const failed = await renovate({ exec, run, cli, projects: projectsDir() }, log);
|
|
276
|
+
if (failed)
|
|
277
|
+
process.exitCode = 1;
|
|
278
|
+
}
|
|
149
279
|
async function main([command, ...args]) {
|
|
150
280
|
if (command === "pre-push")
|
|
151
281
|
return prePush(args[1]);
|
|
152
282
|
if (command === "release")
|
|
153
283
|
return runRelease();
|
|
284
|
+
if (command === "renovate")
|
|
285
|
+
return runRenovate();
|
|
154
286
|
if (command === "post" || command === "pr-title" || command === "status") {
|
|
155
287
|
await assertGhAuth(exec);
|
|
156
288
|
}
|
package/dist/config.d.ts
CHANGED
|
@@ -4,11 +4,15 @@ export declare function resolveCommand(root: string, env?: NodeJS.ProcessEnv): s
|
|
|
4
4
|
/** Whether `local-ci release` is switched on, with `"local-ci": { "release": true }` in package.json. */
|
|
5
5
|
export declare function releaseEnabled(root: string): boolean;
|
|
6
6
|
export declare function stateDir(env?: NodeJS.ProcessEnv): string;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Where a repo's log, record of passed commits and release lock live, and the check lock all repos
|
|
9
|
+
* share, created on first use.
|
|
10
|
+
*/
|
|
8
11
|
export declare function statePaths(repo: string, env?: NodeJS.ProcessEnv): {
|
|
9
12
|
log: string;
|
|
10
13
|
passed: string;
|
|
11
14
|
lock: string;
|
|
15
|
+
checkLock: string;
|
|
12
16
|
};
|
|
13
17
|
/** A line of the repo's log: the message after an ISO timestamp. */
|
|
14
18
|
export declare function logLine(message: string, now?: Date): string;
|
package/dist/config.js
CHANGED
|
@@ -24,7 +24,10 @@ export function releaseEnabled(root) {
|
|
|
24
24
|
export function stateDir(env = process.env) {
|
|
25
25
|
return join(env.XDG_STATE_HOME || join(homedir(), ".local", "state"), "local-ci");
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Where a repo's log, record of passed commits and release lock live, and the check lock all repos
|
|
29
|
+
* share, created on first use.
|
|
30
|
+
*/
|
|
28
31
|
export function statePaths(repo, env = process.env) {
|
|
29
32
|
const dir = stateDir(env);
|
|
30
33
|
mkdirSync(dir, { recursive: true });
|
|
@@ -32,6 +35,7 @@ export function statePaths(repo, env = process.env) {
|
|
|
32
35
|
log: join(dir, `${repo}.log`),
|
|
33
36
|
passed: join(dir, `${repo}.passed`),
|
|
34
37
|
lock: join(dir, `${repo}.release.lock`),
|
|
38
|
+
checkLock: join(dir, "check.lock"),
|
|
35
39
|
};
|
|
36
40
|
}
|
|
37
41
|
/** A line of the repo's log: the message after an ISO timestamp. */
|
package/dist/exec.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
/** Runs a program and resolves with its stdout; rejects with its stderr on a non-zero exit. */
|
|
2
2
|
export type Exec = (file: string, args: string[]) => Promise<string>;
|
|
3
3
|
export declare const exec: Exec;
|
|
4
|
+
/** Runs a program in `cwd` with its output shown, throwing unless it exits 0. */
|
|
5
|
+
export declare function run(file: string, args: string[], cwd: string, env?: NodeJS.ProcessEnv): void;
|
package/dist/exec.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
1
|
+
import { execFile, spawnSync } from "node:child_process";
|
|
2
|
+
import { basename } from "node:path";
|
|
2
3
|
export const exec = (file, args) => new Promise((resolve, reject) => {
|
|
3
4
|
execFile(file, args, { maxBuffer: 16 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
4
5
|
if (error)
|
|
@@ -7,3 +8,11 @@ export const exec = (file, args) => new Promise((resolve, reject) => {
|
|
|
7
8
|
resolve(stdout);
|
|
8
9
|
});
|
|
9
10
|
});
|
|
11
|
+
/** Runs a program in `cwd` with its output shown, throwing unless it exits 0. */
|
|
12
|
+
export function run(file, args, cwd, env) {
|
|
13
|
+
const result = spawnSync(file, args, { cwd, env, stdio: ["ignore", "inherit", "inherit"] });
|
|
14
|
+
if (result.status !== 0) {
|
|
15
|
+
const cause = result.error?.message ?? result.signal ?? `exit ${result.status}`;
|
|
16
|
+
throw new Error(`${basename(file)} ${args.join(" ")} failed (${cause})`);
|
|
17
|
+
}
|
|
18
|
+
}
|
package/dist/github.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface StatusPayload {
|
|
|
8
8
|
export declare function assertGhAuth(exec: Exec): Promise<void>;
|
|
9
9
|
export declare function checkStatus(command: string, host: string, state?: StatusPayload["state"]): StatusPayload;
|
|
10
10
|
export declare function prTitleStatus(errors: string[]): StatusPayload;
|
|
11
|
-
export declare function postStatus(exec: Exec, sha: string, status: StatusPayload): Promise<void>;
|
|
11
|
+
export declare function postStatus(exec: Exec, sha: string, status: StatusPayload, repo?: string): Promise<void>;
|
|
12
12
|
export declare const defaultSleep: (ms: number) => Promise<void>;
|
|
13
13
|
export interface WaitOptions {
|
|
14
14
|
intervalMs?: number;
|
|
@@ -28,5 +28,7 @@ export interface PullRequest {
|
|
|
28
28
|
}
|
|
29
29
|
/** The given PR, or the current branch's PR when `pr` is omitted. */
|
|
30
30
|
export declare function viewPr(exec: Exec, pr?: string): Promise<PullRequest>;
|
|
31
|
+
/** The latest state of each status context on `sha`. */
|
|
32
|
+
export declare function commitStatuses(exec: Exec, sha: string, repo?: string): Promise<Map<string, string>>;
|
|
31
33
|
/** Polls until every context in `contexts` is `success` on `sha`; throws on the first that fails. */
|
|
32
34
|
export declare function waitForStatuses(exec: Exec, sha: string, contexts: string[], options?: WaitOptions): Promise<void>;
|
package/dist/github.js
CHANGED
|
@@ -29,12 +29,14 @@ export function prTitleStatus(errors) {
|
|
|
29
29
|
? { state: "failure", context: "pr-title", description: truncate(errors[0]) }
|
|
30
30
|
: { state: "success", context: "pr-title", description: "Conventional Commit title" };
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
/** `repos/<repo>` for an `owner/repo`, else the placeholders `gh api` fills from the cwd or GH_REPO. */
|
|
33
|
+
const repoPath = (repo) => (repo ? `repos/${repo}` : REPO);
|
|
34
|
+
export async function postStatus(exec, sha, status, repo) {
|
|
33
35
|
await exec("gh", [
|
|
34
36
|
"api",
|
|
35
37
|
"--method",
|
|
36
38
|
"POST",
|
|
37
|
-
`${
|
|
39
|
+
`${repoPath(repo)}/statuses/${sha}`,
|
|
38
40
|
"-f",
|
|
39
41
|
`state=${status.state}`,
|
|
40
42
|
"-f",
|
|
@@ -84,14 +86,18 @@ export async function viewPr(exec, pr) {
|
|
|
84
86
|
const args = ["pr", "view", ...(pr ? [pr] : []), "--json", "number,title,headRefOid"];
|
|
85
87
|
return JSON.parse(await exec("gh", args));
|
|
86
88
|
}
|
|
89
|
+
/** The latest state of each status context on `sha`. */
|
|
90
|
+
export async function commitStatuses(exec, sha, repo) {
|
|
91
|
+
// The combined status holds the latest status per context.
|
|
92
|
+
const { statuses } = JSON.parse(await exec("gh", ["api", `${repoPath(repo)}/commits/${sha}/status`]));
|
|
93
|
+
return new Map(statuses.map(({ context, state }) => [context, state]));
|
|
94
|
+
}
|
|
87
95
|
/** Polls until every context in `contexts` is `success` on `sha`; throws on the first that fails. */
|
|
88
96
|
export async function waitForStatuses(exec, sha, contexts, options = {}) {
|
|
89
97
|
const { intervalMs = 5_000, timeoutMs = 600_000 } = options;
|
|
90
98
|
const { sleep = defaultSleep } = options;
|
|
91
99
|
for (let waited = 0;; waited += intervalMs) {
|
|
92
|
-
|
|
93
|
-
const { statuses } = JSON.parse(await exec("gh", ["api", `${REPO}/commits/${sha}/status`]));
|
|
94
|
-
const states = new Map(statuses.map((status) => [status.context, status.state]));
|
|
100
|
+
const states = await commitStatuses(exec, sha);
|
|
95
101
|
const failed = contexts.find((context) => /^(failure|error)$/.test(states.get(context) ?? ""));
|
|
96
102
|
if (failed)
|
|
97
103
|
throw new Error(`${failed} failed on ${sha.slice(0, 7)}`);
|
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A live process holds the lock. `holder` is what it wrote after its pid, such as its repo. */
|
|
2
|
+
export declare class LockHeld extends Error {
|
|
3
|
+
readonly pid: number;
|
|
4
|
+
readonly holder: string;
|
|
5
|
+
constructor(what: string, file: string, pid: number, holder: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
9
|
+
* lock file holds the pid, then `holder` on a second line when given.
|
|
10
|
+
*/
|
|
11
|
+
export declare function takeLock(file: string, what?: string, holder?: string): () => void;
|
|
12
|
+
export interface WaitForLockOptions {
|
|
13
|
+
what: string;
|
|
14
|
+
holder?: string;
|
|
15
|
+
/** Called when the wait starts, and again whenever another process takes the lock first. */
|
|
16
|
+
onWait?: (held: LockHeld) => void;
|
|
17
|
+
intervalMs?: number;
|
|
18
|
+
sleep?: (ms: number) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/** Takes the lock, polling while a live process holds it, and returns its release. */
|
|
21
|
+
export declare function waitForLock(file: string, { what, holder, onWait, intervalMs, sleep }: WaitForLockOptions): Promise<() => void>;
|
package/dist/lock.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { linkSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { uptime } from "node:os";
|
|
3
|
+
import { defaultSleep } from "./github.js";
|
|
4
|
+
/** Whether a process with this pid is running. */
|
|
5
|
+
function alive(pid) {
|
|
6
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
7
|
+
return false;
|
|
8
|
+
try {
|
|
9
|
+
process.kill(pid, 0);
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
// EPERM: the process exists but belongs to another user.
|
|
14
|
+
return error.code === "EPERM";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether the lock file was written before the machine booted, so its pid, alive or not, belongs to
|
|
19
|
+
* a process that never held it.
|
|
20
|
+
*/
|
|
21
|
+
function beforeBoot(file) {
|
|
22
|
+
try {
|
|
23
|
+
return statSync(file).mtimeMs < Date.now() - uptime() * 1_000;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** A live process holds the lock. `holder` is what it wrote after its pid, such as its repo. */
|
|
30
|
+
export class LockHeld extends Error {
|
|
31
|
+
pid;
|
|
32
|
+
holder;
|
|
33
|
+
constructor(what, file, pid, holder) {
|
|
34
|
+
super(`${what} in progress (pid ${pid}, lock ${file})`);
|
|
35
|
+
this.pid = pid;
|
|
36
|
+
this.holder = holder;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
41
|
+
* lock file holds the pid, then `holder` on a second line when given.
|
|
42
|
+
*/
|
|
43
|
+
export function takeLock(file, what = "release", holder) {
|
|
44
|
+
// Linking a file that already holds the pid publishes the lock atomically, so nobody reads it empty.
|
|
45
|
+
const mine = `${file}.${process.pid}`;
|
|
46
|
+
writeFileSync(mine, holder ? `${process.pid}\n${holder}` : String(process.pid));
|
|
47
|
+
try {
|
|
48
|
+
// Each pass takes the lock, finds a live holder, or clears a released or dead one.
|
|
49
|
+
for (;;) {
|
|
50
|
+
try {
|
|
51
|
+
linkSync(mine, file);
|
|
52
|
+
return () => rmSync(file, { force: true });
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error.code !== "EEXIST")
|
|
56
|
+
throw error;
|
|
57
|
+
let content;
|
|
58
|
+
try {
|
|
59
|
+
content = readFileSync(file, "utf8");
|
|
60
|
+
}
|
|
61
|
+
catch (readError) {
|
|
62
|
+
// Released between the link and the read: try again.
|
|
63
|
+
if (readError.code === "ENOENT")
|
|
64
|
+
continue;
|
|
65
|
+
throw readError;
|
|
66
|
+
}
|
|
67
|
+
const [pid = "", ...rest] = content.split("\n");
|
|
68
|
+
if (alive(Number(pid)) && !beforeBoot(file)) {
|
|
69
|
+
throw new LockHeld(what, file, Number(pid), rest.join("\n"));
|
|
70
|
+
}
|
|
71
|
+
// The holder died without releasing it, or its pid is another process's since a reboot.
|
|
72
|
+
rmSync(file, { force: true });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
rmSync(mine, { force: true });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Takes the lock, polling while a live process holds it, and returns its release. */
|
|
81
|
+
export async function waitForLock(file, { what, holder, onWait, intervalMs = 1_000, sleep = defaultSleep }) {
|
|
82
|
+
let last;
|
|
83
|
+
for (;;) {
|
|
84
|
+
try {
|
|
85
|
+
return takeLock(file, what, holder);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (!(error instanceof LockHeld))
|
|
89
|
+
throw error;
|
|
90
|
+
const seen = `${error.pid}\n${error.holder}`;
|
|
91
|
+
if (seen !== last)
|
|
92
|
+
onWait?.(error);
|
|
93
|
+
last = seen;
|
|
94
|
+
await sleep(intervalMs);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/release.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Exec } from "./exec.ts";
|
|
2
2
|
import { type WaitOptions } from "./github.ts";
|
|
3
3
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
4
4
|
export declare const TOKEN_SERVICE = "local-ci-npm";
|
|
@@ -8,8 +8,6 @@ export interface Package {
|
|
|
8
8
|
name: string;
|
|
9
9
|
version: string;
|
|
10
10
|
}
|
|
11
|
-
/** Takes the release lock and returns its release, or throws while a live process holds it. */
|
|
12
|
-
export declare function takeLock(file: string): () => void;
|
|
13
11
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
14
12
|
export declare function pendingChangesets(dir: string): string[];
|
|
15
13
|
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
package/dist/release.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { spawn
|
|
2
|
-
import { existsSync,
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
3
3
|
import { constants, tmpdir } from "node:os";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { run } from "./exec.js";
|
|
5
6
|
import { defaultSleep, waitForStatuses } from "./github.js";
|
|
7
|
+
import { takeLock } from "./lock.js";
|
|
6
8
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
7
9
|
export const TOKEN_SERVICE = "local-ci-npm";
|
|
8
10
|
/** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
|
|
@@ -19,45 +21,6 @@ const AUTH_ERROR = /\b(?:E401|E403|EOTP|ERR_PNPM_OTP_NON_INTERACTIVE|401 Unautho
|
|
|
19
21
|
// What npm prints when the version is already on the registry. It comes with an E403, so it is checked before AUTH_ERROR.
|
|
20
22
|
const PUBLISH_OVER = /cannot publish over the previously published version/;
|
|
21
23
|
const id = ({ name, version }) => `${name}@${version}`;
|
|
22
|
-
function alive(pid) {
|
|
23
|
-
if (!Number.isInteger(pid) || pid <= 0)
|
|
24
|
-
return false;
|
|
25
|
-
try {
|
|
26
|
-
process.kill(pid, 0);
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
// EPERM: the process exists but belongs to another user.
|
|
31
|
-
return error.code === "EPERM";
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/** Takes the release lock and returns its release, or throws while a live process holds it. */
|
|
35
|
-
export function takeLock(file) {
|
|
36
|
-
// Linking a file that already holds the pid publishes the lock atomically, so nobody reads it empty.
|
|
37
|
-
const mine = `${file}.${process.pid}`;
|
|
38
|
-
writeFileSync(mine, String(process.pid));
|
|
39
|
-
try {
|
|
40
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
41
|
-
try {
|
|
42
|
-
linkSync(mine, file);
|
|
43
|
-
return () => rmSync(file, { force: true });
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
if (error.code !== "EEXIST")
|
|
47
|
-
throw error;
|
|
48
|
-
const pid = Number(readFileSync(file, "utf8"));
|
|
49
|
-
if (alive(pid))
|
|
50
|
-
throw new Error(`release in progress (pid ${pid}, lock ${file})`);
|
|
51
|
-
// The holder died without releasing it.
|
|
52
|
-
rmSync(file, { force: true });
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
finally {
|
|
57
|
-
rmSync(mine, { force: true });
|
|
58
|
-
}
|
|
59
|
-
throw new Error(`could not take the release lock ${file}`);
|
|
60
|
-
}
|
|
61
24
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
62
25
|
export function pendingChangesets(dir) {
|
|
63
26
|
const folder = join(dir, ".changeset");
|
|
@@ -107,14 +70,6 @@ export async function readToken(exec) {
|
|
|
107
70
|
}
|
|
108
71
|
return token;
|
|
109
72
|
}
|
|
110
|
-
/** Runs a command in `cwd` with its output shown, throwing when it exits non-zero. */
|
|
111
|
-
function run(file, args, cwd) {
|
|
112
|
-
const result = spawnSync(file, args, { cwd, stdio: ["ignore", "inherit", "inherit"] });
|
|
113
|
-
if (result.status !== 0) {
|
|
114
|
-
const cause = result.error?.message ?? result.signal ?? `exit ${result.status}`;
|
|
115
|
-
throw new Error(`${file} ${args.join(" ")} failed (${cause})`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
73
|
/** The hint for checking each package on npm by hand. */
|
|
119
74
|
const npmView = (packages) => packages.length
|
|
120
75
|
? `check with: ${packages.map((pkg) => `npm view ${id(pkg)} version`).join("; ")}`
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Exec } from "./exec.ts";
|
|
2
|
+
/** The login `gh` gives the Renovate GitHub app. */
|
|
3
|
+
export declare const RENOVATE_AUTHOR = "app/renovate";
|
|
4
|
+
export declare const RENOVATE_BRANCH = "renovate/";
|
|
5
|
+
/** Runs a program to completion with `env`, throwing when it exits non-zero. */
|
|
6
|
+
export type Run = (file: string, args: string[], cwd: string, env: NodeJS.ProcessEnv) => void;
|
|
7
|
+
export interface FoundPr {
|
|
8
|
+
/** `owner/repo`. */
|
|
9
|
+
repo: string;
|
|
10
|
+
/** The repo's name, which is its clone's directory under the projects directory. */
|
|
11
|
+
name: string;
|
|
12
|
+
number: number;
|
|
13
|
+
}
|
|
14
|
+
export interface PrView {
|
|
15
|
+
author: {
|
|
16
|
+
login: string;
|
|
17
|
+
};
|
|
18
|
+
headRefName: string;
|
|
19
|
+
headRefOid: string;
|
|
20
|
+
isCrossRepository: boolean;
|
|
21
|
+
state: string;
|
|
22
|
+
}
|
|
23
|
+
/** The projects directory holding each repo's clone: `LOCAL_CI_PROJECTS`, else `~/git`. */
|
|
24
|
+
export declare function projectsDir(env?: NodeJS.ProcessEnv): string;
|
|
25
|
+
/** Every open PR the Renovate app opened in `owner`'s repos. */
|
|
26
|
+
export declare function searchRenovatePrs(exec: Exec, owner: string): Promise<FoundPr[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Why `pr` is not a Renovate PR to check, or null when it is: open, opened by the Renovate app,
|
|
29
|
+
* from a `renovate/` branch of the repo itself (not a fork).
|
|
30
|
+
*/
|
|
31
|
+
export declare function notRenovate(pr: PrView): string | null;
|
|
32
|
+
export interface RenovateContext {
|
|
33
|
+
exec: Exec;
|
|
34
|
+
run: Run;
|
|
35
|
+
/** This CLI, run as `status`, `post` or `pr-title` for the PR's head. */
|
|
36
|
+
cli: string;
|
|
37
|
+
projects: string;
|
|
38
|
+
env?: NodeJS.ProcessEnv;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Checks one Renovate PR's head and posts `check` and `pr-title` on it, resolving with what it did.
|
|
42
|
+
* It throws when the check fails or can't run.
|
|
43
|
+
*/
|
|
44
|
+
export declare function checkRenovatePr(context: RenovateContext, found: FoundPr): Promise<string>;
|
|
45
|
+
/**
|
|
46
|
+
* Checks every open Renovate PR in the gh user's repos, one at a time, logging each outcome.
|
|
47
|
+
* Resolves with the number that failed, so one broken PR never stops the rest.
|
|
48
|
+
*/
|
|
49
|
+
export declare function renovate(context: RenovateContext, log: (message: string) => void): Promise<number>;
|
package/dist/renovate.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { homedir, hostname, tmpdir } from "node:os";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { passedCommand, stateDir, statePaths } from "./config.js";
|
|
5
|
+
import { checkStatus, commitStatuses, postStatus } from "./github.js";
|
|
6
|
+
import { takeLock } from "./lock.js";
|
|
7
|
+
import { githubRepo } from "./push.js";
|
|
8
|
+
/** The login `gh` gives the Renovate GitHub app. */
|
|
9
|
+
export const RENOVATE_AUTHOR = "app/renovate";
|
|
10
|
+
export const RENOVATE_BRANCH = "renovate/";
|
|
11
|
+
const TEMP_PREFIX = "local-ci-renovate-";
|
|
12
|
+
// Frozen, and with no lifecycle scripts at all: the head is bot-authored, and the root project's
|
|
13
|
+
// scripts (`prepare` and the like) could write to the clone's shared git config.
|
|
14
|
+
const INSTALL = ["install", "--frozen-lockfile", "--ignore-scripts"];
|
|
15
|
+
/** The projects directory holding each repo's clone: `LOCAL_CI_PROJECTS`, else `~/git`. */
|
|
16
|
+
export function projectsDir(env = process.env) {
|
|
17
|
+
return env.LOCAL_CI_PROJECTS?.trim() || join(homedir(), "git");
|
|
18
|
+
}
|
|
19
|
+
/** Every open PR the Renovate app opened in `owner`'s repos. */
|
|
20
|
+
export async function searchRenovatePrs(exec, owner) {
|
|
21
|
+
const found = JSON.parse(await exec("gh", [
|
|
22
|
+
"search",
|
|
23
|
+
"prs",
|
|
24
|
+
"--author",
|
|
25
|
+
RENOVATE_AUTHOR,
|
|
26
|
+
"--owner",
|
|
27
|
+
owner,
|
|
28
|
+
"--state",
|
|
29
|
+
"open",
|
|
30
|
+
"--json",
|
|
31
|
+
"repository,number",
|
|
32
|
+
// gh returns 30 results unless told otherwise; 1000 is the search API's cap.
|
|
33
|
+
"--limit",
|
|
34
|
+
"1000",
|
|
35
|
+
]));
|
|
36
|
+
return found.map(({ number, repository }) => ({
|
|
37
|
+
repo: repository.nameWithOwner,
|
|
38
|
+
name: repository.name,
|
|
39
|
+
number,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Why `pr` is not a Renovate PR to check, or null when it is: open, opened by the Renovate app,
|
|
44
|
+
* from a `renovate/` branch of the repo itself (not a fork).
|
|
45
|
+
*/
|
|
46
|
+
export function notRenovate(pr) {
|
|
47
|
+
if (pr.state !== "OPEN")
|
|
48
|
+
return `it is ${pr.state.toLowerCase()}`;
|
|
49
|
+
if (pr.author.login !== RENOVATE_AUTHOR)
|
|
50
|
+
return `${pr.author.login} opened it`;
|
|
51
|
+
if (pr.isCrossRepository)
|
|
52
|
+
return "its branch is in a fork";
|
|
53
|
+
if (!pr.headRefName.startsWith(RENOVATE_BRANCH)) {
|
|
54
|
+
return `its branch ${pr.headRefName} is not ${RENOVATE_BRANCH}**`;
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const message = (error) => (error instanceof Error ? error.message : String(error));
|
|
59
|
+
/**
|
|
60
|
+
* Removes the temp worktrees a killed sweep left in `clone`. The sweep lock is held, so none
|
|
61
|
+
* belongs to a live sweep.
|
|
62
|
+
*/
|
|
63
|
+
async function pruneStale(exec, clone) {
|
|
64
|
+
await exec("git", ["-C", clone, "worktree", "prune"]);
|
|
65
|
+
const list = await exec("git", ["-C", clone, "worktree", "list", "--porcelain"]);
|
|
66
|
+
for (const [, path = ""] of list.matchAll(/^worktree (.+)$/gm)) {
|
|
67
|
+
if (basename(path) !== "worktree" || !basename(dirname(path)).startsWith(TEMP_PREFIX))
|
|
68
|
+
continue;
|
|
69
|
+
await exec("git", ["-C", clone, "worktree", "remove", "--force", "--force", path]);
|
|
70
|
+
rmSync(dirname(path), { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks one Renovate PR's head and posts `check` and `pr-title` on it, resolving with what it did.
|
|
75
|
+
* It throws when the check fails or can't run.
|
|
76
|
+
*/
|
|
77
|
+
export async function checkRenovatePr(context, found) {
|
|
78
|
+
const { exec, run, cli, projects, env = process.env } = context;
|
|
79
|
+
const { repo, name, number } = found;
|
|
80
|
+
const pr = JSON.parse(await exec("gh", [
|
|
81
|
+
"pr",
|
|
82
|
+
"view",
|
|
83
|
+
String(number),
|
|
84
|
+
"--repo",
|
|
85
|
+
repo,
|
|
86
|
+
"--json",
|
|
87
|
+
"author,headRefName,headRefOid,isCrossRepository,state",
|
|
88
|
+
]));
|
|
89
|
+
const skip = notRenovate(pr);
|
|
90
|
+
if (skip)
|
|
91
|
+
return `skipped: ${skip}`;
|
|
92
|
+
const clone = join(projects, name);
|
|
93
|
+
if (!existsSync(join(clone, ".git")))
|
|
94
|
+
return `skipped: no clone at ${clone}`;
|
|
95
|
+
// The raw URL, so a clone of another owner's repo of the same name is never checked as this one.
|
|
96
|
+
const origin = (await exec("git", ["-C", clone, "config", "--get", "remote.origin.url"]).catch(() => "")).trim();
|
|
97
|
+
if (githubRepo(origin)?.toLowerCase() !== repo.toLowerCase()) {
|
|
98
|
+
return `skipped: ${clone}'s origin is ${origin || "unset"}, not ${repo}`;
|
|
99
|
+
}
|
|
100
|
+
const sha = pr.headRefOid;
|
|
101
|
+
const short = sha.slice(0, 7);
|
|
102
|
+
// Aim gh at the PR's repo whatever directory it runs in.
|
|
103
|
+
const childEnv = { ...env, GH_REPO: repo };
|
|
104
|
+
// Lints the title and posts `pr-title`. A bad title fails only that status, never the sweep.
|
|
105
|
+
const postTitle = () => {
|
|
106
|
+
try {
|
|
107
|
+
run(process.execPath, [cli, "pr-title", String(number)], clone, childEnv);
|
|
108
|
+
return "";
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
return `; ${message(error)}`;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const posted = await commitStatuses(exec, sha, repo);
|
|
115
|
+
const check = posted.get("check");
|
|
116
|
+
if (check) {
|
|
117
|
+
// A failed check stays failed until someone reruns it, so a sweep doesn't rerun it every time.
|
|
118
|
+
const title = posted.has("pr-title") ? "" : postTitle();
|
|
119
|
+
return `skipped: ${short} already has check ${check}${title}`;
|
|
120
|
+
}
|
|
121
|
+
// Passed here but never posted, e.g. a sweep killed between the two: post without rerunning.
|
|
122
|
+
if (passedCommand(statePaths(name, env).passed, sha)) {
|
|
123
|
+
run(process.execPath, [cli, "post", sha], clone, childEnv);
|
|
124
|
+
return `${short} already passed; posted check and pr-title`;
|
|
125
|
+
}
|
|
126
|
+
await exec("git", ["-C", clone, "fetch", "--quiet", "origin", sha]);
|
|
127
|
+
await pruneStale(exec, clone);
|
|
128
|
+
const temp = mkdtempSync(join(tmpdir(), TEMP_PREFIX));
|
|
129
|
+
const dir = join(temp, "worktree");
|
|
130
|
+
try {
|
|
131
|
+
await exec("git", ["-C", clone, "worktree", "add", "--detach", "--quiet", dir, sha]);
|
|
132
|
+
try {
|
|
133
|
+
run("pnpm", INSTALL, dir, childEnv);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
// Posted like a failed check, so the PR shows red and later sweeps skip the head. A post
|
|
137
|
+
// that fails leaves the head with no `check`, so the next sweep tries it again.
|
|
138
|
+
const failed = checkStatus(`pnpm ${INSTALL.join(" ")}`, hostname(), "failure");
|
|
139
|
+
await postStatus(exec, sha, failed, repo).catch(() => { });
|
|
140
|
+
postTitle();
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
// Runs the check and posts `check` (failure too); on success it also posts `pr-title`.
|
|
144
|
+
try {
|
|
145
|
+
run(process.execPath, [cli, "status", sha], dir, childEnv);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// A failed `status` posts no `pr-title`, so post it here rather than on the next sweep.
|
|
149
|
+
postTitle();
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
// Twice, so a locked worktree goes too; prune forgets one whose add half-failed.
|
|
155
|
+
await exec("git", ["-C", clone, "worktree", "remove", "--force", "--force", dir]).catch(() => { });
|
|
156
|
+
rmSync(temp, { recursive: true, force: true });
|
|
157
|
+
await exec("git", ["-C", clone, "worktree", "prune"]).catch(() => { });
|
|
158
|
+
}
|
|
159
|
+
return `${short} checked; posted check and pr-title`;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Checks every open Renovate PR in the gh user's repos, one at a time, logging each outcome.
|
|
163
|
+
* Resolves with the number that failed, so one broken PR never stops the rest.
|
|
164
|
+
*/
|
|
165
|
+
export async function renovate(context, log) {
|
|
166
|
+
// One sweep at a time, so two never check the same head or race on a clone.
|
|
167
|
+
const state = stateDir(context.env);
|
|
168
|
+
mkdirSync(state, { recursive: true });
|
|
169
|
+
const unlock = takeLock(join(state, "renovate.lock"), "renovate");
|
|
170
|
+
try {
|
|
171
|
+
const owner = (await context.exec("gh", ["api", "user", "--jq", ".login"])).trim();
|
|
172
|
+
const found = await searchRenovatePrs(context.exec, owner);
|
|
173
|
+
if (!found.length)
|
|
174
|
+
log(`no open Renovate PRs in ${owner}'s repos`);
|
|
175
|
+
let failed = 0;
|
|
176
|
+
for (const pr of found) {
|
|
177
|
+
const label = `${pr.repo}#${pr.number}`;
|
|
178
|
+
try {
|
|
179
|
+
log(`${label}: ${await checkRenovatePr(context, pr)}`);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
failed++;
|
|
183
|
+
log(`${label}: ${message(error)}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return failed;
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
unlock();
|
|
190
|
+
}
|
|
191
|
+
}
|