1+ name : Recreate Dependabot PR on develop
2+
3+ on :
4+ pull_request :
5+ types : [opened]
6+ branches :
7+ - main
8+
9+ jobs :
10+ rebase-dependabot-to-develop :
11+ if : github.actor == 'dependabot[bot]' || github.actor == 'dependabot-preview[bot]'
12+ runs-on : ubuntu-latest
13+
14+ steps :
15+ - name : Checkout repository
16+ uses : actions/checkout@v4
17+
18+ - name : Setup Git
19+ run : |
20+ git config user.name "github-actions[bot]"
21+ git config user.email "github-actions[bot]@users.noreply.github.com"
22+
23+ - name : Fetch full history and all branches
24+ run : git fetch --unshallow --all
25+
26+ - name : Create a new branch from develop with dependabot changes
27+ env :
28+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
29+ run : |
30+ ORIGINAL_BRANCH="${{ github.event.pull_request.head.ref }}"
31+ NEW_BRANCH="dependabot-develop-${ORIGINAL_BRANCH}"
32+
33+ # Create new branch based on develop
34+ git checkout origin/develop -b $NEW_BRANCH
35+
36+ # Get last commit message from original Dependabot branch
37+ LAST_COMMIT_MSG=$(git log -1 --pretty=%B origin/$ORIGINAL_BRANCH)
38+
39+ # Merge Dependabot changes into the new branch
40+ git merge --no-commit origin/$ORIGINAL_BRANCH || true
41+ git commit -m "$LAST_COMMIT_MSG"
42+ git push origin $NEW_BRANCH
43+
44+ # Create new pull request against develop
45+ gh pr create \
46+ --base develop \
47+ --head $NEW_BRANCH \
48+ --title "${{ github.event.pull_request.title }} (rebased onto develop)" \
49+ --body "This is an automated copy of #${{ github.event.pull_request.number }}, targeting \`develop\` instead of \`main\`."
50+
51+ # Close the original PR
52+ gh pr close ${{ github.event.pull_request.number }} --comment "Automatically closed: a new PR has been created against \`develop\`."
53+
54+ # Delete the original Dependabot branch
55+ gh api \
56+ -X DELETE \
57+ repos/${{ github.repository }}/git/refs/heads/$ORIGINAL_BRANCH
0 commit comments