Skip to content

Commit 927c357

Browse files
committed
Chore: Rebase Action
1 parent 16e5b31 commit 927c357

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

.github/workflows/rebase.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Rebase PR
2+
3+
on:
4+
issue_comment:
5+
types: [created, edited]
6+
7+
jobs:
8+
rebase-pr:
9+
# Only run if the comment contains the /fast-forward command.
10+
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/rebase') }}
11+
runs-on: macos-latest
12+
permissions:
13+
contents: write
14+
issues: read
15+
pull-requests: read
16+
statuses: write
17+
18+
steps:
19+
- name: Check comment
20+
id: check_comment
21+
env:
22+
COMMENT_BODY: ${{ github.event.comment.body }}
23+
run: |
24+
if [[ "$COMMENT_BODY" =~ (^|[[:space:]])/rebase($|[[:space:]]) ]]; then
25+
echo "MATCH=true" >> $GITHUB_ENV
26+
else
27+
echo "MATCH=false" >> $GITHUB_ENV
28+
fi
29+
30+
- name: Wait for tests to succeed
31+
if: env.MATCH == 'true'
32+
uses: lewagon/wait-on-check-action@v1.3.4
33+
with:
34+
ref: ${{ github.ref }}
35+
check-name: CI
36+
repo-token: ${{ secrets.GITHUB_TOKEN }}
37+
wait-interval: 10
38+
39+
- name: Checkout repository
40+
if: env.MATCH == 'true'
41+
uses: actions/checkout@v4
42+
with:
43+
fetch-depth: 0
44+
45+
- name: Install git-branchless
46+
if: env.MATCH == 'true'
47+
run: |
48+
brew install --no-quarantine git-branchless
49+
git-branchless init
50+
51+
- name: Rebase PR
52+
if: env.MATCH == 'true'
53+
run: |
54+
git config user.name "github-actions[bot]"
55+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
56+
git config rebase.instructionFormat "%s%nexec GIT_COMMITTER_DATE=\"%ad\" git commit --amend --no-edit --date=\"%ad\""
57+
58+
git sync 'stack()' --pull
59+
60+
PR_NUMBER=${{ github.event.issue.number }}
61+
PR_BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
62+
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" \
63+
| jq -r .head.ref)
64+
65+
if [ $? -eq 0 ]; then
66+
git push --force-with-lease origin $PR_BRANCH
67+
else
68+
echo "Rebase failed due to conflicts"
69+
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
70+
-X POST \
71+
-d '{"body": "❌ Rebase failed: A conflict occurred. Please rebase manually locally."}' \
72+
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
73+
exit 1
74+
fi

0 commit comments

Comments
 (0)