Created
September 15, 2024 16:12
-
-
Save ccamel/67af201be688b1d169645e51e9ae6234 to your computer and use it in GitHub Desktop.
A bash script to automatically approve and politely ask Dependabot to merge its own PRs, because I’m a bit lazy and happy to let the bots handle the hard part (if the CI behaves)!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
REVIEWER=$(gh api user --jq '.login' 2>/dev/null) | |
APPROVAL_MESSAGES=( | |
"Dependency update approved! ✅" | |
"Nice upgrade, Dependabot! 🚀" | |
"LGTM on the upgrade! 👍" | |
"Ready to roll with this update! 😃" | |
"Dependencies look solid. 👌" | |
"Good to merge this update! ✔️" | |
"Upgrade all the things! 🔄" | |
"Safe to ship! ⛵" | |
"Another one bites the dust! 🐛" | |
"Keeping things fresh! 🍃" | |
"Dependabot coming in clutch! 🤖" | |
"Smooth update, Dependabot! 🌊" | |
"Automated and approved! 🤖✅" | |
"Patch it up! 🔧" | |
"Security updates FTW! 🔐" | |
) | |
NUM_MESSAGES=${#APPROVAL_MESSAGES[@]} | |
RANDOM_INDEX=$((RANDOM % NUM_MESSAGES)) | |
APPROVAL_MESSAGE=${APPROVAL_MESSAGES[$RANDOM_INDEX]} | |
if ! command -v gh &>/dev/null; then | |
echo "❌ gitHub CLI (gh) is not installed. Please install it first." | |
exit 1 | |
fi | |
if [ -z "$1" ]; then | |
echo "❌ usage: $0 <pull_request_reference>" | |
echo "Reference can be a URL or a pull request ID" | |
exit 1 | |
fi | |
if [[ ! $1 =~ https://github.com/([^/]+)/([^/]+)/pull/([0-9]+) ]]; then | |
echo "❌ please provide the full URL to the pull request" | |
exit 1 | |
fi | |
REPOSITORY="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}" | |
PR_NUMBER="${BASH_REMATCH[3]}" | |
echo "ℹ️ repository: $REPOSITORY" | |
echo "ℹ️ pull request: $PR_NUMBER" | |
PR_AUTHOR=$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json author --jq '.author.login') | |
if [[ "$PR_AUTHOR" != "app/dependabot" ]]; then | |
echo "❌ This PR was not created by Dependabot. Author: $PR_AUTHOR" | |
exit 1 | |
fi | |
if ! gh pr view "$PR_NUMBER" --repo "$REPOSITORY" | grep -q "$REVIEWER"; then | |
echo "🛠️ Adding reviewer $REVIEWER to PR $PR_NUMBER..." | |
gh pr edit "$PR_NUMBER" --repo "$REPOSITORY" --add-reviewer "$REVIEWER" | |
else | |
echo "ℹ️ Reviewer $REVIEWER is already assigned to this PR." | |
fi | |
echo "👍 approving PR $PR_NUMBER: $APPROVAL_MESSAGE" | |
gh pr review "$PR_NUMBER" --approve --body "${APPROVAL_MESSAGE}" --repo "$REPOSITORY" | |
echo "✏️ adding merge comment to PR $PR_NUMBER..." | |
gh pr comment "$PR_NUMBER" --repo "$REPOSITORY" --body "@dependabot merge" | |
echo "✨ all done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment