@@ -3,16 +3,21 @@ import type { JsonObject } from "@executablemd/core";
33import type { Operation } from "effection" ;
44import {
55 GitHubSearch ,
6+ SEARCH_CEILING ,
67 SEARCH_ELEMENT ,
78 type SearchRequest ,
9+ type SearchResult ,
810} from "../../composition/components/GitHubSearch.ts" ;
911import { PullRequestReadError } from "../../composition/errors.ts" ;
1012import { PullRequestReadExecution } from "../../composition/pull-request-read-execution.ts" ;
1113import type { RepositorySelection } from "../../composition/selection.ts" ;
1214import {
1315 authorizedHeaders ,
1416 GITHUB_API ,
17+ type GitHubAccess ,
18+ type GitHubHttpResponse ,
1519 type GitHubSource ,
20+ nextPage ,
1621 parseGitHubRepository ,
1722 readJson ,
1823} from "./github.ts" ;
@@ -40,7 +45,7 @@ export function useGitHubSearch(
4045) : Operation < void > {
4146 const resolved = gitHubPullRequestReadResolver ( options ) ;
4247 return GitHubSearch . around ( {
43- * search ( [ selection , request ] ) : Operation < JsonObject > {
48+ * search ( [ selection , request ] ) : Operation < SearchResult > {
4449 const origin = yield * locator ( selection ) ;
4550 const repository = origin === undefined ? undefined : parseGitHubRepository ( origin ) ;
4651 if ( repository === undefined ) {
@@ -52,30 +57,25 @@ export function useGitHubSearch(
5257 if ( ! configuration ?. allowed || ! pullRequestAllowed ( configuration . allowed , url ) ) {
5358 throw unavailable ( "this host has not authorized search in the selected Repository." ) ;
5459 }
55- const validate = ( value : unknown ) =>
56- validateSearchPage (
57- value ,
58- request ,
59- name ,
60- options . access ?. endpoint ?? configuration . endpoint ?? GITHUB_API ,
61- ) ;
60+ const apiBase = options . access ?. endpoint ?? configuration . endpoint ?? GITHUB_API ;
61+ const validate = ( value : unknown , page : number ) =>
62+ validateSearchPage ( value , request , page , name , apiBase ) ;
6263 return yield * PullRequestReadExecution . operations . search (
6364 url ,
6465 request ,
65- ( ) => readSearchPage ( ( ) => resolved . transport ( configuration ) , name , request , validate ) ,
66- validate ,
66+ ( ) => readSearch ( ( ) => resolved . transport ( configuration ) , name , request , validate ) ,
67+ ( value ) => validateSearchResult ( value , request , validate ) ,
6768 ) ;
6869 } ,
6970 } ) ;
7071}
7172
72- function * readSearchPage (
73+ function * readSearch (
7374 source : ( ) => GitHubSource ,
7475 repository : string ,
7576 request : SearchRequest ,
76- validate : ( value : unknown ) => JsonObject ,
77- ) : Operation < JsonObject > {
78- const endpoint = "/search/issues" ;
77+ validate : ( value : unknown , page : number ) => JsonObject ,
78+ ) : Operation < SearchResult > {
7979 const parameters = new URLSearchParams ( {
8080 q : `repo:${ repository } ${ request . query } ` ,
8181 page : String ( request . page ) ,
@@ -86,32 +86,99 @@ function* readSearchPage(
8686 parameters . set ( "order" , request . order ) ;
8787 }
8888
89- let response ;
89+ let access : GitHubAccess ;
90+ let headers : Record < string , string > | undefined ;
9091 try {
91- const access = yield * source ( ) . open ( ) ;
92- const headers = yield * authorizedHeaders ( access , false ) ;
93- if ( headers !== undefined ) {
92+ access = yield * source ( ) . open ( ) ;
93+ headers = yield * authorizedHeaders ( access , false ) ;
94+ } catch {
95+ throw unavailable ( "GitHub search could not authenticate or complete the request." ) ;
96+ }
97+ if ( headers === undefined ) {
98+ throw unavailable ( "GitHub search requires a credential." ) ;
99+ }
100+
101+ const first = new URL ( `${ access . endpoint } /search/issues?${ parameters } ` ) ;
102+ let url = first . href ;
103+ const pages : JsonObject [ ] = [ ] ;
104+ const last = lastAccessiblePage ( request ) ;
105+ for ( let page = request . page ; page <= last ; page += 1 ) {
106+ let response : GitHubHttpResponse ;
107+ try {
94108 response = yield * access . send ( {
95109 method : "GET" ,
96- url : ` ${ access . endpoint } ${ endpoint } ? ${ parameters } ` ,
110+ url,
97111 headers,
112+ ...( request . paginate ? { redirect : "error" } : { } ) ,
98113 } ) ;
114+ } catch {
115+ throw unavailable ( "GitHub search could not complete the request." ) ;
99116 }
100- } catch {
101- throw unavailable ( "GitHub search could not authenticate or complete the request." ) ;
117+ if ( response . status !== 200 ) {
118+ throw unavailable ( "GitHub did not return a search page." ) ;
119+ }
120+ const value = validate ( readJson ( response . body ) , page ) ;
121+ if ( ! request . paginate ) {
122+ return value ;
123+ }
124+ pages . push ( value ) ;
125+ if ( page === last ) {
126+ break ;
127+ }
128+ const walk = nextPage ( response . link , access . endpoint ) ;
129+ if ( walk . kind === "complete" ) {
130+ break ;
131+ }
132+ if ( walk . kind === "unfollowable" || ! sameSearch ( walk . url , first , page + 1 ) ) {
133+ throw invalid ( ) ;
134+ }
135+ url = walk . url ;
102136 }
103- if ( response === undefined ) {
104- throw unavailable ( "GitHub search requires a credential." ) ;
137+ return pages ;
138+ }
139+
140+ function lastAccessiblePage ( request : SearchRequest ) : number {
141+ return Math . ceil ( SEARCH_CEILING / request . perPage ) ;
142+ }
143+
144+ function sameSearch ( link : string , first : URL , page : number ) : boolean {
145+ const next = new URL ( link ) ;
146+ const expected = new URLSearchParams ( first . searchParams ) ;
147+ expected . set ( "page" , String ( page ) ) ;
148+ expected . sort ( ) ;
149+ next . searchParams . sort ( ) ;
150+ return (
151+ next . origin === first . origin &&
152+ next . pathname === first . pathname &&
153+ next . username === "" &&
154+ next . password === "" &&
155+ next . hash === "" &&
156+ next . searchParams . toString ( ) === expected . toString ( )
157+ ) ;
158+ }
159+
160+ function validateSearchResult (
161+ value : unknown ,
162+ request : SearchRequest ,
163+ validate : ( value : unknown , page : number ) => JsonObject ,
164+ ) : SearchResult {
165+ if ( ! request . paginate ) {
166+ return validate ( value , request . page ) ;
105167 }
106- if ( response . status !== 200 ) {
107- throw unavailable ( "GitHub did not return a search page." ) ;
168+ if (
169+ ! Array . isArray ( value ) ||
170+ value . length === 0 ||
171+ value . length > lastAccessiblePage ( request ) - request . page + 1
172+ ) {
173+ throw invalid ( ) ;
108174 }
109- return validate ( readJson ( response . body ) ) ;
175+ return value . map ( ( page , index ) => validate ( page , request . page + index ) ) ;
110176}
111177
112178function validateSearchPage (
113179 received : unknown ,
114180 request : SearchRequest ,
181+ page : number ,
115182 repository : string ,
116183 apiBase : string ,
117184) : JsonObject {
@@ -125,7 +192,7 @@ function validateSearchPage(
125192 throw invalid ( ) ;
126193 }
127194 const { total_count : total , incomplete_results : incomplete , items } = value ;
128- const start = ( request . page - 1 ) * request . perPage ;
195+ const start = ( page - 1 ) * request . perPage ;
129196 const expectedRepository = `${ apiBase } /repos/${ repository } ` . toLowerCase ( ) ;
130197 const publicRepository = `${ GITHUB_API } /repos/${ repository } ` . toLowerCase ( ) ;
131198 if (
0 commit comments