Skip to content

Commit 9230c90

Browse files
fix(prs): gate reviewer search on a host capability, not the provider
Hosts declare `reviewers.searchCandidates`; GitHub and GitLab do, the rest do not. Clients ask the host only when it says so, so a server that predates the query field (and drops it) is narrowed locally instead of having its unfiltered first page treated as matches. The search, pending, and truncation logic moves into the shared reviewer helper for web and mobile, and the truncated-list text for hosts without search is back to what it was. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent beffdb2 commit 9230c90

12 files changed

Lines changed: 240 additions & 86 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx‎

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { useAtomValue } from "@effect/atom-react";
66
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
77
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
88
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
9-
import { mergeReviewerCandidates } from "@t3tools/client-runtime/pull-request-reviewers";
9+
import {
10+
hostReviewerQuery,
11+
reviewerCandidateView,
12+
type ReviewerTruncation,
13+
} from "@t3tools/client-runtime/pull-request-reviewers";
1014
import type { PullRequestReviewerCandidate } from "@t3tools/contracts";
1115
import { useRef, useState } from "react";
1216
import { FlatList, Pressable, ScrollView, View } from "react-native";
@@ -23,6 +27,16 @@ import { useAtomCommand } from "../../state/use-atom-command";
2327
import { PullRequestAvatar } from "./PullRequestPresentation";
2428
import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
2529

30+
const TRUNCATED_LABELS: Record<ReviewerTruncation, (error: string | null) => string> = {
31+
unsearchable: () =>
32+
"The provider has more reviewers than it returned. Find additional reviewers on the provider.",
33+
searchable: () => "The provider has more reviewers than are listed here. Search to find them.",
34+
"search-failed": (error) =>
35+
`Only listed reviewers are matched: the provider search failed. ${error ?? ""}`,
36+
"search-truncated": () =>
37+
"More reviewers match than the provider returned. Keep typing to narrow it down.",
38+
};
39+
2640
export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequestParams>) {
2741
const navigation = useNavigation();
2842
const contentTopInset = useContentTopInset();
@@ -53,31 +67,30 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
5367
const [pending, setPending] = useState<string | null>(null);
5468
const [error, setError] = useState<string | null>(null);
5569
const inFlight = useRef(false);
56-
const searchQuery = useDebouncedValue(query.trim(), 250);
57-
const supportsSearch = pr?.provider === "github" || pr?.provider === "gitlab";
58-
const searchesHost = supportsSearch && result.data?.truncated === true && searchQuery.length > 0;
59-
const searched = searchesHost && searchQuery === query.trim();
70+
const debouncedQuery = useDebouncedValue(query.trim(), 250);
71+
const searchInput = {
72+
loaded: result.data,
73+
searchable: pr?.capabilities.reviewers.searchCandidates === true,
74+
query,
75+
debouncedQuery,
76+
};
77+
const hostQuery = hostReviewerQuery(searchInput);
6078
const searchResult = useEnvironmentQuery(
61-
searchesHost
79+
hostQuery !== null
6280
? composerPullRequests.reviewerSearch({
6381
...target,
64-
input: { ...target.input, query: searchQuery },
82+
input: { ...target.input, query: hostQuery },
6583
})
6684
: null,
6785
);
68-
const candidates = [
69-
...mergeReviewerCandidates(
70-
result.data?.candidates ?? [],
71-
searched ? (searchResult.data?.candidates ?? []) : [],
72-
query,
73-
),
74-
].sort((a, b) => Number(b.isRequested) - Number(a.isRequested) || a.login.localeCompare(b.login));
75-
const searchPending =
76-
supportsSearch &&
77-
result.data?.truncated === true &&
78-
query.trim().length > 0 &&
79-
(searchQuery !== query.trim() || searchResult.isPending);
80-
const queryError = result.error ?? (searched ? searchResult.error : null);
86+
const view = reviewerCandidateView({
87+
...searchInput,
88+
search: searchResult,
89+
});
90+
const candidates = [...view.candidates].sort(
91+
(a, b) => Number(b.isRequested) - Number(a.isRequested) || a.login.localeCompare(b.login),
92+
);
93+
const queryError = result.error ?? view.searchError;
8194
// A new search shows its best matches from the top.
8295
const search = (next: string) => {
8396
setQuery(next);
@@ -110,7 +123,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
110123
);
111124
return;
112125
}
113-
if (searchesHost) searchResult.refresh();
126+
if (hostQuery !== null) searchResult.refresh();
114127
if (!pr?.capabilities.reviewers.listCandidates) {
115128
search("");
116129
detail.refresh();
@@ -199,7 +212,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
199212
);
200213
}}
201214
ListEmptyComponent={
202-
(result.isPending && !result.data) || searchPending ? (
215+
(result.isPending && !result.data) || view.searchPending ? (
203216
<AppActivityIndicator className="my-8" />
204217
) : (
205218
<View className="px-4 py-6">
@@ -224,18 +237,9 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
224237
)
225238
}
226239
ListFooterComponent={
227-
result.data?.truncated &&
228-
(!supportsSearch ||
229-
query.trim().length === 0 ||
230-
(searched && (searchResult.error !== null || searchResult.data?.truncated))) ? (
240+
view.truncation ? (
231241
<Text className="px-4 py-4 text-xs text-foreground-muted">
232-
{!supportsSearch
233-
? "The provider has more reviewers than are listed here. Find additional reviewers on the provider."
234-
: searched && searchResult.error
235-
? `Only listed reviewers are matched: the provider search failed. ${searchResult.error}`
236-
: searched
237-
? "More reviewers match than the provider returned. Keep typing to narrow it down."
238-
: "The provider has more reviewers than are listed here. Search to find them."}
242+
{TRUNCATED_LABELS[view.truncation](view.searchError)}
239243
</Text>
240244
) : undefined
241245
}

‎apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx‎

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import type {
33
EnvironmentId,
44
PullRequestRef,
55
PullRequestReviewerCandidate,
6-
SourceControlProviderKind,
76
} from "@t3tools/contracts";
8-
import { mergeReviewerCandidates } from "@t3tools/client-runtime/pull-request-reviewers";
7+
import {
8+
hostReviewerQuery,
9+
reviewerCandidateView,
10+
type ReviewerTruncation,
11+
} from "@t3tools/client-runtime/pull-request-reviewers";
912
import { CheckIcon, UserPlusIcon } from "lucide-react";
1013
import { useState } from "react";
1114

@@ -19,16 +22,27 @@ import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
1922
import { PullRequestActorLabel } from "./pullRequestPresentation";
2023
import { readableFailure } from "./pullRequestDetail.logic";
2124

25+
const TRUNCATED_LABELS: Record<ReviewerTruncation, (error: string | null) => string> = {
26+
unsearchable: () =>
27+
"This repository has more people with access than are listed here. Ask for the rest on the host.",
28+
searchable: () =>
29+
"This repository has more people with access than are listed here. Search to find them.",
30+
"search-failed": (error) =>
31+
`Only the people already listed are matched: the host search failed. ${error ?? ""}`,
32+
"search-truncated": () =>
33+
"More people match than the host returned. Keep typing to narrow it down.",
34+
};
35+
2236
export function PullRequestReviewerPicker({
2337
environmentId,
2438
reference,
2539
allowed,
26-
provider,
40+
searchable,
2741
}: {
2842
environmentId: EnvironmentId;
2943
reference: PullRequestRef;
3044
allowed: boolean;
31-
provider: SourceControlProviderKind;
45+
searchable: boolean;
3246
}) {
3347
const [open, setOpen] = useState(false);
3448
const [query, setQuery] = useState("");
@@ -38,29 +52,25 @@ export function PullRequestReviewerPicker({
3852
open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null,
3953
);
4054
// Past the first page the host is asked, since what was loaded cannot answer for the rest.
41-
const searchQuery = useDebouncedValue(query.trim(), 250);
42-
const supportsSearch = provider === "github" || provider === "gitlab";
43-
const searchesHost =
44-
supportsSearch && candidatesQuery.data?.truncated === true && searchQuery.length > 0;
45-
// Only once the debounce has caught up, or rows matching the previous text would linger.
46-
const searched = searchesHost && searchQuery === query.trim();
55+
const debouncedQuery = useDebouncedValue(query.trim(), 250);
56+
const searchInput = { loaded: candidatesQuery.data, searchable, query, debouncedQuery };
57+
const hostQuery = hostReviewerQuery(searchInput);
4758
const searchResult = useEnvironmentQuery(
48-
open && searchesHost
59+
open && hostQuery !== null
4960
? pullRequestEnvironment.reviewerSearch({
5061
environmentId,
51-
input: { ...reference, query: searchQuery },
62+
input: { ...reference, query: hostQuery },
5263
})
5364
: null,
5465
);
5566
const requestReviewers = useAtomCommand(pullRequestEnvironment.requestReviewers, {
5667
reportFailure: false,
5768
});
5869

59-
const candidates = mergeReviewerCandidates(
60-
candidatesQuery.data?.candidates ?? [],
61-
searched ? (searchResult.data?.candidates ?? []) : [],
62-
query,
63-
);
70+
const { candidates, searchPending, searchError, truncation } = reviewerCandidateView({
71+
...searchInput,
72+
search: searchResult,
73+
});
6474

6575
const toggle = async (candidate: PullRequestReviewerCandidate) => {
6676
if (pending !== null) return;
@@ -89,7 +99,7 @@ export function PullRequestReviewerPicker({
8999
}
90100
// The command reads the loaded list again only for somebody it lacked, and a search result
91101
// is keyed by what was typed, so the one on screen is read here or it keeps the old request.
92-
if (searchesHost) searchResult.refresh();
102+
if (hostQuery !== null) searchResult.refresh();
93103
toastManager.add({
94104
type: "success",
95105
title: candidate.isRequested
@@ -109,41 +119,20 @@ export function PullRequestReviewerPicker({
109119
query={query}
110120
onQueryChange={setQuery}
111121
searchLabel="Search people with access"
112-
isPending={
113-
(candidatesQuery.isPending && candidatesQuery.data === null) ||
114-
// Nothing loaded matches yet, and the host has not answered: not a "nobody matches".
115-
(candidates.length === 0 &&
116-
supportsSearch &&
117-
candidatesQuery.data?.truncated === true &&
118-
query.trim().length > 0 &&
119-
(searchQuery !== query.trim() || searchResult.isPending))
120-
}
122+
isPending={(candidatesQuery.isPending && candidatesQuery.data === null) || searchPending}
121123
error={
122124
candidatesQuery.data === null
123125
? candidatesQuery.error
124-
: searched && candidates.length === 0
125-
? searchResult.error
126+
: candidates.length === 0
127+
? searchError
126128
: null
127129
}
128130
candidates={candidates}
129131
emptyLabel="Nobody else has access to this repository."
130132
noMatchLabel="Nobody with access matches that."
131133
errorLabel="The people with access could not be read."
132-
truncated={
133-
candidatesQuery.data?.truncated === true &&
134-
(!supportsSearch ||
135-
query.trim().length === 0 ||
136-
(searched && (searchResult.error !== null || searchResult.data?.truncated === true)))
137-
}
138-
truncatedLabel={
139-
!supportsSearch
140-
? "The provider has more reviewers than are listed here. Find additional reviewers on the provider."
141-
: searched && searchResult.error !== null
142-
? `Only the people already listed are matched: the host search failed. ${searchResult.error}`
143-
: searched
144-
? "More people match than the host returned. Keep typing to narrow it down."
145-
: "This repository has more people with access than are listed here. Search to find them."
146-
}
134+
truncated={truncation !== null}
135+
truncatedLabel={truncation === null ? "" : TRUNCATED_LABELS[truncation](searchError)}
147136
candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`}
148137
disabled={pending !== null}
149138
onSelect={(candidate) => void toggle(candidate)}

‎apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ export function PullRequestSummaryTab({
751751
{detail.capabilities.reviewers.request &&
752752
detail.capabilities.reviewers.listCandidates ? (
753753
<PullRequestReviewerPicker
754-
provider={detail.provider}
754+
searchable={detail.capabilities.reviewers.searchCandidates === true}
755755
environmentId={environmentId}
756756
reference={reference}
757757
allowed={detail.viewerPermissions.requestReviewers}

‎packages/client-runtime/src/pullRequestReviewers.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "@effect/vitest";
22

3-
import { mergeReviewerCandidates } from "./pullRequestReviewers.ts";
3+
import {
4+
hostReviewerQuery,
5+
mergeReviewerCandidates,
6+
reviewerCandidateView,
7+
} from "./pullRequestReviewers.ts";
48

59
const reviewer = (
610
login: string,
@@ -50,3 +54,78 @@ describe("mergeReviewerCandidates", () => {
5054
).toEqual(["team", "user"]);
5155
});
5256
});
57+
58+
describe("reviewerCandidateView", () => {
59+
const firstPage = { candidates: [reviewer("ada"), reviewer("grace")], truncated: true };
60+
const idle = { data: null, isPending: false, error: null };
61+
62+
it.each([
63+
{
64+
name: "a host without search, as from an older server, narrows the loaded page itself",
65+
searchable: false,
66+
query: "zed",
67+
debouncedQuery: "zed",
68+
search: idle,
69+
expected: { hostQuery: null, logins: [], searchPending: false, truncation: "unsearchable" },
70+
},
71+
{
72+
name: "a searchable host is asked once the debounce catches up",
73+
searchable: true,
74+
query: "zed",
75+
debouncedQuery: "zed",
76+
search: {
77+
data: { candidates: [reviewer("zed")], truncated: false },
78+
isPending: false,
79+
error: null,
80+
},
81+
expected: { hostQuery: "zed", logins: ["zed"], searchPending: false, truncation: null },
82+
},
83+
{
84+
name: "results for text the debounce has not reached are neither shown nor a no-match",
85+
searchable: true,
86+
query: "zed",
87+
debouncedQuery: "ze",
88+
search: {
89+
data: { candidates: [reviewer("zelda")], truncated: false },
90+
isPending: false,
91+
error: null,
92+
},
93+
expected: { hostQuery: "ze", logins: [], searchPending: true, truncation: null },
94+
},
95+
{
96+
name: "a failed search falls back to the loaded page and says so",
97+
searchable: true,
98+
query: "gr",
99+
debouncedQuery: "gr",
100+
search: { data: null, isPending: false, error: "rate limited" },
101+
expected: {
102+
hostQuery: "gr",
103+
logins: ["grace"],
104+
searchPending: false,
105+
truncation: "search-failed",
106+
},
107+
},
108+
{
109+
name: "an empty query lists the loaded page and offers search",
110+
searchable: true,
111+
query: "",
112+
debouncedQuery: "",
113+
search: idle,
114+
expected: {
115+
hostQuery: null,
116+
logins: ["ada", "grace"],
117+
searchPending: false,
118+
truncation: "searchable",
119+
},
120+
},
121+
])("$name", ({ searchable, query, debouncedQuery, search, expected }) => {
122+
const input = { loaded: firstPage, searchable, query, debouncedQuery };
123+
const view = reviewerCandidateView({ ...input, search });
124+
expect({
125+
hostQuery: hostReviewerQuery(input),
126+
logins: view.candidates.map((entry) => entry.login),
127+
searchPending: view.searchPending,
128+
truncation: view.truncation,
129+
}).toEqual(expected);
130+
});
131+
});

0 commit comments

Comments
 (0)