Skip to content

Commit

Permalink
ci: tweaks to auto branch switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Feb 22, 2024
1 parent 22e202d commit f63c3b2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions .github/workflows/set-default-pr-branch.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
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';
const allowedSources = ['alpha', 'beta'];
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}')`);
}

0 comments on commit f63c3b2

Please sign in to comment.