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

definition.ts

168 lines6.9 KB

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