dependabot-auto-merge.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. name: Dependabot auto-merge
  2. # Approves and squash-merges a Dependabot pull request when CI passes on its
  3. # head commit. Semver-major bumps are left for a human. Based on aiohttp's
  4. # workflow.
  5. #
  6. # Running on CI's completion means only a successful run merges, a re-run that
  7. # goes green merges too, and --match-head-commit refuses a newer, untested
  8. # push.
  9. #
  10. # dependabot/fetch-metadata needs a pull_request payload, which workflow_run
  11. # does not have, so the update type is read from the `updated-dependencies`
  12. # block Dependabot writes into its commit message (the same data that action
  13. # parses). The head commit must be authored by Dependabot and signed by
  14. # GitHub, so a human commit pushed to the branch is never auto-merged.
  15. #
  16. # The default-branch ruleset requires one approving review, so the job
  17. # approves before merging. That needs "Allow GitHub Actions to create and
  18. # approve pull requests" enabled in the repository's Actions settings. The
  19. # approval is pinned to the tested commit so it never covers a newer push.
  20. #
  21. # Other required checks (pre-commit.ci) may still be pending when CI
  22. # completes and their success doesn't re-trigger this workflow, so the job
  23. # waits for them before merging.
  24. #
  25. # GITHUB_TOKEN on purpose: Dependabot secrets reach every workflow a
  26. # Dependabot PR triggers, including CI that installs the bumped package, so
  27. # the release app key is not exposed to them. The cost is that a Dependabot
  28. # merge doesn't trigger `push` workflows; CI already ran on the PR. No PR code
  29. # is checked out or run here.
  30. on:
  31. workflow_run:
  32. workflows: [CI]
  33. types: [completed]
  34. permissions:
  35. actions: read
  36. checks: read
  37. contents: write
  38. pull-requests: write
  39. statuses: read
  40. jobs:
  41. dependabot:
  42. runs-on: ubuntu-latest
  43. timeout-minutes: 30
  44. if: >-
  45. github.event.workflow_run.conclusion == 'success'
  46. && github.event.workflow_run.event == 'pull_request'
  47. && github.event.workflow_run.head_repository.full_name == github.repository
  48. && startsWith(github.event.workflow_run.head_branch, 'dependabot/')
  49. steps:
  50. - name: Approve and merge if this is a non-major Dependabot bump
  51. env:
  52. GH_REPO: ${{ github.repository }}
  53. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  54. HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
  55. run: |
  56. pr="$(gh api "repos/$GH_REPO/commits/$HEAD_SHA/pulls" \
  57. --jq '[.[] | select(.state == "open")][0].number // empty')"
  58. if [ -z "$pr" ]; then
  59. echo "No open PR for $HEAD_SHA"
  60. exit 0
  61. fi
  62. pr_author="$(gh pr view "$pr" --json author --jq .author.login)"
  63. commit="$(gh api "repos/$GH_REPO/commits/$HEAD_SHA")"
  64. commit_author="$(jq -r '.author.login // empty' <<< "$commit")"
  65. if [ "$pr_author" != "app/dependabot" ] || [ "$commit_author" != "dependabot[bot]" ]; then
  66. echo "PR #$pr ($pr_author) or head commit ($commit_author) is not from Dependabot"
  67. exit 0
  68. fi
  69. # author.login comes from the commit email, which anyone can set;
  70. # Dependabot's own commits are signed by GitHub (web-flow).
  71. signed="$(jq -r '"\(.committer.login // "")/\(.commit.verification.verified)"' <<< "$commit")"
  72. if [ "$signed" != "web-flow/true" ]; then
  73. echo "PR #$pr head commit is not GitHub-signed ($signed)"
  74. exit 0
  75. fi
  76. message="$(jq -r .commit.message <<< "$commit")"
  77. if grep -q 'update-type: version-update:semver-major' <<< "$message"; then
  78. echo "PR #$pr includes a semver-major update; leaving it for review"
  79. exit 0
  80. fi
  81. if ! grep -qE 'update-type: version-update:semver-(minor|patch)' <<< "$message"; then
  82. echo "PR #$pr head commit has no Dependabot update metadata"
  83. exit 0
  84. fi
  85. if ! gh pr checks "$pr" --required --watch --fail-fast --interval 30; then
  86. echo "PR #$pr required checks did not pass; leaving it for review"
  87. exit 0
  88. fi
  89. current_head="$(gh pr view "$pr" --json headRefOid --jq .headRefOid)"
  90. if [ "$current_head" != "$HEAD_SHA" ]; then
  91. echo "PR #$pr head moved to $current_head; not approving $HEAD_SHA"
  92. exit 0
  93. fi
  94. gh api "repos/$GH_REPO/pulls/$pr/reviews" \
  95. -f commit_id="$HEAD_SHA" \
  96. -f event=APPROVE \
  97. -f body="CI passed on $HEAD_SHA; non-major Dependabot update."
  98. gh pr merge "$pr" --squash --match-head-commit "$HEAD_SHA"