Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Rebase PR

on:
issue_comment:
types: [created, edited]

jobs:
rebase-pr:
# Only run if the comment contains the /fast-forward command.
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/rebase') }}
runs-on: macos-latest
permissions:
contents: write
issues: read
pull-requests: read
statuses: write

steps:
- name: Check comment
id: check_comment
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
if [[ "$COMMENT_BODY" =~ (^|[[:space:]])/rebase($|[[:space:]]) ]]; then
echo "MATCH=true" >> $GITHUB_ENV
else
echo "MATCH=false" >> $GITHUB_ENV
fi

- name: Wait for tests to succeed
if: env.MATCH == 'true'
uses: lewagon/wait-on-check-action@v1.3.4
with:
ref: ${{ github.ref }}
check-name: CI
repo-token: ${{ secrets.GITHUB_TOKEN }}
wait-interval: 10

- name: Checkout repository
if: env.MATCH == 'true'
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install git-branchless
if: env.MATCH == 'true'
run: |
brew install --no-quarantine git-branchless
git-branchless init

- name: Rebase PR
if: env.MATCH == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config rebase.instructionFormat "%s%nexec GIT_COMMITTER_DATE=\"%ad\" git commit --amend --no-edit --date=\"%ad\""

git sync 'stack()' --pull

PR_NUMBER=${{ github.event.issue.number }}
PR_BRANCH=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER" \
| jq -r .head.ref)

if [ $? -eq 0 ]; then
git push --force-with-lease origin $PR_BRANCH
else
echo "Rebase failed due to conflicts"
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-X POST \
-d '{"body": "❌ Rebase failed: A conflict occurred. Please rebase manually locally."}' \
"https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
exit 1
fi
Loading