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
34 changes: 34 additions & 0 deletions apps/server/src/issue/IssueService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
85 changes: 48 additions & 37 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<ProjectTrackerBinding> {
return sourceControlDefinitions
.issueBindings(project.repositoryIdentity)
.map((binding) => ({ ...binding, tracker: IssueTrackerKind.make(binding.tracker) }));
}

function findStandaloneIssueUrl(trackers: ReadonlyArray<StandaloneIssueTrackerApi>, value: string) {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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<ProjectId> }
>();
for (const candidate of bound.flat()) {
if (host !== undefined && candidate.host !== host && candidate.host !== filter.authority)
Expand All @@ -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 });
Expand All @@ -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<SupportedProject & { readonly writable: boolean }, IssueError> =>
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 });
}),
);

Expand Down Expand Up @@ -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<string>();
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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.",
Expand Down
11 changes: 10 additions & 1 deletion packages/client-runtime/src/issueLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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;

Expand All @@ -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) =>
({
Expand Down
50 changes: 23 additions & 27 deletions packages/client-runtime/src/issueLinks.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,21 +14,11 @@ import type { EnvironmentProject } from "./state/shell.ts";

type LinkProject = Pick<EnvironmentProject, "id" | "environmentId" | "repositoryIdentity">;

/** 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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -140,14 +135,15 @@ export function resolveIssueLinkTarget<Project extends LinkProject>(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) =>
Expand All @@ -162,7 +158,7 @@ export function resolveIssueLinkTarget<Project extends LinkProject>(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,
Expand Down
35 changes: 17 additions & 18 deletions packages/client-runtime/src/issueList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions packages/client-runtime/src/sourceControlClients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading
Loading