Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change file to rebase #5

Merged
merged 14 commits into from
Jan 13, 2024
54 changes: 31 additions & 23 deletions .github/workflows/ci.yml → .github/workflows/auto-rebase.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
name: Workflow Name
# This workflow automatically rebases pull requests targeting the base branch
# and when a comment containing '/rebase' is created or edited.
name: Automatic rebase strategy

on:
pull_request:
Expand All @@ -14,17 +16,17 @@ on:
- edited

env:
# Change `secrets.GH_TOKEN` to your own secret name if you want to use a different one
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
GH_BRANCH_BASE: ${{ github.event.pull_request.base.ref }}
GH_BRANCH: ${{ github.event.pull_request.head.ref }}
GH_BRANCH_SHA: ${{ github.event.pull_request.head.sha }}
GH_BOT_NAME: github-actions
GH_BOT_EMAIL: github-actions[bot]@users.noreply.github.com

jobs:
rebase:
name: Rebase
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && contains(github.event.comment.html_url, '/rebase')) }}
if: github.event_name == 'pull_request' || contains(github.event.comment.body, '/rebase')
permissions:
pull-requests: write
steps:
Expand All @@ -35,50 +37,56 @@ jobs:
fetch-depth: 0
ref: ${{ env.GH_BRANCH }}

- name: Configure git
run: |
# Use the author of the last commit as the committer
git config user.name $(git log -1 --pretty=format:'%an')
git config user.email $(git log -1 --pretty=format:'%ae')

# Keep the original author date
git config rebase.instructionFormat '%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit --reset-author --date="%cD"'
shell: bash

- id: branch_status
name: Check if branch is up-to-date
run: |
if git merge-base --is-ancestor ${{ github.event.pull_request.base.ref }} ${{ github.event.pull_request.head.sha }};
then
git fetch origin ${{ env.GH_BRANCH_BASE }}

if git merge-base --is-ancestor origin/${{ env.GH_BRANCH_BASE }} ${{ env.GH_BRANCH_SHA }}; then
echo "up_to_date=true" >> $GITHUB_OUTPUT
else
echo "up_to_date=false" >> $GITHUB_OUTPUT
fi

echo ${{github.event_name}}
echo ${{github.event.comment.html_url}}
shell: bash

- name: Try to rebase
if: ${{ steps.branch_status.outputs.up_to_date == 'false' }}
if: steps.branch_status.outputs.up_to_date == 'false'
run: |
# Use the author of the last commit as the committer
git config user.name $(git log -1 --pretty=format:'%an')
git config user.email $(git log -1 --pretty=format:'%ae')

# Keep the original author date
git config rebase.instructionFormat '%s%nexec GIT_COMMITTER_DATE="%cD" git commit --amend --no-edit --reset-author --date="%cD"'

git fetch origin ${{ env.GH_BRANCH_BASE }}
git pull --rebase origin ${{ env.GH_BRANCH_BASE }}
# Rebase with the `--rebase-merges` flag to preserve merge commits
git rebase --rebase-merges origin/${{ env.GH_BRANCH_BASE }}

# Edit last commit message to include [skip ci] and avoid triggering subsequent workflows
git commit --amend -m "$(git log -1 --pretty=%B) [skip ci]"

git push origin ${{ env.GH_BRANCH }} --force-with-lease

- name: Comment on pull request
if: success()
- name: Comment on pull request if rebase was successful
if: success() && steps.branch_status.outputs.up_to_date == 'false'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
OMG ta funcionando
Branch is up-to-date :tada:
pr_number: ${{ github.event.pull_request.number }}

- name: Comment on pull request
- name: Comment on pull request if rebase failed
if: failure()
uses: thollander/actions-comment-pull-request@v2
with:
message: |
arruma seus conflitos parça
:stop_sign::raised_hand: Cannot rebase automatically.

Please rebase manually and push again.

If you don't familiar with git rebase, please check out [this guide](https://docs.github.com/en/get-started/using-git/resolving-merge-conflicts-after-a-git-rebase).
pr_number: ${{ github.event.pull_request.number }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## Recipes

- [Automatic Rebase](./autorebase.md)
- [Automatic Rebase](./auto-rebase.md)
Loading