@jonmilner/local-ci 0.0.0-stage → 0.1.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 +40 -2
- package/commitlint.config.js +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +122 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.js +40 -0
- package/dist/exec.d.ts +3 -0
- package/dist/exec.js +9 -0
- package/dist/github.d.ts +29 -0
- package/dist/github.js +89 -0
- package/dist/push.d.ts +17 -0
- package/dist/push.js +39 -0
- package/dist/title.d.ts +2 -0
- package/dist/title.js +12 -0
- package/package.json +32 -3
package/README.md
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jonmilner/local-ci
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Run a repo's check in a git `pre-push` hook, then post the result to GitHub as commit statuses, so required checks pass without GitHub Actions minutes.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add -D @jonmilner/local-ci
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Needs `gh` logged in with the `repo` scope (`gh auth login --scopes repo`, or `gh auth refresh --scopes repo`).
|
|
12
|
+
|
|
13
|
+
Add the hook, for example as `.githooks/pre-push` with `git config core.hooksPath .githooks`:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
#!/bin/sh
|
|
17
|
+
exec pnpm exec local-ci pre-push "$@"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Commands
|
|
21
|
+
|
|
22
|
+
- `local-ci pre-push`: the hook. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote, and lets the push go ahead.
|
|
23
|
+
- `local-ci post <sha>`: waits up to 3 minutes for GitHub to have the commit, posts status `check` = `success` (`<command> passed on <hostname>`), then posts `pr-title` on it for an open PR that contains it (waiting up to 30s for GitHub to link them). A PR opened after the push, or a retitled one, needs `local-ci pr-title` run by hand.
|
|
24
|
+
- `local-ci pr-title [<pr>]`: lints the PR title (the current branch's PR by default) with `@jonmilner/commitlint-config` and posts status `pr-title` on its head commit, `failure` with the first error when the title does not pass.
|
|
25
|
+
- `local-ci status [<sha>]`: posts `check` for a commit (default `HEAD`) that already passed, or runs the check first. Use it after a push that skipped hooks (`--no-verify`) or a lost background poster.
|
|
26
|
+
|
|
27
|
+
## Check command
|
|
28
|
+
|
|
29
|
+
`pnpm check` by default. Override it per repo in `package.json`:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{ "local-ci": { "command": "pnpm lint && pnpm test" } }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example in the hook: `LOCAL_CI_COMMAND=./scripts/check.sh exec local-ci pre-push "$@"`.
|
|
36
|
+
|
|
37
|
+
## State
|
|
38
|
+
|
|
39
|
+
Logs and the record of passed commits live in `~/.local/state/local-ci/<repo>.log` and `<repo>.passed` (`$XDG_STATE_HOME` if set).
|
|
40
|
+
|
|
41
|
+
Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import { closeSync, openSync, readFileSync } from "node:fs";
|
|
4
|
+
import { hostname } from "node:os";
|
|
5
|
+
import { basename, dirname, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { passedCommand, recordPass, resolveCommand, statePaths } from "./config.js";
|
|
8
|
+
import { exec } from "./exec.js";
|
|
9
|
+
import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
|
|
10
|
+
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
11
|
+
import { lintTitle } from "./title.js";
|
|
12
|
+
const USAGE = `Usage:
|
|
13
|
+
local-ci pre-push run the check for each pushed commit (git pre-push hook)
|
|
14
|
+
local-ci post <sha> wait for <sha> on GitHub, then post its check and pr-title statuses
|
|
15
|
+
local-ci pr-title [<pr>] lint the PR title and post the pr-title status
|
|
16
|
+
local-ci status [<sha>] post check for <sha> (default HEAD), running the check if needed`;
|
|
17
|
+
const git = async (...args) => (await exec("git", args)).trim();
|
|
18
|
+
async function repo() {
|
|
19
|
+
const root = await git("rev-parse", "--show-toplevel");
|
|
20
|
+
// The common dir names the repo even from a linked worktree.
|
|
21
|
+
const common = resolve(root, await git("rev-parse", "--git-common-dir"));
|
|
22
|
+
const name = basename(common) === ".git" ? basename(dirname(common)) : basename(common, ".git");
|
|
23
|
+
return { root, ...statePaths(name) };
|
|
24
|
+
}
|
|
25
|
+
const out = (s) => process.stdout.write(`${s}\n`);
|
|
26
|
+
const err = (s) => process.stderr.write(`${s}\n`);
|
|
27
|
+
const log = (message) => out(`[${new Date().toISOString()}] ${message}`);
|
|
28
|
+
/** Runs the check at `sha` in place, after the clean-tree rule, unless it already passed. */
|
|
29
|
+
async function runCheck({ root, passed }, sha) {
|
|
30
|
+
if (passedCommand(passed, sha))
|
|
31
|
+
return;
|
|
32
|
+
const reason = cannotCheckInPlace(sha, await git("rev-parse", "HEAD"), await git("status", "--porcelain"));
|
|
33
|
+
if (reason)
|
|
34
|
+
throw new Error(`cannot check ${sha.slice(0, 7)}: ${reason}`);
|
|
35
|
+
const command = resolveCommand(root);
|
|
36
|
+
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
37
|
+
const result = spawnSync("sh", ["-c", command], {
|
|
38
|
+
cwd: root,
|
|
39
|
+
stdio: ["ignore", "inherit", "inherit"],
|
|
40
|
+
});
|
|
41
|
+
if (result.status !== 0)
|
|
42
|
+
throw new Error(`${command} failed on ${sha.slice(0, 7)}`);
|
|
43
|
+
recordPass(passed, sha, command);
|
|
44
|
+
}
|
|
45
|
+
async function prePush(remoteUrl) {
|
|
46
|
+
await assertGhAuth(exec);
|
|
47
|
+
const current = await repo();
|
|
48
|
+
const shas = shasToCheck(parsePrePush(readFileSync(0, "utf8")));
|
|
49
|
+
// Check every commit before posting any, so a refused push posts nothing.
|
|
50
|
+
for (const sha of shas)
|
|
51
|
+
await runCheck(current, sha);
|
|
52
|
+
// Post to the remote being pushed, not whichever one gh would pick.
|
|
53
|
+
const ghRepo = remoteUrl ? githubRepo(remoteUrl) : undefined;
|
|
54
|
+
const env = ghRepo ? { ...process.env, GH_REPO: ghRepo } : process.env;
|
|
55
|
+
const fd = openSync(current.log, "a");
|
|
56
|
+
for (const sha of shas) {
|
|
57
|
+
spawn(process.execPath, [fileURLToPath(import.meta.url), "post", sha], {
|
|
58
|
+
cwd: current.root,
|
|
59
|
+
detached: true,
|
|
60
|
+
env,
|
|
61
|
+
stdio: ["ignore", fd, fd],
|
|
62
|
+
}).unref();
|
|
63
|
+
}
|
|
64
|
+
closeSync(fd);
|
|
65
|
+
if (shas.length)
|
|
66
|
+
err(`local-ci: passed; posting statuses in the background (${current.log})`);
|
|
67
|
+
}
|
|
68
|
+
async function post({ passed }, sha) {
|
|
69
|
+
const command = passedCommand(passed, sha);
|
|
70
|
+
if (!command) {
|
|
71
|
+
throw new Error(`${sha.slice(0, 7)} has no passing check; run local-ci status ${sha}`);
|
|
72
|
+
}
|
|
73
|
+
log(`${sha.slice(0, 7)}: waiting for GitHub`);
|
|
74
|
+
await waitForCommit(exec, sha);
|
|
75
|
+
await postStatus(exec, sha, checkStatus(command, hostname()));
|
|
76
|
+
log(`${sha.slice(0, 7)}: posted check`);
|
|
77
|
+
const pr = await waitForOpenPr(exec, sha);
|
|
78
|
+
if (pr)
|
|
79
|
+
await prTitle(String(pr), sha);
|
|
80
|
+
}
|
|
81
|
+
/** Posts `pr-title` on `sha`, or on the PR's head commit when `sha` is omitted. */
|
|
82
|
+
async function prTitle(pr, sha) {
|
|
83
|
+
const { number, title, headRefOid } = await viewPr(exec, pr);
|
|
84
|
+
const target = sha ?? headRefOid;
|
|
85
|
+
const errors = await lintTitle(title);
|
|
86
|
+
const status = prTitleStatus(errors);
|
|
87
|
+
await postStatus(exec, target, status);
|
|
88
|
+
log(`#${number} ${target.slice(0, 7)}: posted pr-title ${status.state}: ${status.description}`);
|
|
89
|
+
return errors.length === 0;
|
|
90
|
+
}
|
|
91
|
+
async function status(ref = "HEAD") {
|
|
92
|
+
const sha = await git("rev-parse", "--verify", `${ref}^{commit}`);
|
|
93
|
+
const current = await repo();
|
|
94
|
+
await runCheck(current, sha);
|
|
95
|
+
await post(current, sha);
|
|
96
|
+
}
|
|
97
|
+
async function main([command, ...args]) {
|
|
98
|
+
if (command === "pre-push")
|
|
99
|
+
return prePush(args[1]);
|
|
100
|
+
if (command === "post" || command === "pr-title" || command === "status") {
|
|
101
|
+
await assertGhAuth(exec);
|
|
102
|
+
}
|
|
103
|
+
switch (command) {
|
|
104
|
+
case "post":
|
|
105
|
+
if (!args[0])
|
|
106
|
+
throw new Error("post needs a commit SHA");
|
|
107
|
+
return post(await repo(), args[0]);
|
|
108
|
+
case "pr-title":
|
|
109
|
+
if (!(await prTitle(args[0])))
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
return;
|
|
112
|
+
case "status":
|
|
113
|
+
return status(args[0]);
|
|
114
|
+
default:
|
|
115
|
+
err(USAGE);
|
|
116
|
+
process.exitCode = command && command !== "--help" ? 1 : 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
main(process.argv.slice(2)).catch((error) => {
|
|
120
|
+
err(`local-ci: ${error instanceof Error ? error.message : String(error)}`);
|
|
121
|
+
process.exitCode = 1;
|
|
122
|
+
});
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const DEFAULT_COMMAND = "pnpm check";
|
|
2
|
+
/** The check command: `local-ci.command` in package.json, else `LOCAL_CI_COMMAND`, else `pnpm check`. */
|
|
3
|
+
export declare function resolveCommand(root: string, env?: NodeJS.ProcessEnv): string;
|
|
4
|
+
export declare function stateDir(env?: NodeJS.ProcessEnv): string;
|
|
5
|
+
/** Where a repo's log and record of passed commits live, created on first use. */
|
|
6
|
+
export declare function statePaths(repo: string, env?: NodeJS.ProcessEnv): {
|
|
7
|
+
log: string;
|
|
8
|
+
passed: string;
|
|
9
|
+
};
|
|
10
|
+
/** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
|
|
11
|
+
export declare function recordPass(file: string, sha: string, command: string): void;
|
|
12
|
+
/** The command that last passed at `sha`, or undefined if it was never checked. */
|
|
13
|
+
export declare function passedCommand(file: string, sha: string): string | undefined;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
export const DEFAULT_COMMAND = "pnpm check";
|
|
5
|
+
/** The check command: `local-ci.command` in package.json, else `LOCAL_CI_COMMAND`, else `pnpm check`. */
|
|
6
|
+
export function resolveCommand(root, env = process.env) {
|
|
7
|
+
const manifest = join(root, "package.json");
|
|
8
|
+
if (existsSync(manifest)) {
|
|
9
|
+
const pkg = JSON.parse(readFileSync(manifest, "utf8"));
|
|
10
|
+
const command = pkg["local-ci"]?.command;
|
|
11
|
+
if (typeof command === "string" && command.trim())
|
|
12
|
+
return command;
|
|
13
|
+
}
|
|
14
|
+
return env.LOCAL_CI_COMMAND?.trim() || DEFAULT_COMMAND;
|
|
15
|
+
}
|
|
16
|
+
export function stateDir(env = process.env) {
|
|
17
|
+
return join(env.XDG_STATE_HOME || join(homedir(), ".local", "state"), "local-ci");
|
|
18
|
+
}
|
|
19
|
+
/** Where a repo's log and record of passed commits live, created on first use. */
|
|
20
|
+
export function statePaths(repo, env = process.env) {
|
|
21
|
+
const dir = stateDir(env);
|
|
22
|
+
mkdirSync(dir, { recursive: true });
|
|
23
|
+
return { log: join(dir, `${repo}.log`), passed: join(dir, `${repo}.passed`) };
|
|
24
|
+
}
|
|
25
|
+
/** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
|
|
26
|
+
export function recordPass(file, sha, command) {
|
|
27
|
+
appendFileSync(file, `${sha}\t${command}\n`);
|
|
28
|
+
}
|
|
29
|
+
/** The command that last passed at `sha`, or undefined if it was never checked. */
|
|
30
|
+
export function passedCommand(file, sha) {
|
|
31
|
+
if (!existsSync(file))
|
|
32
|
+
return undefined;
|
|
33
|
+
let command;
|
|
34
|
+
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
35
|
+
const tab = line.indexOf("\t");
|
|
36
|
+
if (tab > 0 && line.slice(0, tab) === sha)
|
|
37
|
+
command = line.slice(tab + 1);
|
|
38
|
+
}
|
|
39
|
+
return command;
|
|
40
|
+
}
|
package/dist/exec.d.ts
ADDED
package/dist/exec.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
export const exec = (file, args) => new Promise((resolve, reject) => {
|
|
3
|
+
execFile(file, args, { maxBuffer: 16 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
4
|
+
if (error)
|
|
5
|
+
reject(new Error(`${file} ${args[0] ?? ""}: ${stderr.trim() || error.message}`));
|
|
6
|
+
else
|
|
7
|
+
resolve(stdout);
|
|
8
|
+
});
|
|
9
|
+
});
|
package/dist/github.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Exec } from "./exec.ts";
|
|
2
|
+
export interface StatusPayload {
|
|
3
|
+
state: "success" | "failure";
|
|
4
|
+
context: string;
|
|
5
|
+
description: string;
|
|
6
|
+
}
|
|
7
|
+
/** Fails with the command that fixes it unless `gh` is logged in with `repo` scope. */
|
|
8
|
+
export declare function assertGhAuth(exec: Exec): Promise<void>;
|
|
9
|
+
export declare function checkStatus(command: string, host: string): StatusPayload;
|
|
10
|
+
export declare function prTitleStatus(errors: string[]): StatusPayload;
|
|
11
|
+
export declare function postStatus(exec: Exec, sha: string, status: StatusPayload): Promise<void>;
|
|
12
|
+
export interface WaitOptions {
|
|
13
|
+
intervalMs?: number;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
sleep?: (ms: number) => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
/** Polls until GitHub knows `sha`, since a pre-push hook runs before the push lands. */
|
|
18
|
+
export declare function waitForCommit(exec: Exec, sha: string, options?: WaitOptions): Promise<void>;
|
|
19
|
+
/** An open PR that contains `sha`, if any. */
|
|
20
|
+
export declare function openPrForSha(exec: Exec, sha: string): Promise<number | undefined>;
|
|
21
|
+
/** Retries `openPrForSha`, since GitHub links a pushed commit to its PR a little after the push. */
|
|
22
|
+
export declare function waitForOpenPr(exec: Exec, sha: string, options?: WaitOptions): Promise<number | undefined>;
|
|
23
|
+
export interface PullRequest {
|
|
24
|
+
number: number;
|
|
25
|
+
title: string;
|
|
26
|
+
headRefOid: string;
|
|
27
|
+
}
|
|
28
|
+
/** The given PR, or the current branch's PR when `pr` is omitted. */
|
|
29
|
+
export declare function viewPr(exec: Exec, pr?: string): Promise<PullRequest>;
|
package/dist/github.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// `gh api` fills these placeholders from the current directory's git remote.
|
|
2
|
+
const REPO = "repos/{owner}/{repo}";
|
|
3
|
+
const DESCRIPTION_LIMIT = 140;
|
|
4
|
+
/** Fails with the command that fixes it unless `gh` is logged in with `repo` scope. */
|
|
5
|
+
export async function assertGhAuth(exec) {
|
|
6
|
+
let response;
|
|
7
|
+
try {
|
|
8
|
+
response = await exec("gh", ["api", "--include", "user"]);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
throw new Error("gh is not authenticated. Run: gh auth login --scopes repo");
|
|
12
|
+
}
|
|
13
|
+
// Classic and OAuth tokens list their scopes; fine-grained tokens send no header.
|
|
14
|
+
const header = /^x-oauth-scopes:(.*)$/im.exec(response);
|
|
15
|
+
const scopes = header?.[1]?.split(",").map((scope) => scope.trim()) ?? [];
|
|
16
|
+
if (header && !scopes.includes("repo")) {
|
|
17
|
+
throw new Error("gh lacks the repo scope. Run: gh auth refresh --scopes repo");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function truncate(text) {
|
|
21
|
+
return text.length <= DESCRIPTION_LIMIT ? text : `${text.slice(0, DESCRIPTION_LIMIT - 1)}…`;
|
|
22
|
+
}
|
|
23
|
+
export function checkStatus(command, host) {
|
|
24
|
+
return {
|
|
25
|
+
state: "success",
|
|
26
|
+
context: "check",
|
|
27
|
+
description: truncate(`${command} passed on ${host}`),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function prTitleStatus(errors) {
|
|
31
|
+
return errors[0]
|
|
32
|
+
? { state: "failure", context: "pr-title", description: truncate(errors[0]) }
|
|
33
|
+
: { state: "success", context: "pr-title", description: "Conventional Commit title" };
|
|
34
|
+
}
|
|
35
|
+
export async function postStatus(exec, sha, status) {
|
|
36
|
+
await exec("gh", [
|
|
37
|
+
"api",
|
|
38
|
+
"--method",
|
|
39
|
+
"POST",
|
|
40
|
+
`${REPO}/statuses/${sha}`,
|
|
41
|
+
"-f",
|
|
42
|
+
`state=${status.state}`,
|
|
43
|
+
"-f",
|
|
44
|
+
`context=${status.context}`,
|
|
45
|
+
"-f",
|
|
46
|
+
`description=${status.description}`,
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
const defaultSleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
50
|
+
/** Polls until GitHub knows `sha`, since a pre-push hook runs before the push lands. */
|
|
51
|
+
export async function waitForCommit(exec, sha, options = {}) {
|
|
52
|
+
const { intervalMs = 5_000, timeoutMs = 180_000 } = options;
|
|
53
|
+
const { sleep = defaultSleep } = options;
|
|
54
|
+
for (let waited = 0;; waited += intervalMs) {
|
|
55
|
+
try {
|
|
56
|
+
await exec("gh", ["api", `${REPO}/commits/${sha}`, "--jq", ".sha"]);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (waited + intervalMs > timeoutMs) {
|
|
61
|
+
throw new Error(`${sha.slice(0, 7)} did not reach GitHub within ${timeoutMs / 1000}s`, {
|
|
62
|
+
cause: error,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
await sleep(intervalMs);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** An open PR that contains `sha`, if any. */
|
|
70
|
+
export async function openPrForSha(exec, sha) {
|
|
71
|
+
const pulls = JSON.parse(await exec("gh", ["api", `${REPO}/commits/${sha}/pulls`]));
|
|
72
|
+
return pulls.find((pull) => pull.state === "open")?.number;
|
|
73
|
+
}
|
|
74
|
+
/** Retries `openPrForSha`, since GitHub links a pushed commit to its PR a little after the push. */
|
|
75
|
+
export async function waitForOpenPr(exec, sha, options = {}) {
|
|
76
|
+
const { intervalMs = 5_000, timeoutMs = 30_000 } = options;
|
|
77
|
+
const { sleep = defaultSleep } = options;
|
|
78
|
+
for (let waited = 0;; waited += intervalMs) {
|
|
79
|
+
const pr = await openPrForSha(exec, sha);
|
|
80
|
+
if (pr || waited + intervalMs > timeoutMs)
|
|
81
|
+
return pr;
|
|
82
|
+
await sleep(intervalMs);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** The given PR, or the current branch's PR when `pr` is omitted. */
|
|
86
|
+
export async function viewPr(exec, pr) {
|
|
87
|
+
const args = ["pr", "view", ...(pr ? [pr] : []), "--json", "number,title,headRefOid"];
|
|
88
|
+
return JSON.parse(await exec("gh", args));
|
|
89
|
+
}
|
package/dist/push.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface PushedRef {
|
|
2
|
+
localRef: string;
|
|
3
|
+
localSha: string;
|
|
4
|
+
remoteRef: string;
|
|
5
|
+
remoteSha: string;
|
|
6
|
+
}
|
|
7
|
+
/** Parses the `<local ref> <local sha> <remote ref> <remote sha>` lines git feeds a pre-push hook. */
|
|
8
|
+
export declare function parsePrePush(input: string): PushedRef[];
|
|
9
|
+
/** The distinct commits pushed to branches. Deletes and tags are skipped. */
|
|
10
|
+
export declare function shasToCheck(refs: PushedRef[]): string[];
|
|
11
|
+
/**
|
|
12
|
+
* Why `sha` can't be checked in place, or null when it can: it must be HEAD, with a clean
|
|
13
|
+
* working tree, so the result reflects the commit rather than uncommitted files.
|
|
14
|
+
*/
|
|
15
|
+
export declare function cannotCheckInPlace(sha: string, head: string, porcelain: string): string | null;
|
|
16
|
+
/** `owner/repo` from a GitHub remote URL (SSH or HTTPS), or undefined for another host. */
|
|
17
|
+
export declare function githubRepo(url: string): string | undefined;
|
package/dist/push.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const ZERO_SHA = /^0+$/;
|
|
2
|
+
/** Parses the `<local ref> <local sha> <remote ref> <remote sha>` lines git feeds a pre-push hook. */
|
|
3
|
+
export function parsePrePush(input) {
|
|
4
|
+
return input
|
|
5
|
+
.split("\n")
|
|
6
|
+
.map((line) => line.trim())
|
|
7
|
+
.filter(Boolean)
|
|
8
|
+
.map((line) => {
|
|
9
|
+
const [localRef, localSha, remoteRef, remoteSha, ...rest] = line.split(/\s+/);
|
|
10
|
+
if (!localRef || !localSha || !remoteRef || !remoteSha || rest.length > 0) {
|
|
11
|
+
throw new Error(`unexpected pre-push line: ${line}`);
|
|
12
|
+
}
|
|
13
|
+
return { localRef, localSha, remoteRef, remoteSha };
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/** The distinct commits pushed to branches. Deletes and tags are skipped. */
|
|
17
|
+
export function shasToCheck(refs) {
|
|
18
|
+
const shas = refs
|
|
19
|
+
.filter((ref) => !ZERO_SHA.test(ref.localSha) && ref.remoteRef.startsWith("refs/heads/"))
|
|
20
|
+
.map((ref) => ref.localSha);
|
|
21
|
+
return [...new Set(shas)];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Why `sha` can't be checked in place, or null when it can: it must be HEAD, with a clean
|
|
25
|
+
* working tree, so the result reflects the commit rather than uncommitted files.
|
|
26
|
+
*/
|
|
27
|
+
export function cannotCheckInPlace(sha, head, porcelain) {
|
|
28
|
+
if (head !== sha) {
|
|
29
|
+
return `${sha.slice(0, 7)} is not HEAD (${head.slice(0, 7)}); check it out and push again`;
|
|
30
|
+
}
|
|
31
|
+
if (porcelain.trim()) {
|
|
32
|
+
return `the working tree has uncommitted or untracked files; commit or stash them so the check runs on ${sha.slice(0, 7)}`;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** `owner/repo` from a GitHub remote URL (SSH or HTTPS), or undefined for another host. */
|
|
37
|
+
export function githubRepo(url) {
|
|
38
|
+
return /github\.com[:/]([^/]+\/[^/]+?)(?:\.git)?\/?$/.exec(url)?.[1];
|
|
39
|
+
}
|
package/dist/title.d.ts
ADDED
package/dist/title.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import lint from "@commitlint/lint";
|
|
3
|
+
import load from "@commitlint/load";
|
|
4
|
+
/** Lints a PR title with @jonmilner/commitlint-config and returns its error messages. */
|
|
5
|
+
export async function lintTitle(title) {
|
|
6
|
+
// Load this package's own config file, so it resolves the same in any repo being checked.
|
|
7
|
+
const cwd = fileURLToPath(new URL("..", import.meta.url));
|
|
8
|
+
const config = await load({}, { cwd, file: "commitlint.config.js" });
|
|
9
|
+
const parserOpts = config.parserPreset?.parserOpts;
|
|
10
|
+
const result = await lint(title, config.rules, parserOpts ? { parserOpts } : {});
|
|
11
|
+
return result.errors.map((error) => `${error.message} [${error.name}]`);
|
|
12
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jonmilner/local-ci",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Run a repo's check in a pre-push hook and post the result as GitHub commit statuses",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/jonmilner/config.git",
|
|
9
|
+
"directory": "packages/local-ci"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"local-ci": "./dist/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"commitlint.config.js",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@commitlint/config-conventional": "^20.0.0",
|
|
25
|
+
"@commitlint/lint": "^21.2.4",
|
|
26
|
+
"@commitlint/load": "^21.2.4",
|
|
27
|
+
"@jonmilner/commitlint-config": "^0.1.1"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc -p tsconfig.json"
|
|
34
|
+
}
|
|
6
35
|
}
|