@jonmilner/local-ci 0.3.4 → 0.4.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 +8 -0
- package/dist/cli.js +13 -2
- 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/release.d.ts +3 -3
- package/dist/release.js +6 -13
- 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`:
|
package/dist/cli.js
CHANGED
|
@@ -5,17 +5,19 @@ 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
10
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
11
11
|
import { release } from "./release.js";
|
|
12
|
+
import { projectsDir, renovate } from "./renovate.js";
|
|
12
13
|
import { lintTitle } from "./title.js";
|
|
13
14
|
const USAGE = `Usage:
|
|
14
15
|
local-ci pre-push run the check for each pushed commit (git pre-push hook)
|
|
15
16
|
local-ci post <sha> wait for <sha> on GitHub, then post its check and pr-title statuses
|
|
16
17
|
local-ci pr-title [<pr>] lint the PR title and post the pr-title status
|
|
17
18
|
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
|
|
19
|
+
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
20
|
+
local-ci renovate check each open Renovate PR in your repos and post check and pr-title`;
|
|
19
21
|
const git = async (...args) => (await exec("git", args)).trim();
|
|
20
22
|
async function repo() {
|
|
21
23
|
const root = await git("rev-parse", "--show-toplevel");
|
|
@@ -146,11 +148,20 @@ async function runRelease() {
|
|
|
146
148
|
await assertGhAuth(exec);
|
|
147
149
|
out(await release(exec, { root, lock, cli: fileURLToPath(import.meta.url), prTitle, log }));
|
|
148
150
|
}
|
|
151
|
+
async function runRenovate() {
|
|
152
|
+
await assertGhAuth(exec);
|
|
153
|
+
const cli = fileURLToPath(import.meta.url);
|
|
154
|
+
const failed = await renovate({ exec, run, cli, projects: projectsDir() }, log);
|
|
155
|
+
if (failed)
|
|
156
|
+
process.exitCode = 1;
|
|
157
|
+
}
|
|
149
158
|
async function main([command, ...args]) {
|
|
150
159
|
if (command === "pre-push")
|
|
151
160
|
return prePush(args[1]);
|
|
152
161
|
if (command === "release")
|
|
153
162
|
return runRelease();
|
|
163
|
+
if (command === "renovate")
|
|
164
|
+
return runRenovate();
|
|
154
165
|
if (command === "post" || command === "pr-title" || command === "status") {
|
|
155
166
|
await assertGhAuth(exec);
|
|
156
167
|
}
|
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/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,8 @@ export interface Package {
|
|
|
8
8
|
name: string;
|
|
9
9
|
version: string;
|
|
10
10
|
}
|
|
11
|
-
/** Takes the
|
|
12
|
-
export declare function takeLock(file: string): () => void;
|
|
11
|
+
/** Takes the lock and returns its release, or throws `<what> in progress` while a live process holds it. */
|
|
12
|
+
export declare function takeLock(file: string, what?: string): () => void;
|
|
13
13
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
14
14
|
export declare function pendingChangesets(dir: string): string[];
|
|
15
15
|
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
package/dist/release.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { spawn
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
import { existsSync, linkSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, } 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";
|
|
6
7
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
7
8
|
export const TOKEN_SERVICE = "local-ci-npm";
|
|
@@ -31,8 +32,8 @@ function alive(pid) {
|
|
|
31
32
|
return error.code === "EPERM";
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
|
-
/** Takes the
|
|
35
|
-
export function takeLock(file) {
|
|
35
|
+
/** Takes the lock and returns its release, or throws `<what> in progress` while a live process holds it. */
|
|
36
|
+
export function takeLock(file, what = "release") {
|
|
36
37
|
// Linking a file that already holds the pid publishes the lock atomically, so nobody reads it empty.
|
|
37
38
|
const mine = `${file}.${process.pid}`;
|
|
38
39
|
writeFileSync(mine, String(process.pid));
|
|
@@ -47,7 +48,7 @@ export function takeLock(file) {
|
|
|
47
48
|
throw error;
|
|
48
49
|
const pid = Number(readFileSync(file, "utf8"));
|
|
49
50
|
if (alive(pid))
|
|
50
|
-
throw new Error(
|
|
51
|
+
throw new Error(`${what} in progress (pid ${pid}, lock ${file})`);
|
|
51
52
|
// The holder died without releasing it.
|
|
52
53
|
rmSync(file, { force: true });
|
|
53
54
|
}
|
|
@@ -56,7 +57,7 @@ export function takeLock(file) {
|
|
|
56
57
|
finally {
|
|
57
58
|
rmSync(mine, { force: true });
|
|
58
59
|
}
|
|
59
|
-
throw new Error(`could not take the
|
|
60
|
+
throw new Error(`could not take the ${what} lock ${file}`);
|
|
60
61
|
}
|
|
61
62
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
62
63
|
export function pendingChangesets(dir) {
|
|
@@ -107,14 +108,6 @@ export async function readToken(exec) {
|
|
|
107
108
|
}
|
|
108
109
|
return token;
|
|
109
110
|
}
|
|
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
111
|
/** The hint for checking each package on npm by hand. */
|
|
119
112
|
const npmView = (packages) => packages.length
|
|
120
113
|
? `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 { githubRepo } from "./push.js";
|
|
7
|
+
import { takeLock } from "./release.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
|
+
}
|