From f63c3b22b9259e5785314301fa4a66eb996bcc55 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Thu, 22 Feb 2024 10:17:36 +0100 Subject: [PATCH] ci: tweaks to auto branch switcher --- .github/workflows/set-default-pr-branch.yml | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/set-default-pr-branch.yml b/.github/workflows/set-default-pr-branch.yml index 02a5bda46e..7f54df23b2 100644 --- a/.github/workflows/set-default-pr-branch.yml +++ b/.github/workflows/set-default-pr-branch.yml @@ -1,20 +1,25 @@ -name: Update or Allow PR Base Branch +name: Check and Update PR Base Branch on: pull_request: branches: [main] - types: [opened, edited, reopened] + types: [opened, edited, reopened, synchronize] + +permissions: + pull-requests: write jobs: check-and-update-base: runs-on: ubuntu-latest steps: - name: Check and Update Base Branch - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: github-token: ${{secrets.GITHUB_TOKEN}} script: | - const { issue: { number: prNumber }, repository: { owner: { login: owner }, name: repo } } = context.payload; + const prNumber = context.payload.pull_request.number; // Correct way to access PR number + const owner = context.repo.owner; + const repo = context.repo.repo; const currentBase = context.payload.pull_request.base.ref; const sourceBranch = context.payload.pull_request.head.ref; const newBase = 'alpha'; @@ -22,13 +27,16 @@ jobs: if (currentBase === 'main' && !allowedSources.includes(sourceBranch)) { console.log(`Updating the base of PR #${prNumber} from '${currentBase}' to '${newBase}' because the source branch is '${sourceBranch}'`); - const response = await github.rest.pulls.update({ + await github.rest.pulls.update({ owner, repo, pull_number: prNumber, base: newBase, + }).then(response => { + console.log(`Base branch updated to '${newBase}': ${response.data.html_url}`); + }).catch(error => { + console.error(`Failed to update base branch: ${error}`); }); - console.log(`Base branch updated to '${newBase}': ${response.data.url}`); } else { console.log(`No update needed for PR #${prNumber} (source: '${sourceBranch}', base: '${currentBase}')`); }