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

GitLabSourceControlProvider.ts

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