@@ -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 - Z a - z ] [ A - Z a - z 0 - 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`. */
315163export function matchesPullRequestQuery ( entry : PullRequestListEntry , query : string ) : boolean {
316164 const normalizedQuery = query . trim ( ) . toLowerCase ( ) ;
0 commit comments