From 580c38b56aea160dc003be9d834590ed5971b4a7 Mon Sep 17 00:00:00 2001 From: Rachael Sewell Date: Fri, 19 Nov 2021 09:54:45 -0800 Subject: [PATCH] use repo variable and checkout repo in workflow (#22994) --- .github/actions-scripts/enable-automerge.js | 17 +++++-- .../update-merge-queue-branch.js | 45 ++++++++++++------- .github/workflows/autoupdate-branch.yml | 9 ++++ 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/.github/actions-scripts/enable-automerge.js b/.github/actions-scripts/enable-automerge.js index 3a7824835d..649a0e5052 100644 --- a/.github/actions-scripts/enable-automerge.js +++ b/.github/actions-scripts/enable-automerge.js @@ -1,11 +1,20 @@ import { getOctokit } from '@actions/github' -const token = process.env.GITHUB_TOKEN -const prNumber = process.env.AUTOMERGE_PR_NUMBER -const [org, repo] = process.env.GITHUB_REPOSITORY.split('/') -const github = getOctokit(token) main() async function main() { + const [org, repo] = process.env.GITHUB_REPOSITORY.split('/') + if (!org || !repo) { + throw new Error('GITHUB_REPOSITORY environment variable not set') + } + const prNumber = process.env.AUTOMERGE_PR_NUMBER + if (!prNumber) { + throw new Error(`AUTOMERGE_PR_NUMBER environment variable not set`) + } + const token = process.env.GITHUB_TOKEN + if (!token) { + throw new Error(`GITHUB_TOKEN environment variable not set`) + } + const github = getOctokit(token) const pull = await github.rest.pulls.get({ owner: org, repo: repo, diff --git a/.github/actions-scripts/update-merge-queue-branch.js b/.github/actions-scripts/update-merge-queue-branch.js index 3de0ba555f..8c8c061532 100644 --- a/.github/actions-scripts/update-merge-queue-branch.js +++ b/.github/actions-scripts/update-merge-queue-branch.js @@ -20,10 +20,14 @@ const github = getOctokit(token) main() async function main() { + const [org, repo] = process.env.GITHUB_REPOSITORY.split('/') + if (!org || !repo) { + throw new Error('GITHUB_REPOSITORY environment variable not set') + } // Get a list of open PRs and order them from oldest to newest - const query = `query ($first: Int, $after: String, $firstLabels: Int) { - organization(login: "github") { - repository(name: "docs-internal") { + const query = `query ($first: Int, $after: String, $firstLabels: Int, $repo: String!, $org: String!) { + organization(login: $org) { + repository(name: $repo) { pullRequests(first: $first, after: $after, states: OPEN, orderBy: {field: UPDATED_AT, direction: ASC}) { edges{ node { @@ -55,6 +59,8 @@ async function main() { }` const queryVariables = { + repo, + org, first: 100, after: null, // when pagination in null it will get first page firstLabels: 100, @@ -105,20 +111,25 @@ async function main() { // Get the list of prs with the skip label so they can // be put at the beginning of the list - const prioritizedPrList = autoMergeEnabledPRs - .filter((pr) => pr.labels.includes('skip-to-front-of-merge-queue')) - .concat(autoMergeEnabledPRs.filter((pr) => !pr.labels.includes('skip-to-front-of-merge-queue'))) + const prioritizedPrList = autoMergeEnabledPRs.sort( + (a, b) => + Number(b.labels.includes('skip-to-front-of-merge-queue')) - + Number(a.labels.includes('skip-to-front-of-merge-queue')) + ) - const nextInQueue = prioritizedPrList.shift() - // Update the branch for the next PR in the merge queue - github.rest.pulls.updateBranch({ - owner: 'github', - repo: 'docs-internal', - pull_number: parseInt(nextInQueue.number), - }) + if (prioritizedPrList.length) { + const nextInQueue = prioritizedPrList.shift() + // Update the branch for the next PR in the merge queue + github.rest.pulls.updateBranch({ + owner: org, + repo, + pull_number: nextInQueue.number, + }) + console.log(`⏱ Total PRs in the merge queue: ${prioritizedPrList.length + 1}`) + console.log(`🚂 Updated branch for PR #${JSON.stringify(nextInQueue, null, 2)}`) + } - console.log(`⏱ Total PRs in the merge queue: ${prioritizedPrList.length + 1}`) - console.log(`🚂 Updated branch for PR #${JSON.stringify(nextInQueue, null, 2)}`) - console.log(`🚏 Next up in the queue: `) - console.log(JSON.stringify(prioritizedPrList, null, 2)) + prioritizedPrList.length + ? console.log(`🚏 Next up in the queue: \n ${JSON.stringify(prioritizedPrList, null, 2)}`) + : console.log(`⚡ The merge queue is empty`) } diff --git a/.github/workflows/autoupdate-branch.yml b/.github/workflows/autoupdate-branch.yml index ebed40bb5d..6b7fdb3ba6 100644 --- a/.github/workflows/autoupdate-branch.yml +++ b/.github/workflows/autoupdate-branch.yml @@ -30,6 +30,15 @@ jobs: name: autoupdate runs-on: ubuntu-latest steps: + - name: Check out repo content + uses: actions/checkout@1e204e9a9253d643386038d443f96446fa156a97 + + - name: Setup Node + uses: actions/setup-node@270253e841af726300e85d718a5f606959b2903c + with: + node-version: 16.13.x + cache: npm + - name: Update next PR in queue env: GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }}