@rightkit/ci 0.0.0-stage → 0.2.43
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 +177 -2
- package/adopt.mjs +96 -0
- package/checkout.mjs +93 -0
- package/checkout.test.mjs +91 -0
- package/cli/right-git.mjs +218 -0
- package/cli.test.mjs +68 -0
- package/drift.mjs +201 -0
- package/drift.test.mjs +203 -0
- package/guard-coverage.test.mjs +236 -0
- package/install.mjs +81 -0
- package/manifest.mjs +1862 -0
- package/manifest.schema.json +286 -0
- package/manifest.test.mjs +487 -0
- package/ownership-registration.mjs +33 -0
- package/ownership-registration.test.mjs +20 -0
- package/pack-smoke.test.mjs +55 -0
- package/package.json +38 -4
- package/pnpm-lock-patch.test.mjs +161 -0
- package/policy/visibility-gate.mjs +168 -0
- package/render.mjs +69 -0
- package/render.test.mjs +1722 -0
- package/rightkit-ownership.json +15 -0
- package/sync.mjs +180 -0
- package/sync.test.mjs +147 -0
- package/templates/ci.yml +50 -0
- package/templates/package-smoke.yml +38 -0
- package/templates/profiles/rust-hybrid/ci.yml +127 -0
- package/templates/profiles/rust-hybrid/regenerate.yml +96 -0
- package/templates/profiles/rust-hybrid/release-candidate.yml +193 -0
- package/templates/publish-npm.yml +72 -0
- package/templates/publish-package-managers.yml +158 -0
- package/templates/qualification.yml +94 -0
- package/templates/regenerate.yml +80 -0
- package/templates/release-candidate.yml +57 -0
- package/test-support.mjs +34 -0
- package/uninstall.mjs +36 -0
- package/uninstall.test.mjs +89 -0
- package/visibility-gate.test.mjs +141 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { installCommand } from "../install.mjs";
|
|
5
|
+
import { syncCommand } from "../sync.mjs";
|
|
6
|
+
import { driftCommand, driftAll } from "../drift.mjs";
|
|
7
|
+
import { adoptCommand } from "../adopt.mjs";
|
|
8
|
+
import { uninstallCommand } from "../uninstall.mjs";
|
|
9
|
+
|
|
10
|
+
const commands = new Map([
|
|
11
|
+
["install", runInstall],
|
|
12
|
+
["sync", runSync],
|
|
13
|
+
["drift", runDrift],
|
|
14
|
+
["adopt", runAdopt],
|
|
15
|
+
["uninstall", runUninstall],
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
// F7 fix: flags that are always boolean and must never consume the next
|
|
19
|
+
// argv token as a value. Without this, `drift --all <root>` (the documented
|
|
20
|
+
// syntax — see printHelp below) fed `<root>` to parseFlags' generic
|
|
21
|
+
// "--flag value" branch as the *value* of --all, leaving `positional` empty;
|
|
22
|
+
// runDrift then fell back to `.` and silently scanned the current working
|
|
23
|
+
// directory instead of the supplied workspace root. `--all=true` happened to
|
|
24
|
+
// work because the `=` form never consumes a following token. Declared here
|
|
25
|
+
// (above the dispatch below, which calls parseFlags synchronously at module
|
|
26
|
+
// load) rather than next to parseFlags itself, to avoid a temporal-dead-zone
|
|
27
|
+
// ReferenceError on a const declared after its first use point at runtime.
|
|
28
|
+
const BOOLEAN_ONLY_FLAGS = new Set(["all"]);
|
|
29
|
+
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
const first = args[0];
|
|
32
|
+
|
|
33
|
+
if (!first || first === "-h" || first === "--help") {
|
|
34
|
+
printHelp();
|
|
35
|
+
process.exit(first ? 0 : 1);
|
|
36
|
+
} else if (first === "--version" || first === "-v") {
|
|
37
|
+
const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
38
|
+
console.log(pkg.version);
|
|
39
|
+
process.exit(0);
|
|
40
|
+
} else if (commands.has(first)) {
|
|
41
|
+
const exitCode = await commands.get(first)(args.slice(1));
|
|
42
|
+
process.exit(exitCode);
|
|
43
|
+
} else {
|
|
44
|
+
console.error(`right-git: unknown command: ${first}\n`);
|
|
45
|
+
printHelp();
|
|
46
|
+
process.exit(2);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Split argv into positional args and --flag / --flag=value pairs. */
|
|
50
|
+
function parseFlags(argv) {
|
|
51
|
+
const positional = [];
|
|
52
|
+
const flags = {};
|
|
53
|
+
for (let i = 0; i < argv.length; i++) {
|
|
54
|
+
const arg = argv[i];
|
|
55
|
+
if (arg.startsWith("--")) {
|
|
56
|
+
const eq = arg.indexOf("=");
|
|
57
|
+
if (eq !== -1) {
|
|
58
|
+
flags[arg.slice(2, eq)] = arg.slice(eq + 1);
|
|
59
|
+
} else {
|
|
60
|
+
const key = arg.slice(2);
|
|
61
|
+
if (!BOOLEAN_ONLY_FLAGS.has(key) && i + 1 < argv.length && !argv[i + 1].startsWith("--")) {
|
|
62
|
+
flags[key] = argv[++i];
|
|
63
|
+
} else {
|
|
64
|
+
flags[key] = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
positional.push(arg);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { positional, flags };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function printErrors(errors) {
|
|
75
|
+
for (const error of errors || []) console.error(` ${error}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Advisory lines never change drift's exit code: warnings are settings to fix,
|
|
79
|
+
// notes are checks that could not run here.
|
|
80
|
+
function printAdvisories(result) {
|
|
81
|
+
for (const warning of result.warnings || []) console.log(` warn: ${warning}`);
|
|
82
|
+
for (const note of result.notes || []) console.log(` note: ${note}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function runInstall(argv) {
|
|
86
|
+
const { positional, flags } = parseFlags(argv);
|
|
87
|
+
const repoPath = positional[0];
|
|
88
|
+
if (!repoPath) {
|
|
89
|
+
console.error("right-git install <repo-path> --lanes ci,qualification,... [--profile node|rust-hybrid --gate-path scripts/gate.sh]");
|
|
90
|
+
return 2;
|
|
91
|
+
}
|
|
92
|
+
const opts = {
|
|
93
|
+
lanes: typeof flags.lanes === "string" ? flags.lanes.split(",").map((s) => s.trim()).filter(Boolean) : undefined,
|
|
94
|
+
homebrewTap: flags["homebrew-tap"],
|
|
95
|
+
homebrewFormula: flags["homebrew-formula"],
|
|
96
|
+
wingetId: flags["winget-id"],
|
|
97
|
+
provenance: flags["no-provenance"] ? false : undefined,
|
|
98
|
+
profile: flags.profile,
|
|
99
|
+
gatePath: flags["gate-path"],
|
|
100
|
+
};
|
|
101
|
+
const result = await installCommand(path.resolve(repoPath), opts);
|
|
102
|
+
if (!result.ok) {
|
|
103
|
+
console.error(`right-git install: failed at stage '${result.stage}'`);
|
|
104
|
+
printErrors(result.errors);
|
|
105
|
+
return 1;
|
|
106
|
+
}
|
|
107
|
+
console.log(`right-git install: wrote ${result.manifestPath} and synced ${result.plan.changes.length} workflow file(s).`);
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function runSync(argv) {
|
|
112
|
+
const { positional, flags } = parseFlags(argv);
|
|
113
|
+
const repoPath = positional[0];
|
|
114
|
+
if (!repoPath) {
|
|
115
|
+
console.error("right-git sync <repo-path> [--dry-run]");
|
|
116
|
+
return 2;
|
|
117
|
+
}
|
|
118
|
+
const result = await syncCommand(path.resolve(repoPath), { dryRun: Boolean(flags["dry-run"]) });
|
|
119
|
+
if (!result.ok) {
|
|
120
|
+
console.error(`right-git sync: failed at stage '${result.stage}'`);
|
|
121
|
+
printErrors(result.errors);
|
|
122
|
+
return 1;
|
|
123
|
+
}
|
|
124
|
+
const changed = result.changes.filter((c) => c.action !== "unchanged");
|
|
125
|
+
console.log(changed.length === 0 ? "right-git sync: already up to date." : `right-git sync: ${changed.length} file(s) ${flags["dry-run"] ? "would change" : "changed"}.`);
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function runDrift(argv) {
|
|
130
|
+
const { positional, flags } = parseFlags(argv);
|
|
131
|
+
if (flags.all) {
|
|
132
|
+
const root = path.resolve(positional[0] || ".");
|
|
133
|
+
const results = await driftAll(root);
|
|
134
|
+
let anyDrift = false;
|
|
135
|
+
for (const { repoPath, result } of results) {
|
|
136
|
+
if (!result.ok) {
|
|
137
|
+
console.error(`${repoPath}: error`);
|
|
138
|
+
printErrors(result.errors);
|
|
139
|
+
anyDrift = true;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
console.log(`${repoPath}: visibility=${result.visibility.visibility} drift=${result.driftDetected}`);
|
|
143
|
+
for (const finding of result.findings) {
|
|
144
|
+
if (finding.status !== "match") console.log(` ${finding.status}: ${finding.filename}`);
|
|
145
|
+
}
|
|
146
|
+
printAdvisories(result);
|
|
147
|
+
if (result.driftDetected) anyDrift = true;
|
|
148
|
+
}
|
|
149
|
+
return anyDrift ? 1 : 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const repoPath = positional[0];
|
|
153
|
+
if (!repoPath) {
|
|
154
|
+
console.error("right-git drift <repo-path> | right-git drift --all [workspace-root]");
|
|
155
|
+
return 2;
|
|
156
|
+
}
|
|
157
|
+
const result = await driftCommand(path.resolve(repoPath));
|
|
158
|
+
if (!result.ok) {
|
|
159
|
+
console.error("right-git drift: failed");
|
|
160
|
+
printErrors(result.errors);
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
console.log(`visibility=${result.visibility.visibility} drift=${result.driftDetected}`);
|
|
164
|
+
for (const finding of result.findings) console.log(` ${finding.status}: ${finding.filename}`);
|
|
165
|
+
printAdvisories(result);
|
|
166
|
+
return result.driftDetected ? 1 : 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function runAdopt(argv) {
|
|
170
|
+
const { positional, flags } = parseFlags(argv);
|
|
171
|
+
const repoPath = positional[0];
|
|
172
|
+
if (!repoPath) {
|
|
173
|
+
console.error("right-git adopt <repo-path> [--out <dir>]");
|
|
174
|
+
return 2;
|
|
175
|
+
}
|
|
176
|
+
const result = adoptCommand(path.resolve(repoPath));
|
|
177
|
+
if (!result.ok) {
|
|
178
|
+
console.error("right-git adopt: failed");
|
|
179
|
+
printErrors(result.errors);
|
|
180
|
+
return 1;
|
|
181
|
+
}
|
|
182
|
+
for (const { lane, sourceFile } of result.mapped) console.log(`mapped ${lane} <- ${sourceFile}`);
|
|
183
|
+
for (const filename of result.unmapped) console.log(`unmapped: ${filename}`);
|
|
184
|
+
if (typeof flags.out === "string") {
|
|
185
|
+
fs.mkdirSync(flags.out, { recursive: true });
|
|
186
|
+
for (const { lane, content } of result.mapped) {
|
|
187
|
+
fs.writeFileSync(path.join(flags.out, `${lane}.yml`), content, "utf8");
|
|
188
|
+
}
|
|
189
|
+
console.log(`right-git adopt: wrote ${result.mapped.length} candidate template(s) to ${flags.out} for review.`);
|
|
190
|
+
}
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function runUninstall(argv) {
|
|
195
|
+
const { positional } = parseFlags(argv);
|
|
196
|
+
const repoPath = positional[0];
|
|
197
|
+
if (!repoPath) {
|
|
198
|
+
console.error("right-git uninstall <repo-path>");
|
|
199
|
+
return 2;
|
|
200
|
+
}
|
|
201
|
+
const result = uninstallCommand(path.resolve(repoPath));
|
|
202
|
+
console.log(result.removed.length === 0 ? "right-git uninstall: no managed files found." : `right-git uninstall: removed ${result.removed.length} file(s): ${result.removed.join(", ")}`);
|
|
203
|
+
return 0;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function printHelp() {
|
|
207
|
+
console.log(`right-git <command> [options]
|
|
208
|
+
|
|
209
|
+
Commands:
|
|
210
|
+
install <repo-path> --lanes ci,... [--profile rust-hybrid --gate-path scripts/gate.sh] Write .rightgit.json and render workflows; refuses private repos
|
|
211
|
+
sync <repo-path> [--dry-run] Re-render templates from .rightgit.json, show diff, apply
|
|
212
|
+
drift <repo-path> CI-safe check: installed workflows == rendered templates? Also warns when the regenerate lane cannot open pull requests (gh)
|
|
213
|
+
drift --all [workspace-root] Drift-check + re-check visibility for every managed repo found
|
|
214
|
+
adopt <repo-path> [--out <dir>] Map an existing repo's workflows onto right-git lanes for review
|
|
215
|
+
uninstall <repo-path> Remove every right-git-managed workflow file
|
|
216
|
+
|
|
217
|
+
Public-repo only: install/sync refuse private, internal, or unconfirmed-visibility repos. No override flag (invariant I1).`);
|
|
218
|
+
}
|
package/cli.test.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// CLI-level regression coverage for right-git.mjs. cli/right-git.mjs is a
|
|
2
|
+
// true entrypoint (it dispatches on process.argv at module-load time), not a
|
|
3
|
+
// library module, so its argument parsing can only be exercised honestly by
|
|
4
|
+
// spawning it as a real subprocess with real argv — the same way the QA
|
|
5
|
+
// report reproduced F7.
|
|
6
|
+
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import os from "node:os";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { spawnSync } from "node:child_process";
|
|
14
|
+
import { SAMPLE_MANIFEST } from "./test-support.mjs";
|
|
15
|
+
|
|
16
|
+
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)));
|
|
17
|
+
const cliPath = path.join(packageRoot, "cli", "right-git.mjs");
|
|
18
|
+
|
|
19
|
+
function runCli(args, cwd) {
|
|
20
|
+
return spawnSync(process.execPath, [cliPath, ...args], { cwd, encoding: "utf8" });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// F7 regression: `right-git drift --all <root>` is the documented syntax
|
|
24
|
+
// (see printHelp: "drift --all [workspace-root]"). The old parseFlags
|
|
25
|
+
// treated `--all` like any other "--flag value" pair, so it consumed the
|
|
26
|
+
// root path as --all's *value* instead of leaving it positional; runDrift
|
|
27
|
+
// then fell back to `path.resolve(positional[0] || ".")`, silently scanning
|
|
28
|
+
// the current working directory instead of the supplied root. Only the
|
|
29
|
+
// undocumented `--all=true <root>` form worked. Prove the fix by running the
|
|
30
|
+
// CLI from a cwd that has no managed repos, with the real fixture root
|
|
31
|
+
// passed as a plain trailing argument, and asserting the fixture repo is the
|
|
32
|
+
// one actually discovered.
|
|
33
|
+
test("F7: `right-git drift --all <root>` (space-separated) scopes the scan to the given root, not process.cwd()", () => {
|
|
34
|
+
const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-cli-root-"));
|
|
35
|
+
const repoDir = path.join(fixtureRoot, "managed-repo");
|
|
36
|
+
fs.mkdirSync(repoDir, { recursive: true });
|
|
37
|
+
fs.writeFileSync(path.join(repoDir, ".rightgit.json"), `${JSON.stringify(SAMPLE_MANIFEST, null, 2)}\n`, "utf8");
|
|
38
|
+
fs.writeFileSync(path.join(repoDir, "package.json"), JSON.stringify({ name: "widgets" }), "utf8");
|
|
39
|
+
|
|
40
|
+
// Deliberately not fixtureRoot and carries no managed repo of its own —
|
|
41
|
+
// if the bug is present, the CLI scans this directory instead and the
|
|
42
|
+
// fixture repo is never mentioned in the output.
|
|
43
|
+
const emptyCwd = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-cli-cwd-"));
|
|
44
|
+
|
|
45
|
+
const result = runCli(["drift", "--all", fixtureRoot], emptyCwd);
|
|
46
|
+
|
|
47
|
+
const output = `${result.stdout}${result.stderr}`;
|
|
48
|
+
assert.ok(
|
|
49
|
+
output.includes(repoDir),
|
|
50
|
+
`expected 'drift --all ${fixtureRoot}' (run from an unrelated cwd) to discover ${repoDir}; got stdout=${JSON.stringify(result.stdout)} stderr=${JSON.stringify(result.stderr)}`,
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Non-regression: the workaround documented by the QA report, `--all=true`
|
|
55
|
+
// followed by the root, must keep working exactly as before.
|
|
56
|
+
test("`right-git drift --all=true <root>` still scopes the scan to the given root", () => {
|
|
57
|
+
const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-cli-root-eq-"));
|
|
58
|
+
const repoDir = path.join(fixtureRoot, "managed-repo");
|
|
59
|
+
fs.mkdirSync(repoDir, { recursive: true });
|
|
60
|
+
fs.writeFileSync(path.join(repoDir, ".rightgit.json"), `${JSON.stringify(SAMPLE_MANIFEST, null, 2)}\n`, "utf8");
|
|
61
|
+
fs.writeFileSync(path.join(repoDir, "package.json"), JSON.stringify({ name: "widgets" }), "utf8");
|
|
62
|
+
|
|
63
|
+
const emptyCwd = fs.mkdtempSync(path.join(os.tmpdir(), "right-git-cli-cwd-eq-"));
|
|
64
|
+
const result = runCli(["drift", `--all=true`, fixtureRoot], emptyCwd);
|
|
65
|
+
|
|
66
|
+
const output = `${result.stdout}${result.stderr}`;
|
|
67
|
+
assert.ok(output.includes(repoDir), `expected discovery of ${repoDir}; got stdout=${JSON.stringify(result.stdout)} stderr=${JSON.stringify(result.stderr)}`);
|
|
68
|
+
});
|
package/drift.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Compare installed workflows against what the manifest would render today
|
|
2
|
+
// (DESIGN.md §4, invariant I4: no silent forks). `--all` enumerates every
|
|
3
|
+
// repo carrying a `.rightgit.json` under a workspace root and re-checks
|
|
4
|
+
// visibility, flagging public->private flips (DESIGN.md §5 layer 3).
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { execFile as execFileCb } from "node:child_process";
|
|
9
|
+
import { promisify } from "node:util";
|
|
10
|
+
import { assertPublic, parseOwnerRepo } from "./policy/visibility-gate.mjs";
|
|
11
|
+
import { loadManifest } from "./manifest.mjs";
|
|
12
|
+
import { renderLanes, listManagedFiles } from "./sync.mjs";
|
|
13
|
+
import { MANAGED_MARKER } from "./render.mjs";
|
|
14
|
+
|
|
15
|
+
const IGNORED_DIRS = new Set(["node_modules", ".git", ".pnpm-store"]);
|
|
16
|
+
const execFileAsync = promisify(execFileCb);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Drift-check one repo. Never throws. Returns:
|
|
20
|
+
* { ok, driftDetected, visibility, findings: [{filename, status}], errors, warnings, notes }
|
|
21
|
+
* status is one of: match | mismatch | missing | unmanaged-extra | orphan
|
|
22
|
+
* warnings are advisory (never drift): see checkRegenerateApproval.
|
|
23
|
+
* notes record checks that were skipped and why.
|
|
24
|
+
*/
|
|
25
|
+
export async function driftCommand(repoPath, deps = {}) {
|
|
26
|
+
const visibility = await assertPublic(repoPath, deps);
|
|
27
|
+
|
|
28
|
+
const loaded = loadManifest(repoPath);
|
|
29
|
+
if (!loaded.ok) {
|
|
30
|
+
// F7: a repo can carry right-git-managed workflow files (identified by
|
|
31
|
+
// MANAGED_MARKER) with no readable .rightgit.json — e.g. the manifest
|
|
32
|
+
// was deleted, never committed, or lost in an interrupted install. That
|
|
33
|
+
// is drift to report, not a tool failure to hide behind: the old
|
|
34
|
+
// behavior returned a hard `ok:false` here with no findings, which made
|
|
35
|
+
// `findManagedRepos` (below) — and therefore `drift --all` — blind to
|
|
36
|
+
// exactly this repo.
|
|
37
|
+
const orphanManaged = listManagedFiles(repoPath);
|
|
38
|
+
if (orphanManaged.length > 0) {
|
|
39
|
+
return {
|
|
40
|
+
ok: true,
|
|
41
|
+
driftDetected: true,
|
|
42
|
+
visibility,
|
|
43
|
+
findings: orphanManaged.map((filename) => ({ filename, status: "orphan" })),
|
|
44
|
+
errors: [],
|
|
45
|
+
warnings: [],
|
|
46
|
+
notes: [],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return { ok: false, driftDetected: null, visibility, findings: [], errors: loaded.errors, warnings: [], notes: [] };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const rendered = renderLanes(loaded.manifest, repoPath);
|
|
53
|
+
const wantedFilenames = new Set(rendered.map((r) => r.filename));
|
|
54
|
+
const findings = [];
|
|
55
|
+
|
|
56
|
+
for (const { filename, content } of rendered) {
|
|
57
|
+
const target = path.join(repoPath, ".github", "workflows", filename);
|
|
58
|
+
let existing = null;
|
|
59
|
+
try {
|
|
60
|
+
existing = fs.readFileSync(target, "utf8");
|
|
61
|
+
} catch {
|
|
62
|
+
// not installed
|
|
63
|
+
}
|
|
64
|
+
if (existing === null) {
|
|
65
|
+
findings.push({ filename, status: "missing" });
|
|
66
|
+
} else if (existing !== content) {
|
|
67
|
+
findings.push({ filename, status: "mismatch" });
|
|
68
|
+
} else {
|
|
69
|
+
findings.push({ filename, status: "match" });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const managedFilename of listManagedFiles(repoPath)) {
|
|
74
|
+
if (!wantedFilenames.has(managedFilename)) {
|
|
75
|
+
findings.push({ filename: managedFilename, status: "unmanaged-extra" });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const driftDetected = findings.some((f) => f.status !== "match") || !visibility.allowed;
|
|
80
|
+
|
|
81
|
+
const warnings = [];
|
|
82
|
+
const notes = [];
|
|
83
|
+
if (loaded.manifest.lanes.includes("regenerate")) {
|
|
84
|
+
const approval = await checkRegenerateApproval(repoPath, deps);
|
|
85
|
+
if (approval.status === "warn") warnings.push(approval.message);
|
|
86
|
+
else if (approval.status === "skipped") notes.push(approval.message);
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, driftDetected, visibility, findings, errors: [], warnings, notes };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The regenerate lane opens its pull request with the workflow token, which
|
|
93
|
+
* GitHub refuses unless the repository allows Actions to create and approve
|
|
94
|
+
* pull requests. Reads `gh api repos/{owner}/{repo}/actions/permissions/workflow`
|
|
95
|
+
* (field can_approve_pull_request_reviews). Read-only: an unavailable or
|
|
96
|
+
* unreadable lookup is a skipped check with a note, never a drift finding.
|
|
97
|
+
* Returns { status: "ok" | "warn" | "skipped", message }. Never throws.
|
|
98
|
+
*/
|
|
99
|
+
export async function checkRegenerateApproval(repoPath, deps = {}) {
|
|
100
|
+
const execFile = deps.execFile || execFileAsync;
|
|
101
|
+
const skip = (reason) => ({
|
|
102
|
+
status: "skipped",
|
|
103
|
+
message: `regenerate lane: skipped the Actions pull-request approval check (${reason})`,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
let remoteUrl;
|
|
107
|
+
try {
|
|
108
|
+
const { stdout } = await execFile("git", ["remote", "get-url", "origin"], { cwd: repoPath });
|
|
109
|
+
remoteUrl = stdout.trim();
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return skip(`no origin remote: ${firstLine(error)}`);
|
|
112
|
+
}
|
|
113
|
+
const ownerRepo = parseOwnerRepo(remoteUrl);
|
|
114
|
+
if (!ownerRepo) return skip("origin is not a github.com repository");
|
|
115
|
+
|
|
116
|
+
let stdout;
|
|
117
|
+
try {
|
|
118
|
+
({ stdout } = await execFile("gh", ["api", `repos/${ownerRepo.owner}/${ownerRepo.repo}/actions/permissions/workflow`], { cwd: repoPath }));
|
|
119
|
+
} catch (error) {
|
|
120
|
+
return skip(error.code === "ENOENT" ? "gh is not installed" : `gh api failed: ${firstLine(error)}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let settings;
|
|
124
|
+
try {
|
|
125
|
+
settings = JSON.parse(stdout);
|
|
126
|
+
} catch {
|
|
127
|
+
return skip("gh api returned no JSON");
|
|
128
|
+
}
|
|
129
|
+
if (typeof settings?.can_approve_pull_request_reviews !== "boolean") {
|
|
130
|
+
return skip("response has no can_approve_pull_request_reviews");
|
|
131
|
+
}
|
|
132
|
+
if (settings.can_approve_pull_request_reviews) return { status: "ok", message: null };
|
|
133
|
+
return {
|
|
134
|
+
status: "warn",
|
|
135
|
+
message:
|
|
136
|
+
"regenerate lane: the repository setting \"Allow GitHub Actions to create and approve pull requests\" is off, so regenerate.yml cannot open its pull request. Enable it under Settings > Actions > General.",
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function firstLine(error) {
|
|
141
|
+
return String(error?.message ?? error).split("\n")[0];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Recursively find every repo under root carrying right-git-managed state:
|
|
146
|
+
* either a `.rightgit.json` manifest, or (F7) a `.github/workflows/*.yml`
|
|
147
|
+
* file that starts with MANAGED_MARKER with no manifest present — an
|
|
148
|
+
* "orphan" repo the old implementation never discovered because it looked
|
|
149
|
+
* only for the manifest filename.
|
|
150
|
+
*/
|
|
151
|
+
export function findManagedRepos(root) {
|
|
152
|
+
const found = new Set();
|
|
153
|
+
const stack = [root];
|
|
154
|
+
while (stack.length > 0) {
|
|
155
|
+
const dir = stack.pop();
|
|
156
|
+
let entries;
|
|
157
|
+
try {
|
|
158
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
159
|
+
} catch {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (entries.some((e) => e.isFile() && e.name === ".rightgit.json")) {
|
|
164
|
+
found.add(dir);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (path.basename(dir) === "workflows" && path.basename(path.dirname(dir)) === ".github") {
|
|
168
|
+
const hasManagedWorkflow = entries.some(
|
|
169
|
+
(e) => e.isFile() && (e.name.endsWith(".yml") || e.name.endsWith(".yaml")) && startsWithManagedMarker(path.join(dir, e.name)),
|
|
170
|
+
);
|
|
171
|
+
if (hasManagedWorkflow) {
|
|
172
|
+
found.add(path.dirname(path.dirname(dir)));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
for (const entry of entries) {
|
|
177
|
+
if (entry.isDirectory() && !IGNORED_DIRS.has(entry.name)) {
|
|
178
|
+
stack.push(path.join(dir, entry.name));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return [...found];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function startsWithManagedMarker(filePath) {
|
|
186
|
+
try {
|
|
187
|
+
return fs.readFileSync(filePath, "utf8").startsWith(MANAGED_MARKER);
|
|
188
|
+
} catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** `right-git drift --all <workspace-root>`: drift-check every managed repo found under root. */
|
|
194
|
+
export async function driftAll(workspaceRoot, deps = {}) {
|
|
195
|
+
const repos = findManagedRepos(workspaceRoot);
|
|
196
|
+
const results = [];
|
|
197
|
+
for (const repoPath of repos) {
|
|
198
|
+
results.push({ repoPath, result: await driftCommand(repoPath, deps) });
|
|
199
|
+
}
|
|
200
|
+
return results;
|
|
201
|
+
}
|