|
| 1 | +name: Dependabot Automerge |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: |
| 6 | + - CI |
| 7 | + - E2E Backend Integration Tests |
| 8 | + - E2E Controller Tests |
| 9 | + - E2E Gateway Tests |
| 10 | + types: |
| 11 | + - completed |
| 12 | + |
| 13 | +permissions: |
| 14 | + actions: read |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + automerge: |
| 20 | + if: > |
| 21 | + github.event.workflow_run.event == 'pull_request' && |
| 22 | + github.event.workflow_run.conclusion == 'success' && |
| 23 | + github.event.workflow_run.pull_requests[0].number != null |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Load current pull request state |
| 28 | + id: pr |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ github.token }} |
| 31 | + PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} |
| 32 | + REPO: ${{ github.repository }} |
| 33 | + run: | |
| 34 | + set -euo pipefail |
| 35 | +
|
| 36 | + pr_json="$(gh pr view "$PR_NUMBER" --repo "$REPO" --json author,autoMergeRequest,headRefOid,isDraft,state,url)" |
| 37 | +
|
| 38 | + author="$(jq -r '.author.login // ""' <<<"$pr_json")" |
| 39 | + state="$(jq -r '.state' <<<"$pr_json")" |
| 40 | + is_draft="$(jq -r '.isDraft' <<<"$pr_json")" |
| 41 | + head_sha="$(jq -r '.headRefOid' <<<"$pr_json")" |
| 42 | + pr_url="$(jq -r '.url' <<<"$pr_json")" |
| 43 | + auto_merge_enabled="$(jq -r 'if .autoMergeRequest == null then "false" else "true" end' <<<"$pr_json")" |
| 44 | +
|
| 45 | + if [ "$author" != "dependabot[bot]" ]; then |
| 46 | + echo "skip_reason=not_dependabot" >> "$GITHUB_OUTPUT" |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +
|
| 50 | + if [ "$state" != "OPEN" ] || [ "$is_draft" != "false" ]; then |
| 51 | + echo "skip_reason=not_open_ready" >> "$GITHUB_OUTPUT" |
| 52 | + exit 0 |
| 53 | + fi |
| 54 | +
|
| 55 | + if [ "$auto_merge_enabled" = "true" ]; then |
| 56 | + echo "skip_reason=auto_merge_already_enabled" >> "$GITHUB_OUTPUT" |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | +
|
| 60 | + echo "head_sha=$head_sha" >> "$GITHUB_OUTPUT" |
| 61 | + echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT" |
| 62 | +
|
| 63 | + - name: Verify all PR test workflows passed |
| 64 | + id: checks |
| 65 | + if: steps.pr.outputs.head_sha != '' |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ github.token }} |
| 68 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 69 | + REPO: ${{ github.repository }} |
| 70 | + run: | |
| 71 | + set -euo pipefail |
| 72 | +
|
| 73 | + # Keep this list aligned with the pull request workflows so merges wait |
| 74 | + # for the full test suite on the current PR head commit. |
| 75 | + required_workflows=( |
| 76 | + "CI" |
| 77 | + "E2E Backend Integration Tests" |
| 78 | + "E2E Controller Tests" |
| 79 | + "E2E Gateway Tests" |
| 80 | + ) |
| 81 | +
|
| 82 | + runs_json="$(gh api "/repos/$REPO/actions/runs?event=pull_request&head_sha=$HEAD_SHA&per_page=100")" |
| 83 | +
|
| 84 | + all_passed=true |
| 85 | +
|
| 86 | + for workflow in "${required_workflows[@]}"; do |
| 87 | + workflow_run="$(jq -c --arg workflow "$workflow" ' |
| 88 | + [.workflow_runs[] | select(.name == $workflow)] |
| 89 | + | sort_by(.created_at, .run_attempt) |
| 90 | + | last |
| 91 | + ' <<<"$runs_json")" |
| 92 | +
|
| 93 | + if [ "$workflow_run" = "null" ]; then |
| 94 | + echo "Workflow \"$workflow\" has not reported a run for $HEAD_SHA yet." |
| 95 | + all_passed=false |
| 96 | + continue |
| 97 | + fi |
| 98 | +
|
| 99 | + status="$(jq -r '.status' <<<"$workflow_run")" |
| 100 | + conclusion="$(jq -r '.conclusion // ""' <<<"$workflow_run")" |
| 101 | +
|
| 102 | + if [ "$status" != "completed" ] || [ "$conclusion" != "success" ]; then |
| 103 | + echo "Workflow \"$workflow\" is $status with conclusion \"$conclusion\"." |
| 104 | + all_passed=false |
| 105 | + fi |
| 106 | + done |
| 107 | +
|
| 108 | + echo "all_passed=$all_passed" >> "$GITHUB_OUTPUT" |
| 109 | +
|
| 110 | + - name: Enable merge for the pull request |
| 111 | + if: steps.checks.outputs.all_passed == 'true' |
| 112 | + env: |
| 113 | + GH_TOKEN: ${{ github.token }} |
| 114 | + HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
| 115 | + PR_URL: ${{ steps.pr.outputs.pr_url }} |
| 116 | + REPO: ${{ github.repository }} |
| 117 | + run: | |
| 118 | + gh pr merge "$PR_URL" --repo "$REPO" --auto --merge --match-head-commit "$HEAD_SHA" |
0 commit comments