Skip to content

Commit 2d9c89a

Browse files
authored
Initial Commit
1 parent 4c9e0e5 commit 2d9c89a

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

api/bash/delete-empty-repos.sh

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#!/bin/sh
2+
#/
3+
#/ NAME:
4+
#/ delete-empty-repos - For a GitHub Enterprise Instance, lists every empty repository
5+
#/ in format <organization>:<repository> and deletes them if option is passed.
6+
#/
7+
#/ SYNOPSIS:
8+
#/ delete-empty-repos.sh [--org=MyOrganization] [--execute=TRUE]
9+
#/
10+
#/ DESCRIPTION:
11+
#/ For a GitHub Enterprise Instance, lists every empty repository in format
12+
#/ <organization>:<repository> separated by new lines. Deleting them if passed
13+
#/ the option [--execute=true].
14+
#/ - Example Output: List all empty repositories
15+
#/ <organization>:<repository1>
16+
#/ <organization>:<repository2>
17+
#/ <organization>:<repository3>
18+
#/
19+
#/ PRE-REQUISITES:
20+
#/ Before running this script, you must create a Personal Access Token (PAT)
21+
#/ at https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
22+
#/ with the permissions <repo> and <admin:org> scopes and <delete_repo>. Read more
23+
#/ about scopes here: https://developer.github.com/apps/building-oauth-apps/scopes-for-oauth-apps/
24+
#/
25+
#/ Once created, you must export your PAT as an environment variable
26+
#/ named <GITHUB_TOKEN>.
27+
#/ - Exporting PAT as GITHUB_TOKEN
28+
#/ $ export GITHUB_TOKEN=abcd1234efg567
29+
#/
30+
#/ Additionally you will need to set the $API_ROOT at the top of the script to
31+
#/ your instance of GitHub Enterprise.
32+
#/ - _i.e._: https://MyGitHubEnterprise.com/api/v3
33+
#/
34+
#/ OPTIONS:
35+
#/ --org
36+
#/ -o
37+
#/ When running the tool, this flag sets which organization's repositories you
38+
#/ want to inspect and delete (if they're empty).
39+
#/
40+
#/ --execute
41+
#/ -e
42+
#/ When running the tool, this flag will delete every repo listed.
43+
#/ * _NOTE:_ You should run the script without this option first, verifying
44+
#/ that you want to delete every repository listed.
45+
#/
46+
#/ EXAMPLES:
47+
#/
48+
#/ - Lists all empty repositories for the given organization.
49+
#/ $ bash delete-empty-repos.sh --org=MyOrganization
50+
#/
51+
#/ - Deletes all empty repositories for the given organization.
52+
#/ $ bash delete-empty-repos.sh --org=MyOrganization --execute=TRUE
53+
#/
54+
#/ API DOCUMENTATION:
55+
#/ All documentation can be found at https://developer.github.com/v3/
56+
57+
echo ""
58+
echo "#######################################"
59+
echo "# ____ #"
60+
echo "# | _ \ ___ _ __ ___ #"
61+
echo "# | |_) / _ \ '_ \ / _ \ #"
62+
echo "# | _ < __/ |_) | (_) | #"
63+
echo "# |_|_\_\___| .__/ \___/ #"
64+
echo "# | _ \ ___ |_| _ _ __ ___ _ __ #"
65+
echo "# | |_) / _ \/ _| | '_ \ / _ \ '__| #"
66+
echo "# | _ < __/ (_| | |_) | __/ | #"
67+
echo "# |_| \_\___|\__,_| .__/ \___|_| #"
68+
echo "# |_| #"
69+
echo "#######################################"
70+
echo ""
71+
72+
########
73+
# VARS #
74+
########
75+
API_ROOT="https://<your-domain>/api/v3"
76+
EXECUTE="FALSE"
77+
EMPTY_REPO_COUNTER=0
78+
79+
##################################
80+
# Parse options/flags passed in. #
81+
##################################
82+
83+
for param in "$@"
84+
do
85+
case $param in
86+
-e=*|--execute=*)
87+
EXECUTE="${param#*=}"
88+
shift
89+
;;
90+
-o=*|--org=*)
91+
ORG_NAME="${param#*=}"
92+
shift
93+
;;
94+
*)
95+
# unknown option, do nothing
96+
;;
97+
esac
98+
done
99+
100+
#################
101+
# Verify Inputs #
102+
#################
103+
104+
# If GITHUB_TOKEN wasn't set in Environment
105+
if [[ -z ${GITHUB_TOKEN} ]]; then
106+
echo "ERROR: GITHUB_TOKEN was not found in your environment. You must export "
107+
echo "this token prior to running the script."
108+
echo " Ex: EXPORT GITHUB_TOKEN=abc123def456"
109+
echo ""
110+
echo "Exiting script with no changes."
111+
echo ""
112+
exit 1
113+
fi
114+
115+
# If ORG_NAME wasn't passed
116+
if [[ -z ${ORG_NAME} ]]; then
117+
echo "ERROR: ORG_NAME was not provided."
118+
echo " Ex: bash delete-empty-repos.sh --org=MyOrganization --execute=TRUE"
119+
echo ""
120+
echo "Exiting script with no changes."
121+
echo ""
122+
exit 1
123+
fi
124+
125+
# If EXECUTE exists, it needs to equal TRUE or FALSE
126+
if [[ ${EXECUTE} != "TRUE" ]] && [[ ${EXECUTE} != "FALSE" ]]; then
127+
echo "ERROR: EXECUTE was not set to a proper value."
128+
echo " Ex: bash delete-empty-repos.sh --org=MyOrganization --execute=TRUE"
129+
echo ""
130+
echo "Exiting script with no changes."
131+
echo ""
132+
exit 1
133+
fi
134+
135+
if [[ ${EXECUTE} = "TRUE" ]]; then
136+
echo "Searching for empty repositories within the Organization: "${ORG_NAME}
137+
echo "EXECUTE was set to TRUE!!! Empty repositories will be deleted!!!"
138+
echo "You have 5 seconds to cancel this script."
139+
sleep 5
140+
else
141+
echo "Searching for empty repositories within the Organization: "${ORG_NAME}
142+
echo "EXECUTE was set to FALSE, no repositories will be deleted."
143+
fi
144+
145+
##################################################
146+
# Grab JSON of all repositories for organization #
147+
##################################################
148+
echo "Getting a list of the repositories within "${ORG_NAME}
149+
150+
REPO_RESPONSE="$(curl --request GET \
151+
--url ${API_ROOT}/orgs/${ORG_NAME}/repos \
152+
-s \
153+
--header "authorization: Bearer ${GITHUB_TOKEN}" \
154+
--header "content-type: application/json")"
155+
156+
##########################################################################
157+
# Loop through every organization's repo to get repository name and size #
158+
##########################################################################
159+
echo "Generating list of empty repositories."
160+
echo ""
161+
echo "-------------------"
162+
echo "| Empty Repo List |"
163+
echo "| Org : Repo Name |"
164+
echo "-------------------"
165+
166+
for repo in $(echo "${REPO_RESPONSE}" | jq -r '.[] | @base64');
167+
do
168+
#####################################
169+
# Get the info from the json object #
170+
#####################################
171+
get_repo_info()
172+
{
173+
echo ${repo} | base64 --decode | jq -r ${1}
174+
}
175+
176+
# Get the info from the JSON object
177+
REPO_NAME=$(get_repo_info '.name')
178+
REPO_SIZE=$(get_repo_info '.size')
179+
180+
# If repository has data, size will not be zero, therefore skip.
181+
if [[ ${REPO_SIZE} -ne 0 ]]; then
182+
continue;
183+
fi
184+
185+
################################################
186+
# If we are NOT deleting repository, list them #
187+
################################################
188+
if [[ ${EXECUTE} = "FALSE" ]]; then
189+
echo "${ORG_NAME}:${REPO_NAME}"
190+
191+
# Increment counter
192+
EMPTY_REPO_COUNTER=$((EMPTY_REPO_COUNTER+1))
193+
194+
#################################################
195+
# EXECUTE is TRUE, we are deleting repositories #
196+
#################################################
197+
elif [[ ${EXECUTE} = "TRUE" ]]; then
198+
echo "${REPO_NAME} will be deleted from ${ORG_NAME}!"
199+
200+
############################
201+
# Call API to delete repos #
202+
############################
203+
curl --request DELETE \
204+
-s \
205+
--url ${API_ROOT}/repos/${ORG_NAME}/${REPO_NAME} \
206+
--header "authorization: Bearer ${GITHUB_TOKEN}"
207+
208+
echo "${REPO_NAME} was deleted from ${ORG_NAME} successfully."
209+
210+
# Increment counter
211+
EMPTY_REPO_COUNTER=$((EMPTY_REPO_COUNTER+1))
212+
fi
213+
214+
done
215+
216+
##################
217+
# Exit Messaging #
218+
##################
219+
if [[ ${EXECUTE} = "TRUE" ]]; then
220+
echo ""
221+
echo "Successfully deleted ${EMPTY_REPO_COUNTER} empty repos from ${ORG_NAME}."
222+
else
223+
echo ""
224+
echo "Successfully discovered ${EMPTY_REPO_COUNTER} empty repos within ${ORG_NAME}."
225+
fi
226+
exit 0

0 commit comments

Comments
 (0)