Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitLabSourceControlProvider.ts

254 lines8.2 KB

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