The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | ProjectId, |
| 3 | type PullRequestRef, |
| 4 | type RepositoryIdentity, |
| 5 | type ThreadPullRequestKey, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts"; |
| 8 | import { normalizeThreadPullRequestKey } from "../sourceControl/threadPullRequests.ts"; |
| 9 | |
| 10 | export function pullRequestSyncKey( |
| 11 | reference: PullRequestRef, |
| 12 | identity?: RepositoryIdentity | null, |
| 13 | ): ThreadPullRequestKey | null { |
| 14 | const host = identity |
| 15 | ? sourceControlDefinitions.changeRequestHost(identity, identity.provider) |
| 16 | : undefined; |
| 17 | // A host whose tools select a repository by something other than its path (Azure DevOps takes |
| 18 | // the bare name) can only key such a reference through the project's own repository. |
| 19 | const definition = identity?.provider |
| 20 | ? sourceControlDefinitions.find(identity.provider) |
| 21 | : undefined; |
| 22 | if (identity && definition?.repositorySelector && !reference.repository.includes("/")) { |
| 23 | if ( |
| 24 | reference.repository.toLowerCase() !== |
| 25 | definition.repositorySelector(identity)?.toLowerCase() || |
| 26 | (reference.host !== undefined && reference.host.toLowerCase() !== host) |
| 27 | ) |
| 28 | return null; |
| 29 | const [projectHost, ...repository] = identity.canonicalKey.split("/"); |
| 30 | if (!projectHost || repository.length === 0) return null; |
| 31 | return normalizeThreadPullRequestKey({ |
| 32 | host: projectHost, |
| 33 | repository: repository.join("/"), |
| 34 | number: reference.number, |
| 35 | }); |
| 36 | } |
| 37 | const resolvedHost = reference.host ?? host; |
| 38 | return resolvedHost === undefined |
| 39 | ? null |
| 40 | : normalizeThreadPullRequestKey({ |
| 41 | host: resolvedHost, |
| 42 | repository: reference.repository, |
| 43 | number: reference.number, |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The repositories whose pull requests are the project's own, each with the remote to fetch it |
| 49 | * from: its primary one, then the fork its `origin` remote names when that is on the same host. |
| 50 | */ |
| 51 | export function ownPullRequestRepositories( |
| 52 | identity: RepositoryIdentity | null | undefined, |
| 53 | ): ReadonlyArray<{ |
| 54 | readonly key: Pick<ThreadPullRequestKey, "host" | "repository">; |
| 55 | readonly locator: RepositoryIdentity["locator"]; |
| 56 | }> { |
| 57 | if (!identity) return []; |
| 58 | const candidates: Array<RepositoryIdentity> = [identity]; |
| 59 | const origin = identity.origin; |
| 60 | const [host, ...path] = origin?.canonicalKey.split("/") ?? []; |
| 61 | if ( |
| 62 | origin?.remoteUrl !== undefined && |
| 63 | path.length > 0 && |
| 64 | host === identity.canonicalKey.split("/")[0] |
| 65 | ) { |
| 66 | const { owner: _owner, name: _name, ...primary } = identity; |
| 67 | candidates.push({ |
| 68 | ...primary, |
| 69 | canonicalKey: origin.canonicalKey, |
| 70 | displayName: origin.displayName ?? path.join("/"), |
| 71 | locator: { source: "git-remote", remoteName: "origin", remoteUrl: origin.remoteUrl }, |
| 72 | }); |
| 73 | } |
| 74 | return candidates.flatMap((candidate) => { |
| 75 | const key = pullRequestSyncKey( |
| 76 | { |
| 77 | projectId: ProjectId.make("own"), |
| 78 | repository: sourceControlDefinitions.repositorySelector(candidate) ?? "", |
| 79 | number: 1, |
| 80 | }, |
| 81 | candidate, |
| 82 | ); |
| 83 | return key === null |
| 84 | ? [] |
| 85 | : [{ key: { host: key.host, repository: key.repository }, locator: candidate.locator }]; |
| 86 | }); |
| 87 | } |