The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts"; |
| 5 | |
| 6 | import * as AzureDevOpsChangeRequestTemplate from "./azureDevOpsChangeRequestTemplate.ts"; |
| 7 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 8 | import * as AzureDevOpsCli from "./AzureDevOpsCli.ts"; |
| 9 | import type { NormalizedAzureDevOpsPullRequestRecord } from "./azureDevOpsPullRequests.ts"; |
| 10 | import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider"; |
| 11 | import { |
| 12 | combinedAuthOutput, |
| 13 | firstSafeAuthLine, |
| 14 | providerAuth, |
| 15 | type SourceControlAuthProbeInput, |
| 16 | type SourceControlCliDiscoverySpec, |
| 17 | } from "@t3tools/source-control-core/server/discovery"; |
| 18 | |
| 19 | function parseAzureAuth(input: SourceControlAuthProbeInput) { |
| 20 | const account = input.stdout.trim().split(/\r?\n/)[0]?.trim(); |
| 21 | |
| 22 | if (input.exitCode !== 0) { |
| 23 | return providerAuth({ |
| 24 | status: "unauthenticated", |
| 25 | detail: |
| 26 | firstSafeAuthLine(combinedAuthOutput(input)) ?? "Run `az login` to authenticate Azure CLI.", |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | if (account !== undefined && account.length > 0) { |
| 31 | return providerAuth({ |
| 32 | status: "authenticated", |
| 33 | account, |
| 34 | host: "dev.azure.com", |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | return providerAuth({ |
| 39 | status: "unknown", |
| 40 | host: "dev.azure.com", |
| 41 | detail: "Azure CLI account status could not be parsed.", |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | export const discovery = { |
| 46 | type: "cli", |
| 47 | kind: SourceControlProviderKind.make("azure-devops"), |
| 48 | label: "Azure DevOps", |
| 49 | executable: "az", |
| 50 | versionArgs: ["--version"], |
| 51 | authArgs: ["account", "show", "--query", "user.name", "-o", "tsv"], |
| 52 | // `az --version` starts Python and takes ~6s on Windows, exceeding the default probe timeout. |
| 53 | probeTimeoutMs: 20_000, |
| 54 | parseAuth: parseAzureAuth, |
| 55 | installHint: |
| 56 | "Install the Azure command-line tools (`az`), then enable Azure DevOps support with `az extension add --name azure-devops`.", |
| 57 | } satisfies SourceControlCliDiscoverySpec; |
| 58 | |
| 59 | /** |
| 60 | * The repository name a remote ends in; `az` takes it decoded and finds org and project itself. |
| 61 | * Azure clone URLs carry no `.git` suffix, so a trailing `.git` is part of the name. |
| 62 | */ |
| 63 | function repositoryNameFromRemote(remoteUrl: string): string | null { |
| 64 | const path = URL.parse(remoteUrl.trim())?.pathname ?? remoteUrl.trim(); |
| 65 | const encoded = path.replace(/\/+$/u, "").split(/[/:]/u).at(-1); |
| 66 | if (!encoded) return null; |
| 67 | let name = encoded; |
| 68 | try { |
| 69 | name = decodeURIComponent(encoded); |
| 70 | } catch {} |
| 71 | // `az` replaces any argument value starting with `@` by the contents of that local file. |
| 72 | return name.startsWith("@") ? null : name; |
| 73 | } |
| 74 | |
| 75 | /** The remote T3 selected, which scopes `az` instead of the checkout's primary remote. */ |
| 76 | const remoteOf = (input: { readonly context?: { readonly remoteUrl: string } | undefined }) => |
| 77 | input.context === undefined ? {} : { remoteUrl: input.context.remoteUrl }; |
| 78 | |
| 79 | function toChangeRequest(summary: NormalizedAzureDevOpsPullRequestRecord): ChangeRequest { |
| 80 | return { |
| 81 | provider: SourceControlProviderKind.make("azure-devops"), |
| 82 | number: summary.number, |
| 83 | title: summary.title, |
| 84 | url: summary.url, |
| 85 | baseRefName: summary.baseRefName, |
| 86 | headRefName: summary.headRefName, |
| 87 | ...(summary.headSha !== undefined ? { headSha: summary.headSha } : {}), |
| 88 | state: summary.state, |
| 89 | ...(summary.isDraft === true ? { isDraft: true } : {}), |
| 90 | closedAt: summary.closedAt ?? null, |
| 91 | mergedAt: summary.mergedAt ?? null, |
| 92 | updatedAt: summary.updatedAt, |
| 93 | isCrossRepository: summary.isCrossRepository, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | export const make = Effect.gen(function* () { |
| 98 | const azure = yield* AzureDevOpsCli.AzureDevOpsCli; |
| 99 | const { git } = yield* SourceControlHost.SourceControlHost; |
| 100 | |
| 101 | return SourceControlProvider.SourceControlProvider.of({ |
| 102 | kind: SourceControlProviderKind.make("azure-devops"), |
| 103 | // Azure lists fork pull requests by source branch with no owner filter, so read a full page. |
| 104 | headBranchProbe: ({ headSelectors }) => ({ |
| 105 | headSelectors: headSelectors.filter((selector) => !selector.includes(":")), |
| 106 | limit: 100, |
| 107 | }), |
| 108 | readChangeRequestTemplate: Effect.fn( |
| 109 | "AzureDevOpsSourceControlProvider.readChangeRequestTemplate", |
| 110 | )( |
| 111 | function* ({ cwd }) { |
| 112 | // Azure uses the repository default branch even when the PR targets another branch. |
| 113 | const remoteName = yield* git.resolvePrimaryRemoteName(cwd); |
| 114 | const defaultRef = yield* git.execute({ |
| 115 | operation: "AzureDevOpsSourceControlProvider.resolveDefaultBranch", |
| 116 | cwd, |
| 117 | args: ["symbolic-ref", `refs/remotes/${remoteName}/HEAD`], |
| 118 | }); |
| 119 | return yield* AzureDevOpsChangeRequestTemplate.detect( |
| 120 | cwd, |
| 121 | defaultRef.stdout.trim(), |
| 122 | git.execute, |
| 123 | ); |
| 124 | }, |
| 125 | Effect.orElseSucceed(() => Option.none()), |
| 126 | ), |
| 127 | listChangeRequests: Effect.fn("AzureDevOpsSourceControlProvider.listChangeRequests")( |
| 128 | function (input) { |
| 129 | const source = SourceControlProvider.sourceControlRefFromInput(input); |
| 130 | return azure |
| 131 | .listPullRequests({ |
| 132 | cwd: input.cwd, |
| 133 | ...remoteOf(input), |
| 134 | headSelector: input.headSelector, |
| 135 | ...(source !== undefined ? { source } : {}), |
| 136 | state: input.state, |
| 137 | ...(input.limit !== undefined ? { limit: input.limit } : {}), |
| 138 | }) |
| 139 | .pipe( |
| 140 | Effect.map((items) => items.map(toChangeRequest)), |
| 141 | Effect.mapError( |
| 142 | (error) => |
| 143 | new SourceControlProviderError({ |
| 144 | provider: SourceControlProviderKind.make("azure-devops"), |
| 145 | operation: "listChangeRequests", |
| 146 | command: error.command, |
| 147 | cwd: input.cwd, |
| 148 | reference: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 149 | input.headSelector, |
| 150 | ), |
| 151 | detail: error.detail, |
| 152 | cause: error, |
| 153 | }), |
| 154 | ), |
| 155 | ); |
| 156 | }, |
| 157 | ), |
| 158 | getChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.getChangeRequest")( |
| 159 | function (input) { |
| 160 | return azure |
| 161 | .getPullRequest({ cwd: input.cwd, reference: input.reference, ...remoteOf(input) }) |
| 162 | .pipe( |
| 163 | Effect.map(toChangeRequest), |
| 164 | Effect.mapError( |
| 165 | (error) => |
| 166 | new SourceControlProviderError({ |
| 167 | provider: SourceControlProviderKind.make("azure-devops"), |
| 168 | operation: "getChangeRequest", |
| 169 | command: error.command, |
| 170 | cwd: input.cwd, |
| 171 | reference: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 172 | input.reference, |
| 173 | ), |
| 174 | detail: error.detail, |
| 175 | cause: error, |
| 176 | }), |
| 177 | ), |
| 178 | ); |
| 179 | }, |
| 180 | ), |
| 181 | createsDraftChangeRequests: true, |
| 182 | createChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.createChangeRequest")( |
| 183 | function (input) { |
| 184 | const source = SourceControlProvider.sourceControlRefFromInput(input); |
| 185 | return azure |
| 186 | .createPullRequest({ |
| 187 | cwd: input.cwd, |
| 188 | ...remoteOf(input), |
| 189 | baseBranch: input.baseRefName, |
| 190 | headSelector: input.headSelector, |
| 191 | ...(source !== undefined ? { source } : {}), |
| 192 | ...(input.target !== undefined ? { target: input.target } : {}), |
| 193 | title: input.title, |
| 194 | bodyFile: input.bodyFile, |
| 195 | ...(input.draft === true ? { draft: true } : {}), |
| 196 | }) |
| 197 | .pipe( |
| 198 | Effect.mapError( |
| 199 | (error) => |
| 200 | new SourceControlProviderError({ |
| 201 | provider: SourceControlProviderKind.make("azure-devops"), |
| 202 | operation: "createChangeRequest", |
| 203 | command: error.command, |
| 204 | cwd: input.cwd, |
| 205 | reference: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 206 | input.headSelector, |
| 207 | ), |
| 208 | detail: error.detail, |
| 209 | cause: error, |
| 210 | }), |
| 211 | ), |
| 212 | ); |
| 213 | }, |
| 214 | ), |
| 215 | getRepositoryCloneUrls: Effect.fn("AzureDevOpsSourceControlProvider.getRepositoryCloneUrls")( |
| 216 | function (input) { |
| 217 | return azure |
| 218 | .getRepositoryCloneUrls({ |
| 219 | cwd: input.cwd, |
| 220 | repository: input.repository, |
| 221 | ...remoteOf(input), |
| 222 | }) |
| 223 | .pipe( |
| 224 | Effect.mapError( |
| 225 | (error) => |
| 226 | new SourceControlProviderError({ |
| 227 | provider: SourceControlProviderKind.make("azure-devops"), |
| 228 | operation: "getRepositoryCloneUrls", |
| 229 | command: error.command, |
| 230 | cwd: input.cwd, |
| 231 | repository: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 232 | input.repository, |
| 233 | ), |
| 234 | detail: error.detail, |
| 235 | cause: error, |
| 236 | }), |
| 237 | ), |
| 238 | ); |
| 239 | }, |
| 240 | ), |
| 241 | createRepository: Effect.fn("AzureDevOpsSourceControlProvider.createRepository")( |
| 242 | function (input) { |
| 243 | return azure.createRepository(input).pipe( |
| 244 | Effect.mapError( |
| 245 | (error) => |
| 246 | new SourceControlProviderError({ |
| 247 | provider: SourceControlProviderKind.make("azure-devops"), |
| 248 | operation: "createRepository", |
| 249 | command: error.command, |
| 250 | cwd: input.cwd, |
| 251 | repository: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 252 | input.repository, |
| 253 | ), |
| 254 | detail: error.detail, |
| 255 | cause: error, |
| 256 | }), |
| 257 | ), |
| 258 | ); |
| 259 | }, |
| 260 | ), |
| 261 | getDefaultBranch: Effect.fn("AzureDevOpsSourceControlProvider.getDefaultBranch")( |
| 262 | function (input) { |
| 263 | const remoteUrl = input.context?.remoteUrl; |
| 264 | const repository = remoteUrl === undefined ? null : repositoryNameFromRemote(remoteUrl); |
| 265 | if (remoteUrl === undefined || repository === null) return Effect.succeed(null); |
| 266 | return azure.getDefaultBranch({ cwd: input.cwd, repository, remoteUrl }).pipe( |
| 267 | Effect.mapError( |
| 268 | (error) => |
| 269 | new SourceControlProviderError({ |
| 270 | provider: SourceControlProviderKind.make("azure-devops"), |
| 271 | operation: "getDefaultBranch", |
| 272 | command: error.command, |
| 273 | cwd: input.cwd, |
| 274 | detail: error.detail, |
| 275 | cause: error, |
| 276 | }), |
| 277 | ), |
| 278 | ); |
| 279 | }, |
| 280 | ), |
| 281 | // `az repos pr checkout` ignores git failures: in a single-branch clone its checkout fails |
| 282 | // and its pull then merges the pull request into the current branch. |
| 283 | checkoutChangeRequest: Effect.fn("AzureDevOpsSourceControlProvider.checkoutChangeRequest")( |
| 284 | function* (input) { |
| 285 | const pullRequest = yield* azure.getPullRequest({ |
| 286 | cwd: input.cwd, |
| 287 | reference: input.reference, |
| 288 | ...remoteOf(input), |
| 289 | }); |
| 290 | if (pullRequest.isCrossRepository) |
| 291 | return yield* new SourceControlProviderError({ |
| 292 | provider: SourceControlProviderKind.make("azure-devops"), |
| 293 | operation: "checkoutChangeRequest", |
| 294 | cwd: input.cwd, |
| 295 | reference: SourceControlProvider.transportSafeSourceControlErrorValue(input.reference), |
| 296 | detail: "Checking out an Azure DevOps pull request from a fork is not supported.", |
| 297 | }); |
| 298 | const branch = pullRequest.headRefName; |
| 299 | const remoteBranch = { |
| 300 | cwd: input.cwd, |
| 301 | remoteName: input.context?.remoteName ?? "origin", |
| 302 | remoteBranch: branch, |
| 303 | }; |
| 304 | // An existing branch is only switched to; the caller advances it without losing commits. |
| 305 | if ((yield* git.listLocalBranchNames(input.cwd)).includes(branch)) |
| 306 | yield* git.fetchRemoteTrackingBranch(remoteBranch); |
| 307 | else yield* git.fetchRemoteBranch({ ...remoteBranch, localBranch: branch }); |
| 308 | yield* git.switchRef({ cwd: input.cwd, refName: branch }); |
| 309 | }, |
| 310 | (effect, input) => |
| 311 | effect.pipe( |
| 312 | Effect.mapError((error) => |
| 313 | error._tag === "SourceControlProviderError" |
| 314 | ? error |
| 315 | : new SourceControlProviderError({ |
| 316 | provider: SourceControlProviderKind.make("azure-devops"), |
| 317 | operation: "checkoutChangeRequest", |
| 318 | command: error.command, |
| 319 | cwd: input.cwd, |
| 320 | reference: SourceControlProvider.transportSafeSourceControlErrorValue( |
| 321 | input.reference, |
| 322 | ), |
| 323 | detail: error.detail, |
| 324 | cause: error, |
| 325 | }), |
| 326 | ), |
| 327 | ), |
| 328 | ), |
| 329 | }); |
| 330 | }); |