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

GitLabSourceControlProvider.ts

251 lines8.0 KB

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