merge-dependabot-pr.sh
#!/bin/bash
# This script creates a consolidate Pull Request for all outstanding dependabot
# Pull Requests.
#
# Motivation: Sometimes, dependabot opens a large number of PRs that all need to
# be merged. Each time one merges, it causes the rest to be out of date, making
# it a slow, manual process of clicking upate, waiting for ci, then clicking
# merge for each subsequent PR.
#
# Usage:
#
# ```bash
# cd MY_GIT_REPO
# merge-dependabot-prs.sh
# ```
#
# TODO: Only merge PRs with a successful CI status
#
# Maintainer: [Ian Remmel](https://github.com/ianwremmel)
#
# Contribution: This gist will be updated based on feedback in the comments, as
# appropriate
#
# License: MIT
#
# Copyright: Ian Remmel © 2018 until at least now
#
set -euo pipefail
if ! command -v hub >/dev/null 2>&1; then
echo
echo "This script requires hub"
echo "https://hub.github.com/"
echo
exit 1
fi
if ! command -v http >/dev/null 2>&1; then
echo
echo "This script requires httpie"
echo "https://httpie.org/"
echo
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo
echo "This script requires jq"
echo "https://stedolan.github.io/jq/"
echo
exit 1
fi
echo "Making sure master is update to date with origin/master"
git checkout master
git pull --rebase
echo "Checking out new branch"
git checkout -b "merges-$(date +%s)"
echo "Retrieving dependabot branches from GitHub and merging into current branch"
http "$(git remote -v | awk -F"[ :/.]" '/origin.*fetch/{print "https://api.github.com/repos/" $3 "/" $4 "/issues"}')" labels==dependencies | jq .[].html_url | xargs -n 1 hub merge
echo "Pushing current branch to origin"
git push -u origin "$(git branch | awk '/\* /{print $2}')"
echo "Opening Pull Request"
hub pull-request -m 'Consolidated dependabot merges'