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

definition.ts

158 lines6.6 KB

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