Last active
October 25, 2023 11:24
-
-
Save Tatsh/a7de3bc4e422599267b8fccd8c41d771 to your computer and use it in GitHub Desktop.
merge-dependabot-prs
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
#!/usr/bin/env bash | |
get-repos() { | |
gh repo list --limit 9999 --jq '.[].nameWithOwner' --json nameWithOwner --no-archived && | |
while IFS=$'\n' read -r org; do | |
gh api \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"/orgs/${org}/repos" --jq '.[].full_name' | |
done < <(gh org list) | |
} | |
get-repos-sorted() { | |
get-repos | sort -u | |
} | |
uses-dependabot() { | |
gh api \ | |
--silent \ | |
-H 'Accept: application/vnd.github+json' \ | |
-H 'X-GitHub-Api-Version: 2022-11-28' \ | |
"/repos/${1}/contents/.github/dependabot.yml" &>/dev/null || return 1 | |
} | |
comment-rebase() { | |
local -r number="$2" | |
local -r repo="$1" | |
gh pr --repo "$repo" comment "$number" --body '@dependabot rebase' | |
} | |
try-merge-pr() { | |
local -r number="$2" | |
local -r repo="$1" | |
gh pr --repo "${repo}" merge --admin --delete-branch --rebase "$number" || | |
comment-rebase "$repo" "$number" | |
} | |
get-dependabot-open-pr-numbers() { | |
gh pr --repo "$1" list --author app/dependabot --jq '.[].number' --json number --state open | |
} | |
main() { | |
local i number repo | |
for i in gh jq rg; do | |
if ! command -v "$i" &>/dev/null; then | |
echo "Install ${i}" >&2 | |
return 1 | |
fi | |
done | |
while IFS=$'\n' read -r repo; do | |
if ! uses-dependabot "$repo"; then | |
continue | |
fi | |
echo "$repo" | |
while IFS=$'\n' read -r number; do | |
try-merge-pr "$repo" "$number" | |
done < <(get-dependabot-open-pr-numbers "$repo") | |
done < <(get-repos-sorted) | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment