@@ -166,6 +166,65 @@ async function githubPersonLogin(client: NativeClient, involving: string): Promi
166166
167167const GITHUB_LOGIN = / ^ [ A - Z a - z \d ] (?: [ A - Z a - z \d - ] { 0 , 38 } ) $ /
168168
169+ /**
170+ * GitHub's issue search refuses a query that names neither issues nor pull requests, so a query
171+ * without an `is:`/`type:` qualifier runs once for each.
172+ */
173+ function issueTypeQueries ( query : string ) : string [ ] {
174+ return / (?: ^ | \s ) (?: i s | t y p e ) : (?: i s s u e | p r | p u l l - r e q u e s t ) (?: \s | $ ) / i. test ( query )
175+ ? [ query ]
176+ : [ 'issue' , 'pull-request' ] . map ( ( type ) => `${ query } is:${ type } ` )
177+ }
178+
179+ /** The most owners in one activity scope, so the qualifiers stay well inside GitHub's query limit. */
180+ const GITHUB_ACTIVITY_SCOPE_BYTES = 2000
181+
182+ /**
183+ * The accounts that own the member's repositories as `user:`/`org:` qualifiers: the member,
184+ * their organizations, and the owners of repositories they collaborate on. GitHub refuses some
185+ * connections' unscoped searches, and owners cover the member's work without a repository cap.
186+ * `partial` is set when a listing reached its page size or the scope reached its byte bound.
187+ */
188+ async function githubOwnerScope (
189+ client : NativeClient ,
190+ login : string
191+ ) : Promise < { scope : string ; partial : boolean } > {
192+ const [ organizations , collaborations ] = await Promise . all ( [
193+ client . json ( '/user/orgs' , { query : { per_page : '100' } , memo : true } ) . then ( array ) ,
194+ client
195+ . json ( '/user/repos' , {
196+ query : { affiliation : 'collaborator' , per_page : '100' , sort : 'pushed' } ,
197+ memo : true ,
198+ } )
199+ . then ( array ) ,
200+ ] )
201+ const owners = new Map < string , 'user' | 'org' > ( [ [ login . toLowerCase ( ) , 'user' ] ] )
202+ const add = ( name : string , kind : 'user' | 'org' ) => {
203+ if ( GITHUB_LOGIN . test ( name ) && ! owners . has ( name . toLowerCase ( ) ) )
204+ owners . set ( name . toLowerCase ( ) , kind )
205+ }
206+ for ( const row of organizations ) add ( string ( row . login ) , 'org' )
207+ for ( const row of collaborations ) {
208+ const owner = object ( row . owner )
209+ add ( string ( owner . login ) , string ( owner . type ) === 'Organization' ? 'org' : 'user' )
210+ }
211+ const qualifiers : string [ ] = [ ]
212+ let bytes = 0
213+ for ( const [ name , kind ] of owners ) {
214+ const qualifier = `${ kind } :${ name } `
215+ if ( bytes + qualifier . length + 1 > GITHUB_ACTIVITY_SCOPE_BYTES ) break
216+ qualifiers . push ( qualifier )
217+ bytes += qualifier . length + 1
218+ }
219+ return {
220+ scope : qualifiers . join ( ' ' ) ,
221+ partial :
222+ qualifiers . length < owners . size ||
223+ organizations . length === 100 ||
224+ collaborations . length === 100 ,
225+ }
226+ }
227+
169228/** Whether a query already names a qualifier; GitHub ORs a repeated one rather than ANDing it. */
170229const hasGitHubQualifier = ( query : string , name : string ) =>
171230 githubTokens ( query ) . some ( ( token ) => token . toLowerCase ( ) . startsWith ( `${ name } :` ) )
@@ -355,15 +414,13 @@ export async function searchGitHub(
355414 : 'Searched GitHub issues, pull requests, and code.'
356415 )
357416 }
358- if (
359- input . native . kind === 'issues' &&
360- ! / (?: ^ | \s ) (?: i s | t y p e ) : (?: i s s u e | p r | p u l l - r e q u e s t ) (?: \s | $ ) / i. test ( query )
361- )
417+ const typed = input . native . kind === 'issues' ? issueTypeQueries ( query ) : [ query ]
418+ if ( typed . length > 1 )
362419 return collectNativePages (
363- [ 'issue' , 'pull-request' ] . map ( ( kind ) =>
420+ typed . map ( ( typedQuery ) =>
364421 searchGitHub ( client , {
365422 ...input ,
366- native : { ...input . native ! , query : ` ${ query } is: ${ kind } ` } ,
423+ native : { ...input . native ! , query : typedQuery } ,
367424 } )
368425 ) ,
369426 'Searched issues and pull requests separately.'
@@ -518,15 +575,42 @@ export async function githubActivity(
518575 input : NativeActivityInput
519576) : Promise < NativeActivityPage > {
520577 const login = await githubPersonLogin ( client , 'me' )
578+ const { scope, partial : scopeCapped } = await githubOwnerScope ( client , login )
579+ /** Issues and pull requests are separate streams, as GitHub's issue search requires. */
580+ const issueStreams = (
581+ person : string ,
582+ field : 'created' | 'updated' ,
583+ action : 'created' | 'involved'
584+ ) =>
585+ issueTypeQueries ( `${ person } :${ login } ${ scope } ` ) . map ( ( query ) =>
586+ githubActivityStream ( client , input , 'issues' , query , field , action )
587+ )
521588 const streams = await Promise . all ( [
522- githubActivityStream ( client , input , 'issues' , `author:${ login } ` , 'created' , 'created' ) ,
523- githubActivityStream ( client , input , 'commits' , `author:${ login } ` , 'author-date' , 'committed' ) ,
524- githubActivityStream ( client , input , 'issues' , `involves:${ login } ` , 'updated' , 'involved' ) ,
589+ ...issueStreams ( 'author' , 'created' , 'created' ) ,
590+ githubActivityStream (
591+ client ,
592+ input ,
593+ 'commits' ,
594+ `author:${ login } ${ scope } ` ,
595+ 'author-date' ,
596+ 'committed'
597+ ) ,
598+ ...issueStreams ( 'involves' , 'updated' , 'involved' ) ,
525599 ] )
526600 return {
527601 streams : streams . map ( ( { incomplete : _ , ...stream } ) => stream ) ,
528- ...( streams . some ( ( { incomplete } ) => incomplete )
529- ? { partial : true , message : 'GitHub could not complete part of this search in time.' }
602+ ...( streams . some ( ( { incomplete } ) => incomplete ) || scopeCapped
603+ ? {
604+ partial : true ,
605+ message : joinMessages ( [
606+ streams . some ( ( { incomplete } ) => incomplete )
607+ ? 'GitHub could not complete part of this search in time.'
608+ : undefined ,
609+ scopeCapped
610+ ? 'Only some of the organizations and repository owners you work with were searched.'
611+ : undefined ,
612+ ] ) ,
613+ }
530614 : { } ) ,
531615 }
532616}
0 commit comments