The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { RepositoryIdentity } from "@t3tools/contracts"; |
| 2 | import { canonicalizeGitCafePullRequestUrl } from "@t3tools/source-control-definitions/registry"; |
| 3 | |
| 4 | import { sourceControlClients, type ChangeRequestTerminology } from "./sourceControlClients.ts"; |
| 5 | import { mFallback, msg } from "gt-i18n"; |
| 6 | import type { MFunctionType } from "gt-i18n/types"; |
| 7 | |
| 8 | const PULL_REQUEST_NUMBER_PATTERN = /^#?(\d+)$/; |
| 9 | |
| 10 | export function parsePullRequestReference(input: string): string | null { |
| 11 | const trimmed = input.trim(); |
| 12 | if (trimmed.length === 0) { |
| 13 | return null; |
| 14 | } |
| 15 | |
| 16 | const normalizedInput = |
| 17 | sourceControlClients.definitions |
| 18 | .map((definition) => definition.checkoutCommandArgument(trimmed)) |
| 19 | .find((argument) => argument !== null) ?? trimmed; |
| 20 | if (normalizedInput.length === 0) { |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | if ( |
| 25 | sourceControlClients.definitions.some((definition) => |
| 26 | definition.isChangeRequestReference(normalizedInput), |
| 27 | ) |
| 28 | ) { |
| 29 | return normalizedInput; |
| 30 | } |
| 31 | |
| 32 | const numberMatch = PULL_REQUEST_NUMBER_PATTERN.exec(normalizedInput); |
| 33 | if (numberMatch?.[1]) { |
| 34 | return numberMatch[1]; |
| 35 | } |
| 36 | |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | /** The thread project's repository, so a bare number resolves to one of its pull requests. */ |
| 41 | export function pullRequestLinkProject(identity: RepositoryIdentity | null | undefined) { |
| 42 | if (!identity) return null; |
| 43 | const repository = |
| 44 | identity.displayName ?? |
| 45 | (identity.owner && identity.name ? `${identity.owner}/${identity.name}` : null); |
| 46 | if (repository === null) return null; |
| 47 | const definition = sourceControlClients.get(identity.provider ?? "unknown"); |
| 48 | const host = sourceControlClients.changeRequestHost(identity, definition.kind); |
| 49 | return { |
| 50 | host, |
| 51 | repository, |
| 52 | terminology: definition.changeRequest, |
| 53 | webUrl: (number: number) => |
| 54 | definition.changeRequestUrl({ |
| 55 | host, |
| 56 | repository, |
| 57 | number, |
| 58 | remoteUrl: identity.locator.remoteUrl, |
| 59 | webUrl: identity.webUrl, |
| 60 | }), |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | interface ResolvedLink { |
| 65 | readonly host: string; |
| 66 | readonly repository: string; |
| 67 | readonly number: number; |
| 68 | readonly url: string; |
| 69 | } |
| 70 | |
| 71 | export function resolveLinkPullRequestInput( |
| 72 | input: { |
| 73 | readonly reference: string; |
| 74 | readonly project: { |
| 75 | readonly host: string; |
| 76 | readonly repository: string; |
| 77 | readonly terminology: ChangeRequestTerminology; |
| 78 | readonly webUrl: (number: number) => string | null; |
| 79 | } | null; |
| 80 | readonly hasProject: (reference: ResolvedLink) => boolean; |
| 81 | }, |
| 82 | resolve: MFunctionType = mFallback, |
| 83 | ): { link: ResolvedLink } | { error: string } | null { |
| 84 | const parsed = |
| 85 | sourceControlClients.parseChangeRequestUrl(input.reference.trim()) !== null |
| 86 | ? input.reference.trim() |
| 87 | : parsePullRequestReference(input.reference); |
| 88 | if (parsed === null) return null; |
| 89 | const url = sourceControlClients.parseChangeRequestUrl(parsed)?.link; |
| 90 | if (url !== undefined) { |
| 91 | if (!input.hasProject({ ...url, url: parsed })) { |
| 92 | return { |
| 93 | error: resolve( |
| 94 | msg("No project in this environment can read {host}/{repository}.", { |
| 95 | host: url.host, |
| 96 | repository: url.repository, |
| 97 | }), |
| 98 | ), |
| 99 | }; |
| 100 | } |
| 101 | return { |
| 102 | link: { |
| 103 | host: url.host, |
| 104 | repository: url.repository, |
| 105 | number: url.number, |
| 106 | url: canonicalizeGitCafePullRequestUrl(parsed), |
| 107 | }, |
| 108 | }; |
| 109 | } |
| 110 | const number = Number(parsed); |
| 111 | if (!Number.isSafeInteger(number) || number < 1) return null; |
| 112 | if (input.project === null) { |
| 113 | return { |
| 114 | error: resolve( |
| 115 | msg( |
| 116 | "Paste the full pull request URL; this thread has no repository to resolve the number against.", |
| 117 | ), |
| 118 | ), |
| 119 | }; |
| 120 | } |
| 121 | const webUrl = input.project.webUrl(number); |
| 122 | const webReference = |
| 123 | webUrl === null ? undefined : sourceControlClients.parseChangeRequestUrl(webUrl)?.link; |
| 124 | if (webUrl === null || webReference === undefined) { |
| 125 | return { |
| 126 | error: resolve( |
| 127 | msg("Paste a full URL; this project's host has no known {changeRequest} URL.", { |
| 128 | changeRequest: input.project.terminology.singular, |
| 129 | }), |
| 130 | ), |
| 131 | }; |
| 132 | } |
| 133 | return { |
| 134 | link: { |
| 135 | host: webReference.host, |
| 136 | repository: webReference.repository, |
| 137 | number: webReference.number, |
| 138 | url: webUrl, |
| 139 | }, |
| 140 | }; |
| 141 | } |