The best way to control Claude Code, Codex and more from any computer or phone

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