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