[codex] structure source-control repository failures (#3336)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
6ff6c13fc8
commit
a26f0dc89f
2 files changed
+98
-45
No files matched your search
@@ -1,11 +1,13 @@
|
||||
import * as NodePath from "@effect/platform-node/NodePath";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import { GitCommandError, type SourceControlProviderError } from "@t3tools/contracts";
|
||||
import { GitCommandError, SourceControlProviderError } from "@t3tools/contracts";
|
||||
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
|
||||
@@ -54,8 +56,9 @@ function processOutput(): GitVcsDriver.ExecuteGitResult {
|
||||
function makeLayer(input: {
|
||||
readonly provider?: SourceControlProvider.SourceControlProvider["Service"];
|
||||
readonly git?: Partial<GitVcsDriver.GitVcsDriver["Service"]>;
|
||||
readonly fileSystem?: FileSystem.FileSystem;
|
||||
}) {
|
||||
return SourceControlRepositoryService.layer.pipe(
|
||||
const serviceLayer = SourceControlRepositoryService.layer.pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(SourceControlProviderRegistry.SourceControlProviderRegistry)({
|
||||
get: () => Effect.succeed(input.provider ?? makeProvider()),
|
||||
@@ -75,9 +78,20 @@ function makeLayer(input: {
|
||||
...input.git,
|
||||
}),
|
||||
),
|
||||
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-repos-" })),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(
|
||||
ServerConfig.layerTest(
|
||||
process.cwd(),
|
||||
input.fileSystem ? "/tmp/t3-source-control-repos" : { prefix: "t3-source-control-repos-" },
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return input.fileSystem
|
||||
? serviceLayer.pipe(
|
||||
Layer.provide(Layer.succeed(FileSystem.FileSystem, input.fileSystem)),
|
||||
Layer.provideMerge(NodePath.layer),
|
||||
)
|
||||
: serviceLayer.pipe(Layer.provideMerge(NodeServices.layer));
|
||||
}
|
||||
|
||||
it.effect("looks up repositories through the requested provider without search", () => {
|
||||
@@ -103,6 +117,39 @@ it.effect("looks up repositories through the requested provider without search",
|
||||
}).pipe(Effect.provide(makeLayer({ provider })));
|
||||
});
|
||||
|
||||
it.effect("preserves provider failures without deriving the repository message from them", () => {
|
||||
const providerCause = new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "getRepositoryCloneUrls",
|
||||
cwd: "/workspace",
|
||||
repository: "octocat/t3code",
|
||||
detail: "credential token abc123 was rejected",
|
||||
});
|
||||
const provider = makeProvider({
|
||||
getRepositoryCloneUrls: () => Effect.fail(providerCause),
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
|
||||
const error = yield* Effect.flip(
|
||||
service.lookupRepository({
|
||||
provider: "github",
|
||||
repository: "octocat/t3code",
|
||||
cwd: "/workspace",
|
||||
}),
|
||||
);
|
||||
|
||||
assert.strictEqual(error.provider, "github");
|
||||
assert.strictEqual(error.operation, "lookupRepository");
|
||||
assert.strictEqual(error.detail, "The source control operation could not be completed.");
|
||||
assert.strictEqual(
|
||||
error.message,
|
||||
"Source control repository operation lookupRepository failed for github: The source control operation could not be completed.",
|
||||
);
|
||||
assert.strictEqual(error.cause, providerCause);
|
||||
}).pipe(Effect.provide(makeLayer({ provider })));
|
||||
});
|
||||
|
||||
it.effect("clones a looked-up repository into the requested destination", () =>
|
||||
Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
@@ -148,6 +195,38 @@ it.effect("clones a looked-up repository into the requested destination", () =>
|
||||
}).pipe(Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("preserves destination probe failures instead of treating them as missing paths", () => {
|
||||
const fileSystemCause = PlatformError.systemError({
|
||||
_tag: "PermissionDenied",
|
||||
module: "FileSystem",
|
||||
method: "exists",
|
||||
pathOrDescriptor: "/restricted/t3code",
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
|
||||
const error = yield* Effect.flip(
|
||||
service.cloneRepository({
|
||||
remoteUrl: CLONE_URLS.sshUrl,
|
||||
destinationPath: "/restricted/t3code",
|
||||
}),
|
||||
);
|
||||
|
||||
assert.strictEqual(error.provider, "unknown");
|
||||
assert.strictEqual(error.operation, "cloneRepository");
|
||||
assert.strictEqual(error.cause, fileSystemCause);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
makeLayer({
|
||||
fileSystem: FileSystem.makeNoop({
|
||||
exists: () => Effect.fail(fileSystemCause),
|
||||
makeDirectory: () => Effect.void,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("publishes by creating the repository, adding a remote, and pushing upstream", () => {
|
||||
const createCalls: Array<{ cwd: string; repository: string; visibility: string }> = [];
|
||||
const remoteCalls: Array<{ cwd: string; preferredName: string; url: string }> = [];
|
||||
|
||||
@@ -39,41 +39,14 @@ export class SourceControlRepositoryService extends Context.Service<
|
||||
}
|
||||
>()("t3/sourceControl/SourceControlRepositoryService") {}
|
||||
|
||||
function detailFromUnknown(cause: unknown): string {
|
||||
if (typeof cause === "object" && cause !== null) {
|
||||
if ("detail" in cause && typeof cause.detail === "string" && cause.detail.length > 0) {
|
||||
return cause.detail;
|
||||
}
|
||||
if ("message" in cause && typeof cause.message === "string" && cause.message.length > 0) {
|
||||
return cause.message;
|
||||
}
|
||||
}
|
||||
|
||||
return "An unexpected source control error occurred.";
|
||||
}
|
||||
|
||||
function repositoryError(input: {
|
||||
readonly operation: string;
|
||||
readonly provider: SourceControlProviderKind;
|
||||
readonly detail: string;
|
||||
readonly cause?: unknown;
|
||||
}): SourceControlRepositoryError {
|
||||
return new SourceControlRepositoryError({
|
||||
provider: input.provider,
|
||||
operation: input.operation,
|
||||
detail: input.detail,
|
||||
...(input.cause === undefined ? {} : { cause: input.cause }),
|
||||
});
|
||||
}
|
||||
|
||||
function mapRepositoryError(operation: string, provider: SourceControlProviderKind) {
|
||||
return Effect.mapError((cause: unknown) =>
|
||||
isSourceControlRepositoryError(cause)
|
||||
? cause
|
||||
: repositoryError({
|
||||
: new SourceControlRepositoryError({
|
||||
operation,
|
||||
provider,
|
||||
detail: detailFromUnknown(cause),
|
||||
detail: "The source control operation could not be completed.",
|
||||
cause,
|
||||
}),
|
||||
);
|
||||
@@ -130,7 +103,7 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
|
||||
return Effect.fail(
|
||||
repositoryError({
|
||||
new SourceControlRepositoryError({
|
||||
operation: input.operation,
|
||||
provider: input.provider,
|
||||
detail: "Choose a source control provider before continuing.",
|
||||
@@ -157,7 +130,7 @@ export const make = Effect.gen(function* () {
|
||||
function* (destinationPath: string) {
|
||||
const trimmed = destinationPath.trim();
|
||||
if (trimmed.length === 0) {
|
||||
return yield* repositoryError({
|
||||
return yield* new SourceControlRepositoryError({
|
||||
operation: "cloneRepository",
|
||||
provider: "unknown",
|
||||
detail: "Choose a destination path before cloning.",
|
||||
@@ -171,21 +144,22 @@ export const make = Effect.gen(function* () {
|
||||
const prepareDestination = Effect.fn("SourceControlRepositoryService.prepareDestination")(
|
||||
function* (destinationPath: string) {
|
||||
const normalizedDestination = yield* normalizeDestinationPath(destinationPath);
|
||||
if (yield* fileSystem.exists(normalizedDestination).pipe(Effect.orElseSucceed(() => false))) {
|
||||
if (yield* fileSystem.exists(normalizedDestination)) {
|
||||
const entries = yield* fileSystem
|
||||
.readDirectory(normalizedDestination, { recursive: false })
|
||||
.pipe(
|
||||
Effect.mapError((cause) =>
|
||||
repositoryError({
|
||||
operation: "cloneRepository",
|
||||
provider: "unknown",
|
||||
detail: "Destination path already exists and is not a directory.",
|
||||
cause,
|
||||
}),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SourceControlRepositoryError({
|
||||
operation: "cloneRepository",
|
||||
provider: "unknown",
|
||||
detail: "Destination path already exists and is not a directory.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (entries.length > 0) {
|
||||
return yield* repositoryError({
|
||||
return yield* new SourceControlRepositoryError({
|
||||
operation: "cloneRepository",
|
||||
provider: "unknown",
|
||||
detail: "Destination path already exists and is not empty.",
|
||||
@@ -222,7 +196,7 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
|
||||
if (!remoteUrl) {
|
||||
return yield* repositoryError({
|
||||
return yield* new SourceControlRepositoryError({
|
||||
operation: "cloneRepository",
|
||||
provider,
|
||||
detail: "Enter a repository path or clone URL before cloning.",
|
||||
|
||||
Reference in new issue
Block a user