Skip to content

Instantly share code, notes, and snippets.

@roamingthings
Last active July 21, 2025 07:17
Show Gist options
  • Select an option

  • Save roamingthings/572955e25a91be849413cca7ffb7580c to your computer and use it in GitHub Desktop.

Select an option

Save roamingthings/572955e25a91be849413cca7ffb7580c to your computer and use it in GitHub Desktop.
A script to list all open Dependabot PRs and select which PRs to approve & merge
#!/bin/bash
# Script to automate Dependabot PR approval and merging
# Usage: ./approve-dependabot-prs.sh
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}INFO:${NC} $1"
}
print_success() {
echo -e "${GREEN}SUCCESS:${NC} $1"
}
print_warning() {
echo -e "${YELLOW}WARNING:${NC} $1"
}
print_error() {
echo -e "${RED}ERROR:${NC} $1"
}
# Check if gh CLI is installed
if ! command -v gh &> /dev/null; then
print_error "GitHub CLI (gh) is not installed. Please install it first."
exit 1
fi
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
print_error "This script must be run from within a Git repository."
exit 1
fi
# Get repository name for display
REPO_NAME=$(basename $(git rev-parse --show-toplevel))
print_info "Working on repository: $REPO_NAME"
# List all Dependabot pull requests
print_info "Fetching Dependabot pull requests..."
DEPENDABOT_PRS=$(gh pr list --app dependabot --json number,title,url,reviews,statusCheckRollup | jq 'map({ number: .number, state: (if (.reviews | length) > 0 then (.reviews | map(.state // "<Unknown>") | unique | join(",")) else "OPEN" end), title: .title, url: .url, statusCheckConclusions: (if .statusCheckRollup then (.statusCheckRollup | map(.conclusion // "<Unknown>") | unique | join(",")) else "<None>" end)})')
if [ -z "$DEPENDABOT_PRS" ] || [ "$DEPENDABOT_PRS" = "[]" ]; then
print_warning "No Dependabot pull requests found."
exit 0
fi
# Color variables using tput
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)
# Display the pull requests in a formatted table
echo ""
echo "Available Dependabot Pull Requests:"
echo "======================================"
# Create header
printf "${BOLD}%-6s %-100s %-6s %-30s %s${RESET}\n" "PR#" "TITLE" "STATE" "CHECKS" "URL"
printf "%-6s %-100s %-6s %-30s %s\n" "$(printf '%*s' 6 '' | tr ' ' '-')" "$(printf '%*s' 100 '' | tr ' ' '-')" "$(printf '%*s' 6 '' | tr ' ' '-')" "$(printf '%*s' 30 '' | tr ' ' '-')" "$(printf '%*s' 20 '' | tr ' ' '-')"
# Process each PR
echo "$DEPENDABOT_PRS" | jq -r '.[] | "\(.number)|\(.title)|\(.state)|\(.statusCheckConclusions)|\(.url)"' | while IFS='|' read -r number title state statusCheckConclusions url; do
# Truncate title if it's too long
if [ ${#title} -gt 100 ]; then
title="${title:0:97}..."
fi
if [ ${#statusCheckConclusions} -gt 30 ]; then
statusCheckConclusions="${title:0:27}..."
fi
# Color code the state
case "$state" in
"OPEN")
# state_colored="${YELLOW}$state${RESET}"
state_colored="⏳"
;;
"APPROVED")
# state_colored="${GREEN}$state${RESET}"
state_colored="✅"
;;
*)
state_colored="$state"
;;
esac
printf "%-6s %-100s %-6s %-30s %s\n" "$number" "$title" "$state_colored" "$statusCheckConclusions" "$url"
done
# Get user input for which PRs to approve and merge
echo ""
read -p "Enter PR numbers to approve and merge (comma-separated, 'all' for all PRs, 'q' to quit): " USER_INPUT
# Parse user input
if [ "$USER_INPUT" = "q" ]; then
print_info "Exiting script."
exit 0
elif [ -z "$USER_INPUT" ]; then
print_warning "No input provided. Exiting script."
exit 0
elif [ "$USER_INPUT" = "all" ]; then
# Get all PR numbers
PR_NUMBERS=$(echo "$DEPENDABOT_PRS" | jq -r '.[].number')
else
# Parse comma-separated list
PR_NUMBERS=$(echo "$USER_INPUT" | tr ',' '\n' | tr -d ' ')
fi
# Validate PR numbers and process them
echo ""
print_info "Processing selected pull requests..."
for PR_NUM in $PR_NUMBERS; do
# Validate that PR number is numeric
if ! [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then
print_error "Invalid PR number: $PR_NUM"
continue
fi
# Check if PR exists in the Dependabot list
if ! echo "$DEPENDABOT_PRS" | jq -e ".[] | select(.number == $PR_NUM)" > /dev/null; then
print_error "PR #$PR_NUM is not a Dependabot PR or doesn't exist"
continue
fi
print_info "Processing PR #$PR_NUM..."
# Get PR title for display
PR_TITLE=$(echo "$DEPENDABOT_PRS" | jq -r ".[] | select(.number == $PR_NUM) | .title")
echo " Title: $PR_TITLE"
# Approve the PR
print_info "Approving PR #$PR_NUM..."
if gh pr review --approve "$PR_NUM"; then
print_success "PR #$PR_NUM approved"
else
print_error "Failed to approve PR #$PR_NUM"
continue
fi
# Merge the PR
print_info "Merging PR #$PR_NUM..."
if gh pr merge "$PR_NUM"; then
print_success "PR #$PR_NUM merged"
else
print_error "Failed to merge PR #$PR_NUM"
fi
echo ""
done
print_success "Script completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment