|
| 1 | +name: Auto-Merge Dependency PRs |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - synchronize |
| 8 | + - reopened |
| 9 | + - labeled |
| 10 | + |
| 11 | +permissions: |
| 12 | + pull-requests: write |
| 13 | + contents: write |
| 14 | + actions: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + process-prs: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + # ✅ Step 1: Checkout the repo (Fixes "not a git repository" issue) |
| 22 | + - name: Checkout Repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + # ✅ Step 2: Authenticate GitHub CLI (Ensure gh CLI works) |
| 26 | + - name: Authenticate GitHub CLI |
| 27 | + run: gh auth setup-git |
| 28 | + env: |
| 29 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + # ✅ Step 3: Fetch all PRs sorted from oldest to newest |
| 32 | + - name: Get Oldest PRs |
| 33 | + id: get-prs |
| 34 | + run: | |
| 35 | + prs=$(gh pr list --state open --json number,createdAt --jq 'sort_by(.createdAt) | .[].number') |
| 36 | + echo "prs=$prs" >> $GITHUB_ENV |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + |
| 40 | + # ✅ Step 4: Process PRs One by One |
| 41 | + - name: Process PRs |
| 42 | + run: | |
| 43 | + for pr in $prs; do |
| 44 | + echo "Processing PR #$pr" |
| 45 | + # Checkout PR branch |
| 46 | + gh pr checkout $pr |
| 47 | + # Handle conflicts: Ensure latest package.json dependency version is used |
| 48 | + git checkout --ours package.json || true |
| 49 | + git add package.json || true |
| 50 | + # If package.json had conflicts, fix package-lock.json too |
| 51 | + if git diff --name-only | grep "package.json"; then |
| 52 | + echo "package.json had conflicts, regenerating package-lock.json..." |
| 53 | + npm install |
| 54 | + git add package-lock.json |
| 55 | + fi |
| 56 | + # Commit & push resolved conflicts |
| 57 | + git commit -m "Resolve dependency conflicts in PR #$pr" || true |
| 58 | + git push origin HEAD || true |
| 59 | + # Approve PR before merging |
| 60 | + gh pr review $pr --approve |
| 61 | + # Merge PR when all checks pass |
| 62 | + gh pr merge $pr --squash --auto |
| 63 | + echo "Merged PR #$pr successfully!" |
| 64 | + done |
| 65 | + env: |
| 66 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments