Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(workflows): add pull requests auto updater #2855

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/gh_pr_auto_updater.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: '[gh] pull request auto updater'

on:
pull_request_target:
types:
- auto_merge_enabled
push:

jobs:
up_to_date:
if: github.repository == 'Tencent/Hippy'
runs-on: ubuntu-latest
steps:
- name: Token
uses: navikt/github-app-token-generator@v1
id: get-token
with:
private-key: ${{ secrets.ACTION_PRIVATE_KEY }}
app-id: ${{ secrets.ACTION_APP_ID }}
- name: Updater
uses: actions/[email protected]
with:
github-token: ${{ steps.get-token.outputs.token }}
script: |
const { pulls, repos } = github.rest;

let pull_requests;
switch (context.eventName) {
case 'push': {
pull_requests = (await github.paginate(pulls.list, {
per_page: 100,
state: 'open',
base: '${{ github.ref_name }}',
...context.repo
})).filter(pull => pull.draft === false && pull.auto_merge);
break;
}
case 'pull_request_target': {
const { pull_request } = context.payload;
if (pull_request.draft === true) {
return;
}
pull_requests = [pull_request];
break;
}
default: {
throw new Error(`Unsupported event name: ${context.eventName}`);
break;
}
}

await Promise.all(
pull_requests.map(pull =>
repos.compareCommitsWithBasehead({
...context.repo,
basehead: `${pull.base.label}...${pull.head.label}`,
}).then(({ data: comparison }) => {
if (comparison.behind_by > 0) {
return pulls.updateBranch({
...context.repo,
pull_number: pull.number
}).catch(error => console.error(error));
}
})
)
);