-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfork-delete
executable file
·50 lines (39 loc) · 1021 Bytes
/
fork-delete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/zsh
readonly program="$(basename "${0}")"
# Usage
function usage {
echo "
Delete your GitHub forks where you do not have pending pull requests.
Usage:
${program}
Options:
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Checks
if [[ "${1}" =~ ^(-h|--help)$ ]]
then
usage
exit 0
fi
if ! hash 'gh' 2> /dev/null
then
echo 'Missing "gh" in PATH. Install with:' >&2
echo ' brew install gh' >&2
exit 1
fi
# Main
echo 'Delete fork of…'
while IFS= read -r repo
do
parent_repo="$(gh repo view "${repo}" --json parent --jq '.parent | "\(.owner.login)/\(.name)"')"
# Skip if there are open pull requests in repo
[[ "$(gh pr list --repo "${parent_repo}" --author '@me' --json title --jq 'length')" -eq 0 ]] || continue
# Ask for delete confirmation
echo -n "${parent_repo}"
read -rq "? [y/N] "
echo
[[ "${REPLY}" == 'y' ]] || continue
gh repo delete --yes "${repo}"
echo
done < <(gh repo list --fork --json nameWithOwner --jq '.[].nameWithOwner')