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

sourceControlDefinitions.ts

70 lines3.5 KB

1import * as AzureDevOpsDriver from "@t3tools/source-control-azure-devops/server/driver";
2import * as BitbucketDriver from "@t3tools/source-control-bitbucket/server/driver";
3import * as CursorOriginDriver from "@t3tools/source-control-cursor-origin/server/driver";
4import { makeBuiltInSourceControlDefinitions } from "@t3tools/source-control-definitions/registry";
5import * as ForgejoDriver from "@t3tools/source-control-forgejo/server/driver";
6import * as GitCafeDriver from "@t3tools/source-control-gitcafe/server/driver";
7import * as GitHubDriver from "@t3tools/source-control-github/server/driver";
8import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
9import * as TanStackSourceDriver from "@t3tools/source-control-tanstack-source/server/driver";
10​
11/**
12 * TanStack Source is in private beta, so its forge exists only on a server started with
13 * `T3CODE_TANSTACK_SOURCE=1`. Read once at load: the registry below is a module constant.
14 */
15const tanStackSourceEnabled = process.env.T3CODE_TANSTACK_SOURCE?.trim() === "1";
16​
17/** In the shared registry's order, which is the order hosts are listed and probed in. */
18export const BUILT_IN_SOURCE_CONTROL_DRIVER_LIST = [
19 GitHubDriver.driver,
20 CursorOriginDriver.driver,
21 GitLabDriver.driver,
22 GitCafeDriver.driver,
23 ...(tanStackSourceEnabled ? [TanStackSourceDriver.driver] : []),
24 ForgejoDriver.driver,
25 BitbucketDriver.driver,
26 AzureDevOpsDriver.driver,
27];
28​
29export const sourceControlDefinitions = makeBuiltInSourceControlDefinitions({
30 tanStackSource: tanStackSourceEnabled,
31});
32​
33/** A host CLI command that merges or closes a change request, such as `gh pr merge`. */
34export const isChangeRequestCloseCommand = (command: string) =>
35 sourceControlDefinitions.definitions.some(
36 (definition) => definition.changeRequestCloseCommand?.test(command) === true,
37 );
38​
39// A quoted or bare shell word. An unclosed quote, as Codex's `/bin/zsh -lc "gh pr merge 19"`
40// leaves after the match, runs to the end.
41const SHELL_WORD = /"((?:\\[\s\S]|[^"\\])*)"?|'([^']*)'?|((?:\\[\s\S]|[^\s"'\\])+)/gu;
42// A whole word that names one, so a number inside `--subject "Release 20"` does not.
43const NAMED_CHANGE_REQUEST = /^(?:--?[\w-]+=)?#?(\d+)$|^\S*\/(?:pulls?|merge_requests)\/(\d+)\/?$/u;
44​
45/**
46 * The change request numbers a merge or close command names, or "any" when one names none, as a
47 * bare `gh pr merge` on the checked-out branch or `gh stack merge` does.
48 */
49export function changeRequestsClosedBy(command: string): ReadonlySet<number> | "any" {
50 const named = new Set<number>();
51 for (const { changeRequestCloseCommand: close } of sourceControlDefinitions.definitions) {
52 if (close === undefined) continue;
53 for (const match of command.matchAll(new RegExp(close.source, `${close.flags}g`))) {
54 // From the match's start, since Azure's match runs through `--id` to `--status`.
55 const args = command.slice(match.index).split(/[;&|\n]/u, 1)[0] ?? "";
56 // Every host's merge and close takes one change request, so any later one is option text.
57 let target: string | undefined;
58 for (const [, double, single, bare] of args.matchAll(SHELL_WORD)) {
59 // A comment ends the command.
60 if (bare !== undefined && /^#(?!\d+$)/u.test(bare)) break;
61 const [, number, url] = NAMED_CHANGE_REQUEST.exec(double ?? single ?? bare ?? "") ?? [];
62 target = number ?? url;
63 if (target !== undefined) break;
64 }
65 if (target === undefined) return "any";
66 named.add(Number(target));
67 }
68 }
69 return named.size === 0 ? "any" : named;