Skip to content

Commit 93841d4

Browse files
committed
Chore: Rebase Action
1 parent 16e5b31 commit 93841d4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

.github/workflows/rebase.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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: write
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: Checkout repository
31+
if: env.MATCH == 'true'
32+
uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
36+
- name: Install git-branchless
37+
if: env.MATCH == 'true'
38+
run: |
39+
brew install --no-quarantine git-branchless
40+
git-branchless init
41+
42+
- name: Rebase PR
43+
if: env.MATCH == 'true'
44+
run: |
45+
git config user.name "github-actions[bot]"
46+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
47+
git config rebase.instructionFormat "%s%nexec GIT_COMMITTER_DATE=\"%ad\" git commit --amend --no-edit --date=\"%ad\""
48+
49+
PR_NUMBER=${{ github.event.issue.number }}
50+
PR_BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
51+
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" \
52+
| jq -r .head.ref)
53+
54+
git checkout $PR_BRANCH
55+
PR_HEAD=$(git rev-parse HEAD)
56+
REBASE_OUTPUT=$(git sync 'stack()' --pull 2>&1)
57+
REBASE_EXIT_CODE=$?
58+
59+
echo "$REBASE_OUTPUT"
60+
if [ "$(git rev-parse HEAD)" = "$MAIN_HEAD" ]; then
61+
echo "Already up to date with $PR_BRANCH"
62+
elif echo "$REBASE_OUTPUT" | grep -q "^Merge conflict" || [ $REBASE_EXIT_CODE -ne 0 ]; then
63+
echo "Rebase failed due to conflicts"
64+
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
65+
-X POST \
66+
-d '{"body": "❌ Rebase failed: A conflict occurred. Please rebase manually locally."}' \
67+
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
68+
exit 1
69+
else
70+
echo "✅ Rebase successful"
71+
git push --force-with-lease origin $PR_BRANCH
72+
fi

0 commit comments

Comments
 (0)