From 13c6477d1f6352c950ce628e16291c942331804d Mon Sep 17 00:00:00 2001 From: Gabriele Vernetti Date: Fri, 30 Aug 2024 11:48:06 +0200 Subject: [PATCH] Auto rebase workflow addition --- .github/workflows/auto-rebase.yaml | 82 ++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .github/workflows/auto-rebase.yaml diff --git a/.github/workflows/auto-rebase.yaml b/.github/workflows/auto-rebase.yaml new file mode 100644 index 000000000..141f59809 --- /dev/null +++ b/.github/workflows/auto-rebase.yaml @@ -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 }} \ No newline at end of file