Now use the pull_request_target trigger, also update the branch and handle merge conflicts (#2113)

This commit is contained in:
modmuss50 2021-03-01 20:25:01 +00:00 committed by GitHub
parent 0ffc0f985e
commit 153c4572f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 27 deletions

View File

@ -1,13 +1,12 @@
name: Update Base Branch
# Run every 20 mins or manually
on:
schedule:
- cron: '*/20 * * * *'
workflow_dispatch:
pull_request_target:
types: [ labeled ]
jobs:
update:
if: ${{ github.event.label.name == 'update-base' }}
runs-on: ubuntu-20.04
steps:
- uses: actions/github-script@v3
@ -17,37 +16,32 @@ jobs:
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 base = response.data.default_branch
const { data: repoData } = await github.repos.get({ owner, repo });
const base = repoData.default_branch;
async function updateBase(pull) {
const pull_number = issue_number = pull.number;
const { data: pull } = await github.pulls.get({ owner, repo, 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;
}
if (pull.base.ref == base) {
await github.issues.createComment({ owner, repo, issue_number, body: '🚨 Target branch is already set to ' + base });
// Update target PR branch
} else {
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: updateLabel });
}
// Query all of the open pull requests
const pulls = await github.pulls.list({
owner,
repo,
state: 'open'
});
try {
// Updates the pull request with the latest upstream changes.
await github.pulls.updateBranch({ owner, repo, pull_number })
} catch (error) {
// 422 is returned when there is a merge conflict
if (error.status === 422) {
await github.issues.createComment({ owner, repo, issue_number, body: '🚨 Please fix merge conflicts before this can be merged' });
await github.issues.addLabels({ owner, repo, issue_number, labels: ['outdated'] });
return;
}
// 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);
throw error;
}
}