+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
340 lines
12 KiB
TypeScript
340 lines
12 KiB
TypeScript
import * as Cache from "effect/Cache";
|
|
import * as Context from "effect/Context";
|
|
import * as Duration from "effect/Duration";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Exit from "effect/Exit";
|
|
import * as Layer from "effect/Layer";
|
|
import {
|
|
SourceControlProviderError,
|
|
type SourceControlProviderDiscoveryItem,
|
|
} from "@t3tools/contracts";
|
|
import type { SourceControlProviderKind } from "@t3tools/contracts";
|
|
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
|
|
|
|
import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
|
|
import * as BitbucketSourceControlProvider from "./BitbucketSourceControlProvider.ts";
|
|
import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
|
|
import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
|
|
import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts";
|
|
import * as SourceControlProvider from "./SourceControlProvider.ts";
|
|
import {
|
|
probeSourceControlProvider,
|
|
refineUnknownRemoteProvider,
|
|
type SourceControlProviderDiscoverySpec,
|
|
} from "./SourceControlProviderDiscovery.ts";
|
|
import * as ServerConfig from "../config.ts";
|
|
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
|
|
import * as VcsProcess from "../vcs/VcsProcess.ts";
|
|
|
|
const PROVIDER_DETECTION_CACHE_CAPACITY = 2_048;
|
|
const PROVIDER_DETECTION_CACHE_TTL = Duration.seconds(5);
|
|
|
|
export interface SourceControlProviderRegistration {
|
|
readonly kind: SourceControlProviderKind;
|
|
readonly provider: SourceControlProvider.SourceControlProvider["Service"];
|
|
readonly discovery: SourceControlProviderDiscoverySpec;
|
|
}
|
|
|
|
export interface SourceControlProviderHandle {
|
|
readonly provider: SourceControlProvider.SourceControlProvider["Service"];
|
|
readonly context: SourceControlProvider.SourceControlProviderContext | null;
|
|
}
|
|
|
|
export class SourceControlProviderRegistry extends Context.Service<
|
|
SourceControlProviderRegistry,
|
|
{
|
|
readonly resolveLink: SourceControlProvider.ResolveSourceControlLink;
|
|
readonly get: (
|
|
kind: SourceControlProviderKind,
|
|
) => Effect.Effect<
|
|
SourceControlProvider.SourceControlProvider["Service"],
|
|
SourceControlProviderError
|
|
>;
|
|
readonly resolveHandle: (input: {
|
|
readonly cwd: string;
|
|
readonly context?: SourceControlProvider.SourceControlProviderContext;
|
|
}) => Effect.Effect<SourceControlProviderHandle, SourceControlProviderError>;
|
|
readonly resolve: (input: {
|
|
readonly cwd: string;
|
|
}) => Effect.Effect<
|
|
SourceControlProvider.SourceControlProvider["Service"],
|
|
SourceControlProviderError
|
|
>;
|
|
readonly discover: Effect.Effect<ReadonlyArray<SourceControlProviderDiscoveryItem>>;
|
|
}
|
|
>()("t3/sourceControl/SourceControlProviderRegistry") {}
|
|
|
|
function unsupportedProvider(
|
|
kind: SourceControlProviderKind,
|
|
): SourceControlProvider.SourceControlProvider["Service"] {
|
|
return SourceControlProvider.SourceControlProvider.of({
|
|
kind,
|
|
listChangeRequests: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "listChangeRequests",
|
|
cwd: input.cwd,
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
getChangeRequest: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "getChangeRequest",
|
|
cwd: input.cwd,
|
|
reference: SourceControlProvider.transportSafeSourceControlErrorValue(input.reference),
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
createChangeRequest: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "createChangeRequest",
|
|
cwd: input.cwd,
|
|
reference: SourceControlProvider.transportSafeSourceControlErrorValue(input.headSelector),
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
getRepositoryCloneUrls: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "getRepositoryCloneUrls",
|
|
cwd: input.cwd,
|
|
repository: SourceControlProvider.transportSafeSourceControlErrorValue(input.repository),
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
createRepository: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "createRepository",
|
|
cwd: input.cwd,
|
|
repository: SourceControlProvider.transportSafeSourceControlErrorValue(input.repository),
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
getDefaultBranch: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "getDefaultBranch",
|
|
cwd: input.cwd,
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
checkoutChangeRequest: (input) =>
|
|
new SourceControlProviderError({
|
|
provider: kind,
|
|
operation: "checkoutChangeRequest",
|
|
cwd: input.cwd,
|
|
reference: SourceControlProvider.transportSafeSourceControlErrorValue(input.reference),
|
|
detail: `No ${kind} source control provider is registered.`,
|
|
}),
|
|
});
|
|
}
|
|
|
|
function selectProviderContext(
|
|
remotes: ReadonlyArray<{
|
|
readonly name: string;
|
|
readonly url: string;
|
|
}>,
|
|
): SourceControlProvider.SourceControlProviderContext | null {
|
|
const candidates: Array<SourceControlProvider.SourceControlProviderContext> = [];
|
|
for (const remote of remotes) {
|
|
const provider = detectSourceControlProviderFromRemoteUrl(remote.url);
|
|
if (provider) {
|
|
candidates.push({
|
|
provider,
|
|
remoteName: remote.name,
|
|
remoteUrl: remote.url,
|
|
});
|
|
}
|
|
}
|
|
|
|
return (
|
|
candidates.find((candidate) => candidate.remoteName === "origin") ??
|
|
candidates.find((candidate) => candidate.provider.kind !== "unknown") ??
|
|
candidates[0] ??
|
|
null
|
|
);
|
|
}
|
|
|
|
function bindProviderContext(
|
|
provider: SourceControlProvider.SourceControlProvider["Service"],
|
|
context: SourceControlProvider.SourceControlProviderContext | null,
|
|
): SourceControlProvider.SourceControlProvider["Service"] {
|
|
if (context === null) {
|
|
return provider;
|
|
}
|
|
|
|
return SourceControlProvider.SourceControlProvider.of({
|
|
kind: provider.kind,
|
|
...(provider.resolveLink ? { resolveLink: provider.resolveLink } : {}),
|
|
listChangeRequests: (input) =>
|
|
provider.listChangeRequests({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
getChangeRequest: (input) =>
|
|
provider.getChangeRequest({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
createChangeRequest: (input) =>
|
|
provider.createChangeRequest({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
getRepositoryCloneUrls: (input) =>
|
|
provider.getRepositoryCloneUrls({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
createRepository: (input) => provider.createRepository(input),
|
|
getDefaultBranch: (input) =>
|
|
provider.getDefaultBranch({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
checkoutChangeRequest: (input) =>
|
|
provider.checkoutChangeRequest({
|
|
...input,
|
|
context: input.context ?? context,
|
|
}),
|
|
});
|
|
}
|
|
|
|
/** @public Service construction is part of the canonical Effect module API. */
|
|
export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWithProviders")(
|
|
function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const process = yield* VcsProcess.VcsProcess;
|
|
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
|
|
const providers = new Map<
|
|
SourceControlProviderKind,
|
|
SourceControlProvider.SourceControlProvider["Service"]
|
|
>(registrations.map((registration) => [registration.kind, registration.provider]));
|
|
const discoverySpecs = registrations.map((registration) => registration.discovery);
|
|
|
|
const get: SourceControlProviderRegistry["Service"]["get"] = (kind) =>
|
|
Effect.succeed(providers.get(kind) ?? unsupportedProvider(kind));
|
|
|
|
const detectProviderContext = Effect.fn("SourceControlProviderRegistry.detectProviderContext")(
|
|
function* (cwd: string) {
|
|
const handle = yield* vcsRegistry.resolve({ cwd }).pipe(
|
|
Effect.mapError(
|
|
(error) =>
|
|
new SourceControlProviderError({
|
|
provider: "unknown",
|
|
operation: "detectProvider",
|
|
cwd,
|
|
detail: "Failed to detect source control provider.",
|
|
cause: error,
|
|
}),
|
|
),
|
|
);
|
|
const remotes = yield* handle.driver.listRemotes(cwd).pipe(
|
|
Effect.mapError(
|
|
(error) =>
|
|
new SourceControlProviderError({
|
|
provider: "unknown",
|
|
operation: "detectProvider",
|
|
cwd,
|
|
detail: "Failed to detect source control provider.",
|
|
cause: error,
|
|
}),
|
|
),
|
|
);
|
|
const context = selectProviderContext(remotes.remotes);
|
|
|
|
return yield* refineUnknownRemoteProvider({
|
|
specs: discoverySpecs,
|
|
process,
|
|
cwd,
|
|
context,
|
|
});
|
|
},
|
|
);
|
|
|
|
const providerContextCache = yield* Cache.makeWith<
|
|
string,
|
|
SourceControlProvider.SourceControlProviderContext | null,
|
|
SourceControlProviderError
|
|
>(detectProviderContext, {
|
|
capacity: PROVIDER_DETECTION_CACHE_CAPACITY,
|
|
timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero),
|
|
});
|
|
|
|
const resolveHandle: SourceControlProviderRegistry["Service"]["resolveHandle"] = (input) =>
|
|
(input.context === undefined
|
|
? Cache.get(providerContextCache, input.cwd)
|
|
: refineUnknownRemoteProvider({
|
|
specs: discoverySpecs,
|
|
process,
|
|
cwd: input.cwd,
|
|
context: input.context,
|
|
})
|
|
).pipe(
|
|
Effect.map((context) => {
|
|
const kind = context?.provider.kind ?? "unknown";
|
|
const provider = providers.get(kind) ?? unsupportedProvider(kind);
|
|
return {
|
|
provider: bindProviderContext(provider, context),
|
|
context,
|
|
} satisfies SourceControlProviderHandle;
|
|
}),
|
|
);
|
|
|
|
return SourceControlProviderRegistry.of({
|
|
resolveLink: (input) => {
|
|
if (input.url.protocol !== "https:" || input.url.username || input.url.password) {
|
|
return undefined;
|
|
}
|
|
const kind = detectSourceControlProviderFromRemoteUrl(input.url.href)?.kind;
|
|
return kind ? providers.get(kind)?.resolveLink?.(input) : undefined;
|
|
},
|
|
get,
|
|
resolveHandle,
|
|
resolve: (input) => resolveHandle(input).pipe(Effect.map((handle) => handle.provider)),
|
|
discover: Effect.forEach(
|
|
discoverySpecs,
|
|
(spec) =>
|
|
probeSourceControlProvider({
|
|
spec,
|
|
process,
|
|
cwd: config.cwd,
|
|
}),
|
|
{ concurrency: "unbounded" },
|
|
),
|
|
});
|
|
},
|
|
);
|
|
|
|
export const make = Effect.gen(function* () {
|
|
const github = yield* GitHubSourceControlProvider.make;
|
|
const gitlab = yield* GitLabSourceControlProvider.make;
|
|
const forgejo = yield* ForgejoSourceControlProvider.make;
|
|
const forgejoDiscovery = yield* ForgejoSourceControlProvider.makeDiscovery;
|
|
const bitbucket = yield* BitbucketSourceControlProvider.make;
|
|
const bitbucketDiscovery = yield* BitbucketSourceControlProvider.makeDiscovery;
|
|
const azureDevOps = yield* AzureDevOpsSourceControlProvider.make;
|
|
return yield* makeWithProviders([
|
|
{
|
|
kind: "github",
|
|
provider: github,
|
|
discovery: GitHubSourceControlProvider.discovery,
|
|
},
|
|
{
|
|
kind: "gitlab",
|
|
provider: gitlab,
|
|
discovery: GitLabSourceControlProvider.discovery,
|
|
},
|
|
{
|
|
kind: "azure-devops",
|
|
provider: azureDevOps,
|
|
discovery: AzureDevOpsSourceControlProvider.discovery,
|
|
},
|
|
{
|
|
kind: "bitbucket",
|
|
provider: bitbucket,
|
|
discovery: bitbucketDiscovery,
|
|
},
|
|
{ kind: "forgejo", provider: forgejo, discovery: forgejoDiscovery },
|
|
]);
|
|
});
|
|
|
|
export const layer = Layer.effect(SourceControlProviderRegistry, make);
|