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

definition.ts

64 lines2.6 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { canonicalRepositoryKey } from "@t3tools/shared/sourceControl";
3import {
4 defineSourceControlClient,
5 isChangeRequestAuthorityOfProject,
6 isChangeRequestPath,
7} from "@t3tools/source-control-core/client/definition";
8​
9const CHECKOUT_COMMAND = /^az\s+repos\s+pr\s+checkout\s+(.+)$/i;
10const CHANGE_REQUEST_REFERENCE =
11 /^https:\/\/(?:dev\.azure\.com\/[^/\s]+\/[^/\s]+|[^/\s]+\.visualstudio\.com\/[^/\s]+)\/_git\/[^/\s]+\/pullrequest\/(\d+)(?:[/?#].*)?$/i;
12​
13function checkoutCommandId(args: string): string | null {
14 const parts = args.trim().split(/\s+/).filter(Boolean);
15 for (const [index, part] of parts.entries()) {
16 if (part === "--id" || part === "-i") return parts[index + 1] ?? null;
17 if (part.startsWith("--id=")) return part.slice("--id=".length) || null;
18 }
19 return parts.find((part) => !part.startsWith("-")) ?? null;
21​
22export const definition = defineSourceControlClient({
23 kind: SourceControlProviderKind.make("azure-devops"),
24 label: "Azure DevOps",
25 pickerLabel: "Azure DevOps",
26 icon: "azure-devops",
27 changeRequest: { shortLabel: "PR", singular: "pull request" },
28 repositoryPathHint: "project/repository",
29 // `az repos` reads need the checkout's organization and project, so a bare hostname names no
30 // repository this client can act on.
31 publicHost: null,
32 publishDescription: "dev.azure.com",
33 publishHost: () => "dev.azure.com",
34 newRepositoryOwner: () => null,
35 defaultCloneTransport: "ssh",
36 changeRequestUrl: ({ host, repository, number }) =>
37 `https://${canonicalRepositoryKey(`${host}/${repository}`.toLowerCase())}/pullrequest/${number}`,
38 changeRequestActions: new Set([
39 "merge",
40 "ready",
41 "draft",
42 "close",
43 "reopen",
44 "enable-auto-merge",
45 "disable-auto-merge",
46 ] as const),
47 checkoutCommand: ({ number }) => `az repos pr checkout --id ${number}`,
48 authorProfileUrl: () => null,
49 referenceAutolinkRepositoryUrl: () => null,
50 reviewSummaryRequired: () => false,
51 checkoutCommandArgument: (input) => {
52 const args = CHECKOUT_COMMAND.exec(input)?.[1];
53 return args ? checkoutCommandId(args) : null;
54 },
55 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
56 changeRequestUrlHost: (url) => url.hostname,
57 checkoutChangeRequestHost: () => null,
58 isChangeRequestInRepository: (identity, link) =>
59 isChangeRequestAuthorityOfProject(identity, link) &&
60 canonicalRepositoryKey(identity.canonicalKey.toLowerCase()) ===
61 canonicalRepositoryKey(`${link.host}/${link.repository}`.toLowerCase()),
62 canReadChangeRequestOnHost: () => false,
63 isChangeRequestUrl: (url) => isChangeRequestPath(url, "/pullrequest/"),
64});