-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathGitLabSourceControlProvider.ts
More file actions
170 lines (156 loc) · 5.92 KB
/
GitLabSourceControlProvider.ts
File metadata and controls
170 lines (156 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
import * as GitLabCli from "./GitLabCli.ts";
import * as SourceControlProvider from "./SourceControlProvider.ts";
import * as SourceControlProviderDiscovery from "./SourceControlProviderDiscovery.ts";
import { findAuthenticatedGitLabHost, parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts";
function providerError(
operation: string,
cause: GitLabCli.GitLabCliError,
): SourceControlProviderError {
return new SourceControlProviderError({
provider: "gitlab",
operation,
detail: cause.detail,
cause,
});
}
function toChangeRequest(summary: GitLabCli.GitLabMergeRequestSummary): ChangeRequest {
return {
provider: "gitlab",
number: summary.number,
title: summary.title,
url: summary.url,
baseRefName: summary.baseRefName,
headRefName: summary.headRefName,
state: summary.state ?? "open",
updatedAt: summary.updatedAt ?? Option.none(),
...(summary.isCrossRepository !== undefined
? { isCrossRepository: summary.isCrossRepository }
: {}),
...(summary.headRepositoryNameWithOwner !== undefined
? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
: {}),
...(summary.headRepositoryOwnerLogin !== undefined
? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
: {}),
};
}
function parseGitLabAuth(input: SourceControlProviderDiscovery.SourceControlAuthProbeInput) {
const output = SourceControlProviderDiscovery.combinedAuthOutput(input);
const authenticatedHost = findAuthenticatedGitLabHost(parseGitLabAuthStatusHosts(output));
const account =
authenticatedHost?.account ??
SourceControlProviderDiscovery.matchFirst(output, [
/Logged in to .* as\s+([^\s(]+)/iu,
/Logged in to .* account\s+([^\s(]+)/iu,
/account:\s*([^\s(]+)/iu,
]);
const host = authenticatedHost?.host ?? SourceControlProviderDiscovery.parseCliHost(output);
if (account) {
return SourceControlProviderDiscovery.providerAuth({ status: "authenticated", account, host });
}
if (input.exitCode !== 0) {
return SourceControlProviderDiscovery.providerAuth({
status: "unauthenticated",
host,
detail:
SourceControlProviderDiscovery.firstSafeAuthLine(output) ??
"Run `glab auth login` to authenticate GitLab CLI.",
});
}
return SourceControlProviderDiscovery.providerAuth({
status: "unknown",
host,
detail:
SourceControlProviderDiscovery.firstSafeAuthLine(output) ??
"GitLab CLI auth status could not be parsed.",
});
}
function refineUnknownGitLabRemote(
input: SourceControlProviderDiscovery.SourceControlUnknownRemoteRefinementInput,
) {
const host = input.context.provider.name;
const authenticated = parseGitLabAuthStatusHosts(
SourceControlProviderDiscovery.combinedAuthOutput(input.auth),
).some((entry) => entry.account !== null && entry.host === host);
if (!authenticated) {
return null;
}
return {
kind: "gitlab",
name: "GitLab Self-Hosted",
baseUrl: input.context.provider.baseUrl,
} as const;
}
export const discovery = {
type: "cli",
kind: "gitlab",
label: "GitLab",
executable: "glab",
versionArgs: ["--version"],
authArgs: ["auth", "status"],
parseAuth: parseGitLabAuth,
refineUnknownRemote: refineUnknownGitLabRemote,
installHint:
"Install the GitLab command-line tool (`glab`) from https://gitlab.com/gitlab-org/cli or your package manager (for example `brew install glab`).",
} satisfies SourceControlProviderDiscovery.SourceControlCliDiscoverySpec;
export const make = Effect.fn("makeGitLabSourceControlProvider")(function* () {
const gitlab = yield* GitLabCli.GitLabCli;
return SourceControlProvider.SourceControlProvider.of({
kind: "gitlab",
listChangeRequests: (input) => {
const source = SourceControlProvider.sourceControlRefFromInput(input);
return gitlab
.listMergeRequests({
cwd: input.cwd,
headSelector: input.headSelector,
...(source ? { source } : {}),
state: input.state,
...(input.limit !== undefined ? { limit: input.limit } : {}),
})
.pipe(
Effect.map((items) => items.map(toChangeRequest)),
Effect.mapError((error) => providerError("listChangeRequests", error)),
);
},
getChangeRequest: (input) =>
gitlab.getMergeRequest(input).pipe(
Effect.map(toChangeRequest),
Effect.mapError((error) => providerError("getChangeRequest", error)),
),
createChangeRequest: (input) => {
const source = SourceControlProvider.sourceControlRefFromInput(input);
return gitlab
.createMergeRequest({
cwd: input.cwd,
baseBranch: input.baseRefName,
headSelector: input.headSelector,
...(source ? { source } : {}),
...(input.target ? { target: input.target } : {}),
title: input.title,
bodyFile: input.bodyFile,
})
.pipe(Effect.mapError((error) => providerError("createChangeRequest", error)));
},
getRepositoryCloneUrls: (input) =>
gitlab
.getRepositoryCloneUrls(input)
.pipe(Effect.mapError((error) => providerError("getRepositoryCloneUrls", error))),
createRepository: (input) =>
gitlab
.createRepository(input)
.pipe(Effect.mapError((error) => providerError("createRepository", error))),
getDefaultBranch: (input) =>
gitlab
.getDefaultBranch(input)
.pipe(Effect.mapError((error) => providerError("getDefaultBranch", error))),
checkoutChangeRequest: (input) =>
gitlab
.checkoutMergeRequest(input)
.pipe(Effect.mapError((error) => providerError("checkoutChangeRequest", error))),
});
});
export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make());