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 updatedAt: ChangeRequest["updatedAt"];
65}): ChangeRequest {
66 return {
67 provider: "azure-devops",
68 number: summary.number,
69 title: summary.title,
70 url: summary.url,
71 baseRefName: summary.baseRefName,
72 headRefName: summary.headRefName,
73 state: summary.state,
74 updatedAt: summary.updatedAt,
75 isCrossRepository: false,
76 };
77}
78
79export const make = Effect.gen(function* () {
80 const azure = yield* AzureDevOpsCli.AzureDevOpsCli;
81
82 return SourceControlProvider.SourceControlProvider.of({
83 kind: "azure-devops",
84 listChangeRequests: (input) => {
85 const source = SourceControlProvider.sourceControlRefFromInput(input);
86 return azure
87 .listPullRequests({
88 cwd: input.cwd,
89 headSelector: input.headSelector,
90 ...(source !== undefined ? { source } : {}),
91 state: input.state,
92 ...(input.limit !== undefined ? { limit: input.limit } : {}),
93 })
94 .pipe(
95 Effect.map((items) => items.map(toChangeRequest)),
96 Effect.mapError(
97 (error) =>
98 new SourceControlProviderError({
99 provider: "azure-devops",
100 operation: "listChangeRequests",
101 command: error.command,
102 cwd: input.cwd,
103 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
104 input.headSelector,
105 ),
106 detail: error.detail,
107 cause: error,
108 }),
109 ),
110 );
111 },
112 getChangeRequest: (input) =>
113 azure.getPullRequest(input).pipe(
114 Effect.map(toChangeRequest),
115 Effect.mapError(
116 (error) =>
117 new SourceControlProviderError({
118 provider: "azure-devops",
119 operation: "getChangeRequest",
120 command: error.command,
121 cwd: input.cwd,
122 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
123 input.reference,
124 ),
125 detail: error.detail,
126 cause: error,
127 }),
128 ),
129 ),
130 createChangeRequest: (input) => {
131 const source = SourceControlProvider.sourceControlRefFromInput(input);
132 return azure
133 .createPullRequest({
134 cwd: input.cwd,
135 baseBranch: input.baseRefName,
136 headSelector: input.headSelector,
137 ...(source !== undefined ? { source } : {}),
138 ...(input.target !== undefined ? { target: input.target } : {}),
139 title: input.title,
140 bodyFile: input.bodyFile,
141 })
142 .pipe(
143 Effect.mapError(
144 (error) =>
145 new SourceControlProviderError({
146 provider: "azure-devops",
147 operation: "createChangeRequest",
148 command: error.command,
149 cwd: input.cwd,
150 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
151 input.headSelector,
152 ),
153 detail: error.detail,
154 cause: error,
155 }),
156 ),
157 );
158 },
159 getRepositoryCloneUrls: (input) =>
160 azure.getRepositoryCloneUrls(input).pipe(
161 Effect.mapError(
162 (error) =>
163 new SourceControlProviderError({
164 provider: "azure-devops",
165 operation: "getRepositoryCloneUrls",
166 command: error.command,
167 cwd: input.cwd,
168 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
169 input.repository,
170 ),
171 detail: error.detail,
172 cause: error,
173 }),
174 ),
175 ),
176 createRepository: (input) =>
177 azure.createRepository(input).pipe(
178 Effect.mapError(
179 (error) =>
180 new SourceControlProviderError({
181 provider: "azure-devops",
182 operation: "createRepository",
183 command: error.command,
184 cwd: input.cwd,
185 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
186 input.repository,
187 ),
188 detail: error.detail,
189 cause: error,
190 }),
191 ),
192 ),
193 getDefaultBranch: (input) =>
194 azure.getDefaultBranch({ cwd: input.cwd }).pipe(
195 Effect.mapError(
196 (error) =>
197 new SourceControlProviderError({
198 provider: "azure-devops",
199 operation: "getDefaultBranch",
200 command: error.command,
201 cwd: input.cwd,
202 detail: error.detail,
203 cause: error,
204 }),
205 ),
206 ),
207 checkoutChangeRequest: (input) =>
208 azure
209 .checkoutPullRequest({
210 cwd: input.cwd,
211 reference: input.reference,
212 ...(input.context !== undefined ? { remoteName: input.context.remoteName } : {}),
213 })
214 .pipe(
215 Effect.mapError(
216 (error) =>
217 new SourceControlProviderError({
218 provider: "azure-devops",
219 operation: "checkoutChangeRequest",
220 command: error.command,
221 cwd: input.cwd,
222 reference: SourceControlProvider.transportSafeSourceControlErrorValue(
223 input.reference,
224 ),
225 detail: error.detail,
226 cause: error,
227 }),
228 ),
229 ),
230 });
231});
232
233export const layer = Layer.effect(SourceControlProvider.SourceControlProvider, make);