@openparachute/vault 0.5.0-rc.1 → 0.5.0-rc.3
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/package.json +1 -1
- package/src/export-watch.test.ts +23 -0
- package/src/export-watch.ts +14 -0
- package/src/git-preflight.test.ts +70 -0
- package/src/git-preflight.ts +68 -0
- package/src/mirror-config.test.ts +14 -0
- package/src/mirror-config.ts +11 -0
- package/src/mirror-import.test.ts +110 -0
- package/src/mirror-import.ts +71 -13
- package/src/mirror-manager.test.ts +51 -0
- package/src/mirror-manager.ts +73 -11
- package/src/mirror-routes.test.ts +463 -1
- package/src/mirror-routes.ts +474 -4
- package/web/ui/dist/assets/{index-DDRo6F4u.js → index-BKYNb2II.js} +10 -10
- package/web/ui/dist/index.html +1 -1
package/package.json
CHANGED
package/src/export-watch.test.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
runGitCommitCycle,
|
|
36
36
|
shouldCommit,
|
|
37
37
|
} from "./export-watch.ts";
|
|
38
|
+
import { GitNotInstalledError } from "./git-preflight.ts";
|
|
38
39
|
|
|
39
40
|
const CLI = path.resolve(import.meta.dir, "cli.ts");
|
|
40
41
|
|
|
@@ -504,6 +505,28 @@ describe("runGitCommitCycle", () => {
|
|
|
504
505
|
});
|
|
505
506
|
expect(result.message).toBe("note: Inbox/DonorMeeting");
|
|
506
507
|
});
|
|
508
|
+
|
|
509
|
+
test("git missing → throws GitNotInstalledError (sync surfaces friendly error, not raw spawn crash)", async () => {
|
|
510
|
+
// vault#415 — the sync/commit path must surface the actionable
|
|
511
|
+
// git-not-installed message (which the manager threads into
|
|
512
|
+
// status.last_error) instead of crashing with a raw "Executable not
|
|
513
|
+
// found in $PATH". Force the preflight to see no git via the `which`
|
|
514
|
+
// seam; no real spawn should be reached.
|
|
515
|
+
fs.writeFileSync(path.join(dir, "Note.md"), "# n\n");
|
|
516
|
+
await expect(
|
|
517
|
+
runGitCommitCycle({
|
|
518
|
+
repoDir: dir,
|
|
519
|
+
template: DEFAULT_COMMIT_TEMPLATE,
|
|
520
|
+
notesChanged: 1,
|
|
521
|
+
vaultName: "default",
|
|
522
|
+
firstNoteTitle: "Note",
|
|
523
|
+
push: false,
|
|
524
|
+
which: () => null,
|
|
525
|
+
}),
|
|
526
|
+
).rejects.toBeInstanceOf(GitNotInstalledError);
|
|
527
|
+
// The commit cycle bailed at the preflight — no commit landed.
|
|
528
|
+
expect(gitLogOneline(dir)).toHaveLength(1); // only the seed
|
|
529
|
+
});
|
|
507
530
|
});
|
|
508
531
|
|
|
509
532
|
// ---------------------------------------------------------------------------
|
package/src/export-watch.ts
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* detection. See `parachute-patterns/cookbook/vault-portable-export.md`.
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
+
import { ensureGitAvailable } from "./git-preflight.ts";
|
|
17
|
+
|
|
16
18
|
// ---------------------------------------------------------------------------
|
|
17
19
|
// Commit message templating
|
|
18
20
|
// ---------------------------------------------------------------------------
|
|
@@ -269,11 +271,23 @@ export async function runGitCommitCycle(opts: {
|
|
|
269
271
|
push: boolean;
|
|
270
272
|
/** Override for tests — defaults to `new Date().toISOString()`. */
|
|
271
273
|
now?: () => string;
|
|
274
|
+
/**
|
|
275
|
+
* Override the git-presence probe (test seam — defaults to `Bun.which`).
|
|
276
|
+
* Inject a fn returning `null` to exercise the git-not-installed path.
|
|
277
|
+
*/
|
|
278
|
+
which?: (cmd: string) => string | null;
|
|
272
279
|
}): Promise<{
|
|
273
280
|
committed: boolean;
|
|
274
281
|
message?: string;
|
|
275
282
|
push?: { attempted: true; ok: boolean; error?: string };
|
|
276
283
|
}> {
|
|
284
|
+
// Preflight: every step below shells `git`. On a git-less server the first
|
|
285
|
+
// `Bun.spawn(["git", ...])` would throw a raw "Executable not found" error;
|
|
286
|
+
// surface the friendly, actionable GitNotInstalledError so callers can
|
|
287
|
+
// thread it into mirror status (`last_error`) instead of crashing the
|
|
288
|
+
// watch loop with an opaque message.
|
|
289
|
+
ensureGitAvailable(opts.which);
|
|
290
|
+
|
|
277
291
|
const now = opts.now ?? (() => new Date().toISOString());
|
|
278
292
|
|
|
279
293
|
const add = await gitAddAll(opts.repoDir);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the shared git-availability preflight (vault#415).
|
|
3
|
+
*
|
|
4
|
+
* Found live: importing a repo on a git-less Amazon Linux EC2 box failed
|
|
5
|
+
* with a raw `Executable not found in $PATH: "git"` 500. The preflight gives
|
|
6
|
+
* every git entry point a fast, friendly, actionable failure instead.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, test, expect } from "bun:test";
|
|
10
|
+
import {
|
|
11
|
+
GitNotInstalledError,
|
|
12
|
+
ensureGitAvailable,
|
|
13
|
+
isGitNotFoundSpawnError,
|
|
14
|
+
} from "./git-preflight.ts";
|
|
15
|
+
|
|
16
|
+
describe("ensureGitAvailable", () => {
|
|
17
|
+
test("throws GitNotInstalledError when which returns null", () => {
|
|
18
|
+
expect(() => ensureGitAvailable(() => null)).toThrow(GitNotInstalledError);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("does not throw when which resolves git", () => {
|
|
22
|
+
expect(() => ensureGitAvailable(() => "/usr/bin/git")).not.toThrow();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("defaults to Bun.which (git is present in this test env)", () => {
|
|
26
|
+
// The test host has git; the default-arg path resolves it cleanly.
|
|
27
|
+
expect(() => ensureGitAvailable()).not.toThrow();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("GitNotInstalledError message", () => {
|
|
32
|
+
test("is OS-agnostic-but-helpful — names dnf, apt-get, and brew", () => {
|
|
33
|
+
const msg = new GitNotInstalledError().message;
|
|
34
|
+
expect(msg).toContain("git is required for this operation");
|
|
35
|
+
expect(msg).toContain("sudo dnf install git");
|
|
36
|
+
expect(msg).toContain("sudo apt-get install -y git");
|
|
37
|
+
expect(msg).toContain("brew install git");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("carries the GitNotInstalledError name (instanceof + name both work)", () => {
|
|
41
|
+
const err = new GitNotInstalledError();
|
|
42
|
+
expect(err).toBeInstanceOf(GitNotInstalledError);
|
|
43
|
+
expect(err.name).toBe("GitNotInstalledError");
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("isGitNotFoundSpawnError", () => {
|
|
48
|
+
test("matches Bun's executable-not-found message for git", () => {
|
|
49
|
+
expect(
|
|
50
|
+
isGitNotFoundSpawnError(
|
|
51
|
+
new Error('Executable not found in $PATH: "git"'),
|
|
52
|
+
),
|
|
53
|
+
).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("matches an ENOENT spawn error mentioning git", () => {
|
|
57
|
+
const err = new Error("spawn git ENOENT") as Error & { code?: string };
|
|
58
|
+
err.code = "ENOENT";
|
|
59
|
+
expect(isGitNotFoundSpawnError(err)).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("does not match an unrelated error", () => {
|
|
63
|
+
expect(isGitNotFoundSpawnError(new Error("network unreachable"))).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("does not match a non-Error value", () => {
|
|
67
|
+
expect(isGitNotFoundSpawnError("git missing")).toBe(false);
|
|
68
|
+
expect(isGitNotFoundSpawnError(null)).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared git-availability preflight.
|
|
3
|
+
*
|
|
4
|
+
* Every git-using entry point in vault (mirror import, mirror sync/commit/
|
|
5
|
+
* push, internal-mirror bootstrap) shells out to the `git` binary via
|
|
6
|
+
* `Bun.spawn(["git", ...])`. On a server where `git` isn't installed (a
|
|
7
|
+
* fresh Amazon Linux / minimal Docker image, etc.) Bun throws a raw
|
|
8
|
+
* `Executable not found in $PATH: "git"` error, which the import route only
|
|
9
|
+
* caught in its generic `internal` 500 branch — surfacing an unhelpful,
|
|
10
|
+
* un-actionable error to the operator.
|
|
11
|
+
*
|
|
12
|
+
* This module centralizes the preflight so every git entry point fails
|
|
13
|
+
* fast with a clear, actionable message that tells the operator HOW to
|
|
14
|
+
* fix it (install git via their distro's package manager).
|
|
15
|
+
*
|
|
16
|
+
* Found live on the gitcoin-parachute EC2 deploy (Amazon Linux, no git).
|
|
17
|
+
* See vault#415-era fix.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Thrown when `git` is required for an operation but isn't on PATH. Carries
|
|
22
|
+
* an OS-agnostic-but-helpful message with the common install commands so the
|
|
23
|
+
* operator can act without leaving the error surface.
|
|
24
|
+
*/
|
|
25
|
+
export class GitNotInstalledError extends Error {
|
|
26
|
+
constructor() {
|
|
27
|
+
super(
|
|
28
|
+
"git is required for this operation but was not found on the server. " +
|
|
29
|
+
"Install git and retry — e.g. `sudo dnf install git` (Amazon Linux / Fedora), " +
|
|
30
|
+
"`sudo apt-get install -y git` (Debian / Ubuntu), or `brew install git` (macOS).",
|
|
31
|
+
);
|
|
32
|
+
this.name = "GitNotInstalledError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Throw `GitNotInstalledError` if `git` isn't resolvable on PATH.
|
|
38
|
+
*
|
|
39
|
+
* `which` is a TEST SEAM (default `Bun.which`) so tests can force the
|
|
40
|
+
* git-missing branch without uninstalling git from the test host. Production
|
|
41
|
+
* callers pass nothing and get the real `Bun.which`.
|
|
42
|
+
*/
|
|
43
|
+
export function ensureGitAvailable(
|
|
44
|
+
which: (cmd: string) => string | null = Bun.which,
|
|
45
|
+
): void {
|
|
46
|
+
if (which("git") === null) {
|
|
47
|
+
throw new GitNotInstalledError();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Heuristic: does this error look like the "git executable not found" failure
|
|
53
|
+
* Bun throws when it can't resolve the binary? Used as a belt-and-suspenders
|
|
54
|
+
* catch around `Bun.spawn(["git", ...])` so a spawn that slips past the
|
|
55
|
+
* preflight (race where git is removed between check and spawn, or a code path
|
|
56
|
+
* that didn't preflight) still surfaces the friendly error instead of the raw
|
|
57
|
+
* `Executable not found in $PATH: "git"` string.
|
|
58
|
+
*/
|
|
59
|
+
export function isGitNotFoundSpawnError(err: unknown): boolean {
|
|
60
|
+
if (!(err instanceof Error)) return false;
|
|
61
|
+
const msg = err.message ?? "";
|
|
62
|
+
// Bun: `Executable not found in $PATH: "git"`.
|
|
63
|
+
// Node/posix: ENOENT spawn errors mention the missing file.
|
|
64
|
+
return (
|
|
65
|
+
(msg.includes("Executable not found") && msg.includes("git")) ||
|
|
66
|
+
((err as { code?: string }).code === "ENOENT" && msg.includes("git"))
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
validateExternalPath,
|
|
24
24
|
validateMirrorConfigShape,
|
|
25
25
|
} from "./mirror-config.ts";
|
|
26
|
+
import { GitNotInstalledError } from "./git-preflight.ts";
|
|
26
27
|
|
|
27
28
|
function tmp(prefix: string): string {
|
|
28
29
|
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
@@ -496,6 +497,19 @@ describe("validateExternalPath", () => {
|
|
|
496
497
|
expect(r.ok).toBe(true);
|
|
497
498
|
if (r.ok) expect(r.resolved_path).toBe(dir);
|
|
498
499
|
});
|
|
500
|
+
|
|
501
|
+
test("git not installed → throws GitNotInstalledError (route maps to 503)", async () => {
|
|
502
|
+
// vault#415 nit — the isGitRepo() check shells `git`. On a git-less
|
|
503
|
+
// server, throw the friendly error (handleMirrorPut maps it to 503
|
|
504
|
+
// git_not_installed) instead of a raw "Executable not found" crash.
|
|
505
|
+
// Force the preflight via the `which` seam; a real, valid git repo is
|
|
506
|
+
// used so the ONLY failure source is the preflight.
|
|
507
|
+
dir = tmp("mirror-validate-nogit-installed-");
|
|
508
|
+
initRepo(dir);
|
|
509
|
+
await expect(validateExternalPath(dir, () => null)).rejects.toBeInstanceOf(
|
|
510
|
+
GitNotInstalledError,
|
|
511
|
+
);
|
|
512
|
+
});
|
|
499
513
|
});
|
|
500
514
|
|
|
501
515
|
// ---------------------------------------------------------------------------
|
package/src/mirror-config.ts
CHANGED
|
@@ -45,6 +45,7 @@ import { homedir } from "os";
|
|
|
45
45
|
|
|
46
46
|
import { DEFAULT_COMMIT_TEMPLATE, isGitRepo } from "./export-watch.ts";
|
|
47
47
|
import { readCredentials, type MirrorCredentials } from "./mirror-credentials.ts";
|
|
48
|
+
import { ensureGitAvailable } from "./git-preflight.ts";
|
|
48
49
|
|
|
49
50
|
// ---------------------------------------------------------------------------
|
|
50
51
|
// Types
|
|
@@ -888,7 +889,17 @@ export type PathValidation = PathValidationOk | PathValidationError;
|
|
|
888
889
|
*/
|
|
889
890
|
export async function validateExternalPath(
|
|
890
891
|
externalPath: string,
|
|
892
|
+
// Test seam for the git-presence preflight (default `Bun.which`). Inject a
|
|
893
|
+
// fn returning `null` to exercise the git-not-installed path.
|
|
894
|
+
which?: (cmd: string) => string | null,
|
|
891
895
|
): Promise<PathValidation> {
|
|
896
|
+
// Preflight: the git-repo check below shells `git`. On a git-less server,
|
|
897
|
+
// throw the friendly, actionable GitNotInstalledError (which handleMirrorPut
|
|
898
|
+
// maps to a 503 `git_not_installed`, consistent with the import route)
|
|
899
|
+
// instead of letting `isGitRepo`'s `Bun.spawn` throw a raw
|
|
900
|
+
// "Executable not found in $PATH: \"git\"".
|
|
901
|
+
ensureGitAvailable(which);
|
|
902
|
+
|
|
892
903
|
if (!existsSync(externalPath)) {
|
|
893
904
|
return {
|
|
894
905
|
ok: false,
|
|
@@ -30,8 +30,10 @@ import {
|
|
|
30
30
|
_resetImportInFlightForTest,
|
|
31
31
|
authedCloneUrl,
|
|
32
32
|
cloneAndImport,
|
|
33
|
+
defaultGitSpawn,
|
|
33
34
|
type GitSpawn,
|
|
34
35
|
} from "./mirror-import.ts";
|
|
36
|
+
import { GitNotInstalledError } from "./git-preflight.ts";
|
|
35
37
|
import {
|
|
36
38
|
emptyCredentials,
|
|
37
39
|
mirrorCredentialsPath,
|
|
@@ -295,6 +297,10 @@ describe("cloneAndImport — success", () => {
|
|
|
295
297
|
expect(result.notes_imported).toBe(2); // alpha + beta
|
|
296
298
|
expect(result.notes_deleted).toBeUndefined();
|
|
297
299
|
expect(result.warnings).toEqual([]);
|
|
300
|
+
// vault#416: cloneAndImport stays focused on content — sync-enabling is
|
|
301
|
+
// the route's job. The worker always returns sync_enabled: false.
|
|
302
|
+
expect(result.sync_enabled).toBe(false);
|
|
303
|
+
expect(result.sync_warning).toBeUndefined();
|
|
298
304
|
|
|
299
305
|
const restored = await store.getNote("n-alpha");
|
|
300
306
|
expect(restored).toBeTruthy();
|
|
@@ -548,3 +554,107 @@ describe("cloneAndImport — failures", () => {
|
|
|
548
554
|
rmSync(notAnExport, { recursive: true, force: true });
|
|
549
555
|
});
|
|
550
556
|
});
|
|
557
|
+
|
|
558
|
+
// ---------------------------------------------------------------------------
|
|
559
|
+
// cloneAndImport — git not installed (vault#415 — live bug on a git-less EC2)
|
|
560
|
+
// ---------------------------------------------------------------------------
|
|
561
|
+
|
|
562
|
+
describe("cloneAndImport — git not installed", () => {
|
|
563
|
+
let assetsDir: string;
|
|
564
|
+
let store: SqliteStore;
|
|
565
|
+
|
|
566
|
+
beforeEach(() => {
|
|
567
|
+
assetsDir = tmp("import-assets-nogit-");
|
|
568
|
+
store = new SqliteStore(new Database(":memory:"));
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
afterEach(() => {
|
|
572
|
+
if (assetsDir) rmSync(assetsDir, { recursive: true, force: true });
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
test("git missing → GitNotInstalledError, fails fast (no spawn, no tempdir)", async () => {
|
|
576
|
+
const workDirRoot = tmp("import-workroot-nogit-");
|
|
577
|
+
let spawnCalled = false;
|
|
578
|
+
const spyingSpawn: GitSpawn = async () => {
|
|
579
|
+
spawnCalled = true;
|
|
580
|
+
return { exitCode: 0, stderr: "", timedOut: false };
|
|
581
|
+
};
|
|
582
|
+
await expect(
|
|
583
|
+
cloneAndImport({
|
|
584
|
+
vaultName: "default",
|
|
585
|
+
remoteUrl: "https://github.com/owner/repo.git",
|
|
586
|
+
auth: { kind: "none" },
|
|
587
|
+
mode: "merge",
|
|
588
|
+
store,
|
|
589
|
+
assetsDir,
|
|
590
|
+
spawn: spyingSpawn,
|
|
591
|
+
workDirRoot,
|
|
592
|
+
// Force the preflight to see no git on PATH.
|
|
593
|
+
which: () => null,
|
|
594
|
+
}),
|
|
595
|
+
).rejects.toBeInstanceOf(GitNotInstalledError);
|
|
596
|
+
|
|
597
|
+
// Fails fast: the spawner is never reached and no tempdir is created.
|
|
598
|
+
expect(spawnCalled).toBe(false);
|
|
599
|
+
const { readdirSync } = await import("node:fs");
|
|
600
|
+
const entries = readdirSync(workDirRoot);
|
|
601
|
+
expect(entries.filter((e) => e.startsWith("parachute-import-"))).toEqual([]);
|
|
602
|
+
rmSync(workDirRoot, { recursive: true, force: true });
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test("git missing → does not lay an in-flight marker (clean retry after install)", async () => {
|
|
606
|
+
await expect(
|
|
607
|
+
cloneAndImport({
|
|
608
|
+
vaultName: "default",
|
|
609
|
+
remoteUrl: "https://github.com/owner/repo.git",
|
|
610
|
+
auth: { kind: "none" },
|
|
611
|
+
mode: "merge",
|
|
612
|
+
store,
|
|
613
|
+
assetsDir,
|
|
614
|
+
spawn: async () => ({ exitCode: 0, stderr: "", timedOut: false }),
|
|
615
|
+
which: () => null,
|
|
616
|
+
}),
|
|
617
|
+
).rejects.toBeInstanceOf(GitNotInstalledError);
|
|
618
|
+
// The preflight runs BEFORE the concurrency marker, so a failed call
|
|
619
|
+
// leaves no stale in-flight entry blocking the post-install retry.
|
|
620
|
+
expect(_isImportInFlight("default")).toBe(false);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test("git present (injected which) → normal import path still works", async () => {
|
|
624
|
+
const fixtureDir = await buildExportFixture();
|
|
625
|
+
try {
|
|
626
|
+
const result = await cloneAndImport({
|
|
627
|
+
vaultName: "default",
|
|
628
|
+
remoteUrl: "https://github.com/owner/repo.git",
|
|
629
|
+
auth: { kind: "none" },
|
|
630
|
+
mode: "merge",
|
|
631
|
+
store,
|
|
632
|
+
assetsDir,
|
|
633
|
+
spawn: spawnCloneSuccess(fixtureDir),
|
|
634
|
+
which: () => "/usr/bin/git",
|
|
635
|
+
});
|
|
636
|
+
expect(result.notes_imported).toBe(2);
|
|
637
|
+
} finally {
|
|
638
|
+
rmSync(fixtureDir, { recursive: true, force: true });
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
test("GitNotInstalledError message is actionable (names install commands)", () => {
|
|
643
|
+
const msg = new GitNotInstalledError().message;
|
|
644
|
+
expect(msg).toContain("git is required");
|
|
645
|
+
expect(msg).toContain("dnf install git");
|
|
646
|
+
expect(msg).toContain("apt-get install");
|
|
647
|
+
expect(msg).toContain("brew install git");
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
test("defaultGitSpawn rethrows a git-not-found spawn error as GitNotInstalledError", async () => {
|
|
651
|
+
// Belt-and-suspenders: spawn a non-existent binary to trigger Bun's
|
|
652
|
+
// "Executable not found" throw, and confirm the friendly rethrow.
|
|
653
|
+
// (We can't uninstall git, so spawn an impossible command name.)
|
|
654
|
+
await expect(
|
|
655
|
+
defaultGitSpawn(["definitely-not-a-real-binary-xyzzy-git"], {
|
|
656
|
+
timeoutMs: 5_000,
|
|
657
|
+
}),
|
|
658
|
+
).rejects.toBeInstanceOf(GitNotInstalledError);
|
|
659
|
+
});
|
|
660
|
+
});
|
package/src/mirror-import.ts
CHANGED
|
@@ -73,6 +73,11 @@ import {
|
|
|
73
73
|
} from "../core/src/portable-md.ts";
|
|
74
74
|
import type { SqliteStore } from "../core/src/store.ts";
|
|
75
75
|
import { redactRemoteUrl, readCredentials } from "./mirror-credentials.ts";
|
|
76
|
+
import {
|
|
77
|
+
GitNotInstalledError,
|
|
78
|
+
ensureGitAvailable,
|
|
79
|
+
isGitNotFoundSpawnError,
|
|
80
|
+
} from "./git-preflight.ts";
|
|
76
81
|
|
|
77
82
|
// ---------------------------------------------------------------------------
|
|
78
83
|
// Types
|
|
@@ -125,6 +130,11 @@ export interface ImportOpts {
|
|
|
125
130
|
importer?: typeof importPortableVault;
|
|
126
131
|
/** Override the clone timeout (default 60s; test seam to shorten). */
|
|
127
132
|
cloneTimeoutMs?: number;
|
|
133
|
+
/**
|
|
134
|
+
* Override the git-presence probe (test seam — defaults to `Bun.which`).
|
|
135
|
+
* Inject a fn returning `null` to exercise the git-not-installed path.
|
|
136
|
+
*/
|
|
137
|
+
which?: (cmd: string) => string | null;
|
|
128
138
|
}
|
|
129
139
|
|
|
130
140
|
/**
|
|
@@ -144,6 +154,29 @@ export interface ImportResult {
|
|
|
144
154
|
* etc.). The HTTP handler returns these so the operator can audit.
|
|
145
155
|
*/
|
|
146
156
|
warnings: string[];
|
|
157
|
+
/**
|
|
158
|
+
* vault#416 — whether sync (mirror push-back to the imported repo) ended
|
|
159
|
+
* up enabled as part of this import. Default-on UX: the import request
|
|
160
|
+
* carries `enable_sync` (default true), and the route turns the imported
|
|
161
|
+
* repo into a configured, credential-backed, auto-pushing mirror after a
|
|
162
|
+
* successful import. `true` when sync is now wired (or was already wired
|
|
163
|
+
* to this same remote); `false` when sync was opted out, couldn't be
|
|
164
|
+
* enabled (no push-capable credentials), was skipped to avoid clobbering
|
|
165
|
+
* a different existing mirror, or threw during setup (import already
|
|
166
|
+
* succeeded — never lost to a sync error).
|
|
167
|
+
*
|
|
168
|
+
* `cloneAndImport` itself never sets these — the field is populated by the
|
|
169
|
+
* route (`handleMirrorImport`) after a successful import. `importResultFromStats`
|
|
170
|
+
* defaults `sync_enabled` to false; the route overwrites it.
|
|
171
|
+
*/
|
|
172
|
+
sync_enabled: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Human-readable reason sync wasn't enabled (no creds / conflicting
|
|
175
|
+
* existing mirror / setup error). Only set when `sync_enabled` is false
|
|
176
|
+
* AND the caller asked for sync (`enable_sync !== false`). Absent when
|
|
177
|
+
* sync succeeded or the operator opted out.
|
|
178
|
+
*/
|
|
179
|
+
sync_warning?: string;
|
|
147
180
|
}
|
|
148
181
|
|
|
149
182
|
/**
|
|
@@ -315,19 +348,32 @@ export function authedCloneUrl(
|
|
|
315
348
|
* exit code + stderr text + timeout flag.
|
|
316
349
|
*/
|
|
317
350
|
export const defaultGitSpawn: GitSpawn = async (argv, options) => {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
351
|
+
let proc;
|
|
352
|
+
try {
|
|
353
|
+
proc = Bun.spawn(argv, {
|
|
354
|
+
cwd: options.cwd,
|
|
355
|
+
stdout: "pipe",
|
|
356
|
+
stderr: "pipe",
|
|
357
|
+
env: {
|
|
358
|
+
...process.env,
|
|
359
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
360
|
+
// Kill any system credential helper from intercepting — we want
|
|
361
|
+
// the clone to use ONLY the URL-embedded credential, not whatever's
|
|
362
|
+
// in keychain. Same shape as the ls-remote probe.
|
|
363
|
+
GIT_ASKPASS: "/bin/echo",
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
} catch (err) {
|
|
367
|
+
// Belt-and-suspenders: `cloneAndImport` preflights via
|
|
368
|
+
// `ensureGitAvailable`, but if a git-missing spawn still slips through
|
|
369
|
+
// (race, or a future caller that skipped the preflight) rethrow it as
|
|
370
|
+
// the friendly error rather than leaking Bun's raw
|
|
371
|
+
// `Executable not found in $PATH: "git"`.
|
|
372
|
+
if (isGitNotFoundSpawnError(err)) {
|
|
373
|
+
throw new GitNotInstalledError();
|
|
374
|
+
}
|
|
375
|
+
throw err;
|
|
376
|
+
}
|
|
331
377
|
let timedOut = false;
|
|
332
378
|
const timer = setTimeout(() => {
|
|
333
379
|
timedOut = true;
|
|
@@ -365,6 +411,14 @@ export const defaultGitSpawn: GitSpawn = async (argv, options) => {
|
|
|
365
411
|
* Always cleans up the temp dir.
|
|
366
412
|
*/
|
|
367
413
|
export async function cloneAndImport(opts: ImportOpts): Promise<ImportResult> {
|
|
414
|
+
// Fail fast + clean when git isn't installed — BEFORE the concurrency
|
|
415
|
+
// marker, tempdir creation, or any spawn. The route maps the resulting
|
|
416
|
+
// GitNotInstalledError to a friendly 503 (git_not_installed). Without
|
|
417
|
+
// this, the first `Bun.spawn(["git", ...])` threw a raw
|
|
418
|
+
// `Executable not found in $PATH: "git"` that only the generic 500 branch
|
|
419
|
+
// caught — the unhelpful failure mode found live on the gitcoin EC2 box.
|
|
420
|
+
ensureGitAvailable(opts.which);
|
|
421
|
+
|
|
368
422
|
if (inFlight.has(opts.vaultName)) {
|
|
369
423
|
throw new ImportConflictError(opts.vaultName);
|
|
370
424
|
}
|
|
@@ -469,6 +523,10 @@ function importResultFromStats(
|
|
|
469
523
|
tags_imported: stats.schemas_restored,
|
|
470
524
|
attachments_imported: stats.attachments_restored,
|
|
471
525
|
warnings,
|
|
526
|
+
// Default false; the route flips it true after wiring sync. Keeping the
|
|
527
|
+
// default here means a caller that bypasses the route (CLI, tests of
|
|
528
|
+
// cloneAndImport directly) gets a well-typed, conservative result.
|
|
529
|
+
sync_enabled: false,
|
|
472
530
|
};
|
|
473
531
|
if (mode === "replace") {
|
|
474
532
|
result.notes_deleted = stats.notes_wiped;
|
|
@@ -215,6 +215,21 @@ describe("bootstrapInternalMirror", () => {
|
|
|
215
215
|
expect(isGitRepoSync(dir)).toBe(true);
|
|
216
216
|
}
|
|
217
217
|
});
|
|
218
|
+
|
|
219
|
+
test("git missing → ok:false with actionable error (status surfaces it, no raw crash)", async () => {
|
|
220
|
+
// vault#415 — a git-less server can't bootstrap a mirror. The error
|
|
221
|
+
// channel carries the friendly message that MirrorManager.start threads
|
|
222
|
+
// into status.last_error, instead of a raw "Executable not found" crash.
|
|
223
|
+
dir = path.join(tmp("mirror-boot-nogit-"), "mirror");
|
|
224
|
+
const r = await bootstrapInternalMirror(dir, () => null);
|
|
225
|
+
expect(r.ok).toBe(false);
|
|
226
|
+
if (!r.ok) {
|
|
227
|
+
expect(r.error).toContain("git is required");
|
|
228
|
+
expect(r.error).toContain("dnf install git");
|
|
229
|
+
}
|
|
230
|
+
// Failed fast at the preflight — the dir was never created.
|
|
231
|
+
expect(fs.existsSync(dir)).toBe(false);
|
|
232
|
+
});
|
|
218
233
|
});
|
|
219
234
|
|
|
220
235
|
// ---------------------------------------------------------------------------
|
|
@@ -378,6 +393,42 @@ describe("MirrorManager.start — lifecycle matrix", () => {
|
|
|
378
393
|
fs.rmSync(external, { recursive: true, force: true });
|
|
379
394
|
});
|
|
380
395
|
|
|
396
|
+
test("external + git not installed → enabled:false with friendly error, never spawns/exports", async () => {
|
|
397
|
+
// vault#415 nit — the external branch's isGitRepo() check shells `git`
|
|
398
|
+
// with no preflight; on a git-less server it would throw a raw
|
|
399
|
+
// "Executable not found in $PATH: \"git\"" and crash start(). The
|
|
400
|
+
// top-of-start() preflight (forced via the `which` seam) lands the
|
|
401
|
+
// friendly, actionable message in last_error for the external location.
|
|
402
|
+
home = tmp("mgr-ext-nogit-installed-");
|
|
403
|
+
fs.mkdirSync(path.join(home, "vault", "data", "default"), { recursive: true });
|
|
404
|
+
// Use a real, valid external git repo so the ONLY thing that can fail is
|
|
405
|
+
// the git-presence preflight — proving the preflight (not the path/repo
|
|
406
|
+
// checks) produced the disabled state.
|
|
407
|
+
const external = tmp("mgr-ext-nogit-target-");
|
|
408
|
+
initRepo(external);
|
|
409
|
+
seedCommit(external);
|
|
410
|
+
const deps = makeFakeDeps({
|
|
411
|
+
parachuteHome: home,
|
|
412
|
+
initialConfig: {
|
|
413
|
+
...defaultMirrorConfig(),
|
|
414
|
+
enabled: true,
|
|
415
|
+
location: "external",
|
|
416
|
+
external_path: external,
|
|
417
|
+
sync_mode: "events",
|
|
418
|
+
},
|
|
419
|
+
});
|
|
420
|
+
const mgr = new MirrorManager(deps);
|
|
421
|
+
// Force the preflight to see no git on PATH.
|
|
422
|
+
const status = await mgr.start(() => null);
|
|
423
|
+
expect(status.enabled).toBe(false);
|
|
424
|
+
expect(status.last_error).toContain("git is required");
|
|
425
|
+
expect(status.last_error).toContain("dnf install git");
|
|
426
|
+
// Never reached export — the preflight bailed before any git work.
|
|
427
|
+
expect(deps.exportCalls).toHaveLength(0);
|
|
428
|
+
await mgr.stop();
|
|
429
|
+
fs.rmSync(external, { recursive: true, force: true });
|
|
430
|
+
});
|
|
431
|
+
|
|
381
432
|
test("internal bootstrap refuses to clobber pre-existing non-git data", async () => {
|
|
382
433
|
home = tmp("mgr-int-clobber-");
|
|
383
434
|
const mirrorPath = path.join(home, "vault", "data", "default", "mirror");
|