Skip to content

ci: refactor auto-merge flow to use GitHub script and don't block the CI [skip ci] #82

ci: refactor auto-merge flow to use GitHub script and don't block the CI [skip ci]

ci: refactor auto-merge flow to use GitHub script and don't block the CI [skip ci] #82

Workflow file for this run

name: Auto Merge Main into Feature Branch
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, labeled, ready_for_review]
branches:
- main
workflow_dispatch:
jobs:
update-pull-requests:
if: contains(github.event.pull_request.labels.*.name, 'autoupdate')
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Update PRs
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
async function updatePullRequest(pr) {
const { data: changes } = await github.rest.repos.compareCommitsWithBasehead({
...context.repo,
basehead: `${pr.head.ref}...${pr.base.ref}`
});
if (changes.status === 'behind') {
console.info(`PR #${pr.number}: No changes detected`);
} else {
console.info(`PR #${pr.number}: Changes detected`);
const { status: updateStatus } = await github.rest.pulls.updateBranch({
...context.repo,
expected_head_sha: pr.head.sha,
pull_number: pr.number,
});
const commentMessage = updateStatus === 202 ? ":rocket: Merge success!" : ":bangbang: Merge failed!";
await github.rest.issues.createComment({
...context.repo,
issue_number: pr.number,
body: commentMessage,
});
}
}
if (github.event_name === 'pull_request') {
const pr = github.event.pull_request;
await updatePullRequest(pr);
} else if (github.event_name === 'push') {
const { data: pullRequests } = await github.rest.pulls.list({
base: 'main',
sort: 'updated',
state: 'open',
...context.repo
});
const labeledPullRequests = pullRequests.filter(pr => pr.labels.some(label => label.name === 'autoupdate'));
for (const pr of labeledPullRequests) {
console.info(`Checking pull request #${pr.number}`);
await updatePullRequest(pr);
}
}