|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { getOctokit } from '@actions/github' |
| 4 | +const token = process.env.GITHUB_TOKEN |
| 5 | +const github = getOctokit(token) |
| 6 | + |
| 7 | +// Mergeable status documentation here: |
| 8 | +// https://docs.github.com/en/graphql/reference/enums#mergestatestatus |
| 9 | +// https://docs.github.com/en/graphql/reference/enums#mergeablestate |
| 10 | + |
| 11 | +/* |
| 12 | + This script gets a list of automerge-enabled PRs and sorts them |
| 13 | + by priority. The PRs with the skip-to-front-of-merge-queue label |
| 14 | + are prioritized first. The rest of the PRs are sorted by the date |
| 15 | + they were updated. This is basically a FIFO queue, while allowing |
| 16 | + writers the ability to skip the line when high-priority ships are |
| 17 | + needed but a freeze isn't necessary. |
| 18 | +*/ |
| 19 | + |
| 20 | +main() |
| 21 | + |
| 22 | +async 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 | + } |
| 27 | + // Get a list of open PRs and order them from oldest to newest |
| 28 | + const query = `query ($first: Int, $after: String, $firstLabels: Int, $repo: String!, $org: String!) { |
| 29 | + organization(login: $org) { |
| 30 | + repository(name: $repo) { |
| 31 | + pullRequests(first: $first, after: $after, states: OPEN, orderBy: {field: UPDATED_AT, direction: ASC}) { |
| 32 | + edges{ |
| 33 | + node { |
| 34 | + number |
| 35 | + url |
| 36 | + updatedAt |
| 37 | + mergeable |
| 38 | + mergeStateStatus |
| 39 | + autoMergeRequest { |
| 40 | + enabledBy { |
| 41 | + login |
| 42 | + } |
| 43 | + enabledAt |
| 44 | + } |
| 45 | + labels (first:$firstLabels){ |
| 46 | + nodes { |
| 47 | + name |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + pageInfo { |
| 53 | + hasNextPage |
| 54 | + endCursor |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }` |
| 60 | + |
| 61 | + const queryVariables = { |
| 62 | + repo, |
| 63 | + org, |
| 64 | + first: 100, |
| 65 | + after: null, // when pagination in null it will get first page |
| 66 | + firstLabels: 100, |
| 67 | + headers: { |
| 68 | + // required for the mergeStateStatus enum |
| 69 | + accept: 'application/vnd.github.merge-info-preview+json', |
| 70 | + }, |
| 71 | + } |
| 72 | + let hasNextPage = true |
| 73 | + const autoMergeEnabledPRs = [] |
| 74 | + |
| 75 | + // we need to get all the paginated results in the case that |
| 76 | + // there are more than 100 PRs |
| 77 | + while (hasNextPage) { |
| 78 | + const graph = await github.graphql(query, queryVariables) |
| 79 | + const dataRoot = graph.organization.repository.pullRequests |
| 80 | + const pullRequests = dataRoot.edges |
| 81 | + // update pagination variables |
| 82 | + hasNextPage = dataRoot.pageInfo.hasNextPage |
| 83 | + // the endCursor is the start cursor for the next page |
| 84 | + queryVariables.after = dataRoot.pageInfo.endCursor |
| 85 | + |
| 86 | + const filteredPrs = pullRequests |
| 87 | + // this simplifies the format received from the graphql query to |
| 88 | + // remove the unnecessary nested objects |
| 89 | + .map((pr) => { |
| 90 | + // make the labels object just an array of the label names |
| 91 | + const labelArray = pr.node.labels.nodes.map((label) => label.name) |
| 92 | + pr.node.labels = labelArray |
| 93 | + // return the pr object and ✂️ the node property |
| 94 | + return pr.node |
| 95 | + }) |
| 96 | + .filter((pr) => pr.autoMergeRequest !== null) |
| 97 | + .filter((pr) => pr.mergeable === 'MERGEABLE') |
| 98 | + // filter out prs that don't have a calculated mergeable state yet |
| 99 | + .filter((pr) => pr.mergeStateStatus !== 'UNKNOWN') |
| 100 | + // filter out prs that still need a review, have merge conflicts, |
| 101 | + // or have failing ci tests |
| 102 | + .filter((pr) => pr.mergeStateStatus !== 'BLOCKED') |
| 103 | + // **NOTE**: In the future we may want to send slack message to initiators |
| 104 | + // of PRs with the following merge states because these can happen after |
| 105 | + // a PR is green and the automerge is enabled |
| 106 | + .filter((pr) => pr.mergeStateStatus !== 'DIRTY') |
| 107 | + .filter((pr) => pr.mergeStateStatus !== 'UNSTABLE') |
| 108 | + |
| 109 | + autoMergeEnabledPRs.push(...filteredPrs) |
| 110 | + } |
| 111 | + |
| 112 | + // Get the list of prs with the skip label so they can |
| 113 | + // be put at the beginning of the list |
| 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 | + ) |
| 119 | + |
| 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 | + } |
| 131 | + |
| 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`) |
| 135 | +} |
0 commit comments