|
| 1 | +name: Combine Dependabot PRs |
| 2 | + |
| 3 | +# Combine all open Dependabot PRs into a single consolidated PR. |
| 4 | +# |
| 5 | +# Dependabot cannot group dependency updates across ecosystems |
| 6 | +# (npm + github-actions + docker each produce their own PR). This |
| 7 | +# workflow stitches every open PR labeled `dependencies` into one |
| 8 | +# branch and opens a single PR, so reviewers only have to look at |
| 9 | +# one diff and CI runs once. |
| 10 | +# |
| 11 | +# Triggers: |
| 12 | +# - pull_request_target → fires right after Dependabot opens a |
| 13 | +# `dependencies`-labeled PR. A 2-minute settle delay + concurrency |
| 14 | +# cancel ensures that when Dependabot opens 3 PRs in a wave (one |
| 15 | +# per ecosystem) only the final run actually performs the combine. |
| 16 | +# - workflow_dispatch → run on demand from the Actions tab |
| 17 | +# |
| 18 | +# Behavior: |
| 19 | +# - Picks all open PRs from `app/dependabot` carrying the |
| 20 | +# `dependencies` label whose branches still merge cleanly into main. |
| 21 | +# - Updates the stable branch `combined-deps/open` (force-with-lease) |
| 22 | +# and opens or updates a single PR against it, so repeated runs |
| 23 | +# converge on the same PR instead of creating new ones. |
| 24 | +# - Skips PRs that cause a merge conflict and reports them in the |
| 25 | +# PR body so a human can deal with them individually. |
| 26 | +# - Does NOT close the source PRs automatically — merging the |
| 27 | +# combined PR will auto-close them because git sees their |
| 28 | +# commits as already in main. |
| 29 | +# |
| 30 | +# Security note on `pull_request_target`: |
| 31 | +# The job runs in the base-repo context with write permissions, but |
| 32 | +# never checks out PR-supplied code. It only invokes `gh` + `git` on |
| 33 | +# PR refs to merge their commits into our branch. No PR-controlled |
| 34 | +# script executes here. |
| 35 | +on: |
| 36 | + pull_request_target: |
| 37 | + types: [opened, reopened, ready_for_review, labeled] |
| 38 | + workflow_dispatch: |
| 39 | + inputs: |
| 40 | + dry-run: |
| 41 | + description: 'List PRs that would be combined without pushing or opening a PR' |
| 42 | + required: false |
| 43 | + type: boolean |
| 44 | + default: false |
| 45 | + |
| 46 | +permissions: |
| 47 | + contents: read |
| 48 | + |
| 49 | +# A wave of Dependabot PRs triggers this workflow N times in ~1-2 minutes. |
| 50 | +# Cancel any in-flight run when a new one starts; the last run sees all PRs. |
| 51 | +concurrency: |
| 52 | + group: combine-prs |
| 53 | + cancel-in-progress: true |
| 54 | + |
| 55 | +jobs: |
| 56 | + combine: |
| 57 | + name: Combine open Dependabot PRs |
| 58 | + runs-on: ubuntu-latest |
| 59 | + # Only act on Dependabot PRs (or manual dispatch). Without this guard |
| 60 | + # every PR open/label event would otherwise trigger the workflow. |
| 61 | + if: >- |
| 62 | + github.event_name == 'workflow_dispatch' || |
| 63 | + (github.event.pull_request.user.login == 'dependabot[bot]' && |
| 64 | + contains(github.event.pull_request.labels.*.name, 'dependencies')) |
| 65 | + permissions: |
| 66 | + contents: write |
| 67 | + pull-requests: write |
| 68 | + steps: |
| 69 | + - name: Wait for Dependabot wave to settle |
| 70 | + if: github.event_name == 'pull_request_target' |
| 71 | + run: sleep 120 |
| 72 | + |
| 73 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 74 | + with: |
| 75 | + fetch-depth: 0 |
| 76 | + ref: main |
| 77 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + - name: Configure git |
| 80 | + run: | |
| 81 | + git config user.name "github-actions[bot]" |
| 82 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 83 | +
|
| 84 | + - name: Combine open Dependabot PRs |
| 85 | + env: |
| 86 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + DRY_RUN: ${{ inputs.dry-run }} |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | +
|
| 91 | + DATE=$(date +%Y-%m-%d) |
| 92 | + # Stable branch name so repeated runs (Dependabot waves, manual |
| 93 | + # dispatches, retries) converge on a single combined PR instead |
| 94 | + # of spawning a new one per date. |
| 95 | + COMBINED_BRANCH="combined-deps/open" |
| 96 | +
|
| 97 | + mapfile -t PRS < <(gh pr list \ |
| 98 | + --state open \ |
| 99 | + --author "app/dependabot" \ |
| 100 | + --label dependencies \ |
| 101 | + --json number,title,headRefName \ |
| 102 | + --jq '.[] | "\(.number)\t\(.title)\t\(.headRefName)"') |
| 103 | +
|
| 104 | + if [ "${#PRS[@]}" -eq 0 ]; then |
| 105 | + echo "No open Dependabot PRs labeled 'dependencies' found. Nothing to do." |
| 106 | + exit 0 |
| 107 | + fi |
| 108 | +
|
| 109 | + echo "Found ${#PRS[@]} candidate PR(s):" |
| 110 | + printf ' %s\n' "${PRS[@]}" |
| 111 | +
|
| 112 | + if [ "${DRY_RUN}" = "true" ]; then |
| 113 | + echo "Dry run requested — exiting before any push." |
| 114 | + exit 0 |
| 115 | + fi |
| 116 | +
|
| 117 | + git fetch origin main --quiet |
| 118 | + git checkout -B "${COMBINED_BRANCH}" origin/main |
| 119 | +
|
| 120 | + MERGED_NUMBERS=() |
| 121 | + MERGED_LINES=() |
| 122 | + SKIPPED_LINES=() |
| 123 | +
|
| 124 | + for entry in "${PRS[@]}"; do |
| 125 | + IFS=$'\t' read -r number title head_ref <<<"${entry}" |
| 126 | + echo "::group::Merging #${number} (${head_ref})" |
| 127 | + git fetch origin "pull/${number}/head:pr-${number}" --quiet |
| 128 | + if git merge --no-edit --no-ff -m "Merge #${number}: ${title}" "pr-${number}"; then |
| 129 | + MERGED_NUMBERS+=("${number}") |
| 130 | + MERGED_LINES+=("- #${number} ${title}") |
| 131 | + echo "Merged #${number}" |
| 132 | + else |
| 133 | + echo "Conflict merging #${number}, aborting that merge." |
| 134 | + git merge --abort || true |
| 135 | + SKIPPED_LINES+=("- #${number} ${title} _(conflict — must be resolved manually)_") |
| 136 | + fi |
| 137 | + echo "::endgroup::" |
| 138 | + done |
| 139 | +
|
| 140 | + if [ "${#MERGED_NUMBERS[@]}" -eq 0 ]; then |
| 141 | + echo "No PRs merged cleanly. Nothing to push." |
| 142 | + exit 0 |
| 143 | + fi |
| 144 | +
|
| 145 | + git push --force-with-lease origin "${COMBINED_BRANCH}" |
| 146 | +
|
| 147 | + BODY_FILE=$(mktemp) |
| 148 | + { |
| 149 | + echo "Combined dependency update generated by \`combine-prs.yml\` on ${DATE}." |
| 150 | + echo |
| 151 | + echo "Merging this PR will auto-close the source Dependabot PRs because" |
| 152 | + echo "their commits will be reachable from \`main\`." |
| 153 | + echo |
| 154 | + echo "### Combined PRs" |
| 155 | + printf '%s\n' "${MERGED_LINES[@]}" |
| 156 | + if [ "${#SKIPPED_LINES[@]}" -gt 0 ]; then |
| 157 | + echo |
| 158 | + echo "### Skipped (merge conflict)" |
| 159 | + printf '%s\n' "${SKIPPED_LINES[@]}" |
| 160 | + fi |
| 161 | + } > "${BODY_FILE}" |
| 162 | +
|
| 163 | + EXISTING=$(gh pr list \ |
| 164 | + --head "${COMBINED_BRANCH}" \ |
| 165 | + --state open \ |
| 166 | + --json number \ |
| 167 | + --jq '.[0].number // empty') |
| 168 | +
|
| 169 | + if [ -n "${EXISTING}" ]; then |
| 170 | + echo "Updating existing combined PR #${EXISTING}" |
| 171 | + gh pr edit "${EXISTING}" \ |
| 172 | + --title "chore(deps): combined dependency updates ${DATE}" \ |
| 173 | + --body-file "${BODY_FILE}" |
| 174 | + else |
| 175 | + gh pr create \ |
| 176 | + --base main \ |
| 177 | + --head "${COMBINED_BRANCH}" \ |
| 178 | + --title "chore(deps): combined dependency updates ${DATE}" \ |
| 179 | + --label dependencies \ |
| 180 | + --body-file "${BODY_FILE}" |
| 181 | + fi |
0 commit comments