name: Dependabot auto-merge # Squash-merge a Dependabot PR automatically once the CI workflow has gone green # on it. CI runs `npm run test-all` (plus lint and the webpack build) across the # whole Node matrix, so "green" is the real gate you asked for. # # This runs on `workflow_run` (i.e. after CI finishes), NOT on the Dependabot # `pull_request` event, on purpose: # * workflow_run jobs run from the DEFAULT branch, so GITHUB_TOKEN gets the # write permission a merge needs -- a Dependabot pull_request token is # read-only and could not merge. # * it gates on CI having actually *passed*, which a pull_request-time job # can't see (CI hasn't run yet), and # * it needs NO branch-protection rule and NO "Allow auto-merge" repo setting. # # Every semver level is merged, majors included, on the theory that the suite is # the gate. To hold majors for a human instead, see the commented guard below. on: workflow_run: workflows: ["CI"] types: [completed] permissions: contents: write pull-requests: write checks: read # Merge one PR at a time: each merge moves `main`, so a serialized queue lets the # next PR conflict cleanly (Dependabot then rebases it and CI re-runs) instead of # racing on package-lock.json. concurrency: group: dependabot-auto-merge cancel-in-progress: false jobs: auto-merge: runs-on: ubuntu-latest # Only a *passing* CI run that was triggered by a pull request is a candidate. if: > github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' steps: - name: Squash-merge the Dependabot PR now that CI is green env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} REPO: ${{ github.repository }} HEAD_SHA: ${{ github.event.workflow_run.head_sha }} run: | # The PR whose head commit this CI run tested. pr=$(gh api "repos/$REPO/commits/$HEAD_SHA/pulls" \ --jq '.[] | select(.state=="open") | .number' 2>/dev/null | head -n1) if [ -z "${pr:-}" ]; then echo "No open PR for commit $HEAD_SHA; nothing to merge."; exit 0 fi # Only ever touch Dependabot's own PRs. Note gh reports the app as # "app/dependabot" while the REST API elsewhere calls it # "dependabot[bot]" -- match either (and nothing else). author=$(gh pr view "$pr" --repo "$REPO" --json author --jq '.author.login' 2>/dev/null || true) case "$author" in *dependabot*) ;; *) echo "PR #$pr is not a Dependabot PR (author: ${author:-unknown}); leaving it alone."; exit 0 ;; esac # All green: every check run on this commit (every job of every PR # workflow, matrix legs included) must be finished and passing. If # another workflow is still running, its own completion re-fires this. bad=$(gh api --paginate "repos/$REPO/commits/$HEAD_SHA/check-runs" \ --jq '.check_runs[] | select(.status != "completed" or ((.conclusion // "") | IN("success","skipped","neutral") | not)) | "\(.name)=\(.conclusion // .status)"') if [ -n "$bad" ]; then echo "Not every check on $HEAD_SHA is green yet; not merging:"; echo "$bad"; exit 0 fi # --- To hold semver-major bumps for a human, uncomment this guard: --- # title=$(gh pr view "$pr" --repo "$REPO" --json title --jq '.title') # fromM=$(printf '%s' "$title" | sed -nE 's/.* from ([0-9]+)\..*/\1/p') # toM=$(printf '%s' "$title" | sed -nE 's/.* to ([0-9]+)\..*/\1/p') # if [ -n "$fromM" ] && [ -n "$toM" ] && [ "$toM" -gt "$fromM" ]; then # echo "PR #$pr is a semver-major bump ($fromM -> $toM); leaving for review."; exit 0 # fi echo "CI is green on Dependabot PR #$pr; squash-merging." # Not-yet-mergeable (a stale branch needing a rebase) is expected and # not an error: Dependabot rebases it, CI re-runs, and this fires again. # --match-head-commit: never merge a commit CI didn't test (e.g. a rebase # Dependabot pushed while this job was starting). gh pr merge "$pr" --repo "$REPO" --squash --delete-branch --match-head-commit "$HEAD_SHA" \ || echo "PR #$pr not merged yet (needs rebase or has a newer commit); will retry after Dependabot updates it."