Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import {
3 changeRequestUrlBelow,
4 defineSourceControl,
5 isChangeRequestAuthorityOfProject,
6 parseChangeRequestPath,
7 remoteWebOrigin,
8} from "@t3tools/source-control-core/shared/definition";
9import { icon } from "./icon.ts";
10
11// Azure DevOps Server runs on any hostname; its `_git` route identifies it.
12const CHANGE_REQUEST_PATH = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u;
13const CHECKOUT_COMMAND = /^az\s+repos\s+pr\s+checkout\s+(.+)$/i;
14const CHANGE_REQUEST_REFERENCE =
15 /^https:\/\/(?:dev\.azure\.com\/[^/\s]+\/[^/\s]+|[^/\s]+\.visualstudio\.com\/[^/\s]+)\/_git\/[^/\s]+\/pullrequest\/(\d+)(?:[/?#].*)?$/i;
16
17/**
18 * Azure alone addresses one repository under two names that share no part: `ssh.dev.azure.com` and
19 * `v3/{org}/{project}/{repo}` over SSH, against `dev.azure.com` and `{org}/{project}/_git/{repo}`
20 * everywhere a person sees it. A project cloned over SSH would otherwise be a different repository
21 * to every comparison made against a pull request URL, which arrives in the web spelling.
22 */
23function repositoryKeyFromRemote(key: string): string | null {
24 const match =
25 /^(ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u.exec(key);
26 if (match === null) return null;
27 const [, host, organization, project, repository] = match;
28 return host === "ssh.dev.azure.com"
29 ? `dev.azure.com/${organization}/${project}/_git/${repository}`
30 : `${organization}.visualstudio.com/${project}/_git/${repository}`;
31}
32
33/** Every spelling of an Azure DevOps Services repository, on `dev.azure.com`. */
34function canonicalRepositoryKey(key: string): string | null {
35 const canonical = key
36 .replace(
37 /^(?:ssh\.dev\.azure\.com|vs-ssh\.visualstudio\.com)\/v3\/([^/]+)\/([^/]+)\/([^/]+)$/u,
38 "dev.azure.com/$1/$2/_git/$3",
39 )
40 .replace(
41 /^([^.]+)\.visualstudio\.com\/(?:defaultcollection\/)?([^/]+)\/_git\/([^/]+)$/u,
42 "dev.azure.com/$1/$2/_git/$3",
43 );
44 return canonical.startsWith("dev.azure.com/") ? canonical : null;
45}
46
47function decodeUrlPathSegment(segment: string): string {
48 try {
49 return decodeURIComponent(segment);
50 } catch {
51 return segment;
52 }
53}
54
55function checkoutCommandId(args: string): string | null {
56 const parts = args.trim().split(/\s+/).filter(Boolean);
57 for (const [index, part] of parts.entries()) {
58 if (part === "--id" || part === "-i") return parts[index + 1] ?? null;
59 if (part.startsWith("--id=")) return part.slice("--id=".length) || null;
60 }
61 return parts.find((part) => !part.startsWith("-")) ?? null;
62}
63
64const KIND = SourceControlProviderKind.make("azure-devops");
65
66export const definition = defineSourceControl({
67 kind: KIND,
68 label: "Azure DevOps",
69 pickerLabel: "Azure DevOps",
70 icon,
71 changeRequest: {
72 shortLabel: "PR",
73 shortLabelWithArticle: "a PR",
74 singular: "pull request",
75 plural: "pull requests",
76 },
77 repositoryPathHint: "project/repository",
78 // `az repos` reads need the checkout's organization and project, so a bare hostname names no
79 // repository this client can act on.
80 publicHost: null,
81 publishDescription: "dev.azure.com",
82 publishHost: () => "dev.azure.com",
83 newRepositoryOwner: () => null,
84 defaultCloneTransport: "ssh",
85 changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) => {
86 if (webUrl) return changeRequestUrlBelow(webUrl, `pullrequest/${number}`);
87 const key = `${host}/${repository}`.toLowerCase();
88 const [canonicalHost = host, ...path] = (canonicalRepositoryKey(key) ?? key).split("/");
89 return `${remoteWebOrigin(canonicalHost, remoteUrl)}/${path.join("/")}/pullrequest/${number}`;
90 },
91 // Servers on different ports are different servers, so a port stays in the link.
92 parseChangeRequestUrl: (url) => {
93 const link = parseChangeRequestPath(url, CHANGE_REQUEST_PATH);
94 return link === null || url.port === "" ? link : { ...link, authority: url.host.toLowerCase() };
95 },
96 changeRequestUrlFromReferenceAutolink: () => null,
97 changeRequestActions: new Set([
98 "merge",
99 "ready",
100 "draft",
101 "close",
102 "reopen",
103 "enable-auto-merge",
104 "disable-auto-merge",
105 ] as const),
106 checkoutCommand: ({ number }) => `az repos pr checkout --id ${number}`,
107 // Completing or abandoning is a status change, the same `update` this package runs.
108 changeRequestCloseCommand:
109 /\baz\s+repos\s+pr\s+update\b[^;&|]*--status[\s=]+["']?(?:completed|abandoned)\b/u,
110 authorProfileUrl: () => null,
111 reviewSummaryRequired: () => false,
112 checkoutCommandArgument: (input) => {
113 const args = CHECKOUT_COMMAND.exec(input)?.[1];
114 return args ? checkoutCommandId(args) : null;
115 },
116 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
117 checkoutChangeRequestHost: () => null,
118 // `ssh.dev.azure.com` is the default SSH clone host (git@ssh.dev.azure.com:v3/org/project/repo),
119 // so any `*.dev.azure.com` subdomain is Azure. Legacy hosts stay under `.visualstudio.com`,
120 // including `vs-ssh.visualstudio.com`.
121 recognizeRemote: ({ host, hostname }) =>
122 hostname === "dev.azure.com" ||
123 hostname.endsWith(".dev.azure.com") ||
124 hostname.endsWith(".visualstudio.com")
125 ? { kind: KIND, name: "Azure DevOps", baseUrl: `https://${host}` }
126 : null,
127 /**
128 * `az repos pr list --repository` takes a name; organisation and project come from checkout.
129 * Decode the remote URL's name segment: `az` treats escapes literally and returns 404.
130 */
131 repositorySelector: (identity) => {
132 const segments = (identity.displayName ?? "").split("/").filter((part) => part !== "_git");
133 const name = identity.name || segments.at(-1);
134 return name ? decodeUrlPathSegment(name) : null;
135 },
136 repositoryKeyFromRemote,
137 canonicalRepositoryKey,
138 isChangeRequestInRepository: (identity, link) => {
139 if (!isChangeRequestAuthorityOfProject(identity, link)) return false;
140 const projectKey = identity.canonicalKey.toLowerCase();
141 const linkKey = `${link.host}/${link.repository}`.toLowerCase();
142 return (
143 (canonicalRepositoryKey(projectKey) ?? projectKey) ===
144 (canonicalRepositoryKey(linkKey) ?? linkKey)
145 );
146 },
147 canReadChangeRequestOnHost: () => false,
148});