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
32function boundedNames(names: ReadonlyArray<string>): string[] {
33 return names
34 .slice(0, MAX_QUALIFIER_VALUES)
35 .map((name) => name.slice(0, MAX_QUALIFIER_LENGTH).trim())
36 .filter((name) => name.length > 0);
37}
38
39export function parsePullRequestQuery(raw: string): {
40 readonly text: string;
41 readonly filters: PullRequestListFilters;
42} {
43 const text: string[] = [];
44 const labels: string[][] = [];
45 const excludedLabels: string[] = [];
46 let author: string | undefined;
47 let assignee: string | undefined;
48 let draft: PullRequestListFilters["draft"];
49 let review: PullRequestListFilters["review"];
50 let checks: PullRequestListFilters["checks"];
51 for (const [token] of raw.matchAll(QUERY_TOKEN)) {
52 const qualifier = /^(-?)([A-Za-z][A-Za-z0-9_-]*):(.*)$/.exec(token);
53 const value = qualifier === null ? "" : qualifierValue(qualifier[3] ?? "");
54 const negated = qualifier?.[1] === "-";
55 switch (value.length === 0 ? "" : (qualifier?.[2]?.toLowerCase() ?? "")) {
56 case "label": {
57 const names = boundedNames(splitQualifierList(qualifier?.[3] ?? ""));
58 if (names.length === 0) break;
59 if (negated) excludedLabels.push(...names);
60 else labels.push(names);
61 continue;
62 }
63 case "author":
64 if (negated) break;
65 author = value.slice(0, MAX_QUALIFIER_LENGTH).trim();
66 continue;
67 case "assignee":
68 if (negated) break;
69 assignee = value.slice(0, MAX_QUALIFIER_LENGTH).trim();
70 continue;
71 case "draft":
72 if (negated || (value.toLowerCase() !== "true" && value.toLowerCase() !== "false")) break;
73 draft = value.toLowerCase() === "true" ? "only" : "hide";
74 continue;
75 case "review": {
76 const decision = negated ? undefined : REVIEW_VALUES[value.toLowerCase()];
77 if (decision === undefined) break;
78 review = decision;
79 continue;
80 }
81 case "status":
82 case "checks": {
83 const state = negated ? undefined : CHECKS_VALUES[value.toLowerCase()];
84 if (state === undefined) break;
85 checks = state;
86 continue;
87 }
88 case "":
89 break;
90 default:
91 if (!value.startsWith("/")) {
92 const names = boundedNames(
93 splitQualifierList(qualifier?.[3] ?? "").map((name) =>
94 name.includes(":") ? name : `${qualifier?.[2] ?? ""}:${name}`,
95 ),
96 );
97 if (names.length === 0) break;
98 if (negated) excludedLabels.push(...names);
99 else labels.push(names);
100 continue;
101 }
102 }
103 text.push(token);
104 }
105 return {
106 text: text.join(" "),
107 filters: {
108 ...(labels.length === 0 ? {} : { labels: labels.slice(0, MAX_QUALIFIER_VALUES) }),
109 ...(excludedLabels.length === 0
110 ? {}
111 : { excludedLabels: excludedLabels.slice(0, MAX_QUALIFIER_VALUES) }),
112 ...(author === undefined ? {} : { author }),
113 ...(assignee === undefined ? {} : { assignee }),
114 ...(draft === undefined ? {} : { draft }),
115 ...(review === undefined ? {} : { review }),
116 ...(checks === undefined ? {} : { checks }),
117 },
118 };
119}
120
121/**
122 * The Draft menu shows a typed `draft:` qualifier, so picking another value has to take the
123 * qualifier out of the query: left in, it would keep outranking whatever the menu stores.
124 */
125export function applyPullRequestDraftFilterChange(
126 query: string,
127 menuFilters: PullRequestListFilters,
128 next: PullRequestListFilters,
129) {
130 const typedDraft = parsePullRequestQuery(query).filters.draft;
131 if (next.draft === (typedDraft ?? menuFilters.draft))
132 return { query, filters: { ...next, draft: menuFilters.draft } };
133 return {
134 query:
135 typedDraft === undefined
136 ? query
137 : [...query.matchAll(QUERY_TOKEN)]
138 .map(([token]) => token)
139 .filter((token) => parsePullRequestQuery(token).filters.draft === undefined)
140 .join(" "),
141 filters: next,
142 };
143}