From 3b0ba0eaafdc6ef75fa62e13abeddde00f3e9a6f Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:06:19 +0100 Subject: [PATCH 1/2] feat(issues): a fork lists its upstream's issues and its own A project cloned from a fork read issues from one repository only: the `gh repo set-default` choice, else `upstream`, else `origin`. So a fork showed only upstream issues, and its own issues were read-only. The shared `issueBindings` helper now returns the primary repository and, when the `origin` remote names a different repository on the same server, that fork too. The server lists both and lets the project write to both. Clients use the same helper for issue links and filter scopes. A fork still counts as one project, and a failed read names its repository. --- apps/server/src/issue/IssueService.test.ts | 50 +++++++++++ apps/server/src/issue/IssueService.ts | 85 +++++++++++-------- .../client-runtime/src/issueLinks.test.ts | 11 ++- packages/client-runtime/src/issueLinks.ts | 50 +++++------ packages/client-runtime/src/issueList.ts | 35 ++++---- .../src/sourceControlClients.test.ts | 78 +++++++++++++++++ .../src/shared/definition.ts | 71 +++++++++++++--- 7 files changed, 287 insertions(+), 93 deletions(-) diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index e68799eea12e7..8890b14af0102 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -283,6 +283,56 @@ 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 forkProject = project({ id: "fork", repository: "acme/web" }); + const forked = yield* makeService({ + projects: [ + { + ...forkProject, + repositoryIdentity: { + ...forkProject.repositoryIdentity!, + origin: { canonicalKey: "github.com/me/web", displayName: "me/web" }, + }, + }, + ], + trackers: [ + fakeTracker({ + listIssues: (input) => + input.state === "closed" + ? Effect.fail( + new IssueTrackerError({ + tracker: GITHUB, + operation: "listIssues", + reason: "failed", + detail: "down", + }), + ) + : 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 })]); + const failed = yield* forked.list({ state: "closed" }); + expect(failed.errors.map((error) => error.projectTitle).sort()).toEqual([ + "fork (acme/web)", + "fork (me/web)", + ]); + const forkIssue = { ...own, projectId: "fork" as ProjectId, scope: "me/web", key: "5" }; + expect((yield* forked.detail(forkIssue)).writable).toBe(true); + yield* forked.comment({ ...forkIssue, body: "ok" }); + expect(writes).toEqual(["acme/web#1", "me/web#5"]); + expect(yield* forked.resolveUrl("https://github.com/me/web/issues/5")).toMatchObject({ + projectId: "fork", + scope: "me/web", + 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 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..11c020ce8240c 100644 --- a/packages/client-runtime/src/sourceControlClients.test.ts +++ b/packages/client-runtime/src/sourceControlClients.test.ts @@ -376,6 +376,84 @@ describe("sourceControlClients project hosts", () => { ).toBe("service"); expect(sourceControlClients.repositorySelector({ owner: "o", name: "r" })).toBe("o/r"); }); + + it("reads a fork's issues beside its upstream's only when both are on one host", () => { + const upstream = { + canonicalKey: "github.com/acme/web", + locator: { + source: "git-remote" as const, + remoteName: "upstream", + remoteUrl: "https://github.com/acme/web.git", + }, + provider: SourceControlProviderKind.make("github"), + displayName: "acme/web", + }; + const fork = (canonicalKey: string, displayName?: string) => ({ + ...upstream, + origin: { canonicalKey, ...(displayName ? { displayName } : {}) }, + }); + const github = { tracker: "github", host: "github.com" }; + + expect(sourceControlClients.issueBindings(upstream)).toEqual([ + { ...github, scope: "acme/web" }, + ]); + expect(sourceControlClients.issueBindings(fork("github.com/me/web", "Me/web"))).toEqual([ + { ...github, scope: "acme/web" }, + { ...github, scope: "Me/web" }, + ]); + expect(sourceControlClients.issueBindings(fork("gitlab.com/me/web", "me/web"))).toEqual([ + { ...github, scope: "acme/web" }, + ]); + // Keys drop schemes and ports: a fork on another of either is another server. + const ported = fork("github.com/me/web", "me/web"); + expect( + sourceControlClients.issueBindings({ + ...ported, + origin: { ...ported.origin, remoteUrl: "https://github.com:8443/me/web.git" }, + }), + ).toEqual([{ ...github, scope: "acme/web" }]); + expect( + sourceControlClients.issueBindings({ + ...ported, + locator: { ...ported.locator, remoteUrl: "git@github.com:acme/web.git" }, + origin: { ...ported.origin, remoteUrl: "https://github.com:8443/me/web.git" }, + }), + ).toEqual([{ ...github, scope: "acme/web" }]); + expect( + sourceControlClients.issueBindings({ + ...ported, + locator: { ...ported.locator, remoteUrl: "git@github.com:acme/web.git" }, + origin: { ...ported.origin, remoteUrl: "https://github.com/me/web.git" }, + }), + ).toEqual([ + { ...github, scope: "acme/web" }, + { ...github, scope: "me/web" }, + ]); + // A Forgejo upstream over SSH is served from its web address's port, not the origin's. + expect( + sourceControlClients.issueBindings({ + ...ported, + provider: SourceControlProviderKind.make("forgejo"), + canonicalKey: "forgejo.test/acme/web", + webUrl: "https://forgejo.test:8443/acme/web", + locator: { ...ported.locator, remoteUrl: "git@forgejo.test:acme/web.git" }, + origin: { + canonicalKey: "forgejo.test/me/web", + displayName: "me/web", + remoteUrl: "https://forgejo.test/me/web.git", + }, + }), + ).toEqual([{ tracker: "forgejo", host: "forgejo.test:8443", scope: "acme/web" }]); + expect( + sourceControlClients.issueBindings({ + ...ported, + locator: { ...ported.locator, remoteUrl: "http://github.com/acme/web.git" }, + origin: { ...ported.origin, remoteUrl: "https://github.com/me/web.git" }, + }), + ).toEqual([{ ...github, scope: "acme/web" }]); + const { provider: _provider, ...unrecognized } = upstream; + expect(sourceControlClients.issueBindings(unrecognized)).toEqual([]); + }); }); 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; From 589774230028c20de5fb34d4d9b88c95495aff94 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:21:33 +0100 Subject: [PATCH 2/2] test(issues): trim fork issue tests to one table and one service case --- apps/server/src/issue/IssueService.test.ts | 36 ++----- .../src/sourceControlClients.test.ts | 99 +++++-------------- 2 files changed, 32 insertions(+), 103 deletions(-) diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index 8890b14af0102..77a2215fb3916 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -284,30 +284,21 @@ describe("IssueService", () => { ).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 forkProject = project({ id: "fork", repository: "acme/web" }); + const fork = project({ id: "fork", repository: "acme/web" }); const forked = yield* makeService({ projects: [ { - ...forkProject, + ...fork, repositoryIdentity: { - ...forkProject.repositoryIdentity!, - origin: { canonicalKey: "github.com/me/web", displayName: "me/web" }, + ...fork.repositoryIdentity!, + origin: { canonicalKey: "github.com/me/web" }, }, }, ], trackers: [ fakeTracker({ listIssues: (input) => - input.state === "closed" - ? Effect.fail( - new IssueTrackerError({ - tracker: GITHUB, - operation: "listIssues", - reason: "failed", - detail: "down", - }), - ) - : Effect.succeed({ items: [issue(input.scope)], nextCursor: null }), + Effect.succeed({ items: [issue(input.scope)], nextCursor: null }), comment: (input) => Effect.sync(() => { writes.push(`${input.scope}#${input.key}`); @@ -318,20 +309,13 @@ describe("IssueService", () => { 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 })]); - const failed = yield* forked.list({ state: "closed" }); - expect(failed.errors.map((error) => error.projectTitle).sort()).toEqual([ - "fork (acme/web)", - "fork (me/web)", - ]); - const forkIssue = { ...own, projectId: "fork" as ProjectId, scope: "me/web", key: "5" }; - expect((yield* forked.detail(forkIssue)).writable).toBe(true); - yield* forked.comment({ ...forkIssue, body: "ok" }); - expect(writes).toEqual(["acme/web#1", "me/web#5"]); - expect(yield* forked.resolveUrl("https://github.com/me/web/issues/5")).toMatchObject({ - projectId: "fork", + yield* forked.comment({ + ...own, + projectId: "fork" as ProjectId, scope: "me/web", - writable: true, + 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" })); diff --git a/packages/client-runtime/src/sourceControlClients.test.ts b/packages/client-runtime/src/sourceControlClients.test.ts index 11c020ce8240c..acac229e6c1fa 100644 --- a/packages/client-runtime/src/sourceControlClients.test.ts +++ b/packages/client-runtime/src/sourceControlClients.test.ts @@ -377,83 +377,28 @@ describe("sourceControlClients project hosts", () => { expect(sourceControlClients.repositorySelector({ owner: "o", name: "r" })).toBe("o/r"); }); - it("reads a fork's issues beside its upstream's only when both are on one host", () => { - const upstream = { - canonicalKey: "github.com/acme/web", - locator: { - source: "git-remote" as const, - remoteName: "upstream", - remoteUrl: "https://github.com/acme/web.git", - }, - provider: SourceControlProviderKind.make("github"), - displayName: "acme/web", - }; - const fork = (canonicalKey: string, displayName?: string) => ({ - ...upstream, - origin: { canonicalKey, ...(displayName ? { displayName } : {}) }, - }); - const github = { tracker: "github", host: "github.com" }; - - expect(sourceControlClients.issueBindings(upstream)).toEqual([ - { ...github, scope: "acme/web" }, - ]); - expect(sourceControlClients.issueBindings(fork("github.com/me/web", "Me/web"))).toEqual([ - { ...github, scope: "acme/web" }, - { ...github, scope: "Me/web" }, - ]); - expect(sourceControlClients.issueBindings(fork("gitlab.com/me/web", "me/web"))).toEqual([ - { ...github, scope: "acme/web" }, - ]); - // Keys drop schemes and ports: a fork on another of either is another server. - const ported = fork("github.com/me/web", "me/web"); - expect( - sourceControlClients.issueBindings({ - ...ported, - origin: { ...ported.origin, remoteUrl: "https://github.com:8443/me/web.git" }, - }), - ).toEqual([{ ...github, scope: "acme/web" }]); - expect( - sourceControlClients.issueBindings({ - ...ported, - locator: { ...ported.locator, remoteUrl: "git@github.com:acme/web.git" }, - origin: { ...ported.origin, remoteUrl: "https://github.com:8443/me/web.git" }, - }), - ).toEqual([{ ...github, scope: "acme/web" }]); - expect( - sourceControlClients.issueBindings({ - ...ported, - locator: { ...ported.locator, remoteUrl: "git@github.com:acme/web.git" }, - origin: { ...ported.origin, remoteUrl: "https://github.com/me/web.git" }, - }), - ).toEqual([ - { ...github, scope: "acme/web" }, - { ...github, scope: "me/web" }, - ]); - // A Forgejo upstream over SSH is served from its web address's port, not the origin's. - expect( - sourceControlClients.issueBindings({ - ...ported, - provider: SourceControlProviderKind.make("forgejo"), - canonicalKey: "forgejo.test/acme/web", - webUrl: "https://forgejo.test:8443/acme/web", - locator: { ...ported.locator, remoteUrl: "git@forgejo.test:acme/web.git" }, - origin: { - canonicalKey: "forgejo.test/me/web", - displayName: "me/web", - remoteUrl: "https://forgejo.test/me/web.git", - }, - }), - ).toEqual([{ tracker: "forgejo", host: "forgejo.test:8443", scope: "acme/web" }]); - expect( - sourceControlClients.issueBindings({ - ...ported, - locator: { ...ported.locator, remoteUrl: "http://github.com/acme/web.git" }, - origin: { ...ported.origin, remoteUrl: "https://github.com/me/web.git" }, - }), - ).toEqual([{ ...github, scope: "acme/web" }]); - const { provider: _provider, ...unrecognized } = upstream; - expect(sourceControlClients.issueBindings(unrecognized)).toEqual([]); - }); + 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", () => {