feat(source-control): add Bitbucket & Azure Devops providers (#2473)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
This commit is contained in:
59 files changed
+5852
-2345
No files matched your search
@@ -1,40 +1,46 @@
|
||||
import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
|
||||
import { SourceControlProviderError } from "@t3tools/contracts";
|
||||
import {
|
||||
SourceControlProviderError,
|
||||
type SourceControlProviderDiscoveryItem,
|
||||
} from "@t3tools/contracts";
|
||||
import type { SourceControlProviderKind } from "@t3tools/contracts";
|
||||
import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
|
||||
|
||||
import {
|
||||
SourceControlProvider,
|
||||
type SourceControlProviderContext,
|
||||
type SourceControlProviderShape,
|
||||
} from "./SourceControlProvider.ts";
|
||||
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 { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
|
||||
import * as SourceControlProvider from "./SourceControlProvider.ts";
|
||||
import * as SourceControlProviderDiscovery from "./SourceControlProviderDiscovery.ts";
|
||||
import { 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: SourceControlProviderShape;
|
||||
readonly provider: SourceControlProvider.SourceControlProviderShape;
|
||||
readonly discovery: SourceControlProviderDiscovery.SourceControlProviderDiscoverySpec;
|
||||
}
|
||||
|
||||
export interface SourceControlProviderHandle {
|
||||
readonly provider: SourceControlProviderShape;
|
||||
readonly context: SourceControlProviderContext | null;
|
||||
readonly provider: SourceControlProvider.SourceControlProviderShape;
|
||||
readonly context: SourceControlProvider.SourceControlProviderContext | null;
|
||||
}
|
||||
|
||||
export interface SourceControlProviderRegistryShape {
|
||||
readonly get: (
|
||||
kind: SourceControlProviderKind,
|
||||
) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
|
||||
) => Effect.Effect<SourceControlProvider.SourceControlProviderShape, SourceControlProviderError>;
|
||||
readonly resolveHandle: (input: {
|
||||
readonly cwd: string;
|
||||
}) => Effect.Effect<SourceControlProviderHandle, SourceControlProviderError>;
|
||||
readonly resolve: (input: {
|
||||
readonly cwd: string;
|
||||
}) => Effect.Effect<SourceControlProviderShape, SourceControlProviderError>;
|
||||
}) => Effect.Effect<SourceControlProvider.SourceControlProviderShape, SourceControlProviderError>;
|
||||
readonly discover: Effect.Effect<ReadonlyArray<SourceControlProviderDiscoveryItem>>;
|
||||
}
|
||||
|
||||
export class SourceControlProviderRegistry extends Context.Service<
|
||||
@@ -42,7 +48,9 @@ export class SourceControlProviderRegistry extends Context.Service<
|
||||
SourceControlProviderRegistryShape
|
||||
>()("t3/source-control/SourceControlProviderRegistry") {}
|
||||
|
||||
function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProviderShape {
|
||||
function unsupportedProvider(
|
||||
kind: SourceControlProviderKind,
|
||||
): SourceControlProvider.SourceControlProviderShape {
|
||||
const unsupported = (operation: string) =>
|
||||
Effect.fail(
|
||||
new SourceControlProviderError({
|
||||
@@ -52,7 +60,7 @@ function unsupportedProvider(kind: SourceControlProviderKind): SourceControlProv
|
||||
}),
|
||||
);
|
||||
|
||||
return SourceControlProvider.of({
|
||||
return SourceControlProvider.SourceControlProvider.of({
|
||||
kind,
|
||||
listChangeRequests: () => unsupported("listChangeRequests"),
|
||||
getChangeRequest: () => unsupported("getChangeRequest"),
|
||||
@@ -78,7 +86,7 @@ function selectProviderContext(
|
||||
readonly name: string;
|
||||
readonly url: string;
|
||||
}>,
|
||||
): SourceControlProviderContext | null {
|
||||
): SourceControlProvider.SourceControlProviderContext | null {
|
||||
const candidates = remotes
|
||||
.map((remote) => {
|
||||
const provider = detectSourceControlProviderFromRemoteUrl(remote.url);
|
||||
@@ -90,7 +98,7 @@ function selectProviderContext(
|
||||
}
|
||||
: null;
|
||||
})
|
||||
.filter((value): value is SourceControlProviderContext => value !== null);
|
||||
.filter((value): value is SourceControlProvider.SourceControlProviderContext => value !== null);
|
||||
|
||||
return (
|
||||
candidates.find((candidate) => candidate.remoteName === "origin") ??
|
||||
@@ -100,12 +108,60 @@ function selectProviderContext(
|
||||
);
|
||||
}
|
||||
|
||||
function bindProviderContext(
|
||||
provider: SourceControlProvider.SourceControlProviderShape,
|
||||
context: SourceControlProvider.SourceControlProviderContext | null,
|
||||
): SourceControlProvider.SourceControlProviderShape {
|
||||
if (context === null) {
|
||||
return provider;
|
||||
}
|
||||
|
||||
return SourceControlProvider.SourceControlProvider.of({
|
||||
kind: provider.kind,
|
||||
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,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWithProviders")(
|
||||
function* (registrations: ReadonlyArray<SourceControlProviderRegistration>) {
|
||||
const vcsRegistry = yield* VcsDriverRegistry;
|
||||
const providers = new Map<SourceControlProviderKind, SourceControlProviderShape>(
|
||||
registrations.map((registration) => [registration.kind, registration.provider]),
|
||||
);
|
||||
const config = yield* ServerConfig;
|
||||
const process = yield* VcsProcess.VcsProcess;
|
||||
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
|
||||
const providers = new Map<
|
||||
SourceControlProviderKind,
|
||||
SourceControlProvider.SourceControlProviderShape
|
||||
>(registrations.map((registration) => [registration.kind, registration.provider]));
|
||||
const discoverySpecs = registrations.map((registration) => registration.discovery);
|
||||
|
||||
const get: SourceControlProviderRegistryShape["get"] = (kind) =>
|
||||
Effect.succeed(providers.get(kind) ?? unsupportedProvider(kind));
|
||||
@@ -125,7 +181,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
|
||||
|
||||
const providerContextCache = yield* Cache.makeWith<
|
||||
string,
|
||||
SourceControlProviderContext | null,
|
||||
SourceControlProvider.SourceControlProviderContext | null,
|
||||
SourceControlProviderError
|
||||
>(detectProviderContext, {
|
||||
capacity: PROVIDER_DETECTION_CACHE_CAPACITY,
|
||||
@@ -136,8 +192,9 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
|
||||
Cache.get(providerContextCache, input.cwd).pipe(
|
||||
Effect.map((context) => {
|
||||
const kind = context?.provider.kind ?? "unknown";
|
||||
const provider = providers.get(kind) ?? unsupportedProvider(kind);
|
||||
return {
|
||||
provider: providers.get(kind) ?? unsupportedProvider(kind),
|
||||
provider: bindProviderContext(provider, context),
|
||||
context,
|
||||
} satisfies SourceControlProviderHandle;
|
||||
}),
|
||||
@@ -147,6 +204,16 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
|
||||
get,
|
||||
resolveHandle,
|
||||
resolve: (input) => resolveHandle(input).pipe(Effect.map((handle) => handle.provider)),
|
||||
discover: Effect.all(
|
||||
discoverySpecs.map((spec) =>
|
||||
SourceControlProviderDiscovery.probeSourceControlProvider({
|
||||
spec,
|
||||
process,
|
||||
cwd: config.cwd,
|
||||
}),
|
||||
),
|
||||
{ concurrency: "unbounded" },
|
||||
),
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -154,14 +221,29 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
|
||||
export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () {
|
||||
const github = yield* GitHubSourceControlProvider.make();
|
||||
const gitlab = yield* GitLabSourceControlProvider.make();
|
||||
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,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user