@@ -20,10 +20,14 @@ const github = getOctokit(token)
2020main ( )
2121
2222async function main ( ) {
23+ const [ org , repo ] = process . env . GITHUB_REPOSITORY . split ( '/' )
24+ if ( ! org || ! repo ) {
25+ throw new Error ( 'GITHUB_REPOSITORY environment variable not set' )
26+ }
2327 // Get a list of open PRs and order them from oldest to newest
24- const query = `query ($first: Int, $after: String, $firstLabels: Int) {
25- organization(login: "github" ) {
26- repository(name: "docs-internal" ) {
28+ const query = `query ($first: Int, $after: String, $firstLabels: Int, $repo: String!, $org: String! ) {
29+ organization(login: $org ) {
30+ repository(name: $repo ) {
2731 pullRequests(first: $first, after: $after, states: OPEN, orderBy: {field: UPDATED_AT, direction: ASC}) {
2832 edges{
2933 node {
@@ -55,6 +59,8 @@ async function main() {
5559 }`
5660
5761 const queryVariables = {
62+ repo,
63+ org,
5864 first : 100 ,
5965 after : null , // when pagination in null it will get first page
6066 firstLabels : 100 ,
@@ -105,20 +111,25 @@ async function main() {
105111
106112 // Get the list of prs with the skip label so they can
107113 // be put at the beginning of the list
108- const prioritizedPrList = autoMergeEnabledPRs
109- . filter ( ( pr ) => pr . labels . includes ( 'skip-to-front-of-merge-queue' ) )
110- . concat ( autoMergeEnabledPRs . filter ( ( pr ) => ! pr . labels . includes ( 'skip-to-front-of-merge-queue' ) ) )
114+ const prioritizedPrList = autoMergeEnabledPRs . sort (
115+ ( a , b ) =>
116+ Number ( b . labels . includes ( 'skip-to-front-of-merge-queue' ) ) -
117+ Number ( a . labels . includes ( 'skip-to-front-of-merge-queue' ) )
118+ )
111119
112- const nextInQueue = prioritizedPrList . shift ( )
113- // Update the branch for the next PR in the merge queue
114- github . rest . pulls . updateBranch ( {
115- owner : 'github' ,
116- repo : 'docs-internal' ,
117- pull_number : parseInt ( nextInQueue . number ) ,
118- } )
120+ if ( prioritizedPrList . length ) {
121+ const nextInQueue = prioritizedPrList . shift ( )
122+ // Update the branch for the next PR in the merge queue
123+ github . rest . pulls . updateBranch ( {
124+ owner : org ,
125+ repo,
126+ pull_number : nextInQueue . number ,
127+ } )
128+ console . log ( `⏱ Total PRs in the merge queue: ${ prioritizedPrList . length + 1 } ` )
129+ console . log ( `🚂 Updated branch for PR #${ JSON . stringify ( nextInQueue , null , 2 ) } ` )
130+ }
119131
120- console . log ( `⏱ Total PRs in the merge queue: ${ prioritizedPrList . length + 1 } ` )
121- console . log ( `🚂 Updated branch for PR #${ JSON . stringify ( nextInQueue , null , 2 ) } ` )
122- console . log ( `🚏 Next up in the queue: ` )
123- console . log ( JSON . stringify ( prioritizedPrList , null , 2 ) )
132+ prioritizedPrList . length
133+ ? console . log ( `🚏 Next up in the queue: \n ${ JSON . stringify ( prioritizedPrList , null , 2 ) } ` )
134+ : console . log ( `⚡ The merge queue is empty` )
124135}
0 commit comments