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

AzureDevOpsSourceControlProvider.ts

226 lines7.7 KB

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