Skip to content

Commit

Permalink
feat(workflows): add pull requests auto updater (Tencent#2855)
Browse files Browse the repository at this point in the history
  • Loading branch information
medns authored and pba-cra committed Feb 1, 2023
1 parent 9077414 commit a972ffd
Showing 1 changed file with 66 additions and 0 deletions.
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/github-script@v6.3.3
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));
}
})
)
);

0 comments on commit a972ffd

Please sign in to comment.