Last active
March 20, 2023 04:28
-
-
Save siketyan/104ffd7bc1cf4290a9f1c48195910257 to your computer and use it in GitHub Desktop.
Deletes all stale branches on GitHub (or Enterprise Server) using gh CLI and jq
This file contains hidden or 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
set owner "siketyan" | |
set repo "foobar" | |
set last_committed_before "2023-03-01" | |
for name in (gh api "repos/$owner/$repo/branches?per_page=100" | jq -r .[].name) | |
echo "Checking $name" | |
set stale (gh api "repos/$owner/$repo/branches/$name" | jq -r "select(.commit.commit.committer.date < $last_committed_before) | .name") | |
if test -n "$stale" | |
echo "Branch $stale is stale, deleting" | |
gh api -X DELETE "repos/$owner/$repo/git/refs/heads/$stale" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment