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

AzureDevOpsSourceControlProvider.ts

235 lines7.6 KB

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