The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { canonicalRepositoryPath } from "@t3tools/shared/git"; |
| 3 | import { |
| 4 | buildCommitUrl, |
| 5 | changeRequestUrlBelow, |
| 6 | defineSourceControl, |
| 7 | isChangeRequestAuthorityOfProject, |
| 8 | parseChangeRequestPath, |
| 9 | parseIssuePath, |
| 10 | parseCommitPath, |
| 11 | remoteWebOrigin, |
| 12 | } from "@t3tools/source-control-core/shared/definition"; |
| 13 | import { icon } from "./icon.ts"; |
| 14 | |
| 15 | // Azure DevOps Server runs on any hostname; its `_git` route identifies it. |
| 16 | const CHANGE_REQUEST_PATH = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u; |
| 17 | const COMMIT_PATH = /^\/((?:[^/]+\/)*_git\/[^/]+)\/commit\/([0-9a-f]+)\/?$/iu; |
| 18 | // Work items belong to the project: /{organization}/{project}/_workitems/edit/{id}. |
| 19 | const WORK_ITEM_PATH = /^\/([^/]+\/[^/]+)\/_workitems\/edit\/(\d+)(?:\/|$)/u; |
| 20 | // A legacy organization is its hostname: https://{organization}.visualstudio.com/{project}/... |
| 21 | const LEGACY_WORK_ITEM_PATH = /^\/([^/]+)\/_workitems\/edit\/(\d+)(?:\/|$)/u; |
| 22 | const CHECKOUT_COMMAND = /^az\s+repos\s+pr\s+checkout\s+(.+)$/i; |
| 23 | const CHANGE_REQUEST_REFERENCE = |
| 24 | /^https:\/\/(?:dev\.azure\.com\/[^/\s]+\/[^/\s]+|[^/\s]+\.visualstudio\.com\/(?:DefaultCollection\/)?[^/\s]+)\/_git\/[^/\s]+\/pullrequest\/(\d+)(?:[/?#].*)?$/i; |
| 25 | |
| 26 | // `ssh.dev.azure.com` is the default SSH clone host (git@ssh.dev.azure.com:v3/org/project/repo), |
| 27 | // so any `*.dev.azure.com` subdomain is Azure. Legacy hosts stay under `.visualstudio.com`, |
| 28 | // including `vs-ssh.visualstudio.com`. |
| 29 | function isAzureHostname(hostname: string): boolean { |
| 30 | return ( |
| 31 | hostname === "dev.azure.com" || |
| 32 | hostname.endsWith(".dev.azure.com") || |
| 33 | hostname.endsWith(".visualstudio.com") |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Azure alone addresses one repository under two names that share no part: `ssh.dev.azure.com` and |
| 39 | * `v3/{org}/{project}/{repo}` over SSH, against `dev.azure.com` and `{org}/{project}/_git/{repo}` |
| 40 | * everywhere a person sees it. A project cloned over SSH would otherwise be a different repository |
| 41 | * to every comparison made against a pull request URL, which arrives in the web spelling. |
| 42 | */ |
| 43 | function repositoryKeyFromRemote(key: string): string | null { |
| 44 | const match = |
| 45 | /^(ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u.exec(key); |
| 46 | if (match === null) { |
| 47 | // Azure takes a name as written, so its keys keep the `.git` other hosts drop. |
| 48 | return isAzureHostname(key.split("/", 1)[0]!) ? key : null; |
| 49 | } |
| 50 | const [, host, organization, project, repository] = match; |
| 51 | return host === "ssh.dev.azure.com" |
| 52 | ? `dev.azure.com/${organization}/${project}/_git/${repository}` |
| 53 | : `${organization}.visualstudio.com/${project}/_git/${repository}`; |
| 54 | } |
| 55 | |
| 56 | /** Every spelling of an Azure DevOps Services repository, on `dev.azure.com`. */ |
| 57 | function canonicalRepositoryKey(key: string): string | null { |
| 58 | const canonical = key |
| 59 | .replace( |
| 60 | /^(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u, |
| 61 | "dev.azure.com/$1/$2/_git/$3", |
| 62 | ) |
| 63 | .replace( |
| 64 | /^([^.]+)\.visualstudio\.com\/(?:defaultcollection\/)?([^/]+)\/_git\/([^/]+)$/u, |
| 65 | "dev.azure.com/$1/$2/_git/$3", |
| 66 | ); |
| 67 | return canonical.startsWith("dev.azure.com/") ? canonical : null; |
| 68 | } |
| 69 | |
| 70 | function decodeUrlPathSegment(segment: string): string { |
| 71 | try { |
| 72 | return decodeURIComponent(segment); |
| 73 | } catch { |
| 74 | return segment; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function checkoutCommandId(args: string): string | null { |
| 79 | const parts = args.trim().split(/\s+/).filter(Boolean); |
| 80 | for (const [index, part] of parts.entries()) { |
| 81 | if (part === "--id" || part === "-i") return parts[index + 1] ?? null; |
| 82 | if (part.startsWith("--id=")) return part.slice("--id=".length) || null; |
| 83 | } |
| 84 | return parts.find((part) => !part.startsWith("-")) ?? null; |
| 85 | } |
| 86 | |
| 87 | const KIND = SourceControlProviderKind.make("azure-devops"); |
| 88 | |
| 89 | export const definition = defineSourceControl({ |
| 90 | kind: KIND, |
| 91 | label: "Azure DevOps", |
| 92 | pickerLabel: "Azure DevOps", |
| 93 | icon, |
| 94 | changeRequest: { |
| 95 | shortLabel: "PR", |
| 96 | shortLabelWithArticle: "a PR", |
| 97 | singular: "pull request", |
| 98 | plural: "pull requests", |
| 99 | }, |
| 100 | repositoryPathHint: "project/repository", |
| 101 | // `az repos` reads need the checkout's organization and project, so a bare hostname names no |
| 102 | // repository this client can act on. |
| 103 | publicHost: null, |
| 104 | publishDescription: "dev.azure.com", |
| 105 | publishHost: () => "dev.azure.com", |
| 106 | newRepositoryOwner: () => null, |
| 107 | defaultCloneTransport: "ssh", |
| 108 | changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) => { |
| 109 | if (webUrl) return changeRequestUrlBelow(webUrl, `pullrequest/${number}`); |
| 110 | const key = `${host}/${repository}`.toLowerCase(); |
| 111 | const [canonicalHost = host, ...path] = (canonicalRepositoryKey(key) ?? key).split("/"); |
| 112 | return `${remoteWebOrigin(canonicalHost, remoteUrl)}/${path.join("/")}/pullrequest/${number}`; |
| 113 | }, |
| 114 | commitUrl: (input) => { |
| 115 | const key = `${input.host}/${input.repository}`.toLowerCase(); |
| 116 | const [host = input.host, ...path] = (canonicalRepositoryKey(key) ?? key).split("/"); |
| 117 | return buildCommitUrl({ ...input, host, repository: path.join("/") }, "commit"); |
| 118 | }, |
| 119 | parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH), |
| 120 | // Servers on different ports are different servers, so a port stays in the link. |
| 121 | parseChangeRequestUrl: (url) => { |
| 122 | const link = parseChangeRequestPath(url, CHANGE_REQUEST_PATH); |
| 123 | return link === null || url.port === "" ? link : { ...link, authority: url.host.toLowerCase() }; |
| 124 | }, |
| 125 | // A project named with spaces arrives percent-encoded; the binding it is matched against is not. |
| 126 | parseIssueUrl: (url) => { |
| 127 | const legacy = url.hostname.toLowerCase().endsWith(".visualstudio.com"); |
| 128 | const link = |
| 129 | parseIssuePath(url, WORK_ITEM_PATH) ?? |
| 130 | (legacy ? parseIssuePath(url, LEGACY_WORK_ITEM_PATH) : null); |
| 131 | if (link === null) return null; |
| 132 | const scope = link.scope.split("/").map(decodeUrlPathSegment).join("/").toLowerCase(); |
| 133 | if (!legacy) return { ...link, scope }; |
| 134 | // On legacy hosts the default collection is optional in a work item URL. |
| 135 | const scopeAlias = |
| 136 | /^defaultcollection\/([^/]+)$/u.exec(scope)?.[1] ?? |
| 137 | (scope.includes("/") ? undefined : `defaultcollection/${scope}`); |
| 138 | return { ...link, scope, ...(scopeAlias === undefined ? {} : { scopeAlias }) }; |
| 139 | }, |
| 140 | changeRequestUrlFromReferenceAutolink: () => null, |
| 141 | changeRequestActions: new Set([ |
| 142 | "merge", |
| 143 | "ready", |
| 144 | "draft", |
| 145 | "close", |
| 146 | "reopen", |
| 147 | "enable-auto-merge", |
| 148 | "disable-auto-merge", |
| 149 | ] as const), |
| 150 | checkoutCommand: ({ number }) => `az repos pr checkout --id ${number}`, |
| 151 | // Completing or abandoning is a status change, the same `update` this package runs. |
| 152 | changeRequestCloseCommand: |
| 153 | /\baz\s+repos\s+pr\s+update\b[^;&|]*--status[\s=]+["']?(?:completed|abandoned)\b/u, |
| 154 | authorProfileUrl: () => null, |
| 155 | hardLineBreaks: true, |
| 156 | reviewSummaryRequired: () => false, |
| 157 | checkoutCommandArgument: (input) => { |
| 158 | const args = CHECKOUT_COMMAND.exec(input)?.[1]; |
| 159 | return args ? checkoutCommandId(args) : null; |
| 160 | }, |
| 161 | isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url), |
| 162 | checkoutChangeRequestHost: () => null, |
| 163 | recognizeRemote: ({ host, hostname }) => |
| 164 | isAzureHostname(hostname) |
| 165 | ? { kind: KIND, name: "Azure DevOps", baseUrl: `https://${host}` } |
| 166 | : null, |
| 167 | /** |
| 168 | * `az repos pr list --repository` takes a name; organisation and project come from checkout. |
| 169 | * Decode the remote URL's name segment: `az` treats escapes literally and returns 404. |
| 170 | */ |
| 171 | repositorySelector: (identity) => { |
| 172 | const segments = (identity.displayName ?? "").split("/").filter((part) => part !== "_git"); |
| 173 | const name = identity.name || segments.at(-1); |
| 174 | return name ? decodeUrlPathSegment(name) : null; |
| 175 | }, |
| 176 | // Work items belong to the project: `organization/project` out of the remote's |
| 177 | // `organization/project/_git/repository`. A shorter path depends on the host: `acme/_git/web` |
| 178 | // is a project named after its repository on dev.azure.com and a bare project on a legacy host. |
| 179 | issueScope: (identity) => { |
| 180 | const segments = (identity.displayName ?? "") |
| 181 | .split("/") |
| 182 | .filter((part) => part !== "") |
| 183 | .map(decodeUrlPathSegment); |
| 184 | const at = segments.indexOf("_git"); |
| 185 | if (at === 2) return segments.slice(0, at).join("/"); |
| 186 | const repository = segments[at + 1]; |
| 187 | if ((at !== 0 && at !== 1) || repository === undefined) return null; |
| 188 | const host = identity.canonicalKey?.split("/", 1)[0]?.toLowerCase() ?? ""; |
| 189 | const legacy = host.endsWith(".visualstudio.com"); |
| 190 | const [first = ""] = segments; |
| 191 | if (at === 0) return legacy ? repository : null; |
| 192 | if (host === "dev.azure.com") return `${first}/${repository}`; |
| 193 | if (!legacy) return null; |
| 194 | // Work item URLs on a legacy host leave the collection out. |
| 195 | return first.toLowerCase() === "defaultcollection" ? repository : first; |
| 196 | }, |
| 197 | repositoryKeyFromRemote, |
| 198 | canonicalRepositoryKey, |
| 199 | isChangeRequestInRepository: (identity, link) => { |
| 200 | if (!isChangeRequestAuthorityOfProject(identity, link)) return false; |
| 201 | const projectKey = canonicalRepositoryPath(identity.canonicalKey); |
| 202 | const linkKey = canonicalRepositoryPath(`${link.host}/${link.repository}`); |
| 203 | return ( |
| 204 | (canonicalRepositoryKey(projectKey) ?? projectKey) === |
| 205 | (canonicalRepositoryKey(linkKey) ?? linkKey) |
| 206 | ); |
| 207 | }, |
| 208 | canReadChangeRequestOnHost: () => false, |
| 209 | }); |