Skip to content

Commit b1f1abf

Browse files
committed
Auto rebase
Signed-off-by: Ben <[email protected]>
1 parent 63580e9 commit b1f1abf

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

.github/workflows/auto-rebase.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Auto Rebase PRs
2+
3+
on:
4+
push:
5+
branches:
6+
- '**' # This will trigger on pushes to all branches
7+
8+
jobs:
9+
auto-rebase:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
contents: write
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Configure Git
22+
run: |
23+
git config --global user.name 'github-actions[bot]'
24+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
25+
26+
- name: Find and rebase PRs
27+
uses: actions/github-script@v7
28+
with:
29+
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
script: |
31+
const targetBranch = context.ref.replace('refs/heads/', '');
32+
console.log(`Checking PRs targeting branch: ${targetBranch}`);
33+
34+
const { data: prs } = await github.rest.pulls.list({
35+
owner: context.repo.owner,
36+
repo: context.repo.repo,
37+
state: 'open',
38+
labels: ['use-auto-rebase'],
39+
base: targetBranch
40+
});
41+
42+
for (const pr of prs) {
43+
try {
44+
// Skip if PR is already up to date
45+
const { data: comparison } = await github.rest.repos.compareCommits({
46+
owner: context.repo.owner,
47+
repo: context.repo.repo,
48+
base: pr.base.sha,
49+
head: pr.head.sha
50+
});
51+
52+
if (comparison.behind_by === 0) {
53+
console.log(`PR #${pr.number} is already up to date`);
54+
continue;
55+
}
56+
57+
// Create a comment to indicate rebase attempt
58+
await github.rest.issues.createComment({
59+
owner: context.repo.owner,
60+
repo: context.repo.repo,
61+
issue_number: pr.number,
62+
body: `🤖 Attempting to rebase this PR onto ${targetBranch}...`
63+
});
64+
65+
// Perform the rebase using Git commands
66+
const { execSync } = require('child_process');
67+
68+
// Fetch the PR branch
69+
execSync(`git fetch origin pull/${pr.number}/head:pr-${pr.number}`);
70+
71+
// Checkout the PR branch
72+
execSync(`git checkout pr-${pr.number}`);
73+
74+
// Perform the rebase
75+
execSync(`git rebase origin/${targetBranch}`);
76+
77+
// Force push the rebased branch
78+
execSync(`git push origin pr-${pr.number}:${pr.head.ref} --force-with-lease`);
79+
80+
console.log(`Successfully rebased PR #${pr.number} onto ${targetBranch}`);
81+
} catch (error) {
82+
console.error(`Failed to rebase PR #${pr.number}:`, error.message);
83+
84+
// Create a comment about the failure
85+
await github.rest.issues.createComment({
86+
owner: context.repo.owner,
87+
repo: context.repo.repo,
88+
issue_number: pr.number,
89+
body: `❌ Failed to automatically rebase this PR onto ${targetBranch}. Please rebase manually.`
90+
});
91+
}
92+
}

0 commit comments

Comments
 (0)