Skip to content

Commit 10f4b3a

Browse files
committed
fix(web): prefer the scoped project for repository-only PR links
The pull requests page resolves a link carrying only a repository and number by finding the project whose identity answers that repository. It compared `owner/name`, which is not what the server accepts: on Azure DevOps the repository is the bare name, so no project matched and the link opened nothing. Matching the provider-native selector instead exposes a second problem. That selector is unique per host on GitHub and GitLab, but on Azure two repositories sharing a name in two Azure projects both answer it, and the first one won even when the page was already scoped to the other. The reference then failed the server's project check or opened the wrong project's pull request. Move the lookup into `pullRequestList.logic.ts` as `findProjectForRepository`. It collects every project answering the repository and host, then prefers the one the page is scoped to, resolved the same way the page resolves its own scope. A scope naming a project the repository does not belong to still does not override the match, so the fallback order is unchanged for that case.
1 parent 46c8579 commit 10f4b3a

3 files changed

Lines changed: 158 additions & 20 deletions

File tree

‎apps/web/src/components/pullRequest/pullRequestList.logic.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
33

44
import {
55
filterPullRequestsByInvolvement,
6+
findProjectForRepository,
67
findScopedProject,
78
mergePullRequestLists,
89
pullRequestEntryKey,
@@ -1496,3 +1497,93 @@ describe("the priority groups against a paginated feed", () => {
14961497
).toEqual([6123]);
14971498
});
14981499
});
1500+
1501+
describe("findProjectForRepository", () => {
1502+
const azure = (id: string, environmentId: string, path: string) => ({
1503+
id,
1504+
environmentId,
1505+
repositoryIdentity: {
1506+
canonicalKey: `dev.azure.com/${path}`,
1507+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
1508+
provider: "azure-devops",
1509+
displayName: path,
1510+
owner: path.split("/")[0]!,
1511+
name: path.split("/").at(-1)!,
1512+
},
1513+
});
1514+
// Two Azure DevOps repositories that merely share a name, in two Azure projects on one host.
1515+
// Both answer the selector `api`, which is all the server accepts for that provider.
1516+
const payments = azure("p1", "env-1", "contoso/payments/_git/api");
1517+
const billing = azure("p2", "env-1", "contoso/billing/_git/api");
1518+
1519+
it("prefers the project the page is scoped to among several answering one repository", () => {
1520+
expect(
1521+
findProjectForRepository([payments, billing], {
1522+
repository: "api",
1523+
scopedProjectId: "p2",
1524+
scopedEnvironmentId: "env-1",
1525+
}),
1526+
).toBe(billing);
1527+
});
1528+
1529+
it("does not let a scope naming another repository's project override the match", () => {
1530+
const other = azure("p3", "env-1", "contoso/payments/_git/web");
1531+
expect(
1532+
findProjectForRepository([payments, other], {
1533+
repository: "api",
1534+
scopedProjectId: "p3",
1535+
}),
1536+
).toBe(payments);
1537+
});
1538+
1539+
it("keeps a scope on another server from claiming a same-id project here", () => {
1540+
const elsewhere = azure("p1", "env-2", "contoso/billing/_git/api");
1541+
expect(
1542+
findProjectForRepository([payments, elsewhere], {
1543+
repository: "api",
1544+
scopedProjectId: "p1",
1545+
scopedEnvironmentId: "env-2",
1546+
}),
1547+
).toBe(elsewhere);
1548+
});
1549+
1550+
it("reads a nested GitLab group as the whole path below the host", () => {
1551+
const gitlab = {
1552+
id: "gl1",
1553+
environmentId: "env-1",
1554+
repositoryIdentity: {
1555+
canonicalKey: "gitlab.com/t3tools/platform/t3code",
1556+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
1557+
provider: "gitlab",
1558+
displayName: "T3Tools/Platform/T3Code",
1559+
owner: "t3tools",
1560+
name: "t3code",
1561+
},
1562+
};
1563+
expect(findProjectForRepository([gitlab], { repository: "t3tools/platform/t3code" })).toBe(
1564+
gitlab,
1565+
);
1566+
expect(findProjectForRepository([gitlab], { repository: "t3tools/t3code" })).toBeUndefined();
1567+
});
1568+
1569+
it("matches case-insensitively and keeps two hosts apart", () => {
1570+
const github = {
1571+
id: "g1",
1572+
environmentId: "env-1",
1573+
repositoryIdentity: {
1574+
canonicalKey: "github.com/t3tools/t3code",
1575+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
1576+
provider: "github",
1577+
owner: "t3tools",
1578+
name: "t3code",
1579+
},
1580+
};
1581+
expect(findProjectForRepository([github], { repository: "T3Tools/T3Code" })).toBe(github);
1582+
expect(
1583+
findProjectForRepository([github], {
1584+
repository: "t3tools/t3code",
1585+
host: "github.acme.test",
1586+
}),
1587+
).toBeUndefined();
1588+
});
1589+
});

‎apps/web/src/components/pullRequest/pullRequestList.logic.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PullRequestListEntry,
66
PullRequestListProjectError,
77
PullRequestListResult,
8+
pullRequestHostOf,
89
resolvePullRequestAuthorFilter,
910
} from "@t3tools/contracts";
1011
import type {
@@ -16,7 +17,10 @@ import type {
1617
PullRequestListCursors,
1718
PullRequestListFilters,
1819
PullRequestListState,
20+
RepositoryIdentity,
21+
SourceControlProviderKind,
1922
} from "@t3tools/contracts";
23+
import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
2024

2125
import { toSortableTimestamp } from "../../lib/threadSort";
2226
import type { PullRequestListSort } from "./pullRequestListPreferences";
@@ -911,6 +915,53 @@ export function findScopedProject<
911915
return matches.find((project) => project.environmentId === environmentId);
912916
}
913917

918+
/**
919+
* The project a repository-only link belongs to. The repository is the provider-native selector
920+
* the server checks a request against, and more than one project can answer it: two checkouts of
921+
* one repository, and on Azure DevOps two repositories that merely share a name across that
922+
* host's projects, since the selector there is the bare name `az repos pr` accepts.
923+
*
924+
* Where the page is already scoped to one of the candidates that one is chosen, because the scope
925+
* is the reader's own word about which project the link came from. A scope naming a project the
926+
* repository does not belong to is not believed over the repository: the first candidate stands,
927+
* as it would with no scope at all.
928+
*/
929+
export function findProjectForRepository<
930+
Project extends {
931+
readonly id: string;
932+
readonly environmentId: string;
933+
readonly repositoryIdentity?: RepositoryIdentity | null | undefined;
934+
},
935+
>(
936+
projects: ReadonlyArray<Project>,
937+
input: {
938+
readonly repository: string;
939+
readonly host?: string | undefined;
940+
readonly scopedProjectId?: string | undefined;
941+
readonly scopedEnvironmentId?: string | null | undefined;
942+
},
943+
): Project | undefined {
944+
const repository = input.repository.toLowerCase();
945+
const host = input.host?.toLowerCase();
946+
const candidates = projects.filter((project) => {
947+
const identity = project.repositoryIdentity;
948+
if (!identity || sourceControlRepositorySelector(identity)?.toLowerCase() !== repository) {
949+
return false;
950+
}
951+
// The same `owner/name` can exist on two hosts. Without this the first match wins, and a link
952+
// that named its host opens the pull request from the other one.
953+
return (
954+
host === undefined ||
955+
pullRequestHostOf(identity, identity.provider as SourceControlProviderKind) === host
956+
);
957+
});
958+
const scoped =
959+
input.scopedProjectId === undefined
960+
? undefined
961+
: findScopedProject(candidates, input.scopedEnvironmentId, input.scopedProjectId);
962+
return scoped ?? candidates[0];
963+
}
964+
914965
/**
915966
* Which environments a listing should ask, once the project scope is known. Scoping to a project
916967
* scopes to the server that owns it, saving every other server a read that could only answer with

‎apps/web/src/routes/_chat.pull-requests.tsx‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444

4545
import {
4646
filterPullRequestsByInvolvement,
47+
findProjectForRepository,
4748
findScopedProject,
4849
collectPullRequestListFacets,
4950
groupPullRequestsByInvolvement,
@@ -374,27 +375,22 @@ function PullRequestsRouteView() {
374375
);
375376

376377
// A link from a thread or the sidebar only knows the repository, so the owning project is
377-
// resolved here; an explicit `projectId` in the URL still wins.
378+
// resolved here; an explicit `projectId` in the URL still wins. The repository is spelled the
379+
// way the server names it, and where several projects answer that name the page's own scope
380+
// picks among them, so a repository-only link never wanders off the project being read.
378381
const selectedHost = search.selectedHost ?? search.host;
379-
const projectIdForRepository = useMemo(() => {
380-
const repository = search.repository?.toLowerCase();
381-
if (repository === undefined) return undefined;
382-
const identity = projects.find(
383-
(project) =>
384-
project.repositoryIdentity?.owner &&
385-
project.repositoryIdentity.name &&
386-
`${project.repositoryIdentity.owner}/${project.repositoryIdentity.name}`.toLowerCase() ===
387-
repository &&
388-
// The same `owner/name` can exist on two hosts. Without this the first match wins, and
389-
// a link that named its host opens the pull request from the other one.
390-
(selectedHost === undefined ||
391-
pullRequestHostOf(
392-
project.repositoryIdentity,
393-
project.repositoryIdentity.provider as SourceControlProviderKind,
394-
) === selectedHost.toLowerCase()),
395-
);
396-
return identity?.id;
397-
}, [projects, selectedHost, search.repository]);
382+
const projectIdForRepository = useMemo(
383+
() =>
384+
search.repository === undefined
385+
? undefined
386+
: findProjectForRepository(projects, {
387+
repository: search.repository,
388+
host: selectedHost,
389+
scopedProjectId,
390+
scopedEnvironmentId,
391+
})?.id,
392+
[projects, scopedEnvironmentId, scopedProjectId, selectedHost, search.repository],
393+
);
398394

399395
// The selection is resolved the same way the scope is: an id no connected environment has can
400396
// never be read here, and one that arrived before the projects did is not yet wrong.

0 commit comments

Comments
 (0)