diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index e68799eea12e7..77a2215fb3916 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -283,6 +283,40 @@ describe("IssueService", () => { yield* enterprise.resolveUrl("https://code.acme.test/acme/web/issues/7"), ).toMatchObject({ projectId: "ghes", host: "code.acme.test", key: "7", writable: true }); + // A fork lists its upstream's issues and its own, counted once, and may write to both. + const fork = project({ id: "fork", repository: "acme/web" }); + const forked = yield* makeService({ + projects: [ + { + ...fork, + repositoryIdentity: { + ...fork.repositoryIdentity!, + origin: { canonicalKey: "github.com/me/web" }, + }, + }, + ], + trackers: [ + fakeTracker({ + listIssues: (input) => + Effect.succeed({ items: [issue(input.scope)], nextCursor: null }), + comment: (input) => + Effect.sync(() => { + writes.push(`${input.scope}#${input.key}`); + }), + }), + ], + }); + const listed = yield* forked.list({ state: "open" }); + expect(listed.entries.map((entry) => entry.scope).sort()).toEqual(["acme/web", "me/web"]); + expect(listed.trackers).toEqual([expect.objectContaining({ projectCount: 1 })]); + yield* forked.comment({ + ...own, + projectId: "fork" as ProjectId, + scope: "me/web", + body: "ok", + }); + expect(writes).toEqual(["acme/web#1", "me/web#1"]); + // 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 e4ffc8817628b..f2844e757b15a 100644 --- a/apps/server/src/issue/IssueService.ts +++ b/apps/server/src/issue/IssueService.ts @@ -1,5 +1,5 @@ // A project bound to a standalone tracker (Linear) in settings lists it beside its git host's. -// References outside the project's canonical repository may be read on the same host, +// References outside the project's own repositories (upstream and fork) may be read on the host, // but every write must pass through requireWritable. import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; @@ -83,18 +83,14 @@ interface SupportedProject extends ProjectTrackerBinding { /** * The git-host path of resolution: the tracker is the host's own, named like the remote's - * provider kind. Null for a project with no remote or a kind no tracker claims. + * provider kind. A fork reads its upstream and its own repository. */ -function resolveGitHostBinding(project: OrchestrationProjectShell): ProjectTrackerBinding | null { - const identity = project.repositoryIdentity; - const scope = sourceControlDefinitions.issueScope(identity); - const kind = identity?.provider; - if (!identity || kind === undefined || kind === "unknown" || scope === null) return null; - return { - tracker: IssueTrackerKind.make(kind), - host: sourceControlDefinitions.changeRequestHost(identity, kind), - scope, - }; +function resolveGitHostBindings( + project: OrchestrationProjectShell, +): ReadonlyArray { + return sourceControlDefinitions + .issueBindings(project.repositoryIdentity) + .map((binding) => ({ ...binding, tracker: IssueTrackerKind.make(binding.tracker) })); } function findStandaloneIssueUrl(trackers: ReadonlyArray, value: string) { @@ -209,12 +205,11 @@ const make = Effect.gen(function* () { ? Effect.succeed(project.repositoryIdentity) : repositoryIdentities.resolve(project.workspaceRoot) ).pipe( - Effect.map((repositoryIdentity) => - [ - resolveStandaloneBinding(binding), - resolveGitHostBinding({ ...project, repositoryIdentity }), - ].filter((candidate) => candidate !== null), - ), + Effect.map((repositoryIdentity) => { + const standalone = resolveStandaloneBinding(binding); + const gitHost = resolveGitHostBindings({ ...project, repositoryIdentity }); + return standalone === null ? gitHost : [standalone, ...gitHost]; + }), ); const listProjects = (filter: { @@ -267,7 +262,7 @@ const make = Effect.gen(function* () { const supported: SupportedProject[] = []; const unimplemented = new Map< string, - { tracker: IssueTrackerKind; projectCount: number } + { tracker: IssueTrackerKind; projectIds: Set } >(); for (const candidate of bound.flat()) { if (host !== undefined && candidate.host !== host && candidate.host !== filter.authority) @@ -276,8 +271,11 @@ const make = Effect.gen(function* () { if (api === null) { const counted = unimplemented.get(candidate.host); if (counted === undefined) - unimplemented.set(candidate.host, { tracker: candidate.tracker, projectCount: 1 }); - else counted.projectCount += 1; + unimplemented.set(candidate.host, { + tracker: candidate.tracker, + projectIds: new Set([candidate.project.id]), + }); + else counted.projectIds.add(candidate.project.id); continue; } supported.push({ ...candidate, api }); @@ -288,26 +286,29 @@ const make = Effect.gen(function* () { /** * The project whose checkout and credentials serve a reference, and whether the reference is - * the project's own scope. A reference naming another scope on the same tracker and host is - * served by the project but marked read-only. A project with two trackers serves the one the - * reference names. + * one of the project's own scopes. A reference naming another scope on the same tracker and + * host is served by the project but marked read-only. A project with two trackers serves the + * one the reference names. */ const requireProject = ( ref: IssueScopeRef, ): Effect.Effect => listProjects({ projectId: ref.projectId }).pipe( Effect.flatMap(({ supported }) => { - const own = supported.find((candidate) => candidate.tracker === ref.tracker); - if (own === undefined) { - return Effect.fail(new IssueUnavailableError({ reason: "tracker-unsupported" })); - } - const host = ref.host?.trim().toLowerCase() ?? own.host; - if (host !== own.host) { + const host = ref.host?.trim().toLowerCase(); + const onHost = supported.filter( + (candidate) => + candidate.tracker === ref.tracker && (host === undefined || candidate.host === host), + ); + const scope = ref.scope.trim(); + const own = onHost.find( + (candidate) => candidate.scope.toLowerCase() === scope.toLowerCase(), + ); + const served = own ?? onHost[0]; + if (served === undefined) { return Effect.fail(new IssueUnavailableError({ reason: "tracker-unsupported" })); } - const scope = ref.scope.trim(); - const writable = scope.toLowerCase() === own.scope.toLowerCase(); - return Effect.succeed({ ...own, scope, writable }); + return Effect.succeed({ ...served, scope, writable: own !== undefined }); }), ); @@ -353,8 +354,12 @@ const make = Effect.gen(function* () { viewerRequests.set(project.host, request); } - // Every project counts, including worktrees of a scope read once below. + // Every project counts once, including worktrees of a scope read once below and forks. + const counted = new Set(); for (const project of supported) { + const countKey = `${project.host} ${project.project.id}`; + if (counted.has(countKey)) continue; + counted.add(countKey); const existing = trackers.get(project.host); trackers.set( project.host, @@ -416,9 +421,15 @@ const make = Effect.gen(function* () { if (page._tag === "Failure") { if (cursor !== undefined) nextCursors[key] = cursor; recordFailure(project, page.failure); + // A fork reads two repositories, so its failure names the one that failed. + const forked = supported.some( + (other) => other.project.id === project.project.id && other.scope !== project.scope, + ); errors.push({ projectId: project.project.id, - projectTitle: project.project.title, + projectTitle: forked + ? `${project.project.title} (${project.scope})` + : project.project.title, message: trackerDetail(page.failure), }); return; @@ -438,11 +449,11 @@ const make = Effect.gen(function* () { { concurrency: SCOPE_CONCURRENCY, discard: true }, ); - for (const [host, { tracker, projectCount }] of unimplemented) { + for (const [host, { tracker, projectIds }] of unimplemented) { trackers.set(host, { tracker, host, - projectCount, + projectCount: projectIds.size, capabilities: null, configured: false, detail: "Issues cannot be browsed for this host yet.", diff --git a/packages/client-runtime/src/issueLinks.test.ts b/packages/client-runtime/src/issueLinks.test.ts index fd206063bfcff..ff9fabfc8964c 100644 --- a/packages/client-runtime/src/issueLinks.test.ts +++ b/packages/client-runtime/src/issueLinks.test.ts @@ -8,7 +8,7 @@ import { describe, expect, it } from "vite-plus/test"; import { resolveIssueLinkTarget } from "./issueLinks.ts"; -const project = (id: string, repository: string) => +const project = (id: string, repository: string, origin?: string) => ({ id: ProjectId.make(id), environmentId: EnvironmentId.make("env"), @@ -18,6 +18,7 @@ const project = (id: string, repository: string) => displayName: repository, owner: repository.split("/")[0], name: repository.split("/")[1], + ...(origin ? { origin: { canonicalKey: `github.com/${origin}`, displayName: origin } } : {}), }, }) as never; @@ -40,6 +41,14 @@ describe("resolveIssueLinkTarget", () => { expect(target?.ref).toMatchObject({ tracker: "github", host: "github.com", ...expected }); }); + it("reads a fork's own issue through the fork, not as a borrowed repository", () => { + const target = resolveIssueLinkTarget({ + url: "https://github.com/me/web/issues/3", + projects: [project("p1", "acme/api"), project("fork", "acme/web", "me/web")], + }); + expect(target?.ref).toMatchObject({ projectId: "fork", scope: "me/web", key: "3" }); + }); + it("prefers the project bound to the link's own port over one on the bare hostname", () => { const enterprise = (id: string, host: string) => ({ diff --git a/packages/client-runtime/src/issueLinks.ts b/packages/client-runtime/src/issueLinks.ts index 4e1d4ec15a543..311d7e9982629 100644 --- a/packages/client-runtime/src/issueLinks.ts +++ b/packages/client-runtime/src/issueLinks.ts @@ -1,4 +1,8 @@ -import type { IssueRef, IssueTrackerConnectionList } from "@t3tools/contracts"; +import { + IssueTrackerKind, + type IssueRef, + type IssueTrackerConnectionList, +} from "@t3tools/contracts"; import { LINEAR_HOST, LINEAR_KIND, @@ -10,21 +14,11 @@ import type { EnvironmentProject } from "./state/shell.ts"; type LinkProject = Pick; -/** The host and scope a project's git-host tracker is bound to, the way the server resolves it. */ -function projectIssueBinding(project: LinkProject): { - readonly tracker: string; - readonly host: string; - readonly scope: string; -} | 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 { - tracker: kind, - host: sourceControlClients.changeRequestHost(identity, kind), - scope: scope.toLowerCase(), - }; +/** The hosts and scopes a project's git-host tracker is bound to, the way the server resolves them. */ +function projectIssueBindings(project: LinkProject) { + return sourceControlClients + .issueBindings(project.repositoryIdentity) + .map((binding) => ({ ...binding, scope: binding.scope.toLowerCase() })); } function linkAuthority(url: string): string | null { @@ -45,8 +39,9 @@ function parseIssueLink( 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 bindings = projectIssueBindings(project); + if (!bindings.some((binding) => binding.host === hostname || binding.host === authority)) + continue; const known = sourceControlClients.parseIssueUrl(url, project.repositoryIdentity?.provider); if (known !== null) return known; } @@ -140,14 +135,15 @@ export function resolveIssueLinkTarget(input: { input.preferredProjectId, ); const { definition, link } = parsed; - 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 onHost = input.projects.flatMap((project) => + projectIssueBindings(project) + .filter( + (binding) => + binding.tracker === definition.kind && + (binding.host === link.host || binding.host === authority), + ) + .map((binding) => ({ project, binding })), + ); const onAuthority = onHost.filter((candidate) => candidate.binding.host === authority); const bound = onAuthority.length > 0 ? onAuthority : onHost; const preferFirst = (candidates: typeof bound) => @@ -162,7 +158,7 @@ export function resolveIssueLinkTarget(input: { project: lender.project, ref: { projectId: lender.project.id, - tracker: lender.binding.tracker as IssueRef["tracker"], + tracker: IssueTrackerKind.make(lender.binding.tracker), host: lender.binding.host, scope: own === undefined ? link.scope : lender.binding.scope, key: link.key, diff --git a/packages/client-runtime/src/issueList.ts b/packages/client-runtime/src/issueList.ts index a5f3f2d5d1709..a3de4e705d33e 100644 --- a/packages/client-runtime/src/issueList.ts +++ b/packages/client-runtime/src/issueList.ts @@ -177,25 +177,24 @@ export function issueFilterScopes( for (const project of projects) { if (selectedProject && selectedProject.projectId !== project.id) continue; if (project.environmentId !== environmentId) continue; - const identity = project.repositoryIdentity; - const kind = identity?.provider as SourceControlProviderKind | undefined; - const scope = sourceControlClients.issueScope(identity); - if (!identity || !kind || scope === null) continue; - const host = sourceControlClients.changeRequestHost(identity, kind); - const tracker = page.answer.trackers.find( - (t) => String(t.tracker) === kind && t.host.toLowerCase() === host.toLowerCase(), - ); - if (!tracker?.configured || tracker.capabilities === null) continue; - if ( - scopes.some( - (s) => s.input.scope.toLowerCase() === scope.toLowerCase() && s.input.host === host, + for (const { tracker: kind, host, scope } of sourceControlClients.issueBindings( + project.repositoryIdentity, + )) { + const tracker = page.answer.trackers.find( + (t) => String(t.tracker) === kind && t.host.toLowerCase() === host.toLowerCase(), + ); + if (!tracker?.configured || tracker.capabilities === null) continue; + if ( + scopes.some( + (s) => s.input.scope.toLowerCase() === scope.toLowerCase() && s.input.host === host, + ) ) - ) - continue; - scopes.push({ - environmentId, - input: { projectId: project.id, tracker: tracker.tracker, host, scope }, - }); + continue; + scopes.push({ + environmentId, + input: { projectId: project.id, tracker: tracker.tracker, host, scope }, + }); + } } // A tracker bound in settings (a Linear team) is no repository's; the answer names its scope, // matched or not. Older servers do not, so their rows stand in. diff --git a/packages/client-runtime/src/sourceControlClients.test.ts b/packages/client-runtime/src/sourceControlClients.test.ts index 8c7c11194b82d..acac229e6c1fa 100644 --- a/packages/client-runtime/src/sourceControlClients.test.ts +++ b/packages/client-runtime/src/sourceControlClients.test.ts @@ -376,6 +376,29 @@ describe("sourceControlClients project hosts", () => { ).toBe("service"); expect(sourceControlClients.repositorySelector({ owner: "o", name: "r" })).toBe("o/r"); }); + + it.each([ + ["https://github.com/acme/web.git", "https://github.com/me/web.git", true], + ["git@github.com:acme/web.git", "https://github.com/me/web.git", true], + ["https://github.com/acme/web.git", "https://gitlab.com/me/web.git", false], + ["https://github.com/acme/web.git", "https://github.com:8443/me/web.git", false], + ["git@github.com:acme/web.git", "https://github.com:8443/me/web.git", false], + ["http://github.com/acme/web.git", "https://github.com/me/web.git", false], + ] as const)( + "reads a fork's issues beside upstream %s's only when origin %s is on its server", + (remoteUrl, originUrl, both) => { + const identity = { + canonicalKey: "github.com/acme/web", + locator: { source: "git-remote" as const, remoteName: "upstream", remoteUrl }, + provider: SourceControlProviderKind.make("github"), + displayName: "acme/web", + origin: { canonicalKey: `${new URL(originUrl).hostname}/me/web`, remoteUrl: originUrl }, + }; + expect(sourceControlClients.issueBindings(identity).map((binding) => binding.scope)).toEqual( + both ? ["acme/web", "me/web"] : ["acme/web"], + ); + }, + ); }); describe("sourceControlClients.findByPublicDomain", () => { diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index d9fe29114a96c..bca514d9478ce 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -99,6 +99,13 @@ export type RepositorySelectorIdentity = Pick< export type IssueScopeIdentity = RepositorySelectorIdentity & Partial>; +/** A git-host tracker a project's issues are read from and filed in. */ +export interface IssueBinding { + readonly tracker: SourceControlProviderKind; + readonly host: string; + readonly scope: string; +} + /** A git remote's host, as `recognizeRemote` reads it. */ export interface GitRemoteHost { /** Trimmed. */ @@ -705,6 +712,13 @@ export interface SourceControlDefinitionRegistry { ) => string | null; /** Where the project's issues live: the host's `issueScope`, else its repository. */ readonly issueScope: (identity: IssueScopeIdentity | null | undefined) => string | null; + /** + * Every repository whose issues are the project's own: its primary one, then the fork its + * `origin` remote names when that is on the same host. Empty for a project with no host. + */ + readonly issueBindings: ( + identity: RepositoryIdentity | null | undefined, + ) => ReadonlyArray; readonly changeRequestHost: ( identity: ChangeRequestHostIdentity, kind: Kind, @@ -783,6 +797,50 @@ export function makeSourceControlDefinitionRegistry( } return null; }; + const issueScope = (identity: IssueScopeIdentity | null | undefined) => { + const scopeOf = + identity?.provider === undefined ? undefined : byKind.get(identity.provider)?.issueScope; + return scopeOf === undefined || identity == null + ? repositorySelector(identity) + : scopeOf(identity); + }; + const changeRequestHost = ( + identity: ChangeRequestHostIdentity, + kind: Kind, + ): string | Kind => + (kind === undefined ? undefined : byKind.get(kind)?.changeRequestHost?.(identity)) ?? + projectChangeRequestHost(identity, kind); + const issueBindings = (identity: RepositoryIdentity | null | undefined) => { + const tracker = identity?.provider; + if (!identity || tracker === undefined || tracker === "unknown") return []; + const scope = issueScope(identity); + if (scope === null) return []; + const host = changeRequestHost(identity, tracker); + const primary = { tracker, host, scope }; + const origin = identity.origin; + // The origin keeps no provider or web URL, so it is read only on the primary's server. Keys + // drop schemes and ports, so an HTTP origin must match the primary's remote, its web address + // when the remote is SSH, else the issue host. + const originUrl = origin?.remoteUrl === undefined ? null : httpUrl(origin.remoteUrl); + const primaryUrl = + (identity.locator?.remoteUrl === undefined ? null : httpUrl(identity.locator.remoteUrl)) ?? + (identity.webUrl === undefined ? null : httpUrl(identity.webUrl)); + if ( + origin === undefined || + projectChangeRequestHost(origin, undefined) !== + projectChangeRequestHost(identity, undefined) || + (originUrl !== null && + (primaryUrl === null ? originUrl.host !== host : originUrl.origin !== primaryUrl.origin)) + ) + return [primary]; + const originScope = issueScope({ + provider: tracker, + canonicalKey: origin.canonicalKey, + displayName: origin.displayName ?? origin.canonicalKey.split("/").slice(1).join("/"), + }); + if (originScope === null || originScope.toLowerCase() === scope.toLowerCase()) return [primary]; + return [primary, { tracker, host, scope: originScope }]; + }; return { definitions, get: (kind) => @@ -885,16 +943,9 @@ export function makeSourceControlDefinitionRegistry( canonicalRepositoryKey: (key) => repositoryKeyOwner(key)?.canonical ?? canonicalKeyPath(key), findByRepositoryKey: (key) => repositoryKeyOwner(key)?.definition, repositorySelector, - issueScope: (identity) => { - const issueScope = - identity?.provider === undefined ? undefined : byKind.get(identity.provider)?.issueScope; - return issueScope === undefined || identity == null - ? repositorySelector(identity) - : issueScope(identity); - }, - changeRequestHost: (identity, kind) => - (kind === undefined ? undefined : byKind.get(kind)?.changeRequestHost?.(identity)) ?? - projectChangeRequestHost(identity, kind), + issueScope, + issueBindings, + changeRequestHost, mediaUrl: (value) => { const url = httpUrl(value); if (url === null) return null;