Fix github action to automaticaly update the base branch of a PR with a label

This commit is contained in:
modmuss50 2021-02-28 18:35:52 +00:00
parent e0934a304c
commit 8bc09fecc2
1 changed files with 38 additions and 10 deletions

View File

@ -1,27 +1,55 @@
name: Update Base Branch name: Update Base Branch
# Run every 20 mins or manually
on: on:
pull_request: schedule:
types: [ labeled ] - cron: '*/20 * * * *'
workflow_dispatch:
jobs: jobs:
update: update:
if: ${{ github.event.label.name == 'update-base' }}
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
steps: steps:
- uses: actions/github-script@v3 - uses: actions/github-script@v3
id: default-branch
with: with:
github-token: ${{secrets.GITHUB_TOKEN}} github-token: ${{secrets.GITHUB_TOKEN}}
result-encoding: string
script: | script: |
const updateLabel = 'update-base';
const owner = context.repo.owner; const owner = context.repo.owner;
const repo = context.repo.repo; 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 const base = response.data.default_branch
await github.pulls.update({ owner, repo, pull_number, base}); async function updateBase(pull) {
await github.issues.createComment({ owner, repo, issue_number, body: '🚀 Target branch has been updated to ' + base}); const pull_number = issue_number = pull.number;
await github.issues.removeLabel({owner, repo, issue_number, name: 'update-base'});
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);
}
}