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

AzureDevOpsSourceControlProvider.ts

237 lines7.7 KB

1import * as Effect from "effect/Effect";
2import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
3​
4import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
5import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
6import {
7 combinedAuthOutput,
8 firstSafeAuthLine,
9 providerAuth,
10 type SourceControlAuthProbeInput,
11 type SourceControlCliDiscoverySpec,
12} from "@t3tools/source-control-core/server/discovery";
13​
14function parseAzureAuth(input: SourceControlAuthProbeInput) {
15 const account = input.stdout.trim().split(/\r?\n/)[0]?.trim();
16​
17 if (input.exitCode !== 0) {
18 return providerAuth({
19 status: "unauthenticated",
20 detail:
21 firstSafeAuthLine(combinedAuthOutput(input)) ?? "Run `az login` to authenticate Azure CLI.",
22 });
23 }
24​
25 if (account !== undefined && account.length > 0) {
26 return providerAuth({
27 status: "authenticated",
28 account,
29 host: "dev.azure.com",
30 });
31 }
32​
33 return providerAuth({
34 status: "unknown",
35 host: "dev.azure.com",
36 detail: "Azure CLI account status could not be parsed.",
37 });
39​
40export const discovery = {
41 type: "cli",
42 kind: "azure-devops",
43 label: "Azure DevOps",
44 executable: "az",
45 versionArgs: ["--version"],
46 authArgs: ["account", "show", "--query", "user.name", "-o", "tsv"],
47 // `az` boots a fresh Python interpreter on every invocation, so even `az --version`
48 // takes ~6s on Windows and overruns the default budget, leaving the provider reported
49 // as missing on machines where it is installed. `gh` and `glab` answer in ~0.3s.
50 probeTimeoutMs: 20_000,
51 parseAuth: parseAzureAuth,
52 installHint:
53 "Install the Azure command-line tools (`az`), then enable Azure DevOps support with `az extension add --name azure-devops`.",
54} satisfies SourceControlCliDiscoverySpec;
55​
56function toChangeRequest(summary: {
57 readonly number: number;
58 readonly title: string;
59 readonly url: string;
60 readonly baseRefName: string;
61 readonly headRefName: string;
62 readonly state: "open" | "closed" | "merged";
63 readonly isDraft?: boolean;
64 readonly isCrossRepository: boolean;
65 readonly closedAt?: string | null;
66 readonly mergedAt?: string | null;
67 readonly updatedAt: ChangeRequest["updatedAt"];
68}): ChangeRequest {
69 return {
70 provider: "azure-devops",
71 number: summary.number,
72 title: summary.title,
73 url: summary.url,
74 baseRefName: summary.baseRefName,
75 headRefName: summary.headRefName,
76 state: summary.state,
77 ...(summary.isDraft === true ? { isDraft: true } : {}),
78 closedAt: summary.closedAt ?? null,
79 mergedAt: summary.mergedAt ?? null,
80 updatedAt: summary.updatedAt,
81 isCrossRepository: summary.isCrossRepository,
82 };
84​
85export const make = Effect.gen(function* () {
86 const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
87​
88 return SourceControlProvider.SourceControlProvider.of({
89 kind: "azure-devops",
90 listChangeRequests: (input) => {
91 const source = SourceControlProvider.sourceControlRefFromInput(input);
92 return azure
93 .listPullRequests({
94 cwd: input.cwd,
95 headSelector: input.headSelector,
96 ...(source !== undefined ? { source } : {}),
97 state: input.state,
98 ...(input.limit !== undefined ? { limit: input.limit } : {}),
99 })
100 .pipe(
101 Effect.map((items) => items.map(toChangeRequest)),
102 Effect.mapError(
103 (error) =>
104 new SourceControlProviderError({
105 provider: "azure-devops",
106 operation: "listChangeRequests",
107 command: error.command,
108 cwd: input.cwd,
109 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
110 input.headSelector,
111 ),
112 detail: error.detail,
113 cause: error,
114 }),
115 ),
116 );
117 },
118 getChangeRequest: (input) =>
119 azure.getPullRequest(input).pipe(
120 Effect.map(toChangeRequest),
121 Effect.mapError(
122 (error) =>
123 new SourceControlProviderError({
124 provider: "azure-devops",
125 operation: "getChangeRequest",
126 command: error.command,
127 cwd: input.cwd,
128 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
129 input.reference,
130 ),
131 detail: error.detail,
132 cause: error,
133 }),
134 ),
135 ),
136 createChangeRequest: (input) => {
137 const source = SourceControlProvider.sourceControlRefFromInput(input);
138 return azure
139 .createPullRequest({
140 cwd: input.cwd,
141 baseBranch: input.baseRefName,
142 headSelector: input.headSelector,
143 ...(source !== undefined ? { source } : {}),
144 ...(input.target !== undefined ? { target: input.target } : {}),
145 title: input.title,
146 bodyFile: input.bodyFile,
147 })
148 .pipe(
149 Effect.mapError(
150 (error) =>
151 new SourceControlProviderError({
152 provider: "azure-devops",
153 operation: "createChangeRequest",
154 command: error.command,
155 cwd: input.cwd,
156 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
157 input.headSelector,
158 ),
159 detail: error.detail,
160 cause: error,
161 }),
162 ),
163 );
164 },
165 getRepositoryCloneUrls: (input) =>
166 azure.getRepositoryCloneUrls(input).pipe(
167 Effect.mapError(
168 (error) =>
169 new SourceControlProviderError({
170 provider: "azure-devops",
171 operation: "getRepositoryCloneUrls",
172 command: error.command,
173 cwd: input.cwd,
174 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
175 input.repository,
176 ),
177 detail: error.detail,
178 cause: error,
179 }),
180 ),
181 ),
182 createRepository: (input) =>
183 azure.createRepository(input).pipe(
184 Effect.mapError(
185 (error) =>
186 new SourceControlProviderError({
187 provider: "azure-devops",
188 operation: "createRepository",
189 command: error.command,
190 cwd: input.cwd,
191 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
192 input.repository,
193 ),
194 detail: error.detail,
195 cause: error,
196 }),
197 ),
198 ),
199 getDefaultBranch: (input) =>
200 azure.getDefaultBranch({ cwd: input.cwd }).pipe(
201 Effect.mapError(
202 (error) =>
203 new SourceControlProviderError({
204 provider: "azure-devops",
205 operation: "getDefaultBranch",
206 command: error.command,
207 cwd: input.cwd,
208 detail: error.detail,
209 cause: error,
210 }),
211 ),
212 ),
213 checkoutChangeRequest: (input) =>
214 azure
215 .checkoutPullRequest({
216 cwd: input.cwd,
217 reference: input.reference,
218 ...(input.context !== undefined ? { remoteName: input.context.remoteName } : {}),
219 })
220 .pipe(
221 Effect.mapError(
222 (error) =>
223 new SourceControlProviderError({
224 provider: "azure-devops",
225 operation: "checkoutChangeRequest",
226 command: error.command,
227 cwd: input.cwd,
228 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
229 input.reference,
230 ),
231 detail: error.detail,
232 cause: error,
233 }),
234 ),
235 ),
236 });
237});