Open History to see the last commit
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 });
39}
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 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: false,
82 };
83}
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});
238
239export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make);