Merge pull request #1097 from jbesraa/2024-08-13-refactor-poolsv2 #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |