The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { parsePullRequestAnchor } from "@t3tools/shared/pullRequestAnchor"; |
| 2 | import { resolveChangeRequestLinkTarget } from "@t3tools/client-runtime/change-request-links"; |
| 3 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 4 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 5 | import { EnvironmentId, ProjectId, type IssueRef } from "@t3tools/contracts"; |
| 6 | |
| 7 | /** The PullRequest route for a change request link from a project, or null for the browser. */ |
| 8 | export function nativePullRequestDestination( |
| 9 | url: string, |
| 10 | context: { environmentId: string; projectId: string } | null, |
| 11 | projects: ReadonlyArray<Pick<EnvironmentProject, "id" | "environmentId" | "repositoryIdentity">>, |
| 12 | configLoaded: (environmentId: EnvironmentId) => boolean, |
| 13 | ) { |
| 14 | if (!context) return null; |
| 15 | const target = resolveChangeRequestLinkTarget({ |
| 16 | url, |
| 17 | projects, |
| 18 | configLoaded, |
| 19 | environmentId: EnvironmentId.make(context.environmentId), |
| 20 | projectId: ProjectId.make(context.projectId), |
| 21 | lendHostProject: true, |
| 22 | }); |
| 23 | if (target === null) return null; |
| 24 | return { |
| 25 | environmentId: context.environmentId, |
| 26 | projectId: String(target.project.id), |
| 27 | repository: target.repository, |
| 28 | number: target.number, |
| 29 | ...(parsePullRequestAnchor(url) ? { navigationUrl: url } : {}), |
| 30 | ...(target.host === undefined ? {} : { host: target.host }), |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * The pull request an issue link may name instead, on a host that numbers both in one sequence |
| 36 | * and writes every `#12` through its issue route. Null elsewhere. Only a candidate to read. |
| 37 | */ |
| 38 | export function pullRequestCandidateForIssueLink( |
| 39 | url: string, |
| 40 | ref: Pick<IssueRef, "projectId" | "host" | "scope" | "key">, |
| 41 | ) { |
| 42 | const candidateUrl = sourceControlClients.changeRequestUrlFromReferenceAutolink(url); |
| 43 | const number = Number(ref.key); |
| 44 | if (candidateUrl === null || !Number.isInteger(number) || number <= 0) return null; |
| 45 | return { |
| 46 | target: { |
| 47 | projectId: ref.projectId, |
| 48 | ...(ref.host === undefined ? {} : { host: ref.host }), |
| 49 | repository: ref.scope, |
| 50 | number, |
| 51 | }, |
| 52 | navigationUrl: parsePullRequestAnchor(candidateUrl) ? candidateUrl : undefined, |
| 53 | }; |
| 54 | } |