1- import { documented , formDispatcher , isJsonObject } from "@executablemd/core" ;
2- import { bindResult } from "@executablemd/core/api" ;
1+ import { documented , formDispatcher } from "@executablemd/core" ;
32import type { ComponentRegistration , Json , JsonObject } from "@executablemd/core" ;
43import { createApi } from "@effectionx/context-api" ;
54import type { Operation } from "effection" ;
@@ -13,6 +12,7 @@ export const SEARCH_CEILING = 1000;
1312
1413export type SearchRequest = JsonObject & {
1514 query : string ;
15+ kind : "issue" | "pr" ;
1616 sort ?: string ;
1717 order ?: string ;
1818 page : number ;
@@ -46,33 +46,64 @@ const sorts = [
4646 "updated" ,
4747] ;
4848
49- function admitQuery ( query : string ) : void {
50- // Keep quoted search terms intact while inspecting qualifier names and values.
51- const tokens = query . match ( / (?: [ ^ \s " ( ) ] | " (?: \\ .| [ ^ " \\ ] ) * " ) + / gu) ?? [ ] ;
52- const kinds = tokens . filter ( ( token ) => / ^ [ + - ] ? (?: i s | t y p e ) : " ? (?: i s s u e | p r ) " ? $ / i. test ( token ) ) ;
53- if (
54- tokens . some ( ( token ) => / ^ [ + - ] ? (?: r e p o | o r g | u s e r ) : / i. test ( token ) ) ||
55- query . replace ( / " (?: \\ .| [ ^ " \\ ] ) * " / gu, "" ) . includes ( '"' )
56- ) {
49+ function admitQuery ( query : string ) : SearchRequest [ "kind" ] {
50+ const quoted = / " (?: \\ .| [ ^ " \\ ] ) * " / gu;
51+ const unquoted = query . replace ( quoted , " " ) ;
52+ if ( / (?: ^ | [ \s ( ) ] ) [ + - ] * (?: r e p o | o r g | u s e r | o w n e r ) : / iu. test ( unquoted ) ) {
53+ throw new GitOperationAdmissionError (
54+ SEARCH_ELEMENT ,
55+ "omit repository, organization, user and owner scope qualifiers." ,
56+ ) ;
57+ }
58+ if ( / [ " ( ) ] / u. test ( unquoted ) ) {
5759 throw new GitOperationAdmissionError (
5860 SEARCH_ELEMENT ,
59- "use a GitHub query without repository, organization or user scope qualifiers and with balanced quotes ." ,
61+ "balance quotes and place parentheses inside quoted text ." ,
6062 ) ;
6163 }
62- if ( kinds . length !== 1 || ! / ^ i s : / i. test ( kinds [ 0 ] ?? "" ) ) {
64+ const matches = [ ...query . matchAll ( / (?: [ + - ] * [ \w - ] + : ) ? " (?: \\ .| [ ^ " \\ ] ) * " | [ ^ \s " ] + / gu) ] ;
65+ const kinds = matches . filter ( ( [ token ] ) =>
66+ / ^ [ + - ] * (?: i s | t y p e ) : (?: " (?: i s s u e | p r | p u l l - r e q u e s t ) " | (?: i s s u e | p r | p u l l - r e q u e s t ) ) $ / i. test ( token ) ,
67+ ) ;
68+ const kind = kinds [ 0 ] ;
69+ const token = kind ?. [ 0 ] ?? "" ;
70+ const selected =
71+ kind !== undefined &&
72+ ( kind . index === 0 || / \s / u. test ( query [ kind . index - 1 ] ?? "" ) ) &&
73+ ( kind . index + token . length === query . length ||
74+ / \s / u. test ( query [ kind . index + token . length ] ?? "" ) )
75+ ? token . toLowerCase ( )
76+ : undefined ;
77+ if ( kinds . length !== 1 || ( selected !== "is:issue" && selected !== "is:pr" ) ) {
6378 throw new GitOperationAdmissionError (
6479 SEARCH_ELEMENT ,
6580 "select exactly one kind with is:issue or is:pr." ,
6681 ) ;
6782 }
83+ const operators = matches . map ( ( [ token ] ) => token ) ;
84+ if (
85+ operators . some (
86+ ( token , index ) =>
87+ / ^ (?: A N D | O R | N O T ) $ / u. test ( token ) &&
88+ [ operators [ index - 1 ] , operators [ index + 1 ] ] . some ( ( operand ) =>
89+ / ^ [ + - ] * [ \w - ] + : / u. test ( operand ?? "" ) ,
90+ ) ,
91+ )
92+ ) {
93+ throw new GitOperationAdmissionError (
94+ SEARCH_ELEMENT ,
95+ "use Boolean operators with search terms instead of qualifiers." ,
96+ ) ;
97+ }
98+ return selected === "is:issue" ? "issue" : "pr" ;
6899}
69100
70101function * search ( props : Record < string , Json > ) : Operation < JsonObject > {
71- const { query, sort, order, page = 1 , perPage = 20 , nextAs } = props ;
102+ const { query, sort, order, page = 1 , perPage = 20 } = props ;
72103 if ( typeof query !== "string" || typeof page !== "number" || typeof perPage !== "number" ) {
73104 throw new GitOperationAdmissionError ( SEARCH_ELEMENT , "the search arguments are invalid." ) ;
74105 }
75- admitQuery ( query ) ;
106+ const kind = admitQuery ( query ) ;
76107 if ( ( page - 1 ) * perPage >= SEARCH_CEILING ) {
77108 throw new GitOperationAdmissionError (
78109 SEARCH_ELEMENT ,
@@ -82,27 +113,19 @@ function* search(props: Record<string, Json>): Operation<JsonObject> {
82113 if ( order !== undefined && sort === undefined ) {
83114 throw new GitOperationAdmissionError ( SEARCH_ELEMENT , "order requires sort." ) ;
84115 }
85- const publishNext = typeof nextAs === "string" ? yield * bindResult ( nextAs ) : undefined ;
86116 const repository = yield * selectedRepository ( ) ;
87117 if ( repository === undefined ) {
88118 throw new GitOperationAdmissionError ( SEARCH_ELEMENT , "select a Repository before searching." ) ;
89119 }
90- const result = yield * GitHubSearch . operations . search ( repository , {
120+ return yield * GitHubSearch . operations . search ( repository , {
91121 query,
122+ kind,
92123 page,
93124 perPage,
94125 ...( typeof sort === "string"
95126 ? { sort, order : typeof order === "string" ? order : "desc" }
96127 : { } ) ,
97128 } ) ;
98- if ( ! isJsonObject ( result . value ) || ( result . next !== null && ! isJsonObject ( result . next ) ) ) {
99- throw new GitOperationAdmissionError (
100- SEARCH_ELEMENT ,
101- "the search provider returned an invalid page." ,
102- ) ;
103- }
104- publishNext ?.( result . next ) ;
105- return result . value ;
106129}
107130
108131export const githubSearchRegistration : ComponentRegistration = {
@@ -114,11 +137,6 @@ export const githubSearchRegistration: ComponentRegistration = {
114137 query : { type : "string" , minLength : 1 } ,
115138 sort : { enum : sorts } ,
116139 order : { enum : [ "asc" , "desc" ] } ,
117- nextAs : {
118- type : "string" ,
119- minLength : 1 ,
120- description : "Bind the next request arguments or null to this name." ,
121- } ,
122140 page : { type : "integer" , minimum : 1 , maximum : SEARCH_CEILING } ,
123141 perPage : { type : "integer" , minimum : 1 , maximum : 100 } ,
124142 } ,
0 commit comments