Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/server/src/issue/IssueService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
25 changes: 23 additions & 2 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IssueOperationError,
IssueTrackerKind,
IssueUnavailableError,
SourceControlProviderKind,
type IssueCandidates,
type IssueCommentInput,
type IssueCommentsPage,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions packages/client-runtime/src/issueLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
24 changes: 21 additions & 3 deletions packages/client-runtime/src/issueLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LinkProject>,
) {
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
Expand All @@ -42,11 +60,11 @@ export function resolveIssueLinkTarget<Project extends LinkProject>(input: {
readonly projects: ReadonlyArray<Project>;
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 ||
Expand Down
17 changes: 11 additions & 6 deletions packages/source-control-core/src/shared/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -339,6 +343,30 @@ class GitHubFailure extends Schema.TaggedError<GitHubFailure>()("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.";

Expand Down Expand Up @@ -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)),
),
Expand Down
6 changes: 4 additions & 2 deletions packages/source-control-github/src/shared/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading