Add pluggable source control routing
- Route git workflows through source control providers - Replace git status broadcaster with VCS status service - Update orchestration, server, and web contracts for provider-aware git
This commit is contained in:
1 parent
b449d78038
commit
f473d20593
40 files changed
+1650
-688
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, Layer } from "effect";
|
||||
|
||||
import { GitHubCli, type GitHubCliShape } from "../git/Services/GitHubCli.ts";
|
||||
import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
|
||||
|
||||
const processResult = (stdout: string) => ({
|
||||
stdout,
|
||||
stderr: "",
|
||||
code: 0,
|
||||
signal: null,
|
||||
timedOut: false,
|
||||
});
|
||||
|
||||
function makeProvider(github: Partial<GitHubCliShape>) {
|
||||
return GitHubSourceControlProvider.make().pipe(Effect.provide(Layer.mock(GitHubCli)(github)));
|
||||
}
|
||||
|
||||
it.effect("maps GitHub PR summaries into provider-neutral change requests", () =>
|
||||
Effect.gen(function* () {
|
||||
const provider = yield* makeProvider({
|
||||
getPullRequest: () =>
|
||||
Effect.succeed({
|
||||
number: 42,
|
||||
title: "Add GitHub provider",
|
||||
url: "https://github.com/pingdotgg/t3code/pull/42",
|
||||
baseRefName: "main",
|
||||
headRefName: "feature/source-control",
|
||||
state: "open",
|
||||
isCrossRepository: true,
|
||||
headRepositoryNameWithOwner: "fork/t3code",
|
||||
headRepositoryOwnerLogin: "fork",
|
||||
}),
|
||||
});
|
||||
|
||||
const changeRequest = yield* provider.getChangeRequest({
|
||||
cwd: "/repo",
|
||||
reference: "42",
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(changeRequest, {
|
||||
provider: "github",
|
||||
number: 42,
|
||||
title: "Add GitHub provider",
|
||||
url: "https://github.com/pingdotgg/t3code/pull/42",
|
||||
baseRefName: "main",
|
||||
headRefName: "feature/source-control",
|
||||
state: "open",
|
||||
updatedAt: null,
|
||||
isCrossRepository: true,
|
||||
headRepositoryNameWithOwner: "fork/t3code",
|
||||
headRepositoryOwnerLogin: "fork",
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("uses gh json listing for non-open change request state queries", () =>
|
||||
Effect.gen(function* () {
|
||||
let executeArgs: ReadonlyArray<string> = [];
|
||||
const provider = yield* makeProvider({
|
||||
execute: (input) => {
|
||||
executeArgs = input.args;
|
||||
return Effect.succeed(
|
||||
processResult(
|
||||
JSON.stringify([
|
||||
{
|
||||
number: 7,
|
||||
title: "Merged work",
|
||||
url: "https://github.com/pingdotgg/t3code/pull/7",
|
||||
baseRefName: "main",
|
||||
headRefName: "feature/merged",
|
||||
state: "merged",
|
||||
updatedAt: "2026-01-02T00:00:00.000Z",
|
||||
},
|
||||
]),
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const changeRequests = yield* provider.listChangeRequests({
|
||||
cwd: "/repo",
|
||||
headSelector: "feature/merged",
|
||||
state: "all",
|
||||
limit: 10,
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(executeArgs, [
|
||||
"pr",
|
||||
"list",
|
||||
"--head",
|
||||
"feature/merged",
|
||||
"--state",
|
||||
"all",
|
||||
"--limit",
|
||||
"10",
|
||||
"--json",
|
||||
"number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
|
||||
]);
|
||||
assert.strictEqual(changeRequests[0]?.provider, "github");
|
||||
assert.strictEqual(changeRequests[0]?.state, "merged");
|
||||
assert.strictEqual(changeRequests[0]?.updatedAt, "2026-01-02T00:00:00.000Z");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("creates GitHub PRs through provider-neutral input names", () =>
|
||||
Effect.gen(function* () {
|
||||
let createInput: Parameters<GitHubCliShape["createPullRequest"]>[0] | null = null;
|
||||
const provider = yield* makeProvider({
|
||||
createPullRequest: (input) => {
|
||||
createInput = input;
|
||||
return Effect.void;
|
||||
},
|
||||
});
|
||||
|
||||
yield* provider.createChangeRequest({
|
||||
cwd: "/repo",
|
||||
baseRefName: "main",
|
||||
headSelector: "owner:feature/provider",
|
||||
title: "Provider PR",
|
||||
bodyFile: "/tmp/body.md",
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(createInput, {
|
||||
cwd: "/repo",
|
||||
baseBranch: "main",
|
||||
headSelector: "owner:feature/provider",
|
||||
title: "Provider PR",
|
||||
bodyFile: "/tmp/body.md",
|
||||
});
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,141 @@
|
||||
import { Effect, Layer, Result, Schema } from "effect";
|
||||
import {
|
||||
SourceControlProviderError,
|
||||
type ChangeRequest,
|
||||
type ChangeRequestState,
|
||||
type GitHubCliError,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { GitHubCli, type GitHubPullRequestSummary } from "../git/Services/GitHubCli.ts";
|
||||
import { decodeGitHubPullRequestListJson } from "../git/githubPullRequests.ts";
|
||||
import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts";
|
||||
|
||||
function providerError(operation: string, cause: GitHubCliError): SourceControlProviderError {
|
||||
return new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation,
|
||||
detail: cause.detail,
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
function toChangeRequest(summary: GitHubPullRequestSummary): ChangeRequest {
|
||||
return {
|
||||
provider: "github",
|
||||
number: summary.number,
|
||||
title: summary.title,
|
||||
url: summary.url,
|
||||
baseRefName: summary.baseRefName,
|
||||
headRefName: summary.headRefName,
|
||||
state: summary.state ?? "open",
|
||||
updatedAt: null,
|
||||
...(summary.isCrossRepository !== undefined
|
||||
? { isCrossRepository: summary.isCrossRepository }
|
||||
: {}),
|
||||
...(summary.headRepositoryNameWithOwner !== undefined
|
||||
? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
|
||||
: {}),
|
||||
...(summary.headRepositoryOwnerLogin !== undefined
|
||||
? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
export const make = Effect.fn("makeGitHubSourceControlProvider")(function* () {
|
||||
const github = yield* GitHubCli;
|
||||
|
||||
const listChangeRequests: SourceControlProviderShape["listChangeRequests"] = (input) => {
|
||||
if (input.state === "open") {
|
||||
return github
|
||||
.listOpenPullRequests({
|
||||
cwd: input.cwd,
|
||||
headSelector: input.headSelector,
|
||||
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((items) => items.map(toChangeRequest)),
|
||||
Effect.mapError((error) => providerError("listChangeRequests", error)),
|
||||
);
|
||||
}
|
||||
|
||||
const stateArg: ChangeRequestState | "all" = input.state;
|
||||
return github
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"pr",
|
||||
"list",
|
||||
"--head",
|
||||
input.headSelector,
|
||||
"--state",
|
||||
stateArg,
|
||||
"--limit",
|
||||
String(input.limit ?? 20),
|
||||
"--json",
|
||||
"number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
|
||||
],
|
||||
})
|
||||
.pipe(
|
||||
Effect.flatMap((result) =>
|
||||
Effect.sync(() => decodeGitHubPullRequestListJson(result.stdout.trim())).pipe(
|
||||
Effect.flatMap((decoded) =>
|
||||
Result.isSuccess(decoded)
|
||||
? Effect.succeed(
|
||||
decoded.success.map((item) => ({
|
||||
...toChangeRequest(item),
|
||||
updatedAt: item.updatedAt,
|
||||
})),
|
||||
)
|
||||
: Effect.fail(
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "listChangeRequests",
|
||||
detail: "GitHub CLI returned invalid change request JSON.",
|
||||
cause: decoded.failure,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.mapError((error) =>
|
||||
Schema.is(SourceControlProviderError)(error)
|
||||
? error
|
||||
: providerError("listChangeRequests", error),
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
return SourceControlProvider.of({
|
||||
kind: "github",
|
||||
listChangeRequests,
|
||||
getChangeRequest: (input) =>
|
||||
github.getPullRequest(input).pipe(
|
||||
Effect.map(toChangeRequest),
|
||||
Effect.mapError((error) => providerError("getChangeRequest", error)),
|
||||
),
|
||||
createChangeRequest: (input) =>
|
||||
github
|
||||
.createPullRequest({
|
||||
cwd: input.cwd,
|
||||
baseBranch: input.baseRefName,
|
||||
headSelector: input.headSelector,
|
||||
title: input.title,
|
||||
bodyFile: input.bodyFile,
|
||||
})
|
||||
.pipe(Effect.mapError((error) => providerError("createChangeRequest", error))),
|
||||
getRepositoryCloneUrls: (input) =>
|
||||
github
|
||||
.getRepositoryCloneUrls(input)
|
||||
.pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))),
|
||||
getDefaultBranch: (input) =>
|
||||
github
|
||||
.getDefaultBranch(input)
|
||||
.pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))),
|
||||
checkoutChangeRequest: (input) =>
|
||||
github
|
||||
.checkoutPullRequest(input)
|
||||
.pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))),
|
||||
});
|
||||
});
|
||||
|
||||
export const layer = Layer.effect(SourceControlProvider, make());
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Context, Effect } from "effect";
|
||||
import type {
|
||||
ChangeRequest,
|
||||
ChangeRequestState,
|
||||
SourceControlProviderError,
|
||||
SourceControlProviderKind,
|
||||
SourceControlRepositoryCloneUrls,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export interface SourceControlProviderShape {
|
||||
readonly kind: SourceControlProviderKind;
|
||||
readonly listChangeRequests: (input: {
|
||||
readonly cwd: string;
|
||||
readonly headSelector: string;
|
||||
readonly state: ChangeRequestState | "all";
|
||||
readonly limit?: number;
|
||||
}) => Effect.Effect<ReadonlyArray<ChangeRequest>, SourceControlProviderError>;
|
||||
readonly getChangeRequest: (input: {
|
||||
readonly cwd: string;
|
||||
readonly reference: string;
|
||||
}) => Effect.Effect<ChangeRequest, SourceControlProviderError>;
|
||||
readonly createChangeRequest: (input: {
|
||||
readonly cwd: string;
|
||||
readonly baseRefName: string;
|
||||
readonly headSelector: string;
|
||||
readonly title: string;
|
||||
readonly bodyFile: string;
|
||||
}) => Effect.Effect<void, SourceControlProviderError>;
|
||||
readonly getRepositoryCloneUrls: (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
}) => Effect.Effect<SourceControlRepositoryCloneUrls, SourceControlProviderError>;
|
||||
readonly getDefaultBranch: (input: {
|
||||
readonly cwd: string;
|
||||
}) => Effect.Effect<string | null, SourceControlProviderError>;
|
||||
readonly checkoutChangeRequest: (input: {
|
||||
readonly cwd: string;
|
||||
readonly reference: string;
|
||||
readonly force?: boolean;
|
||||
}) => Effect.Effect<void, SourceControlProviderError>;
|
||||
}
|
||||
|
||||
export class SourceControlProvider extends Context.Service<
|
||||
SourceControlProvider,
|
||||
SourceControlProviderShape
|
||||
>()("t3/source-control/SourceControlProvider") {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Context, Effect, Layer } from "effect";
|
||||
import type { SourceControlProviderError } from "@t3tools/contracts";
|
||||
|
||||
import { SourceControlProvider, type SourceControlProviderShape } from "./SourceControlProvider.ts";
|
||||
import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
|
||||
|
||||
export interface SourceControlProviderRegistryShape {
|
||||
readonly resolve: (input: {
|
||||
readonly cwd: string;
|
||||
}) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
|
||||
}
|
||||
|
||||
export class SourceControlProviderRegistry extends Context.Service<
|
||||
SourceControlProviderRegistry,
|
||||
SourceControlProviderRegistryShape
|
||||
>()("t3/source-control/SourceControlProviderRegistry") {}
|
||||
|
||||
export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () {
|
||||
const github = yield* SourceControlProvider;
|
||||
|
||||
return SourceControlProviderRegistry.of({
|
||||
resolve: () => Effect.succeed(github),
|
||||
});
|
||||
});
|
||||
|
||||
export const layer = Layer.effect(SourceControlProviderRegistry, make()).pipe(
|
||||
Layer.provide(GitHubSourceControlProvider.layer),
|
||||
);
|
||||
Reference in new issue
Block a user