diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index fd6f7b2f0a0c1..ad6d43644dbb4 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -57,6 +57,8 @@ function project(input: { readonly repository?: string; readonly provider?: string; readonly host?: string; + readonly remoteUrl?: string; + readonly webUrl?: string; }): OrchestrationProjectShell { const host = input.host ?? "github.com"; return { @@ -66,14 +68,17 @@ function project(input: { ...(input.repository ? { repositoryIdentity: { - canonicalKey: normalizeGitRemoteUrl(`https://${host}/${input.repository}.git`), + canonicalKey: normalizeGitRemoteUrl( + input.remoteUrl ?? `https://${host}/${input.repository}.git`, + ), locator: { source: "git-remote" as const, remoteName: "origin", - remoteUrl: `https://${host}/${input.repository}.git`, + remoteUrl: input.remoteUrl ?? `https://${host}/${input.repository}.git`, }, provider: SourceControlProviderKind.make(input.provider ?? "github"), displayName: input.repository, + ...(input.webUrl ? { webUrl: input.webUrl } : {}), }, } : {}), @@ -234,9 +239,28 @@ describe("IssueService", () => { }); expect(writes).toEqual(["acme/web#1"]); + // A portless link keeps SSH and HTTPS remotes on the same host as candidates. + const mixed = yield* makeService({ + projects: [ + project({ id: "https", repository: "acme/web" }), + project({ id: "ssh", repository: "acme/web", remoteUrl: "git@github.com:acme/web.git" }), + ], + trackers: [fakeTracker()], + }); + expect( + yield* mixed.resolveUrl("https://github.com/acme/web/issues/7", "ssh" as ProjectId), + ).toMatchObject({ projectId: "ssh", writable: true }); + // A link's port picks the server: the project bound with it, not a default-port namesake. const ported = yield* makeService({ projects: [ + project({ + id: "ssh", + repository: "acme/web", + provider: "forgejo", + host: "gitea.test", + remoteUrl: "git@gitea.test:acme/web.git", + }), project({ id: "plain", repository: "acme/web", provider: "forgejo", host: "gitea.test" }), project({ id: "ported", @@ -253,11 +277,69 @@ describe("IssueService", () => { key: "3", writable: true, }); + expect( + yield* ported.resolveUrl("https://gitea.test:8443/acme/web/issues/3", "ssh" as ProjectId), + ).toMatchObject({ projectId: "ported", host: "gitea.test:8443", writable: true }); expect(yield* ported.resolveUrl("https://gitea.test/acme/web/issues/3")).toMatchObject({ - projectId: "plain", + projectId: "ssh", host: "gitea.test", }); + const defaultPortOnly = yield* makeService({ + projects: [ + project({ id: "plain", repository: "acme/web", provider: "forgejo", host: "gitea.test" }), + ], + trackers: [fakeTracker({ kind: IssueTrackerKind.make("forgejo") })], + }); + expect( + yield* defaultPortOnly + .resolveUrl("https://gitea.test:8443/acme/web/issues/3") + .pipe(Effect.result), + ).toMatchObject({ + _tag: "Failure", + failure: { _tag: "IssueUnavailableError", reason: "tracker-unsupported" }, + }); + + // A configured web or HTTPS authority admits only its own port; SSH names no port. + for (const { provider, host, path } of [ + { provider: "forgejo", host: "gitea.test", path: "issues" }, + { provider: "gitlab", host: "gitlab.test", path: "-/issues" }, + ]) { + for (const transport of ["ssh", "https", "web"] as const) { + const configured = yield* makeService({ + projects: [ + project({ + id: "configured", + repository: "acme/web", + provider, + host, + remoteUrl: + transport === "https" + ? `https://${host}:8443/acme/web.git` + : `git@${host}:acme/web.git`, + ...(transport === "web" ? { webUrl: `https://${host}:8443/acme/web` } : {}), + }), + ], + trackers: [fakeTracker({ kind: IssueTrackerKind.make(provider) })], + }); + expect( + yield* configured.resolveUrl(`https://${host}:8443/acme/web/${path}/3`), + ).toMatchObject({ + projectId: "configured", + host: provider === "forgejo" && transport !== "ssh" ? `${host}:8443` : host, + writable: true, + }); + if (transport === "ssh") continue; + for (const elsewhere of [`${host}:9443`, host]) { + expect( + yield* configured + .resolveUrl(`https://${elsewhere}/acme/web/${path}/3`) + .pipe(Effect.result), + ).toMatchObject({ _tag: "Failure", failure: { reason: "tracker-unsupported" } }); + } + } + } + // Another host is not reachable through this project's credentials at all. const elsewhere = yield* Effect.flip(service.detail({ ...own, host: "ghe.example.test" })); expect(elsewhere).toMatchObject({ diff --git a/apps/server/src/issue/IssueService.ts b/apps/server/src/issue/IssueService.ts index a53aeb698ce89..8e6d5ce3d4e49 100644 --- a/apps/server/src/issue/IssueService.ts +++ b/apps/server/src/issue/IssueService.ts @@ -31,6 +31,7 @@ import { type OrchestrationProjectShell, type ProjectId, } from "@t3tools/contracts"; +import { projectsOnIssueHost } from "@t3tools/source-control-core/shared/definition"; import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; import { IssueTrackerError, @@ -64,6 +65,7 @@ export function trackerDetail(error: IssueTrackerError): string { /** One project's resolved tracker: which driver, where it is served from, and its container. */ interface ProjectTrackerBinding { + readonly repositoryIdentity?: OrchestrationProjectShell["repositoryIdentity"]; readonly tracker: IssueTrackerKind; readonly host: string; readonly scope: string; @@ -87,6 +89,7 @@ function resolveGitHostBinding(project: OrchestrationProjectShell): ProjectTrack return { tracker: IssueTrackerKind.make(kind), host: sourceControlDefinitions.changeRequestHost(identity, kind), + repositoryIdentity: identity, scope, }; } @@ -218,8 +221,6 @@ const make = Effect.gen(function* () { const listProjects = (filter: { readonly projectId?: IssueListInput["projectId"]; readonly host?: string | undefined; - /** The link's `host:port`, for a host whose binding keeps its port. */ - readonly authority?: string | undefined; }) => (filter.projectId === undefined ? projects.listShells() @@ -251,8 +252,7 @@ const make = Effect.gen(function* () { { tracker: IssueTrackerKind; projectCount: number } >(); for (const candidate of bound.flat()) { - if (host !== undefined && candidate.host !== host && candidate.host !== filter.authority) - continue; + if (host !== undefined && candidate.host !== host) continue; const api = registry.get(candidate.tracker); if (api === null) { const counted = unimplemented.get(candidate.host); @@ -630,14 +630,11 @@ const make = Effect.gen(function* () { }); } const { definition, link } = parsed; - // A link names its server's port; a binding does only when its remote or web URL did. - const authority = new URL(url).host.toLowerCase(); - const { supported } = yield* listProjects({ host: link.host, authority }); - const onHost = supported.filter( - (project) => project.tracker === IssueTrackerKind.make(definition.kind), + const { supported } = yield* listProjects({}); + const onTracker = projectsOnIssueHost( + supported.filter((project) => project.tracker === IssueTrackerKind.make(definition.kind)), + link, ); - const onAuthority = onHost.filter((project) => project.host === authority); - const onTracker = onAuthority.length > 0 ? onAuthority : onHost; const preferFirst = (projects: ReadonlyArray) => projects.find((project) => project.project.id === preferredProjectId) ?? projects[0]; const own = preferFirst( diff --git a/packages/client-runtime/src/issueLinks.test.ts b/packages/client-runtime/src/issueLinks.test.ts index 004b526d99cfd..a2c07e429a8d1 100644 --- a/packages/client-runtime/src/issueLinks.test.ts +++ b/packages/client-runtime/src/issueLinks.test.ts @@ -10,6 +10,7 @@ const project = (id: string, repository: string) => repositoryIdentity: { canonicalKey: `github.com/${repository}`, provider: SourceControlProviderKind.make("github"), + webUrl: `https://github.com/${repository}`, displayName: repository, owner: repository.split("/")[0], name: repository.split("/")[1], @@ -35,30 +36,36 @@ describe("resolveIssueLinkTarget", () => { expect(target?.ref).toMatchObject({ tracker: "github", host: "github.com", ...expected }); }); - it("prefers the project bound to the link's own port over one on the bare hostname", () => { - const enterprise = (id: string, host: string) => - ({ - id: ProjectId.make(id), - environmentId: EnvironmentId.make("env"), - repositoryIdentity: { - canonicalKey: `${host}/acme/web`, - provider: SourceControlProviderKind.make("github"), - displayName: "acme/web", - owner: "acme", - name: "web", - }, - }) as never; - const target = resolveIssueLinkTarget({ - url: "https://github.example.com:8443/acme/web/issues/7", - projects: [ - enterprise("bare", "github.example.com"), - enterprise("ported", "github.example.com:8443"), - ], - }); - expect(target?.ref.projectId).toBe("ported"); - }); + it.each(["github.example.com", "github.example.com:8443"])( + "prefers the configured authority with binding host %s over a preferred SSH project", + (portedHost) => { + const enterprise = (id: string, host: string, remoteUrl = `git@${host}:acme/web.git`) => + ({ + id: ProjectId.make(id), + environmentId: EnvironmentId.make("env"), + repositoryIdentity: { + canonicalKey: `${host}/acme/web`, + locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl }, + provider: SourceControlProviderKind.make("github"), + displayName: "acme/web", + owner: "acme", + name: "web", + }, + }) as never; + const target = resolveIssueLinkTarget({ + url: "https://github.example.com:8443/acme/web/issues/7", + preferredProjectId: "bare", + projects: [ + enterprise("bare", "github.example.com"), + enterprise("ported", portedHost, "https://github.example.com:8443/acme/web.git"), + ], + }); + expect(target?.ref).toMatchObject({ projectId: "ported", host: portedHost }); + }, + ); it.each([ + "https://github.com:8443/acme/web/issues/7", "https://github.com.evil.example/acme/web/issues/7", "https://github.com/acme/web/pull/7", ])("leaves %s an ordinary link", (url) => { diff --git a/packages/client-runtime/src/issueLinks.ts b/packages/client-runtime/src/issueLinks.ts index 528ea16f12d35..108dd96fcf572 100644 --- a/packages/client-runtime/src/issueLinks.ts +++ b/packages/client-runtime/src/issueLinks.ts @@ -1,4 +1,5 @@ import type { IssueRef } from "@t3tools/contracts"; +import { projectsOnIssueHost } from "@t3tools/source-control-core/shared/definition"; import { sourceControlClients } from "./sourceControlClients.ts"; import type { EnvironmentProject } from "./state/shell.ts"; @@ -10,26 +11,20 @@ function projectIssueBinding(project: LinkProject): { readonly tracker: string; readonly host: string; readonly scope: string; + readonly repositoryIdentity: NonNullable; } | null { const identity = project.repositoryIdentity; const scope = sourceControlClients.issueScope(identity); const kind = identity?.provider; if (!identity || kind === undefined || kind === "unknown" || scope === null) return null; return { + repositoryIdentity: identity, tracker: kind, host: sourceControlClients.changeRequestHost(identity, kind), scope: scope.toLowerCase(), }; } -function linkAuthority(url: string): string | null { - try { - return new URL(url).host.toLowerCase(); - } catch { - return null; - } -} - /** * The reference an issue link resolves to, and the project it is read through: the project whose * tracker binding is that host and scope, else any project on the same tracker and host lending @@ -45,31 +40,28 @@ export function resolveIssueLinkTarget(input: { const parsed = sourceControlClients.parseIssueUrl(input.url); if (parsed === null) return null; const { definition, link } = parsed; - // A link names its server's port; a binding does only when its remote or web URL did. - const authority = linkAuthority(input.url); - const onHost = input.projects.flatMap((project) => { - const binding = projectIssueBinding(project); - return binding === null || - binding.tracker !== definition.kind || - (binding.host !== link.host && binding.host !== authority) - ? [] - : [{ project, binding }]; - }); - const onAuthority = onHost.filter((candidate) => candidate.binding.host === authority); - const bound = onAuthority.length > 0 ? onAuthority : onHost; + const bound = projectsOnIssueHost( + input.projects.flatMap((project) => { + const binding = projectIssueBinding(project); + return binding === null || binding.tracker !== definition.kind + ? [] + : [{ project, ...binding }]; + }), + link, + ); const preferFirst = (candidates: typeof bound) => candidates.find((candidate) => candidate.project.id === input.preferredProjectId) ?? candidates[0]; - const own = preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scope)); + const own = preferFirst(bound.filter((candidate) => candidate.scope === link.scope)); const lender = own ?? preferFirst(bound); if (lender === undefined) return null; return { project: lender.project, ref: { projectId: lender.project.id, - tracker: lender.binding.tracker as IssueRef["tracker"], - host: lender.binding.host, - scope: own === undefined ? link.scope : lender.binding.scope, + tracker: lender.tracker as IssueRef["tracker"], + host: lender.host, + scope: own === undefined ? link.scope : lender.scope, key: link.key, }, }; diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index d8e22b811fc96..bfaf7ec02dba8 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -38,6 +38,7 @@ export interface ChangeRequestLink { */ export interface IssueLink { readonly host: string; + readonly authority?: string; readonly scope: string; readonly key: string; } @@ -454,7 +455,12 @@ export function parseIssuePath(url: URL, route: RegExp): IssueLink | null { const scope = match?.[1]; const number = Number(match?.[2]); return scope && Number.isSafeInteger(number) && number > 0 - ? { host: url.hostname.toLowerCase(), scope: scope.toLowerCase(), key: String(number) } + ? { + host: url.hostname.toLowerCase(), + ...(url.port ? { authority: url.host.toLowerCase() } : {}), + scope: scope.toLowerCase(), + key: String(number), + } : null; } @@ -497,6 +503,32 @@ export function defineSourceControl(projects: ReadonlyArray, link: IssueLink): ReadonlyArray { + const matches = projects.flatMap((project) => { + const web = issueWebUrl(project.repositoryIdentity); + if (web !== null) { + return web.host.toLowerCase() === (link.authority ?? link.host) + ? [{ project, authority: true }] + : []; + } + if (project.host === link.authority) return [{ project, authority: true }]; + return project.host === link.host ? [{ project, authority: false }] : []; + }); + const configured = link.authority === undefined ? [] : matches.filter((match) => match.authority); + return (configured.length > 0 ? configured : matches).map((match) => match.project); +} + export function isChangeRequestAuthorityOfProject( identity: ChangeRequestProjectIdentity, link: ChangeRequestLink | CommitLink,