|
| 1 | +name: Dependabot auto-merge |
| 2 | +on: pull_request |
| 3 | + |
| 4 | +permissions: |
| 5 | + contents: write |
| 6 | + pull-requests: write |
| 7 | + checks: read |
| 8 | + statuses: read |
| 9 | + |
| 10 | +jobs: |
| 11 | + dependabot: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + if: github.event.pull_request.user.login == 'dependabot[bot]' |
| 14 | + steps: |
| 15 | + - name: Checkout repository |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Fetch Dependabot metadata |
| 21 | + id: metadata |
| 22 | + uses: dependabot/fetch-metadata@v2 |
| 23 | + with: |
| 24 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + |
| 26 | + - name: Wait for checks to pass |
| 27 | + run: | |
| 28 | + MAX_RETRIES=6 |
| 29 | + RETRY_INTERVAL=300 |
| 30 | + ATTEMPT=0 |
| 31 | + while [ $ATTEMPT -lt $MAX_RETRIES ]; do |
| 32 | + echo "Fetching PR status checks..." |
| 33 | + WORKFLOWS=$(gh pr view "$PR_URL" --json statusCheckRollup -q '.statusCheckRollup') |
| 34 | + all_passed=true |
| 35 | + while IFS= read -r check; do |
| 36 | + name=$(jq -r '.name' <<< "$check") |
| 37 | + status=$(jq -r 'if .type == "CheckRun" then .conclusion else .state end' <<< "$check") |
| 38 | + if [[ "$name" != "Dependabot auto-merge" && "$status" != "SUCCESS" ]]; then |
| 39 | + all_passed=false |
| 40 | + echo "Failed check: $name - Status: $status" |
| 41 | + break |
| 42 | + fi |
| 43 | + done < <(echo "$WORKFLOWS" | jq -c '.[]') |
| 44 | + if $all_passed; then |
| 45 | + echo "All checks passed. Merging PR." |
| 46 | + exit 0 |
| 47 | + else |
| 48 | + echo "Some checks failed. Retrying in $RETRY_INTERVAL seconds..." |
| 49 | + sleep $RETRY_INTERVAL |
| 50 | + ATTEMPT=$((ATTEMPT + 1)) |
| 51 | + fi |
| 52 | + done |
| 53 | + echo "Failed to merge after $MAX_RETRIES attempts." |
| 54 | + exit 1 |
| 55 | + env: |
| 56 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 57 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Enable auto-merge for Dependabot PRs |
| 60 | + run: gh pr merge --auto --squash "$PR_URL" |
| 61 | + env: |
| 62 | + PR_URL: ${{github.event.pull_request.html_url}} |
| 63 | + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
0 commit comments