Skip to content

Commit 2da013e

Browse files
authored
Merge pull request #37 from dbsystel/mfranzke-patch-2
feat: added workflow for auto-updating PRs
2 parents 2c728fc + 4dfa4c1 commit 2da013e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

.github/scripts/update-prs.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Fetch all open PRs and updates them with main branch.*/
2+
3+
const updatePrs = async ({ github, context }) => {
4+
const { repo, owner } = context.repo;
5+
const pulls = await github.rest.pulls.list({
6+
owner,
7+
repo,
8+
state: 'open',
9+
base: 'main',
10+
per_page: 100
11+
});
12+
13+
const nonDraftPulls = pulls?.data?.filter((pr) => !pr.draft);
14+
let updatedBranches = 0;
15+
16+
if (nonDraftPulls?.length > 0) {
17+
for (const pr of nonDraftPulls) {
18+
try {
19+
await github.rest.pulls.updateBranch({
20+
owner,
21+
repo,
22+
pull_number: pr.number
23+
});
24+
updatedBranches++;
25+
} catch (e) {
26+
console.error(e);
27+
}
28+
}
29+
}
30+
31+
return `Updated branches: ${updatedBranches}/${nonDraftPulls.length}`;
32+
};
33+
34+
export default updatePrs;

.github/workflows/update-prs.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Update all PR branches if main gets update
3+
4+
on:
5+
push:
6+
branches:
7+
- "main"
8+
9+
jobs:
10+
update-prs:
11+
name: Update PRs
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: ⬇ Checkout repo
15+
uses: actions/checkout@v3
16+
17+
- name: ⌚ Update PRs
18+
id: update-prs
19+
uses: actions/github-script@v6
20+
with:
21+
script: |
22+
const { default: updatePrs } = await import('${{ github.workspace }}/.github/scripts/update-prs.js');
23+
// print how many PRs are updated
24+
console.log(await updatePrs({github, context}));

0 commit comments

Comments
 (0)