Open History to see the last commit
1import type { PullRequestListFilters } from "@t3tools/contracts";
2
3const REVIEW_VALUES: Record<string, PullRequestListFilters["review"]> = {
4 approved: "approved",
5 changes_requested: "changes-requested",
6 "changes-requested": "changes-requested",
7 required: "review-required",
8 "review-required": "review-required",
9 none: "none",
10};
11const CHECKS_VALUES: Record<string, PullRequestListFilters["checks"]> = {
12 success: "passing",
13 passing: "passing",
14 failure: "failing",
15 failing: "failing",
16};
17
18const QUERY_TOKEN = /(?:[^\s"]|"[^"]*")+/g;
19const MAX_QUALIFIER_VALUES = 10;
20const MAX_QUALIFIER_LENGTH = 200;
21
22function qualifierValue(raw: string): string {
23 return raw.replaceAll('"', "").trim();
24}
25
26function splitQualifierList(raw: string): string[] {
27 return (raw.match(/(?:[^,"]|"[^"]*")+/g) ?? [])
28 .map((part) => qualifierValue(part))
29 .filter((part) => part.length > 0);
30}
31
32/** `me` and `@me` both name the viewer; logins are case-insensitive. */
33function authorKey(login: string): string {
34 return login.toLowerCase().replace(/^@me$/, "me");
35}
36
37function boundedNames(names: ReadonlyArray<string>): string[] {
38 return names
39 .slice(0, MAX_QUALIFIER_VALUES)
40 .map((name) => name.slice(0, MAX_QUALIFIER_LENGTH).trim())
41 .filter((name) => name.length > 0);
42}
43
44export function parsePullRequestQuery(raw: string): {
45 readonly text: string;
46 readonly filters: PullRequestListFilters;
47} {
48 const text: string[] = [];
49 const labels: string[][] = [];
50 const excludedLabels: string[] = [];
51 // `null` once two different logins are named: GitHub ORs them, the contract carries one.
52 let author: string | null | undefined;
53 let draft: PullRequestListFilters["draft"];
54 let review: PullRequestListFilters["review"];
55 let checks: PullRequestListFilters["checks"];
56 for (const [token] of raw.matchAll(QUERY_TOKEN)) {
57 const qualifier = /^(-?)([A-Za-z][A-Za-z0-9_-]*):(.*)$/.exec(token);
58 const value = qualifier === null ? "" : qualifierValue(qualifier[3] ?? "");
59 const negated = qualifier?.[1] === "-";
60 switch (value.length === 0 ? "" : (qualifier?.[2]?.toLowerCase() ?? "")) {
61 case "label": {
62 const names = boundedNames(splitQualifierList(qualifier?.[3] ?? ""));
63 if (names.length === 0) break;
64 if (negated) excludedLabels.push(...names);
65 else labels.push(names);
66 continue;
67 }
68 case "author": {
69 if (negated) break;
70 const login = value.slice(0, MAX_QUALIFIER_LENGTH).trim();
71 if (author === undefined) author = login;
72 else if (author === null || authorKey(author) !== authorKey(login)) author = null;
73 continue;
74 }
75 case "draft":
76 if (value.toLowerCase() !== "true" && value.toLowerCase() !== "false") break;
77 draft = (value.toLowerCase() === "true") !== negated ? "only" : "hide";
78 continue;
79 case "review": {
80 const key = value.toLowerCase();
81 const decision =
82 !negated && Object.hasOwn(REVIEW_VALUES, key) ? REVIEW_VALUES[key] : undefined;
83 if (decision === undefined) break;
84 review = decision;
85 continue;
86 }
87 case "status":
88 case "checks": {
89 const key = value.toLowerCase();
90 const state =
91 !negated && Object.hasOwn(CHECKS_VALUES, key) ? CHECKS_VALUES[key] : undefined;
92 if (state === undefined) break;
93 checks = state;
94 continue;
95 }
96 // GitHub's own search qualifiers, not labels: the list sets scope and order itself.
97 case "is":
98 case "archived":
99 case "state":
100 case "repo":
101 case "org":
102 case "user":
103 case "sort":
104 continue;
105 case "":
106 break;
107 default:
108 if (!value.startsWith("/")) {
109 const names = boundedNames(
110 splitQualifierList(qualifier?.[3] ?? "").map((name) =>
111 name.includes(":") ? name : `${qualifier?.[2] ?? ""}:${name}`,
112 ),
113 );
114 if (names.length === 0) break;
115 if (negated) excludedLabels.push(...names);
116 else labels.push(names);
117 continue;
118 }
119 }
120 text.push(token);
121 }
122 return {
123 text: text.join(" "),
124 filters: {
125 ...(labels.length === 0 ? {} : { labels: labels.slice(0, MAX_QUALIFIER_VALUES) }),
126 ...(excludedLabels.length === 0
127 ? {}
128 : { excludedLabels: excludedLabels.slice(0, MAX_QUALIFIER_VALUES) }),
129 ...(typeof author === "string" ? { author } : {}),
130 ...(draft === undefined ? {} : { draft }),
131 ...(review === undefined ? {} : { review }),
132 ...(checks === undefined ? {} : { checks }),
133 },
134 };
135}
136
137/**
138 * The Draft menu shows a typed `draft:` qualifier, so picking another value has to take the
139 * qualifier out of the query: left in, it would keep outranking whatever the menu stores.
140 */
141export function applyPullRequestDraftFilterChange(
142 query: string,
143 menuFilters: PullRequestListFilters,
144 next: PullRequestListFilters,
145) {
146 const typedDraft = parsePullRequestQuery(query).filters.draft;
147 if (next.draft === (typedDraft ?? menuFilters.draft))
148 return { query, filters: { ...next, draft: menuFilters.draft } };
149 return {
150 query:
151 typedDraft === undefined
152 ? query
153 : [...query.matchAll(QUERY_TOKEN)]
154 .map(([token]) => token)
155 .filter((token) => parsePullRequestQuery(token).filters.draft === undefined)
156 .join(" "),
157 filters: next,
158 };
159}