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