Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Layer from "effect/Layer";
3import * as Option from "effect/Option";
4import { SourceControlProviderError, type ChangeRequest } from "@v12/contracts";
5
6import * as GitLabCli from "./GitLabCli.ts";
7import * as SourceControlProvider from "./SourceControlProvider.ts";
8import {
9 combinedAuthOutput,
10 firstSafeAuthLine,
11 matchFirst,
12 parseCliHost,
13 providerAuth,
14 type SourceControlAuthProbeInput,
15 type SourceControlCliDiscoverySpec,
16 type SourceControlUnknownRemoteRefinementInput,
17} from "./SourceControlProviderDiscovery.ts";
18import { findAuthenticatedGitLabHost, parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts";
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: SourceControlAuthProbeInput) {
43 const output = combinedAuthOutput(input);
44 const authenticatedHost = findAuthenticatedGitLabHost(parseGitLabAuthStatusHosts(output));
45 const account =
46 authenticatedHost?.account ??
47 matchFirst(output, [
48 /Logged in to .* as\s+([^\s(]+)/iu,
49 /Logged in to .* account\s+([^\s(]+)/iu,
50 /account:\s*([^\s(]+)/iu,
51 ]);
52 const host = authenticatedHost?.host ?? parseCliHost(output);
53
54 if (account) {
55 return providerAuth({ status: "authenticated", account, host });
56 }
57
58 if (input.exitCode !== 0) {
59 return providerAuth({
60 status: "unauthenticated",
61 host,
62 detail: firstSafeAuthLine(output) ?? "Run `glab auth login` to authenticate GitLab CLI.",
63 });
64 }
65
66 return providerAuth({
67 status: "unknown",
68 host,
69 detail: firstSafeAuthLine(output) ?? "GitLab CLI auth status could not be parsed.",
70 });
71}
72
73function refineUnknownGitLabRemote(input: SourceControlUnknownRemoteRefinementInput) {
74 const host = input.context.provider.name.toLowerCase();
75 const authenticated = parseGitLabAuthStatusHosts(combinedAuthOutput(input.auth)).some(
76 (entry) => entry.account !== null && entry.host === host,
77 );
78
79 if (!authenticated) {
80 return null;
81 }
82
83 return {
84 kind: "gitlab",
85 name: "GitLab Self-Hosted",
86 baseUrl: input.context.provider.baseUrl,
87 } as const;
88}
89
90export const discovery = {
91 type: "cli",
92 kind: "gitlab",
93 label: "GitLab",
94 executable: "glab",
95 versionArgs: ["--version"],
96 authArgs: ["auth", "status"],
97 parseAuth: parseGitLabAuth,
98 refineUnknownRemote: refineUnknownGitLabRemote,
99 installHint:
100 "Install the GitLab command-line tool (`glab`) from https://gitlab.com/gitlab-org/cli or your package manager (for example `brew install glab`).",
101} satisfies SourceControlCliDiscoverySpec;
102
103export const make = Effect.gen(function* () {
104 const gitlab = yield* GitLabCli.GitLabCli;
105
106 return SourceControlProvider.SourceControlProvider.of({
107 kind: "gitlab",
108 listChangeRequests: (input) => {
109 const source = SourceControlProvider.sourceControlRefFromInput(input);
110 return gitlab
111 .listMergeRequests({
112 cwd: input.cwd,
113 headSelector: input.headSelector,
114 ...(source ? { source } : {}),
115 state: input.state,
116 ...(input.limit !== undefined ? { limit: input.limit } : {}),
117 })
118 .pipe(
119 Effect.map((items) => items.map(toChangeRequest)),
120 Effect.mapError(
121 (error) =>
122 new SourceControlProviderError({
123 provider: "gitlab",
124 operation: "listChangeRequests",
125 command: error.command,
126 cwd: input.cwd,
127 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
128 input.headSelector,
129 ),
130 detail: error.detail,
131 cause: error,
132 }),
133 ),
134 );
135 },
136 getChangeRequest: (input) =>
137 gitlab.getMergeRequest(input).pipe(
138 Effect.map(toChangeRequest),
139 Effect.mapError(
140 (error) =>
141 new SourceControlProviderError({
142 provider: "gitlab",
143 operation: "getChangeRequest",
144 command: error.command,
145 cwd: input.cwd,
146 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
147 input.reference,
148 ),
149 detail: error.detail,
150 cause: error,
151 }),
152 ),
153 ),
154 createChangeRequest: (input) => {
155 const source = SourceControlProvider.sourceControlRefFromInput(input);
156 return gitlab
157 .createMergeRequest({
158 cwd: input.cwd,
159 baseBranch: input.baseRefName,
160 headSelector: input.headSelector,
161 ...(source ? { source } : {}),
162 ...(input.target ? { target: input.target } : {}),
163 title: input.title,
164 bodyFile: input.bodyFile,
165 })
166 .pipe(
167 Effect.mapError(
168 (error) =>
169 new SourceControlProviderError({
170 provider: "gitlab",
171 operation: "createChangeRequest",
172 command: error.command,
173 cwd: input.cwd,
174 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
175 input.headSelector,
176 ),
177 detail: error.detail,
178 cause: error,
179 }),
180 ),
181 );
182 },
183 getRepositoryCloneUrls: (input) =>
184 gitlab.getRepositoryCloneUrls(input).pipe(
185 Effect.mapError(
186 (error) =>
187 new SourceControlProviderError({
188 provider: "gitlab",
189 operation: "getRepositoryCloneUrls",
190 command: error.command,
191 cwd: input.cwd,
192 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
193 input.repository,
194 ),
195 detail: error.detail,
196 cause: error,
197 }),
198 ),
199 ),
200 createRepository: (input) =>
201 gitlab.createRepository(input).pipe(
202 Effect.mapError(
203 (error) =>
204 new SourceControlProviderError({
205 provider: "gitlab",
206 operation: "createRepository",
207 command: error.command,
208 cwd: input.cwd,
209 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
210 input.repository,
211 ),
212 detail: error.detail,
213 cause: error,
214 }),
215 ),
216 ),
217 getDefaultBranch: (input) =>
218 gitlab.getDefaultBranch(input).pipe(
219 Effect.mapError(
220 (error) =>
221 new SourceControlProviderError({
222 provider: "gitlab",
223 operation: "getDefaultBranch",
224 command: error.command,
225 cwd: input.cwd,
226 detail: error.detail,
227 cause: error,
228 }),
229 ),
230 ),
231 checkoutChangeRequest: (input) =>
232 gitlab.checkoutMergeRequest(input).pipe(
233 Effect.mapError(
234 (error) =>
235 new SourceControlProviderError({
236 provider: "gitlab",
237 operation: "checkoutChangeRequest",
238 command: error.command,
239 cwd: input.cwd,
240 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
241 input.reference,
242 ),
243 detail: error.detail,
244 cause: error,
245 }),
246 ),
247 ),
248 });
249});
250
251export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make);