@osdlc/repo-cleaner 0.20.5 → 0.22.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/CHANGELOG.md +64 -0
- package/README.md +545 -165
- package/dist/approval.d.ts +91 -0
- package/dist/approval.js +233 -0
- package/dist/archive.d.ts +87 -0
- package/dist/archive.js +252 -0
- package/dist/branches.d.ts +4 -4
- package/dist/branches.js +56 -43
- package/dist/clean.d.ts +30 -70
- package/dist/clean.js +251 -197
- package/dist/cli.d.ts +16 -10
- package/dist/cli.js +252 -103
- package/dist/discover.d.ts +28 -7
- package/dist/discover.js +185 -42
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -2
- package/dist/landing.d.ts +10 -5
- package/dist/landing.js +15 -16
- package/dist/plan.d.ts +18 -11
- package/dist/plan.js +33 -13
- package/dist/processes.d.ts +19 -9
- package/dist/processes.js +51 -25
- package/dist/providers/github.d.ts +21 -3
- package/dist/providers/github.js +149 -27
- package/dist/providers/gitlab.js +37 -17
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/types.d.ts +15 -2
- package/dist/prs.d.ts +19 -10
- package/dist/prs.js +21 -13
- package/dist/receipt.d.ts +68 -3
- package/dist/receipt.js +30 -3
- package/dist/reference.d.ts +1 -1
- package/dist/reference.js +93 -50
- package/dist/remote-mirror.d.ts +2 -1
- package/dist/remote-mirror.js +3 -14
- package/dist/render.d.ts +17 -2
- package/dist/render.js +80 -13
- package/dist/scope.d.ts +15 -0
- package/dist/scope.js +125 -0
- package/dist/size.d.ts +20 -0
- package/dist/size.js +34 -0
- package/dist/status-line.js +1 -0
- package/dist/step-board.js +6 -4
- package/dist/sweep.d.ts +45 -18
- package/dist/sweep.js +151 -127
- package/dist/types.d.ts +89 -10
- package/dist/types.js +50 -6
- package/dist/util.d.ts +2 -0
- package/dist/util.js +25 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/worktrees.js +87 -11
- package/package.json +6 -5
package/dist/plan.js
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
import { readFileSync } from 'node:fs';
|
|
7
7
|
import { z } from 'zod';
|
|
8
8
|
import { PROVIDER_CHOICES } from "./providers/types.js";
|
|
9
|
-
import { ACTIONS, KINDS, RISKS, RepoCleanerError, VERDICTS, parseRowId, } from "./types.js";
|
|
9
|
+
import { judgedRemotes, ACTIONS, KINDS, RISKS, RepoCleanerError, VERDICTS, parseRowId, } from "./types.js";
|
|
10
10
|
import { writeJsonAtomic } from "./write-json.js";
|
|
11
|
-
export const PLAN_VERSION =
|
|
11
|
+
export const PLAN_VERSION = 3;
|
|
12
12
|
/** A row id, `<kind>:<name>` with a name; shared by the plan and the receipt. */
|
|
13
13
|
export const RowIdSchema = z.custom((v) => typeof v === 'string' && (parseRowId(v)?.name ?? '') !== '', { message: 'expected <kind>:<name>' });
|
|
14
14
|
export const PlanRowSchema = z.object({
|
|
@@ -22,8 +22,10 @@ export const PlanRowSchema = z.object({
|
|
|
22
22
|
});
|
|
23
23
|
export const PlanRepoSchema = z.object({
|
|
24
24
|
root: z.string(),
|
|
25
|
-
/**
|
|
26
|
-
|
|
25
|
+
/** The remotes whose branches the sweep judged, by name; the re-check judges the same ones. */
|
|
26
|
+
remotes: z.array(z.string()),
|
|
27
|
+
/** The remotes the sweep fetched; the re-check fetches them too, so it judges against the same default branches. */
|
|
28
|
+
fetched: z.array(z.string()),
|
|
27
29
|
rows: z.array(PlanRowSchema),
|
|
28
30
|
});
|
|
29
31
|
export const PlanSchema = z.object({
|
|
@@ -32,7 +34,10 @@ export const PlanSchema = z.object({
|
|
|
32
34
|
/** What was swept; the re-check sweeps the same. `SweptWith` is this object's type. */
|
|
33
35
|
options: z.object({
|
|
34
36
|
only: z.array(z.enum(KINDS)),
|
|
35
|
-
|
|
37
|
+
/** The remotes asked for: a name, `''` for the default remote, `*` for every remote. */
|
|
38
|
+
remotes: z.array(z.string()),
|
|
39
|
+
/** Fetch before judging local rows. */
|
|
40
|
+
fetch: z.boolean(),
|
|
36
41
|
offline: z.boolean(),
|
|
37
42
|
against: z.string().nullable(),
|
|
38
43
|
provider: z.enum(PROVIDER_CHOICES),
|
|
@@ -41,11 +46,12 @@ export const PlanSchema = z.object({
|
|
|
41
46
|
repos: z.array(PlanRepoSchema),
|
|
42
47
|
});
|
|
43
48
|
/**
|
|
44
|
-
* The evidence a row's verdict rests on.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
+
* The evidence a row's verdict rests on. Every row: the default branch it was
|
|
50
|
+
* judged against. A branch: its sha and its PR's number and state. A worktree:
|
|
51
|
+
* HEAD, branch, how much unlanded work it holds, whether its lock was stale,
|
|
52
|
+
* whether it is on disk, each submodule's state, and the remote branches that
|
|
53
|
+
* hold HEAD, so a lock taken or revived after planning, a directory that came
|
|
54
|
+
* back, or a submodule that changed, drops the row.
|
|
49
55
|
*/
|
|
50
56
|
export function fingerprint(row) {
|
|
51
57
|
if (row.kind === 'worktree') {
|
|
@@ -60,10 +66,17 @@ export function fingerprint(row) {
|
|
|
60
66
|
// must not turn a prune into a remove.
|
|
61
67
|
onDisk: e.onDisk ?? null,
|
|
62
68
|
submodules: e.submodules ?? null,
|
|
69
|
+
recoverableFrom: e.recoverableFrom ?? null,
|
|
70
|
+
landedAgainst: e.landedAgainst ?? null,
|
|
63
71
|
};
|
|
64
72
|
}
|
|
65
|
-
const
|
|
66
|
-
return {
|
|
73
|
+
const e = row.evidence;
|
|
74
|
+
return {
|
|
75
|
+
sha: e.sha,
|
|
76
|
+
prNumber: e.pr?.number ?? null,
|
|
77
|
+
prState: e.pr?.state ?? null,
|
|
78
|
+
landedAgainst: e.landedAgainst ?? null,
|
|
79
|
+
};
|
|
67
80
|
}
|
|
68
81
|
/** Equal fingerprints, treating a missing key as null. */
|
|
69
82
|
export function sameFingerprint(a, b) {
|
|
@@ -100,7 +113,14 @@ export function plan(sweep, now = new Date()) {
|
|
|
100
113
|
options: { ...sweep.options },
|
|
101
114
|
repos: sweep.repos.flatMap((s) => s.repo === null
|
|
102
115
|
? []
|
|
103
|
-
: [
|
|
116
|
+
: [
|
|
117
|
+
{
|
|
118
|
+
root: s.repo.root,
|
|
119
|
+
remotes: judgedRemotes(s.repo).map((r) => r.name),
|
|
120
|
+
fetched: s.repo.fetchedRemotes,
|
|
121
|
+
rows: planRows(s.rows),
|
|
122
|
+
},
|
|
123
|
+
]),
|
|
104
124
|
};
|
|
105
125
|
}
|
|
106
126
|
/** A UTC timestamp safe in a file name, e.g. `2026-10-04T12-30-05-123Z`. */
|
package/dist/processes.d.ts
CHANGED
|
@@ -28,9 +28,9 @@ export interface ProcessHolder {
|
|
|
28
28
|
export interface ProcessProbe {
|
|
29
29
|
/**
|
|
30
30
|
* Whether `pid` is running. With `startedAt`, the process counts only if it
|
|
31
|
-
*
|
|
32
|
-
* owner. `startedAt` is the text the lock gave; a probe
|
|
33
|
-
*
|
|
31
|
+
* is the one that started then: a pid reused by a process that started
|
|
32
|
+
* later is not the owner. `startedAt` is the text the lock gave; a probe
|
|
33
|
+
* that cannot read it must answer "alive" (the owner cannot be ruled out).
|
|
34
34
|
*/
|
|
35
35
|
alive(pid: number, startedAt?: string): boolean;
|
|
36
36
|
/**
|
|
@@ -68,12 +68,8 @@ export declare function isIdleHolder(h: ProcessHolder): boolean;
|
|
|
68
68
|
/** The holders that make a worktree live: everything but idle shells. */
|
|
69
69
|
export declare function busyHolders(holders: readonly ProcessHolder[]): ProcessHolder[];
|
|
70
70
|
/**
|
|
71
|
-
* Epoch milliseconds of
|
|
72
|
-
*
|
|
73
|
-
* seconds (9-10 digits), epoch milliseconds (12-13 digits), or the `lstart`
|
|
74
|
-
* form. A date alone, a short digit string or a partial date such as `Oct 25`
|
|
75
|
-
* is NaN, so the owner is treated as alive: the comparison would otherwise
|
|
76
|
-
* call a live owner dead.
|
|
71
|
+
* Epoch milliseconds of what `ps -o lstart` printed, read as UTC (the zone the
|
|
72
|
+
* default probe runs `ps` under, see `PS_ENV`), or NaN for any other text.
|
|
77
73
|
*/
|
|
78
74
|
export declare function startMs(text: string): number;
|
|
79
75
|
/**
|
|
@@ -89,6 +85,20 @@ export interface DefaultProbeOptions {
|
|
|
89
85
|
lsof?: CommandRunner | undefined;
|
|
90
86
|
/** Cap on each command, in ms. Defaults to 60 s. */
|
|
91
87
|
timeoutMs?: number | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Whether a pid exists, asked before `ps` so a dead pid costs no spawn:
|
|
90
|
+
* `false` is gone, `true` exists, `undefined` cannot tell. Defaults to a
|
|
91
|
+
* `process.kill(pid, 0)` check when `ps` is not injected, and to "cannot
|
|
92
|
+
* tell" when it is, since a fake `ps` has its own idea of the pids.
|
|
93
|
+
*/
|
|
94
|
+
pidExists?: ((pid: number) => boolean | undefined) | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* The platform whose process identities are read: on Linux a recorded
|
|
97
|
+
* boot-id identity is checked against /proc. Defaults to `process.platform`,
|
|
98
|
+
* or to a non-Linux one when `ps` is injected, since a fake `ps` has its own
|
|
99
|
+
* pids.
|
|
100
|
+
*/
|
|
101
|
+
platform?: NodeJS.Platform | undefined;
|
|
92
102
|
}
|
|
93
103
|
/**
|
|
94
104
|
* The default probe: `ps` for liveness, `lsof` for working directories, both
|
package/dist/processes.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { realpathSync } from 'node:fs';
|
|
15
15
|
import { spawnRunner } from '@osdlc/command-seam';
|
|
16
|
+
import { sameProcess } from '@osdlc/file-lock';
|
|
16
17
|
import { nodeGuard } from '@osdlc/path-guard/node';
|
|
17
18
|
/**
|
|
18
19
|
* The owner claimed by a lock reason, or null when it carries no `pid <N>`
|
|
@@ -52,30 +53,22 @@ export function isIdleHolder(h) {
|
|
|
52
53
|
export function busyHolders(holders) {
|
|
53
54
|
return holders.filter((h) => !isIdleHolder(h));
|
|
54
55
|
}
|
|
55
|
-
/** Allowed difference between a lock's start time and the process's, in ms. */
|
|
56
|
-
const START_TOLERANCE_MS = 2000;
|
|
57
|
-
// A zone is required: without one Date.parse reads local time, and a writer that
|
|
58
|
-
// emitted UTC with no `Z` would shift the start time by hours and make a live owner
|
|
59
|
-
// look like a reused pid.
|
|
60
|
-
const ISO_WITH_TIME = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
61
56
|
/** `ps -o lstart`: `Sat Oct 25 18:22:19 2026`, the day space-padded on Linux. */
|
|
62
|
-
const PS_LSTART = /^[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s
|
|
57
|
+
const PS_LSTART = /^[A-Z][a-z]{2}\s+([A-Z][a-z]{2})\s+(\d{1,2})\s+(\d{2}):(\d{2}):(\d{2})\s+(\d{4})$/;
|
|
58
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
63
59
|
/**
|
|
64
|
-
* Epoch milliseconds of
|
|
65
|
-
*
|
|
66
|
-
* seconds (9-10 digits), epoch milliseconds (12-13 digits), or the `lstart`
|
|
67
|
-
* form. A date alone, a short digit string or a partial date such as `Oct 25`
|
|
68
|
-
* is NaN, so the owner is treated as alive: the comparison would otherwise
|
|
69
|
-
* call a live owner dead.
|
|
60
|
+
* Epoch milliseconds of what `ps -o lstart` printed, read as UTC (the zone the
|
|
61
|
+
* default probe runs `ps` under, see `PS_ENV`), or NaN for any other text.
|
|
70
62
|
*/
|
|
71
63
|
export function startMs(text) {
|
|
72
64
|
const trimmed = text.trim();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return Date.
|
|
65
|
+
const l = PS_LSTART.exec(trimmed);
|
|
66
|
+
if (l !== null) {
|
|
67
|
+
const month = MONTHS.indexOf(l[1] ?? '');
|
|
68
|
+
if (month < 0)
|
|
69
|
+
return Number.NaN;
|
|
70
|
+
return Date.UTC(Number(l[6]), month, Number(l[2]), Number(l[3]), Number(l[4]), Number(l[5]));
|
|
71
|
+
}
|
|
79
72
|
return Number.NaN;
|
|
80
73
|
}
|
|
81
74
|
/**
|
|
@@ -102,6 +95,28 @@ export function parseLsofCwd(output) {
|
|
|
102
95
|
}
|
|
103
96
|
return rows;
|
|
104
97
|
}
|
|
98
|
+
/** `process.kill(pid, 0)`: ESRCH is gone, EPERM is alive, anything else is unknown. */
|
|
99
|
+
function killProbe(pid) {
|
|
100
|
+
// Pid 0 and negative pids name process groups, not a process.
|
|
101
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
102
|
+
return undefined;
|
|
103
|
+
try {
|
|
104
|
+
process.kill(pid, 0);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
const code = e.code;
|
|
109
|
+
if (code === 'ESRCH')
|
|
110
|
+
return false;
|
|
111
|
+
return code === 'EPERM' ? true : undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* `ps` reads its own zone and locale. `TZ=UTC` makes the zoneless `lstart` one
|
|
116
|
+
* instant, so the repeated hour of a DST fall-back cannot read as two; `LC_ALL=C`
|
|
117
|
+
* keeps the month and day names English.
|
|
118
|
+
*/
|
|
119
|
+
const PS_ENV = { TZ: 'UTC', LC_ALL: 'C' };
|
|
105
120
|
/** True when the command ran and reported an exit status. */
|
|
106
121
|
const answered = (r) => r.neverExited !== true;
|
|
107
122
|
/**
|
|
@@ -118,11 +133,16 @@ export function defaultProcessProbe(opts = {}) {
|
|
|
118
133
|
const ps = opts.ps ?? spawnRunner('ps');
|
|
119
134
|
const lsof = opts.lsof ?? spawnRunner('lsof');
|
|
120
135
|
const timeoutMs = opts.timeoutMs ?? 60_000;
|
|
136
|
+
const pidExists = opts.pidExists ?? (opts.ps === undefined ? killProbe : () => undefined);
|
|
137
|
+
// A fake `ps` answers for its own pids, which /proc knows nothing of.
|
|
138
|
+
const platform = opts.platform ?? (opts.ps === undefined ? process.platform : 'darwin');
|
|
121
139
|
let table = null;
|
|
122
140
|
return {
|
|
123
141
|
alive(pid, startedAt) {
|
|
142
|
+
if (pidExists(pid) === false)
|
|
143
|
+
return false;
|
|
124
144
|
// `lstart` is the same five-field date on macOS and Linux procps.
|
|
125
|
-
const r = ps.run(['-o', 'lstart=', '-p', String(pid)], { cwd: '/', timeoutMs });
|
|
145
|
+
const r = ps.run(['-o', 'lstart=', '-p', String(pid)], { cwd: '/', env: PS_ENV, timeoutMs });
|
|
126
146
|
if (!answered(r))
|
|
127
147
|
return true;
|
|
128
148
|
const started = r.stdout.trim();
|
|
@@ -134,11 +154,17 @@ export function defaultProcessProbe(opts = {}) {
|
|
|
134
154
|
return true;
|
|
135
155
|
if (startedAt === undefined)
|
|
136
156
|
return true;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
157
|
+
// `@osdlc/file-lock` reads every form a lock records: its own process
|
|
158
|
+
// identity (Linux boot id and start ticks, checked against /proc), an
|
|
159
|
+
// instant (ISO time, epoch seconds or ms, `start:<ms>`), or `lstart`
|
|
160
|
+
// text in any zone. Only a process that started later is another one,
|
|
161
|
+
// so a writer that recorded when it took the lock keeps it.
|
|
162
|
+
const actual = startMs(started);
|
|
163
|
+
return sameProcess(pid, startedAt, {
|
|
164
|
+
alive: () => true,
|
|
165
|
+
startedAt: () => (Number.isNaN(actual) ? null : actual),
|
|
166
|
+
platform,
|
|
167
|
+
});
|
|
142
168
|
},
|
|
143
169
|
holders(path, holdersOpts) {
|
|
144
170
|
if (table === null || holdersOpts?.fresh === true) {
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
/** Pull requests from GitHub, through `gh`. */
|
|
2
2
|
import type { Gh } from '@osdlc/typed-gh';
|
|
3
3
|
import type { PrProvider } from './types.ts';
|
|
4
|
-
/** The most PRs one listing reads. */
|
|
4
|
+
/** The most PRs one listing of every state reads, newest first. */
|
|
5
5
|
export declare const PR_LIMIT = 300;
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* The most open PRs one listing reads. Open PRs are what gate a delete, and a
|
|
8
|
+
* repository has few of them; a listing that reaches this is incomplete.
|
|
9
|
+
*/
|
|
10
|
+
export declare const OPEN_PR_LIMIT = 1000;
|
|
11
|
+
/**
|
|
12
|
+
* A provider over `gh pr list`, aimed at the remote the sweep judges
|
|
13
|
+
* (`--repo [host/]owner/name`), not the repository `gh` would pick from the
|
|
14
|
+
* checkout's remotes (`upstream` over `origin`).
|
|
15
|
+
*
|
|
16
|
+
* It reads every open PR (up to {@link OPEN_PR_LIMIT}) and the newest
|
|
17
|
+
* {@link PR_LIMIT} of any state, so an old open PR is not lost to newer merged
|
|
18
|
+
* ones. The listing is incomplete, and says so, when the open PRs hit their
|
|
19
|
+
* cap, when one branch has more than {@link PR_LIMIT} PRs, and when the
|
|
20
|
+
* repository's fork status cannot be read.
|
|
21
|
+
*
|
|
22
|
+
* PRs opened from a fork live in the parent they target. For a fork, the
|
|
23
|
+
* parent's PRs whose head repository is the fork are listed too, under the
|
|
24
|
+
* same caps, and count for the fork's branches; a parent listing that fails
|
|
25
|
+
* or is capped leaves the listing incomplete, as does a fork whose parent
|
|
26
|
+
* cannot be named. A listing `gh` fails, such as one for
|
|
9
27
|
* a repository the signed-in account cannot see, throws with `gh`'s reason.
|
|
10
28
|
*/
|
|
11
29
|
export declare function githubProvider(gh: Gh): PrProvider;
|
package/dist/providers/github.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Pull requests from GitHub, through `gh`. */
|
|
2
|
+
import { errorText } from "../util.js";
|
|
2
3
|
/** The fields classification consults. Asking for more costs latency. */
|
|
3
4
|
const FIELDS = [
|
|
4
5
|
'number',
|
|
@@ -8,17 +9,50 @@ const FIELDS = [
|
|
|
8
9
|
'headRefOid',
|
|
9
10
|
'baseRefName',
|
|
10
11
|
'isCrossRepository',
|
|
12
|
+
'headRepositoryOwner',
|
|
11
13
|
'title',
|
|
12
14
|
'url',
|
|
13
15
|
];
|
|
14
|
-
/** The most PRs one listing reads. */
|
|
16
|
+
/** The most PRs one listing of every state reads, newest first. */
|
|
15
17
|
export const PR_LIMIT = 300;
|
|
16
18
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
+
* The most open PRs one listing reads. Open PRs are what gate a delete, and a
|
|
20
|
+
* repository has few of them; a listing that reaches this is incomplete.
|
|
21
|
+
*/
|
|
22
|
+
export const OPEN_PR_LIMIT = 1000;
|
|
23
|
+
/**
|
|
24
|
+
* A provider over `gh pr list`, aimed at the remote the sweep judges
|
|
25
|
+
* (`--repo [host/]owner/name`), not the repository `gh` would pick from the
|
|
26
|
+
* checkout's remotes (`upstream` over `origin`).
|
|
27
|
+
*
|
|
28
|
+
* It reads every open PR (up to {@link OPEN_PR_LIMIT}) and the newest
|
|
29
|
+
* {@link PR_LIMIT} of any state, so an old open PR is not lost to newer merged
|
|
30
|
+
* ones. The listing is incomplete, and says so, when the open PRs hit their
|
|
31
|
+
* cap, when one branch has more than {@link PR_LIMIT} PRs, and when the
|
|
32
|
+
* repository's fork status cannot be read.
|
|
33
|
+
*
|
|
34
|
+
* PRs opened from a fork live in the parent they target. For a fork, the
|
|
35
|
+
* parent's PRs whose head repository is the fork are listed too, under the
|
|
36
|
+
* same caps, and count for the fork's branches; a parent listing that fails
|
|
37
|
+
* or is capped leaves the listing incomplete, as does a fork whose parent
|
|
38
|
+
* cannot be named. A listing `gh` fails, such as one for
|
|
19
39
|
* a repository the signed-in account cannot see, throws with `gh`'s reason.
|
|
20
40
|
*/
|
|
21
41
|
export function githubProvider(gh) {
|
|
42
|
+
// Fork status per repository: it does not change between one listing and the next.
|
|
43
|
+
const forks = new Map();
|
|
44
|
+
// Each repository's listing, per branch: a parent judged as a remote of its
|
|
45
|
+
// own is listed once, for itself and for its forks.
|
|
46
|
+
const listings = new Map();
|
|
47
|
+
// Whether gh is signed in to a host, per host. A remote's host can be an SSH
|
|
48
|
+
// alias (`git@github-work:acme/app.git`) that gh maps to the real host itself.
|
|
49
|
+
const known = new Map();
|
|
50
|
+
const listOnce = (client, key, branch) => {
|
|
51
|
+
const at = `${key}\0${branch ?? ''}`;
|
|
52
|
+
if (!listings.has(at))
|
|
53
|
+
listings.set(at, listing(client, branch));
|
|
54
|
+
return listings.get(at);
|
|
55
|
+
};
|
|
22
56
|
return {
|
|
23
57
|
name: 'github',
|
|
24
58
|
words: { tool: 'gh', noun: 'PR', listing: 'gh pr list' },
|
|
@@ -26,32 +60,120 @@ export function githubProvider(gh) {
|
|
|
26
60
|
const available = gh.at(root).installed();
|
|
27
61
|
return { available, authenticated: available && gh.at(root).authenticated() };
|
|
28
62
|
},
|
|
29
|
-
listPrs(root,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
63
|
+
listPrs(root, target, branch) {
|
|
64
|
+
let client = gh.at(root);
|
|
65
|
+
if (target.project !== null) {
|
|
66
|
+
client = client.withRepo(target.project);
|
|
67
|
+
// Name the host only when gh holds credentials for it; otherwise gh's own host rules apply.
|
|
68
|
+
if (target.host !== null) {
|
|
69
|
+
const named = client.withHostname(target.host);
|
|
70
|
+
let signedIn = known.get(target.host);
|
|
71
|
+
if (signedIn === undefined) {
|
|
72
|
+
signedIn = named.authenticated();
|
|
73
|
+
known.set(target.host, signedIn);
|
|
74
|
+
}
|
|
75
|
+
if (signedIn)
|
|
76
|
+
client = named;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const host = target.host ?? '';
|
|
80
|
+
const own = listOnce(client, `${host}/${target.project ?? root}`, branch);
|
|
81
|
+
if (own === null)
|
|
37
82
|
return null;
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
83
|
+
const incomplete = capReasons(own, branch, null);
|
|
84
|
+
// A fork's head branch is not this repository's branch, whatever its name.
|
|
85
|
+
const rows = own.rows.filter((r) => !r.isCrossRepository);
|
|
86
|
+
if (target.project !== null) {
|
|
87
|
+
const key = `${host}/${target.project}`;
|
|
88
|
+
let fork = forks.get(key);
|
|
89
|
+
if (fork === undefined) {
|
|
90
|
+
fork = forkOf(client);
|
|
91
|
+
forks.set(key, fork);
|
|
92
|
+
}
|
|
93
|
+
if (fork.kind === 'unknown') {
|
|
94
|
+
incomplete.push('whether the repository is a fork could not be read');
|
|
95
|
+
}
|
|
96
|
+
else if (fork.kind === 'fork' && fork.parent === null) {
|
|
97
|
+
incomplete.push('the repository is a fork of its parent, and PRs from it to its parent are not listed');
|
|
98
|
+
}
|
|
99
|
+
else if (fork.kind === 'fork' && fork.parent !== null) {
|
|
100
|
+
// A PR from a fork lives in the parent it targets: read the parent's
|
|
101
|
+
// PRs whose head repository is this fork.
|
|
102
|
+
const parent = fork.parent;
|
|
103
|
+
const owner = target.project.split('/')[0].toLowerCase();
|
|
104
|
+
let theirs = null;
|
|
105
|
+
let why = 'the listing gave no reason';
|
|
106
|
+
try {
|
|
107
|
+
theirs = listOnce(client.withRepo(parent), `${host}/${parent}`, branch);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
why = errorText(err);
|
|
111
|
+
}
|
|
112
|
+
if (theirs === null) {
|
|
113
|
+
incomplete.push(`the repository is a fork of ${parent}, whose PRs could not be listed (${why})`);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
incomplete.push(...capReasons(theirs, branch, parent));
|
|
117
|
+
rows.push(...theirs.rows.filter((r) => r.isCrossRepository && r.headRepositoryOwner?.toLowerCase() === owner));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
53
120
|
}
|
|
54
|
-
|
|
121
|
+
const prs = rows
|
|
122
|
+
.filter((row) => row.headRefName !== '')
|
|
123
|
+
.map((row) => ({
|
|
124
|
+
number: row.number,
|
|
125
|
+
state: row.state,
|
|
126
|
+
mergedAt: row.mergedAt,
|
|
127
|
+
headRef: row.headRefName,
|
|
128
|
+
headOid: row.headRefOid,
|
|
129
|
+
baseRef: row.baseRefName,
|
|
130
|
+
title: row.title,
|
|
131
|
+
url: row.url,
|
|
132
|
+
}));
|
|
133
|
+
return incomplete.length === 0 ? prs : { prs, incomplete: incomplete.join('; ') };
|
|
55
134
|
},
|
|
56
135
|
};
|
|
57
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Every open PR and the newest {@link PR_LIMIT} of any state, or a branch's
|
|
139
|
+
* PRs only. A branch narrows the listing to its own PRs, so its one listing
|
|
140
|
+
* is whole unless that branch alone has more PRs than the cap.
|
|
141
|
+
*/
|
|
142
|
+
function listing(client, branch) {
|
|
143
|
+
const rows = [];
|
|
144
|
+
let openCapped = false;
|
|
145
|
+
if (branch === undefined) {
|
|
146
|
+
const open = client.prList({ state: 'open', limit: OPEN_PR_LIMIT, fields: FIELDS });
|
|
147
|
+
if (open === null)
|
|
148
|
+
return null;
|
|
149
|
+
rows.push(...open.rows);
|
|
150
|
+
openCapped = open.truncated;
|
|
151
|
+
}
|
|
152
|
+
const all = client.prList({ state: 'all', limit: PR_LIMIT, fields: FIELDS, head: branch });
|
|
153
|
+
if (all === null)
|
|
154
|
+
return null;
|
|
155
|
+
rows.push(...all.rows);
|
|
156
|
+
return { rows, openCapped, headCapped: branch !== undefined && all.truncated };
|
|
157
|
+
}
|
|
158
|
+
/** Why `l` may leave PRs out, naming the repository `name` (null: the one listed for). */
|
|
159
|
+
function capReasons(l, branch, name) {
|
|
160
|
+
const out = [];
|
|
161
|
+
if (l.openCapped)
|
|
162
|
+
out.push(`${name ?? 'the repository'} has more than ${OPEN_PR_LIMIT} open PRs`);
|
|
163
|
+
if (l.headCapped) {
|
|
164
|
+
const where = name === null ? '' : ` in ${name}`;
|
|
165
|
+
out.push(`more than ${PR_LIMIT} PRs${where} have the head ${branch}`);
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
/** The client's repository's fork status; `unknown` when it could not be read. */
|
|
170
|
+
function forkOf(client) {
|
|
171
|
+
const answer = client.api('repos/{owner}/{repo}');
|
|
172
|
+
const body = answer.body;
|
|
173
|
+
if (!answer.ok || typeof body !== 'object' || body === null)
|
|
174
|
+
return { kind: 'unknown' };
|
|
175
|
+
const { fork, parent } = body;
|
|
176
|
+
if (fork !== true)
|
|
177
|
+
return { kind: 'not-fork' };
|
|
178
|
+
return { kind: 'fork', parent: typeof parent?.full_name === 'string' ? parent.full_name : null };
|
|
179
|
+
}
|
package/dist/providers/gitlab.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* against the one the sweep measures.
|
|
8
8
|
*/
|
|
9
9
|
import { parseJsonResult, runFailed, stderrOrError, } from '@osdlc/command-seam';
|
|
10
|
-
import { PR_LIMIT } from "./github.js";
|
|
10
|
+
import { OPEN_PR_LIMIT, PR_LIMIT } from "./github.js";
|
|
11
11
|
const PER_PAGE = 100;
|
|
12
12
|
/** GitLab's states, as the GitHub names classification reads. A locked MR is an open one mid-update. */
|
|
13
13
|
const STATE = {
|
|
@@ -78,12 +78,11 @@ export function gitlabProvider(glab) {
|
|
|
78
78
|
if (target.project === null)
|
|
79
79
|
throw new Error('the remote URL names no GitLab project');
|
|
80
80
|
const project = encodeURIComponent(target.project);
|
|
81
|
-
const
|
|
82
|
-
for (let page = 1; (page - 1) * PER_PAGE < PR_LIMIT; page++) {
|
|
81
|
+
const page = (state, number) => {
|
|
83
82
|
const query = new URLSearchParams({
|
|
84
|
-
state
|
|
83
|
+
state,
|
|
85
84
|
per_page: String(PER_PAGE),
|
|
86
|
-
page: String(
|
|
85
|
+
page: String(number),
|
|
87
86
|
order_by: 'updated_at',
|
|
88
87
|
sort: 'desc',
|
|
89
88
|
});
|
|
@@ -99,25 +98,46 @@ export function gitlabProvider(glab) {
|
|
|
99
98
|
if (!succeeded(result)) {
|
|
100
99
|
// Not stdout: a page cut short is half a JSON listing, not a reason.
|
|
101
100
|
throw new Error(result.outputIncomplete === true
|
|
102
|
-
? `page ${
|
|
101
|
+
? `page ${number} was cut short`
|
|
103
102
|
: stderrOrError(result) || `glab exited ${String(result.exitCode)}`);
|
|
104
103
|
}
|
|
105
104
|
const listed = parseJsonResult(result);
|
|
106
105
|
if (!listed.ok || !Array.isArray(listed.value)) {
|
|
107
|
-
throw new Error(`page ${
|
|
106
|
+
throw new Error(`page ${number} is not a merge request list`);
|
|
108
107
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
108
|
+
return listed.value;
|
|
109
|
+
};
|
|
110
|
+
const prs = [];
|
|
111
|
+
/** Reads pages of `state` until one is short or `limit` is reached; true when it stopped at the limit. */
|
|
112
|
+
const read = (state, limit) => {
|
|
113
|
+
for (let number = 1; (number - 1) * PER_PAGE < limit; number++) {
|
|
114
|
+
const rows = page(state, number);
|
|
115
|
+
for (const row of rows) {
|
|
116
|
+
if (!isMergeRequest(row))
|
|
117
|
+
continue;
|
|
118
|
+
const pr = mergeRequestToPr(row);
|
|
119
|
+
if (pr !== null)
|
|
120
|
+
prs.push(pr);
|
|
121
|
+
}
|
|
122
|
+
if (rows.length < PER_PAGE)
|
|
123
|
+
return false;
|
|
116
124
|
}
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
return true;
|
|
126
|
+
};
|
|
127
|
+
// Open MRs first and to their own cap: they gate a delete, and the newest
|
|
128
|
+
// `PR_LIMIT` of every state can leave an old one out. `locked` is an open MR
|
|
129
|
+
// mid-update; the listing of every state below covers it.
|
|
130
|
+
const cut = branch === undefined && read('opened', OPEN_PR_LIMIT);
|
|
131
|
+
const more = read('all', PR_LIMIT);
|
|
132
|
+
if (branch !== undefined && more) {
|
|
133
|
+
return {
|
|
134
|
+
prs,
|
|
135
|
+
incomplete: `more than ${PR_LIMIT} merge requests have the source branch ${branch}`,
|
|
136
|
+
};
|
|
119
137
|
}
|
|
120
|
-
return
|
|
138
|
+
return cut
|
|
139
|
+
? { prs, incomplete: `the project has ${OPEN_PR_LIMIT} or more open merge requests` }
|
|
140
|
+
: prs;
|
|
121
141
|
},
|
|
122
142
|
};
|
|
123
143
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { type CommandRunner } from '@osdlc/command-seam';
|
|
6
6
|
import type { Gh } from '@osdlc/typed-gh';
|
|
7
7
|
import type { ForgeTarget, PrProvider, ProviderChoice, ProviderName, ProviderStatus } from './types.ts';
|
|
8
|
-
export { githubProvider } from './github.ts';
|
|
8
|
+
export { githubProvider, OPEN_PR_LIMIT, PR_LIMIT } from './github.ts';
|
|
9
9
|
export { gitlabProvider } from './gitlab.ts';
|
|
10
10
|
export * from './types.ts';
|
|
11
11
|
/** Every known provider, over the given clients. `glab` defaults to the real binary. */
|
package/dist/providers/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { resolveRunner } from '@osdlc/command-seam';
|
|
6
6
|
import { githubProvider } from "./github.js";
|
|
7
7
|
import { gitlabProvider } from "./gitlab.js";
|
|
8
|
-
export { githubProvider } from "./github.js";
|
|
8
|
+
export { githubProvider, OPEN_PR_LIMIT, PR_LIMIT } from "./github.js";
|
|
9
9
|
export { gitlabProvider } from "./gitlab.js";
|
|
10
10
|
export * from "./types.js";
|
|
11
11
|
/** Every known provider, over the given clients. `glab` defaults to the real binary. */
|
|
@@ -39,6 +39,18 @@ export interface ProviderStatus extends ForgeTarget, ProviderWords {
|
|
|
39
39
|
* in the provider's words. Absent when it answered or was not asked.
|
|
40
40
|
*/
|
|
41
41
|
listError?: string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Why the provider's listing may leave out an open request; set by the
|
|
44
|
+
* sweep. A branch with no known request then needs the same confirmation
|
|
45
|
+
* as after a failed listing.
|
|
46
|
+
*/
|
|
47
|
+
incomplete?: string | undefined;
|
|
48
|
+
}
|
|
49
|
+
/** A listing that answered, with what it could not read. */
|
|
50
|
+
export interface PrListing {
|
|
51
|
+
prs: PRInfo[];
|
|
52
|
+
/** Why requests that could be open are missing from `prs`; absent when none are. */
|
|
53
|
+
incomplete?: string | undefined;
|
|
42
54
|
}
|
|
43
55
|
/** One source of pull request evidence. */
|
|
44
56
|
export interface PrProvider {
|
|
@@ -53,10 +65,11 @@ export interface PrProvider {
|
|
|
53
65
|
* The repository's pull requests, fork PRs already dropped, in any order;
|
|
54
66
|
* null, or a throw whose message says why, when the listing failed. A
|
|
55
67
|
* failure is not "no PRs": the caller classifies on git evidence alone and
|
|
56
|
-
* notes the failure on the repository.
|
|
68
|
+
* notes the failure on the repository. A listing that answered but cannot
|
|
69
|
+
* rule out an open request returns a {@link PrListing} with `incomplete`.
|
|
57
70
|
*
|
|
58
71
|
* @param branch Narrow the listing to this exact head branch. The caller
|
|
59
72
|
* checks the head again, so a provider that cannot narrow may list all.
|
|
60
73
|
*/
|
|
61
|
-
listPrs(root: string, target: ForgeTarget, branch?: string): PRInfo[] | null;
|
|
74
|
+
listPrs(root: string, target: ForgeTarget, branch?: string): PRInfo[] | PrListing | null;
|
|
62
75
|
}
|