@@ -551,6 +551,65 @@ function parseRepoUrl(repoUrl) {
551551export async function getOpenPullRequests ( octokit , owner , repo , options ) {
552552 let query =
553553 `is:pr is:open` + ( repo ? ` repo:${ owner + "/" + repo } ` : ` org:${ owner } ` ) ;
554+ const BOT_USERS = process . env . GITHUB_BOT_USERS
555+ ? process . env . GITHUB_BOT_USERS . split ( "," ) ?. map ( ( item ) => item ?. trim ( ) )
556+ : null ;
557+ const GITHUB_ORG_MEMBERS = process . env . GITHUB_ORG_MEMBERS
558+ ? process . env . GITHUB_ORG_MEMBERS . split ( "," ) ?. map ( ( item ) => item ?. trim ( ) )
559+ : null ;
560+ if ( options ?. after && / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( options . after ) ) {
561+ query += " created:>=" + options . after ;
562+ }
563+ if ( options ?. before && / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( options . before ) ) {
564+ query += " created:<=" + options . before ;
565+ }
566+ if ( typeof options ?. merged === "boolean" ) {
567+ if ( ! options . merged ) {
568+ query += " -is:merged" ;
569+ } else {
570+ query += " is:merged" ;
571+ }
572+ }
573+ // Remove results from bots or internal team members
574+ BOT_USERS ?. forEach ( ( botUser ) => ( query += " -author:" + botUser ) ) ;
575+ GITHUB_ORG_MEMBERS ?. forEach (
576+ ( orgMember ) => ( query += " -author:" + orgMember ) ,
577+ ) ;
578+ const response = await octokit . rest . search . issuesAndPullRequests ( {
579+ q : query ,
580+ per_page : 100 ,
581+ page : options ?. page || 1 ,
582+ sort : "created" ,
583+ order : "desc" ,
584+ } ) ;
585+ console . log (
586+ response ?. data ?. total_count + " results found for search: " + query ,
587+ ) ;
588+ const humanPRs = response ?. data ?. items ?. filter (
589+ ( pr ) => pr . user && pr . user . type === "User" ,
590+ ) ;
591+ return humanPRs ;
592+ }
593+
594+ export async function getPullRequests ( octokit , owner , repo , options ) {
595+ let query =
596+ `is:pr` + ( repo ? ` repo:${ owner + "/" + repo } ` : ` org:${ owner } ` ) ;
597+ if ( options ?. status ) {
598+ query += " is:" + options . status ;
599+ }
600+ if ( options ?. after && / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( options . after ) ) {
601+ query += " created:>=" + options . after ;
602+ }
603+ if ( options ?. before && / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / . test ( options . before ) ) {
604+ query += " created:<=" + options . before ;
605+ }
606+ if ( typeof options ?. merged === "boolean" ) {
607+ if ( ! options . merged ) {
608+ query += " -is:merged" ;
609+ } else {
610+ query += " is:merged" ;
611+ }
612+ }
554613 const BOT_USERS = process . env . GITHUB_BOT_USERS
555614 ? process . env . GITHUB_BOT_USERS . split ( "," ) ?. map ( ( item ) => item ?. trim ( ) )
556615 : null ;
@@ -611,6 +670,39 @@ export async function getOpenExternalPullRequests(app, owner, repo, options) {
611670 }
612671}
613672
673+ export async function getExternalPullRequests ( app , owner , repo , options ) {
674+ try {
675+ const octokit = await getOctokitForOrg ( app , owner ) ;
676+ if ( ! octokit ) {
677+ throw new Error (
678+ "Failed to search PR because of undefined octokit intance" ,
679+ ) ;
680+ }
681+ const allPRs = await getPullRequests ( octokit , owner , repo , options ) ;
682+ if ( ! Array . isArray ( allPRs ) ) {
683+ return ;
684+ }
685+ // Send only the external PRs
686+ const externalPRs = [ ] ;
687+ for ( const pr of allPRs ) {
688+ try {
689+ pr . isExternalContribution = await isExternalContribution ( octokit , pr ) ;
690+ if ( pr . isExternalContribution ) {
691+ externalPRs . push ( pr ) ;
692+ }
693+ } catch ( err ) {
694+ // Some error occurred, so we cannot deterministically say whether it is an external contribution or not
695+ pr . isExternalContribution = undefined ;
696+ // We are anyways going to send this in the external PR list
697+ externalPRs . push ( pr ) ;
698+ }
699+ }
700+ return externalPRs ;
701+ } catch ( err ) {
702+ return ;
703+ }
704+ }
705+
614706export function timeAgo ( date ) {
615707 if ( ! date ) return "" ;
616708 if ( typeof date === "string" ) {
@@ -673,7 +765,7 @@ async function isAllowedToWriteToTheRepo(octokit, username, owner, repo) {
673765 // The app is not installed in that repo
674766 // Only "metadata:repository" permission is needed for this api, which all gh apps have wherever they are installed
675767 console . log (
676- "Failed to check if a " +
768+ "Failed to check if " +
677769 username +
678770 " is allowed to write to " +
679771 owner +
0 commit comments