Skip to content

Commit 20f082e

Browse files
fix(web): preserve repository selection across environments
1 parent 2a130b5 commit 20f082e

3 files changed

Lines changed: 61 additions & 30 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,43 @@ describe("findProjectForRepository", () => {
16541654
).toBe(payments);
16551655
});
16561656

1657+
it("keeps repository matches on the selected environment when project IDs overlap", () => {
1658+
const project = (id: string, environmentId: string, repository: string) => ({
1659+
id,
1660+
environmentId,
1661+
repositoryIdentity: {
1662+
canonicalKey: `github.com/${repository}`,
1663+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
1664+
provider: "github",
1665+
displayName: repository,
1666+
},
1667+
});
1668+
const unrelated = project("p1", ENV_1, "acme/web");
1669+
const local = project("p2", ENV_1, "acme/api");
1670+
const remote = project("p1", ENV_2, "acme/api");
1671+
const projects = [unrelated, local, remote];
1672+
1673+
expect(findProjectForRepository(projects, { repository: "acme/api" })).toBe(local);
1674+
expect(
1675+
findProjectForRepository(projects, {
1676+
repository: "acme/api",
1677+
selectedEnvironmentId: ENV_2,
1678+
}),
1679+
).toBe(remote);
1680+
expect(
1681+
findProjectForRepository(projects, {
1682+
repository: "acme/web",
1683+
selectedEnvironmentId: ENV_2,
1684+
}),
1685+
).toBeUndefined();
1686+
expect(
1687+
findProjectForRepository(projects, {
1688+
repository: "acme/api",
1689+
selectedEnvironmentId: "env-connecting",
1690+
}),
1691+
).toBeUndefined();
1692+
});
1693+
16571694
it.each([
16581695
["gitlab", "t3tools/platform/t3code", "T3Tools/Platform/T3Code"],
16591696
["github", "t3tools/t3code", "T3Tools/T3Code"],

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -974,17 +974,6 @@ export function largestPullRequestReadCount(
974974
return Math.max(0, ...counts.values());
975975
}
976976

977-
/**
978-
* The project a repository-only link belongs to. The repository is the provider-native selector
979-
* the server checks a request against, and more than one project can answer it: two checkouts of
980-
* one repository, and on Azure DevOps two repositories that merely share a name across that
981-
* host's projects, since the selector there is the bare name `az repos pr` accepts.
982-
*
983-
* Where the page is already scoped to one of the candidates that one is chosen, because the scope
984-
* is the reader's own word about which project the link came from. A scope naming a project the
985-
* repository does not belong to is not believed over the repository: the first candidate stands,
986-
* as it would with no scope at all.
987-
*/
988977
export function findProjectForRepository<
989978
Project extends {
990979
readonly id: string;
@@ -996,27 +985,30 @@ export function findProjectForRepository<
996985
input: {
997986
readonly repository: string;
998987
readonly host?: string | undefined;
988+
readonly selectedEnvironmentId?: string | null | undefined;
999989
readonly scopedProjectId?: string | undefined;
1000990
readonly scopedEnvironmentId?: string | null | undefined;
1001991
},
1002992
): Project | undefined {
1003993
const repository = input.repository.toLowerCase();
1004994
const host = input.host?.toLowerCase();
1005995
const candidates = projects.filter((project) => {
996+
if (
997+
input.selectedEnvironmentId != null &&
998+
project.environmentId !== input.selectedEnvironmentId
999+
) {
1000+
return false;
1001+
}
10061002
const identity = project.repositoryIdentity;
10071003
if (!identity || sourceControlRepositorySelector(identity)?.toLowerCase() !== repository) {
10081004
return false;
10091005
}
1010-
// The same `owner/name` can exist on two hosts. Without this the first match wins, and a link
1011-
// that named its host opens the pull request from the other one.
10121006
return (
10131007
host === undefined ||
10141008
pullRequestHostOf(identity, identity.provider as SourceControlProviderKind) === host
10151009
);
10161010
});
1017-
const scoped =
1018-
input.scopedProjectId === undefined
1019-
? undefined
1020-
: findScopedProject(candidates, input.scopedEnvironmentId, input.scopedProjectId);
1011+
// Azure repository names can collide across projects on the same host.
1012+
const scoped = findScopedProject(candidates, input.scopedEnvironmentId, input.scopedProjectId);
10211013
return scoped ?? candidates[0];
10221014
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,18 +1800,6 @@ function usePullRequestScope(search: PullRequestsSearch) {
18001800
const scopedProjects = pullRequestFilterProjects(projects, environmentLabels, scopedProject);
18011801

18021802
const selectedHost = search.selectedHost ?? search.host;
1803-
const projectIdForRepository =
1804-
search.repository === undefined
1805-
? undefined
1806-
: findProjectForRepository(projects, {
1807-
repository: search.repository,
1808-
host: selectedHost,
1809-
scopedProjectId,
1810-
scopedEnvironmentId,
1811-
})?.id;
1812-
1813-
const linkedProjectId = resolveProjectScope(search.selectedProjectId, projects, projectsKnown);
1814-
const selectedProjectId = linkedProjectId ?? projectIdForRepository ?? scopedProjectId;
18151803
// A named server still connecting is kept named rather than falling back: falling back while it
18161804
// is merely not ready yet would resolve the project against whatever other server has answered,
18171805
// which can be the wrong one where two servers share a project id.
@@ -1820,7 +1808,21 @@ function usePullRequestScope(search: PullRequestsSearch) {
18201808
knownEnvironmentIds,
18211809
scopedEnvironmentId,
18221810
);
1823-
const selectedProject = findScopedProject(projects, selectedEnvironmentId, selectedProjectId);
1811+
const projectForRepository =
1812+
search.repository === undefined
1813+
? undefined
1814+
: findProjectForRepository(projects, {
1815+
repository: search.repository,
1816+
host: selectedHost,
1817+
selectedEnvironmentId,
1818+
scopedProjectId,
1819+
scopedEnvironmentId,
1820+
});
1821+
const linkedProjectId = resolveProjectScope(search.selectedProjectId, projects, projectsKnown);
1822+
const selectedProject =
1823+
linkedProjectId === undefined && search.repository !== undefined
1824+
? projectForRepository
1825+
: findScopedProject(projects, selectedEnvironmentId, linkedProjectId ?? scopedProjectId);
18241826
const queryEnvironmentIds = resolveQueryEnvironmentIds(
18251827
environmentIds,
18261828
projects,

0 commit comments

Comments
 (0)