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