|
| 1 | +#!/usr/bin/env bash |
| 2 | +# combine-deps — fold every open Dependabot pull request into a single one. |
| 3 | +# |
| 4 | +# Dependabot groups within an ecosystem but never across them, so even a |
| 5 | +# correctly grouped repository still opens one pull request per ecosystem. |
| 6 | +# This replays all of them onto one branch and closes the originals against it. |
| 7 | +# |
| 8 | +# Re-running is safe: the branch is rebuilt from the base branch every time, so |
| 9 | +# a pull request that has since merged or closed simply drops out. |
| 10 | +# |
| 11 | +# A repository with artefacts that must be regenerated after a bump (a lockfile |
| 12 | +# in a second module, a checked-in hash manifest) puts that in |
| 13 | +# .github/scripts/combine-deps-local.sh, which runs after the replay and before |
| 14 | +# the pull request is opened. Anything it commits rides along. |
| 15 | +# |
| 16 | +# One caveat before you close the combined pull request: closing a Dependabot |
| 17 | +# pull request tells Dependabot not to re-open one for that same version. |
| 18 | +# Merging moves the dependency, so the next pass proposes the next version and |
| 19 | +# nothing is lost. Abandoning it instead forfeits that batch until a newer |
| 20 | +# version ships. |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +BRANCH="deps/combined" |
| 24 | +BASE="$(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)" |
| 25 | + |
| 26 | +git fetch --quiet origin "$BASE" |
| 27 | + |
| 28 | +mapfile -t ROWS < <( |
| 29 | + gh pr list --state open --author "app/dependabot" --base "$BASE" \ |
| 30 | + --limit 100 --json number,title --jq '.[] | [.number, .title] | @tsv' | sort -n |
| 31 | +) |
| 32 | + |
| 33 | +if [ "${#ROWS[@]}" -eq 0 ]; then |
| 34 | + echo "combine-deps: no open Dependabot pull requests; nothing to combine." |
| 35 | + exit 0 |
| 36 | +fi |
| 37 | + |
| 38 | +git config user.name "github-actions[bot]" |
| 39 | +git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 40 | +git checkout --quiet -B "$BRANCH" "origin/$BASE" |
| 41 | + |
| 42 | +combined=() |
| 43 | +skipped=() |
| 44 | + |
| 45 | +for row in "${ROWS[@]}"; do |
| 46 | + number="${row%%$'\t'*}" |
| 47 | + title="${row#*$'\t'}" |
| 48 | + |
| 49 | + git fetch --quiet origin "refs/pull/${number}/head:refs/combine/${number}" |
| 50 | + base_point="$(git merge-base "origin/$BASE" "refs/combine/${number}")" |
| 51 | + |
| 52 | + if git cherry-pick --allow-empty "${base_point}..refs/combine/${number}" >/dev/null 2>&1; then |
| 53 | + combined+=("${number}"$'\t'"${title}") |
| 54 | + echo "combine-deps: replayed #${number} — ${title}" |
| 55 | + else |
| 56 | + git cherry-pick --abort >/dev/null 2>&1 || true |
| 57 | + skipped+=("${number}"$'\t'"${title}") |
| 58 | + echo "combine-deps: SKIPPED #${number} (conflicts with an earlier bump) — ${title}" |
| 59 | + fi |
| 60 | +done |
| 61 | + |
| 62 | +if [ "${#combined[@]}" -eq 0 ]; then |
| 63 | + echo "combine-deps: every pull request conflicted; leaving them all open." |
| 64 | + exit 0 |
| 65 | +fi |
| 66 | + |
| 67 | +if [ -x .github/scripts/combine-deps-local.sh ]; then |
| 68 | + echo "combine-deps: running repository-local post-processing." |
| 69 | + .github/scripts/combine-deps-local.sh |
| 70 | +fi |
| 71 | + |
| 72 | +body_file="$(mktemp)" |
| 73 | +{ |
| 74 | + echo "Every open Dependabot pull request, replayed onto one branch by" |
| 75 | + echo "\`.github/workflows/combine-deps.yml\` so the week's dependency work is" |
| 76 | + echo "reviewed and merged once instead of per ecosystem." |
| 77 | + echo |
| 78 | + echo "### Combined" |
| 79 | + echo |
| 80 | + for row in "${combined[@]}"; do |
| 81 | + echo "- #${row%%$'\t'*} — ${row#*$'\t'}" |
| 82 | + done |
| 83 | + if [ "${#skipped[@]}" -gt 0 ]; then |
| 84 | + echo |
| 85 | + echo "### Left open (conflicted on replay, merge these by hand)" |
| 86 | + echo |
| 87 | + for row in "${skipped[@]}"; do |
| 88 | + echo "- #${row%%$'\t'*} — ${row#*$'\t'}" |
| 89 | + done |
| 90 | + fi |
| 91 | +} > "$body_file" |
| 92 | + |
| 93 | +git push --quiet --force-with-lease origin "$BRANCH" |
| 94 | + |
| 95 | +existing="$(gh pr list --state open --head "$BRANCH" --json number --jq '.[0].number // empty')" |
| 96 | +if [ -n "$existing" ]; then |
| 97 | + gh pr edit "$existing" --body-file "$body_file" |
| 98 | + combined_pr="$existing" |
| 99 | +else |
| 100 | + gh pr create --base "$BASE" --head "$BRANCH" \ |
| 101 | + --title "build(deps): combined dependency updates" --body-file "$body_file" |
| 102 | + combined_pr="$(gh pr list --state open --head "$BRANCH" --json number --jq '.[0].number')" |
| 103 | +fi |
| 104 | + |
| 105 | +for row in "${combined[@]}"; do |
| 106 | + number="${row%%$'\t'*}" |
| 107 | + gh pr comment "$number" --body "Superseded by #${combined_pr}, which carries this bump together with the rest of the week's dependency updates." |
| 108 | + gh pr close "$number" |
| 109 | +done |
| 110 | + |
| 111 | +echo "combine-deps: combined ${#combined[@]} pull request(s) into #${combined_pr}." |
0 commit comments