Skip to content

Instantly share code, notes, and snippets.

@rnorth
Last active June 17, 2021 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rnorth/e2805089ffb301dd670b5bbaa5c85647 to your computer and use it in GitHub Desktop.
Save rnorth/e2805089ffb301dd670b5bbaa5c85647 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
COMBINED_BRANCH=combined-pr-branch
BODY_FILE=/tmp/.combined-pr-body.md
LIMIT=50
echo -n > $BODY_FILE
git fetch
git checkout master
git pull --ff-only
git branch -D combined-pr-branch || true
git checkout -b $COMBINED_BRANCH
echo $'Combining multiple dependencies PRs into one:\n\n' > $BODY_FILE
count=0
gh pr list --search "author:app/dependabot" --limit 100 --json headRefName,number | jq -r ".[] | [.number,.headRefName] | @tsv" | while read -r NUMBER HEADREF
do
if [[ $(gh pr checks "$NUMBER" | cut -d$'\t' -f2 | grep -E "fail|pending" -c) -gt 0 ]]; then
echo "Checks are not all passing - skipping PR #$NUMBER"
continue
fi
echo "Trying to merge $HEADREF into $COMBINED_BRANCH"
if [[ ! $(git merge "origin/$HEADREF" --no-edit) ]]; then
echo "Unable to merge $HEADREF - skipping PR #$NUMBER"
continue
fi
echo "Merged $HEADREF into $COMBINED_BRANCH"
DESCRIPTION=$(gh pr view "$NUMBER" --json title,author,number --template '{{.title}} (#{{.number}}) @{{.author.login}}')
echo "* ${DESCRIPTION}" >> $BODY_FILE
cat $BODY_FILE
((count=count+1))
if [[ $count == "$LIMIT" ]]; then
echo "Hit limit of $LIMIT - no more PRs will be added"
break
fi
done
echo "Creating PR if one does not exist"
gh pr create --title "Combined dependencies PR" --body-file $BODY_FILE --label dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment