diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index f42a1aea6f563..57f795e6c5491 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -275,6 +275,15 @@ describe("IssueService", () => { host: "gitea.test", }); + // A GitHub Enterprise Server's hostname need not say "github"; its project vouches for it. + const enterprise = yield* makeService({ + projects: [project({ id: "ghes", repository: "acme/web", host: "code.acme.test" })], + trackers: [fakeTracker()], + }); + expect( + yield* enterprise.resolveUrl("https://code.acme.test/acme/web/issues/7"), + ).toMatchObject({ projectId: "ghes", host: "code.acme.test", key: "7", writable: true }); + // 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 2832ab04d15bb..8f18b80a985a3 100644 --- a/apps/server/src/issue/IssueService.ts +++ b/apps/server/src/issue/IssueService.ts @@ -9,6 +9,7 @@ import { IssueOperationError, IssueTrackerKind, IssueUnavailableError, + SourceControlProviderKind, type IssueCandidates, type IssueCommentInput, type IssueCommentsPage, @@ -640,10 +641,30 @@ const make = Effect.gen(function* () { return { projectId, tracker: own.tracker, host: own.host, scope: own.scope }; }); + /** A host whose hostname does not name it, such as GitHub Enterprise Server, is vouched for by a project on it. */ + const parseOnProjectHost = (value: string) => { + const url = parseUrl(value); + if (url === null) return Effect.succeed(null); + return listProjects({ host: url.hostname, authority: url.host.toLowerCase() }).pipe( + Effect.map(({ supported }) => { + for (const project of supported) { + const parsed = sourceControlDefinitions.parseIssueUrl( + value, + SourceControlProviderKind.make(project.tracker), + ); + if (parsed !== null) return parsed; + } + return null; + }), + ); + }; + const resolveUrl: IssueService["Service"]["resolveUrl"] = Effect.fn("IssueService.resolveUrl")( function* (url, preferredProjectId) { - const gitHost = sourceControlDefinitions.parseIssueUrl(url); - const standalone = gitHost === null ? findStandaloneIssueUrl(registry.standalone, url) : null; + const recognized = sourceControlDefinitions.parseIssueUrl(url); + const standalone = + recognized === null ? findStandaloneIssueUrl(registry.standalone, url) : null; + const gitHost = recognized ?? (standalone === null ? yield* parseOnProjectHost(url) : null); const parsed = gitHost === null ? standalone diff --git a/packages/client-runtime/src/issueLinks.test.ts b/packages/client-runtime/src/issueLinks.test.ts index 004b526d99cfd..eef7524db4f8b 100644 --- a/packages/client-runtime/src/issueLinks.test.ts +++ b/packages/client-runtime/src/issueLinks.test.ts @@ -56,6 +56,13 @@ describe("resolveIssueLinkTarget", () => { ], }); expect(target?.ref.projectId).toBe("ported"); + + // An Enterprise Server's hostname need not say "github"; its project vouches for it. + const custom = resolveIssueLinkTarget({ + url: "https://code.acme.test/acme/web/issues/7", + projects: [enterprise("custom", "code.acme.test")], + }); + expect(custom?.ref).toMatchObject({ projectId: "custom", host: "code.acme.test", key: "7" }); }); it.each([ diff --git a/packages/client-runtime/src/issueLinks.ts b/packages/client-runtime/src/issueLinks.ts index 528ea16f12d35..3509bbbf33ed5 100644 --- a/packages/client-runtime/src/issueLinks.ts +++ b/packages/client-runtime/src/issueLinks.ts @@ -30,6 +30,24 @@ function linkAuthority(url: string): string | null { } } +/** A host whose hostname does not name it, such as GitHub Enterprise Server, is vouched for by a project on it. */ +function parseIssueLink( + url: string, + authority: string | null, + projects: ReadonlyArray, +) { + const parsed = sourceControlClients.parseIssueUrl(url); + if (parsed !== null || authority === null) return parsed; + const hostname = new URL(url).hostname.toLowerCase(); + for (const project of projects) { + const binding = projectIssueBinding(project); + if (binding === null || (binding.host !== hostname && binding.host !== authority)) continue; + const known = sourceControlClients.parseIssueUrl(url, project.repositoryIdentity?.provider); + if (known !== null) return known; + } + 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 @@ -42,11 +60,11 @@ export function resolveIssueLinkTarget(input: { readonly projects: ReadonlyArray; readonly preferredProjectId?: string | undefined; }): { readonly project: Project; readonly ref: IssueRef } | null { - 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 parsed = parseIssueLink(input.url, authority, input.projects); + if (parsed === null) return null; + const { definition, link } = parsed; const onHost = input.projects.flatMap((project) => { const binding = projectIssueBinding(project); return binding === null || diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index d8e22b811fc96..acd85f0c877a7 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -274,8 +274,11 @@ export interface SourceControlDefinition { readonly parseCommitUrl?: (url: URL, known: boolean) => CommitLink | null; /** Null when the URL is not one of this host's change requests. */ readonly parseChangeRequestUrl: (url: URL) => ParsedChangeRequestUrl | null; - /** Null when the URL is not one of this host's issues. Absent for a host with none. */ - readonly parseIssueUrl?: (url: URL) => IssueLink | null; + /** + * Null when the URL is not one of this host's issues. Absent for a host with none. `known` is + * set when the caller already knows this host serves the URL's repository. + */ + readonly parseIssueUrl?: (url: URL, known: boolean) => IssueLink | null; /** * The repository URL when `url` is a page inside one of this host's repositories. `known` is set * when the caller already knows this host serves the URL's repository. Off a known host, return @@ -670,9 +673,10 @@ export interface SourceControlDefinitionRegistry { readonly findByPublicDomain: (hostname: string) => SourceControlDefinition | undefined; /** The first host, in registry order, that reads `url` as one of its change requests. */ readonly parseChangeRequestUrl: (url: string) => ParsedChangeRequest | null; - /** The first host, in registry order, that reads `url` as one of its issues. */ + /** The first host, in registry order, that reads `url` as one of its issues, or `kind` alone. */ readonly parseIssueUrl: ( url: string, + kind?: SourceControlProviderKind, ) => { readonly definition: SourceControlDefinition; readonly link: IssueLink } | null; readonly parseCommitUrl: (url: string, kind?: SourceControlProviderKind) => ParsedCommit | null; readonly findByChangeRequestUrl: (url: string) => SourceControlDefinition | undefined; @@ -792,11 +796,12 @@ export function makeSourceControlDefinitionRegistry( ); }, parseChangeRequestUrl, - parseIssueUrl: (value) => { + parseIssueUrl: (value, kind) => { const url = httpUrl(value); if (url === null) return null; - for (const definition of claimOrder) { - const link = definition.parseIssueUrl?.(url) ?? null; + const preferred = kind === undefined ? undefined : byKind.get(kind); + for (const definition of kind === undefined ? claimOrder : preferred ? [preferred] : []) { + const link = definition.parseIssueUrl?.(url, preferred !== undefined) ?? null; if (link !== null) return { definition, link }; } return null; diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index d848a1f54be33..816f855d86439 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -265,6 +265,35 @@ const githubContext = (host: string) => ({ }); describe("GitHubSourceControlProvider repository resolution", () => { + // regression: #38251 an Enterprise Server's hostname need not say "github"; a gh login vouches for it + it.effect.each([ + ["github", "github"], + ["unknown", "unknown"], + ] as const)("refines a remote on an unknown host the gh login reads as %s", ([seen, expected]) => + Effect.gen(function* () { + const provider = yield* makeProvider({}); + const remoteUrl = "https://code.acme.test/acme/web.git"; + const identity = { + canonicalKey: "code.acme.test/acme/web", + locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl }, + rootPath: "/repo", + provider: SourceControlProviderKind.make("unknown"), + }; + const refined = yield* provider.refineRepositoryIdentity!({ + identity, + resolveContext: ({ context }) => + Effect.succeed({ + ...context, + provider: { ...context.provider, kind: SourceControlProviderKind.make(seen) }, + }), + }); + assert.deepStrictEqual(refined, { + ...identity, + provider: SourceControlProviderKind.make(expected), + }); + }), + ); + it.effect("reads the repository gh would pick from the remotes", () => { const paths: string[] = []; const { layer } = harness({ diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index a7733d6dd5ee0..7ff3d4a9b4d80 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -61,7 +61,11 @@ import { type SourceControlManagedCliDiscoverySpec, } from "@t3tools/source-control-core/server/discovery"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; -import { readSourceControlHostSettings } from "@t3tools/source-control-core/shared/definition"; +import { + parseGitRemoteHost, + readSourceControlHostSettings, + unknownRemoteProvider, +} from "@t3tools/source-control-core/shared/definition"; import * as GitHubClient from "../shared/definition.ts"; import { @@ -339,6 +343,30 @@ class GitHubFailure extends Schema.TaggedError()("GitHubFailure", const failure = (detail: string, cause?: unknown) => new GitHubFailure({ detail, cause }); +// A gh login confirms a GitHub Enterprise Server whose hostname does not identify its provider. +const refineRepositoryIdentity: NonNullable< + SourceControlProvider.SourceControlProvider["Service"]["refineRepositoryIdentity"] +> = Effect.fn("GitHubSourceControlProvider.refineRepositoryIdentity")(function* ({ + identity, + resolveContext, +}) { + if (!identity.rootPath || (identity.provider !== undefined && identity.provider !== "unknown")) + return identity; + const remote = parseGitRemoteHost(identity.locator.remoteUrl); + if (remote === null) return identity; + const context = yield* resolveContext({ + cwd: identity.rootPath, + context: { + provider: unknownRemoteProvider(remote), + remoteName: identity.locator.remoteName, + remoteUrl: identity.locator.remoteUrl, + }, + }); + return context?.provider.kind === "github" + ? { ...identity, provider: SourceControlProviderKind.make("github") } + : identity; +}); + const PULL_REQUEST_NOT_FOUND = "Pull request not found. Check the PR number or URL and try again."; const REPOSITORY_NOT_FOUND = "Repository not found. Check the owner and name and try again."; @@ -968,6 +996,7 @@ export const make = Effect.gen(function* () { return SourceControlProvider.SourceControlProvider.of({ kind: SourceControlProviderKind.make("github"), + refineRepositoryIdentity, resolveRemoteWebHost: Effect.fn("GitHubSourceControlProvider.resolveRemoteWebHost")( ({ context }) => Effect.succeed(gitHubApiHostForRemote(context.remoteUrl)), ), diff --git a/packages/source-control-github/src/shared/definition.ts b/packages/source-control-github/src/shared/definition.ts index dbc8ac9f80e20..a143b3787fdec 100644 --- a/packages/source-control-github/src/shared/definition.ts +++ b/packages/source-control-github/src/shared/definition.ts @@ -101,8 +101,10 @@ export const definition = defineSourceControl({ parseCommitUrl: (url, known) => known || isGitHubHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null, parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH), - // `/issues/` is too generic a path to believe from a host that is not GitHub's. - parseIssueUrl: (url) => (isGitHubHost(url.hostname) ? parseIssuePath(url, ISSUE_PATH) : null), + // `/issues/` is too generic a path to believe from a host that is not GitHub's, unless a known + // project's host vouches for it as a GitHub Enterprise Server. + parseIssueUrl: (url, known) => + known || isGitHubHost(url.hostname) ? parseIssuePath(url, ISSUE_PATH) : null, repositoryUrlFromPage: (url) => isGitHubHost(url.hostname) ? repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH) : null, // GitHub writes every bare reference through `/issues/`, including pull requests.