Open History to see the last commit
1import { Effect, Layer, Option } from "effect";
2import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
3
4import * as GitLabCli from "./GitLabCli.ts";
5import * as SourceControlProvider from "./SourceControlProvider.ts";
6import * as SourceControlProviderDiscovery from "./SourceControlProviderDiscovery.ts";
7
8function providerError(
9 operation: string,
10 cause: GitLabCli.GitLabCliError,
11): SourceControlProviderError {
12 return new SourceControlProviderError({
13 provider: "gitlab",
14 operation,
15 detail: cause.detail,
16 cause,
17 });
18}
19
20function toChangeRequest(summary: GitLabCli.GitLabMergeRequestSummary): ChangeRequest {
21 return {
22 provider: "gitlab",
23 number: summary.number,
24 title: summary.title,
25 url: summary.url,
26 baseRefName: summary.baseRefName,
27 headRefName: summary.headRefName,
28 state: summary.state ?? "open",
29 updatedAt: summary.updatedAt ?? Option.none(),
30 ...(summary.isCrossRepository !== undefined
31 ? { isCrossRepository: summary.isCrossRepository }
32 : {}),
33 ...(summary.headRepositoryNameWithOwner !== undefined
34 ? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
35 : {}),
36 ...(summary.headRepositoryOwnerLogin !== undefined
37 ? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
38 : {}),
39 };
40}
41
42function parseGitLabAuth(input: SourceControlProviderDiscovery.SourceControlAuthProbeInput) {
43 const output = SourceControlProviderDiscovery.combinedAuthOutput(input);
44 const account = SourceControlProviderDiscovery.matchFirst(output, [
45 /Logged in to .* as\s+([^\s(]+)/iu,
46 /Logged in to .* account\s+([^\s(]+)/iu,
47 /account:\s*([^\s(]+)/iu,
48 ]);
49 const host = SourceControlProviderDiscovery.parseCliHost(output);
50
51 if (input.exitCode !== 0) {
52 return SourceControlProviderDiscovery.providerAuth({
53 status: "unauthenticated",
54 host,
55 detail:
56 SourceControlProviderDiscovery.firstSafeAuthLine(output) ??
57 "Run `glab auth login` to authenticate GitLab CLI.",
58 });
59 }
60
61 if (account) {
62 return SourceControlProviderDiscovery.providerAuth({ status: "authenticated", account, host });
63 }
64
65 return SourceControlProviderDiscovery.providerAuth({
66 status: "unknown",
67 host,
68 detail:
69 SourceControlProviderDiscovery.firstSafeAuthLine(output) ??
70 "GitLab CLI auth status could not be parsed.",
71 });
72}
73
74export const discovery = {
75 type: "cli",
76 kind: "gitlab",
77 label: "GitLab",
78 executable: "glab",
79 versionArgs: ["--version"],
80 authArgs: ["auth", "status"],
81 parseAuth: parseGitLabAuth,
82 installHint:
83 "Install the GitLab command-line tool (`glab`) from https://gitlab.com/gitlab-org/cli or your package manager (for example `brew install glab`).",
84} satisfies SourceControlProviderDiscovery.SourceControlCliDiscoverySpec;
85
86export const make = Effect.fn("makeGitLabSourceControlProvider")(function* () {
87 const gitlab = yield* GitLabCli.GitLabCli;
88
89 return SourceControlProvider.SourceControlProvider.of({
90 kind: "gitlab",
91 listChangeRequests: (input) => {
92 const source = SourceControlProvider.sourceControlRefFromInput(input);
93 return gitlab
94 .listMergeRequests({
95 cwd: input.cwd,
96 headSelector: input.headSelector,
97 ...(source ? { source } : {}),
98 state: input.state,
99 ...(input.limit !== undefined ? { limit: input.limit } : {}),
100 })
101 .pipe(
102 Effect.map((items) => items.map(toChangeRequest)),
103 Effect.mapError((error) => providerError("listChangeRequests", error)),
104 );
105 },
106 getChangeRequest: (input) =>
107 gitlab.getMergeRequest(input).pipe(
108 Effect.map(toChangeRequest),
109 Effect.mapError((error) => providerError("getChangeRequest", error)),
110 ),
111 createChangeRequest: (input) => {
112 const source = SourceControlProvider.sourceControlRefFromInput(input);
113 return gitlab
114 .createMergeRequest({
115 cwd: input.cwd,
116 baseBranch: input.baseRefName,
117 headSelector: input.headSelector,
118 ...(source ? { source } : {}),
119 ...(input.target ? { target: input.target } : {}),
120 title: input.title,
121 bodyFile: input.bodyFile,
122 })
123 .pipe(Effect.mapError((error) => providerError("createChangeRequest", error)));
124 },
125 getRepositoryCloneUrls: (input) =>
126 gitlab
127 .getRepositoryCloneUrls(input)
128 .pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))),
129 createRepository: (input) =>
130 gitlab
131 .createRepository(input)
132 .pipe(Effect.mapError((error) => providerError("createRepository", error))),
133 getDefaultBranch: (input) =>
134 gitlab
135 .getDefaultBranch(input)
136 .pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))),
137 checkoutChangeRequest: (input) =>
138 gitlab
139 .checkoutMergeRequest(input)
140 .pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))),
141 });
142});
143
144export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make());