Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestQuery.ts

137 lines4.5 KB

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