|
| 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 | + 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 | + PR_HEAD=$(git rev-parse HEAD) |
| 55 | + REBASE_OUTPUT=$(git sync 'stack()' --pull 2>&1) |
| 56 | + REBASE_EXIT_CODE=$? |
| 57 | +
|
| 58 | + echo "$REBASE_OUTPUT" |
| 59 | + if [ "$(git rev-parse HEAD)" = "$MAIN_HEAD" ]; then |
| 60 | + echo "Already up to date with $PR_BRANCH" |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + if echo "$REBASE_OUTPUT" | grep -q "^Merge conflict" || [ $REBASE_EXIT_CODE -ne 0 ]; then |
| 65 | + git push --force-with-lease origin $PR_BRANCH |
| 66 | + else |
| 67 | + echo "Rebase failed due to conflicts" |
| 68 | + curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 69 | + -X POST \ |
| 70 | + -d '{"body": "❌ Rebase failed: A conflict occurred. Please rebase manually locally."}' \ |
| 71 | + "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments" |
| 72 | + exit 1 |
| 73 | + fi |
0 commit comments