Skip to content

Commit c82b8fd

Browse files
committed
fix(mobile): support draft qualifiers in PR search
1 parent 42da166 commit c82b8fd

8 files changed

Lines changed: 199 additions & 159 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parsePullRequestQuery } from "@t3tools/client-runtime/pull-request-query";
12
import { LegendList, type LegendListRef } from "@legendapp/list/react-native";
23
import { useNavigation } from "@react-navigation/native";
34
import { use, useCallback, useEffect, useMemo, useRef, useState } from "react";
@@ -81,6 +82,7 @@ export function PullRequestsScreen(
8182
const debouncedQuery = useDebouncedValue(tab.query.trim(), 180);
8283
// Dismissal must restore the cached unfiltered list before restoring its offset.
8384
const query = tab.searchMode === "resting" ? "" : debouncedQuery;
85+
const parsedQuery = useMemo(() => parsePullRequestQuery(query), [query]);
8486
const listControls = usePullRequestListControls();
8587
const selectedEnvironmentId = listControls.environmentId;
8688
const environmentId = environments.some((item) => item.environmentId === selectedEnvironmentId)
@@ -126,13 +128,13 @@ export function PullRequestsScreen(
126128
input: {
127129
state: controls.state,
128130
involvement: controls.involvement,
129-
filters: controls.filters,
131+
filters: { ...controls.filters, ...parsedQuery.filters },
130132
limit: PR_LIST_PAGE_SIZE,
131133
...(controls.projectId ? { projectId: ProjectId.make(controls.projectId) } : {}),
132-
...(query ? { query } : {}),
134+
...(parsedQuery.text ? { query: parsedQuery.text } : {}),
133135
},
134136
})),
135-
[environments, environmentId, configs, controls, query],
137+
[environments, environmentId, configs, controls, parsedQuery],
136138
);
137139
const result = usePullRequestList(targets);
138140
const connectedTargets = targets.filter((target) =>

‎apps/mobile/src/features/pull-requests/pullRequestList.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ describe("recycled PR list rows", () => {
105105
});
106106
});
107107

108+
describe("mobile PR draft queries", () => {
109+
const rows = [entry(1), entry(2, { isDraft: true })];
110+
111+
it.each([
112+
["draft:false", [1]],
113+
["draft:true", [2]],
114+
["DRAFT:FALSE", [1]],
115+
["", [2, 1]],
116+
])("filters %s on local and host search", (query, expected) => {
117+
for (const localSearchHosts of [new Set<string>(), new Set(["github.com"])]) {
118+
expect(
119+
filterPullRequestList(rows, DEFAULT_PR_LIST_CONTROLS, {}, query, localSearchHosts).map(
120+
(row) => row.number,
121+
),
122+
).toEqual(expected);
123+
}
124+
});
125+
126+
it("lets the typed draft qualifier override the menu and keeps free text", () => {
127+
expect(
128+
filterPullRequestList(
129+
rows,
130+
{ ...DEFAULT_PR_LIST_CONTROLS, filters: { draft: "only" } },
131+
{},
132+
"draft:false Change 1",
133+
new Set(["github.com"]),
134+
).map((row) => row.number),
135+
).toEqual([1]);
136+
});
137+
});
138+
108139
describe("mobile PR list pagination", () => {
109140
it("continues only the repositories that have more, preserving the query and filters", () => {
110141
const first = pullRequestListHead(

‎apps/mobile/src/features/pull-requests/pullRequestList.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parsePullRequestQuery } from "@t3tools/client-runtime/pull-request-query";
12
import type {
23
EnvironmentId,
34
PullRequestListEntry,
@@ -189,8 +190,9 @@ export function filterPullRequestList<
189190
connectedEnvironmentIds?: ReadonlySet<string>,
190191
) {
191192
const normalize = (value: string) => value.toLowerCase();
192-
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
193-
const { filters } = controls;
193+
const parsed = parsePullRequestQuery(query);
194+
const words = parsed.text.toLowerCase().split(/\s+/).filter(Boolean);
195+
const filters = { ...controls.filters, ...parsed.filters };
194196
const filtered = entries.filter((entry) => {
195197
if (controls.state !== "all" && entry.state !== controls.state) return false;
196198
if (controls.projectId && entry.projectId !== controls.projectId) return false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parsePullRequestQuery } from "@t3tools/client-runtime/pull-request-query";
12
import { SourceControlProviderKind } from "@t3tools/contracts";
23
import type {
34
EnvironmentId,
@@ -18,7 +19,6 @@ import {
1819
groupPullRequestsByInvolvement,
1920
matchesPullRequestFilters,
2021
matchesPullRequestQuery,
21-
parsePullRequestQuery,
2222
pullRequestStatsBatches,
2323
pullRequestStatsKeysToRequest,
2424
pullRequestStatsRefreshBatches,

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

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -159,158 +159,6 @@ function isAuthoredByViewer(entry: ScopedEntry, viewers: PullRequestViewers): bo
159159
return viewer !== null && normalize(entry.author?.login) === viewer;
160160
}
161161

162-
/** What `review:` and `status:` take, in GitHub's spelling and in the contract's. */
163-
const REVIEW_VALUES: Record<string, PullRequestListFilters["review"]> = {
164-
approved: "approved",
165-
changes_requested: "changes-requested",
166-
"changes-requested": "changes-requested",
167-
required: "review-required",
168-
"review-required": "review-required",
169-
none: "none",
170-
};
171-
const CHECKS_VALUES: Record<string, PullRequestListFilters["checks"]> = {
172-
success: "passing",
173-
passing: "passing",
174-
failure: "failing",
175-
failing: "failing",
176-
};
177-
178-
/**
179-
* One token of a typed query: a run of non-space characters in which a quoted stretch counts as
180-
* part of the token, so `label:"needs design"` stays whole. An unbalanced quote is dropped rather
181-
* than swallowing the rest of the line.
182-
*/
183-
const QUERY_TOKEN = /(?:[^\s"]|"[^"]*")+/g;
184-
/** The contract's own ceilings on a qualifier: this many names, each this long. */
185-
const MAX_QUALIFIER_VALUES = 10;
186-
const MAX_QUALIFIER_LENGTH = 200;
187-
188-
function qualifierValue(raw: string): string {
189-
return raw.replaceAll('"', "").trim();
190-
}
191-
192-
/** A qualifier's value as the list it may be: split on commas, each name unquoted on its own. */
193-
function splitQualifierList(raw: string): string[] {
194-
// Quoting names one whole label however many commas it holds: `label:"needs,triage"` asks for
195-
// the label written that way, not for either half of it.
196-
if (/^\s*"[^"]*"\s*$/.test(raw)) {
197-
const whole = qualifierValue(raw);
198-
return whole.length === 0 ? [] : [whole];
199-
}
200-
return raw
201-
.split(",")
202-
.map((part) => qualifierValue(part))
203-
.filter((part) => part.length > 0);
204-
}
205-
206-
/**
207-
* A typed query is written by hand, and the contract bounds what a qualifier may carry — so what
208-
* is typed past those bounds is cut here rather than refused by the request that carries it.
209-
*/
210-
function boundedNames(names: ReadonlyArray<string>): string[] {
211-
return names
212-
.slice(0, MAX_QUALIFIER_VALUES)
213-
.map((name) => name.slice(0, MAX_QUALIFIER_LENGTH).trim())
214-
.filter((name) => name.length > 0);
215-
}
216-
217-
/**
218-
* A typed query split into the qualifiers the hosts can act on and the text that is left. Written
219-
* GitHub's way — `label:foo`, `-label:"needs design"`, `author:octocat`, `draft:true`,
220-
* `review:approved`, `status:success` — because a project's labels are its own and a menu cannot
221-
* list them.
222-
*
223-
* A key this does not know is read as a label of that whole name: repositories namespace their
224-
* labels with a colon — `size:XXL`, `area:web`, `vouch:trusted` — and someone typing one means
225-
* the label, not a description that happens to contain it. `-size:XXL` excludes the same way.
226-
*
227-
* Quoting is the way back to plain text: `"size:XXL"` is searched for as written, which is what
228-
* makes a literal search for a colon still possible. A known key whose value it does not take is
229-
* text too, so a search for "status:" itself is still findable.
230-
*/
231-
export function parsePullRequestQuery(raw: string): {
232-
readonly text: string;
233-
readonly filters: PullRequestListFilters;
234-
} {
235-
const text: string[] = [];
236-
const labels: string[][] = [];
237-
const excludedLabels: string[] = [];
238-
let author: string | undefined;
239-
let draft: PullRequestListFilters["draft"];
240-
let review: PullRequestListFilters["review"];
241-
let checks: PullRequestListFilters["checks"];
242-
for (const [token] of raw.matchAll(QUERY_TOKEN)) {
243-
const qualifier = /^(-?)([A-Za-z][A-Za-z0-9_-]*):(.*)$/.exec(token);
244-
const value = qualifier === null ? "" : qualifierValue(qualifier[3] ?? "");
245-
const negated = qualifier?.[1] === "-";
246-
switch (value.length === 0 ? "" : (qualifier?.[2]?.toLowerCase() ?? "")) {
247-
case "label": {
248-
// GitHub's own OR: `label:a,b` is one qualifier satisfied by either name. Negated, the
249-
// comma excludes each — a row carrying any of them goes.
250-
const names = boundedNames(splitQualifierList(qualifier?.[3] ?? ""));
251-
if (names.length === 0) break;
252-
if (negated) excludedLabels.push(...names);
253-
else labels.push(names);
254-
continue;
255-
}
256-
case "author":
257-
if (negated) break;
258-
author = value.slice(0, MAX_QUALIFIER_LENGTH).trim();
259-
continue;
260-
case "draft":
261-
if (negated || (value.toLowerCase() !== "true" && value.toLowerCase() !== "false")) break;
262-
draft = value.toLowerCase() === "true" ? "only" : "hide";
263-
continue;
264-
case "review": {
265-
const decision = negated ? undefined : REVIEW_VALUES[value.toLowerCase()];
266-
if (decision === undefined) break;
267-
review = decision;
268-
continue;
269-
}
270-
case "status":
271-
case "checks": {
272-
const state = negated ? undefined : CHECKS_VALUES[value.toLowerCase()];
273-
if (state === undefined) break;
274-
checks = state;
275-
continue;
276-
}
277-
case "":
278-
break;
279-
default:
280-
// An unknown key, read as the namespaced label it almost always is. A pasted link is
281-
// not one — `https://…` would otherwise become a label named after its own scheme.
282-
if (!value.startsWith("/")) {
283-
// The key names the namespace, so the bare parts of `size:S,XS` are both sizes. A part
284-
// that already carries a colon names its whole label — `size:S,size:XS` is the same
285-
// pair written out, and prefixing it again would ask for `size:size:XS`.
286-
const names = boundedNames(
287-
splitQualifierList(qualifier?.[3] ?? "").map((name) =>
288-
name.includes(":") ? name : `${qualifier?.[2] ?? ""}:${name}`,
289-
),
290-
);
291-
if (names.length === 0) break;
292-
if (negated) excludedLabels.push(...names);
293-
else labels.push(names);
294-
continue;
295-
}
296-
}
297-
text.push(token);
298-
}
299-
return {
300-
text: text.join(" "),
301-
filters: {
302-
...(labels.length === 0 ? {} : { labels: labels.slice(0, MAX_QUALIFIER_VALUES) }),
303-
...(excludedLabels.length === 0
304-
? {}
305-
: { excludedLabels: excludedLabels.slice(0, MAX_QUALIFIER_VALUES) }),
306-
...(author === undefined ? {} : { author }),
307-
...(draft === undefined ? {} : { draft }),
308-
...(review === undefined ? {} : { review }),
309-
...(checks === undefined ? {} : { checks }),
310-
},
311-
};
312-
}
313-
314162
/** Free-text filter over the fields a row actually shows, plus `#123` / `123`. */
315163
export function matchesPullRequestQuery(entry: PullRequestListEntry, query: string): boolean {
316164
const normalizedQuery = query.trim().toLowerCase();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parsePullRequestQuery } from "@t3tools/client-runtime/pull-request-query";
12
import { RefreshIcon } from "~/components/ui/refresh-icon";
23
import { Spinner } from "~/components/ui/spinner";
34
import { useShortcutModifierState } from "~/shortcutModifierState";
@@ -58,7 +59,6 @@ import {
5859
groupPullRequestsByInvolvement,
5960
matchesPullRequestFilters,
6061
matchesPullRequestQuery,
61-
parsePullRequestQuery,
6262
narrowPullRequestsToFilters,
6363
mergePullRequestDiffStats,
6464
partitionPullRequestsWithPriority,

‎packages/client-runtime/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"private": true,
44
"type": "module",
55
"exports": {
6+
"./pull-request-query": {
7+
"types": "./src/pullRequestQuery.ts",
8+
"default": "./src/pullRequestQuery.ts"
9+
},
610
"./composerThreadItems": {
711
"types": "./src/composerThreadItems.ts",
812
"default": "./src/composerThreadItems.ts"

0 commit comments

Comments
 (0)