From 8bc09fecc295e006c32c08d00421e518148b6dcd Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 28 Feb 2021 18:35:52 +0000 Subject: [PATCH] Fix github action to automaticaly update the base branch of a PR with a label --- .github/workflows/update-base.yml | 48 ++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/.github/workflows/update-base.yml b/.github/workflows/update-base.yml index cd92072e64..2e03ff9e5a 100644 --- a/.github/workflows/update-base.yml +++ b/.github/workflows/update-base.yml @@ -1,27 +1,55 @@ name: Update Base Branch +# Run every 20 mins or manually on: - pull_request: - types: [ labeled ] + schedule: + - cron: '*/20 * * * *' + workflow_dispatch: jobs: update: - if: ${{ github.event.label.name == 'update-base' }} runs-on: ubuntu-20.04 steps: - uses: actions/github-script@v3 - id: default-branch with: github-token: ${{secrets.GITHUB_TOKEN}} - result-encoding: string script: | + const updateLabel = 'update-base'; const owner = context.repo.owner; const repo = context.repo.repo; - const pull_number = issue_number = ${{github.event.number}}; - const response = await github.repos.get({owner, repo}) + const response = await github.repos.get({ owner, repo }) const base = response.data.default_branch - await github.pulls.update({ owner, repo, pull_number, base}); - await github.issues.createComment({ owner, repo, issue_number, body: '🚀 Target branch has been updated to ' + base}); - await github.issues.removeLabel({owner, repo, issue_number, name: 'update-base'}); \ No newline at end of file + async function updateBase(pull) { + const pull_number = issue_number = pull.number; + + if (pull.base.ref == base) { + await github.issues.createComment({ owner, repo, issue_number, body: '🚨 Target branch is already set to ' + base }); + await github.issues.removeLabel({ owner, repo, issue_number, name: updateLabel }); + return; + } + + // Update target PR branch + await github.pulls.update({ owner, repo, pull_number, base }); + // Updates the pull request with the latest upstream changes. + await github.pulls.updateBranch({ owner, repo, pull_number, }); + await github.issues.createComment({ owner, repo, issue_number, body: '🚀 Target branch has been updated to ' + base }); + await github.issues.removeLabel({ owner, repo, issue_number, name: updateLabel }); + } + + // Query all of the open pull requests + const pulls = await github.pulls.list({ + owner, + repo, + state: 'open' + }); + + // Loop all of the pull requests, finding any with the target label. + for (const pull of pulls.data) { + const requiresUpdate = pull.labels.some((label) => label.name === updateLabel); + + if (requiresUpdate) { + await updateBase(pull); + } + } \ No newline at end of file