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

Auto rebase workflow addition #1152

Merged
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
82 changes: 82 additions & 0 deletions .github/workflows/auto-rebase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Auto Rebase

on:
push:
branches:
- main
workflow_dispatch:

jobs:
rebase-outdated-prs:
runs-on: ubuntu-latest
steps:
- uses: tibdex/github-app-token@v1
id: generate-token
with:
app_id: ${{ secrets.AUTO_REBASE_APP_ID}}
private_key: ${{ secrets.AUTO_REBASE_APP_PRIVATE_KEY }}

- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ steps.generate-token.outputs.token }}
fetch-depth: 0 # Fetch full history to have the entire commit history

- name: Fetch open pull requests with label
run: |
gh auth setup-git
gh pr list --state open --label "ready-to-be-merged" --json number,headRepositoryOwner,headRefName --jq '.[] | "\(.number) \(.headRepositoryOwner.login) \(.headRefName)"' > pr_details.txt
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}

- name: Rebase pull requests
run: |
while read pr_number pr_owner pr_branch; do
echo "Processing PR #$pr_number"

# Add the contributor's fork as a remote
git remote add contributor https://github.com/$pr_owner/$(gh repo view --json name -q '.name').git

# Fetch the contributor's branches
git fetch contributor

# Create a unique branch name for this PR
unique_branch_name="contributor-branch-$pr_number"

# Checkout the branch from the contributor's fork
git checkout -b $unique_branch_name contributor/$pr_branch

# Set the committer name and email to match the PR author
PR_AUTHOR_NAME=$(gh pr view $pr_number --json author --jq '.author.login')
PR_AUTHOR_EMAIL="${PR_AUTHOR_NAME}@users.noreply.github.com"

git config user.name "$PR_AUTHOR_NAME"
git config user.email "$PR_AUTHOR_EMAIL"

# Rebase the branch on top of the main branch
git fetch origin main
if ! git rebase origin/main; then
echo "Conflict detected. Aborting rebase and continuing."
git rebase --abort

# Post a comment on the PR to notify the author about the conflict
gh pr comment $pr_number --body "Hey @$PR_AUTHOR_NAME, your PR cannot be rebased due to conflicts. Could you resolve them, please?"

continue
fi

# Push the rebased branch back to the contributor's fork
git push --force-with-lease contributor $unique_branch_name:$pr_branch

# Remove the remote
git remote remove contributor

# Ensure we are not on the branch to be deleted
git checkout main

# Delete the local unique branch
git branch -D $unique_branch_name

done < pr_details.txt
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
Loading