Skip to content

Commit 0b6cac7

Browse files
committed
Chore: Rebase Action
1 parent 16e5b31 commit 0b6cac7

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

.github/workflows/rebase.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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: 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+
git sync 'stack()' --pull
50+
51+
PR_NUMBER=${{ github.event.issue.number }}
52+
PR_BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
53+
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" \
54+
| jq -r .head.ref)
55+
56+
if [ $? -eq 0 ]; then
57+
git push --force-with-lease origin $PR_BRANCH
58+
else
59+
echo "Rebase failed due to conflicts"
60+
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
61+
-X POST \
62+
-d '{"body": "❌ Rebase failed: A conflict occurred. Please rebase manually locally."}' \
63+
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
64+
exit 1
65+
fi

0 commit comments

Comments
 (0)