@@ -79,6 +79,7 @@ echo ""
79
79
API_ROOT=" https://<your-domain>/api/v3"
80
80
EXECUTE=" FALSE"
81
81
EMPTY_REPO_COUNTER=0
82
+ ERROR_COUNT=0 # Total errors found
82
83
83
84
# #################################
84
85
# Parse options/flags passed in. #
@@ -154,72 +155,106 @@ echo "Getting a list of the repositories within "${ORG_NAME}
154
155
REPO_RESPONSE=" $( curl --request GET \
155
156
--url ${API_ROOT} /orgs/${ORG_NAME} /repos \
156
157
-s \
158
+ --write-out response=%{http_code} \
157
159
--header " authorization: Bearer ${GITHUB_TOKEN} " \
158
160
--header " content-type: application/json" ) "
159
161
160
- # #########################################################################
161
- # Loop through every organization's repo to get repository name and size #
162
- # #########################################################################
163
- echo " Generating list of empty repositories."
164
- echo " "
165
- echo " -------------------"
166
- echo " | Empty Repo List |"
167
- echo " | Org : Repo Name |"
168
- echo " -------------------"
162
+ REPO_RESPONSE_CODE=$( echo " ${REPO_RESPONSE} " | grep ' response=' | sed ' s/response=\(.*\)/\1/' )
169
163
170
- for repo in $( echo " ${REPO_RESPONSE} " | jq -r ' .[] | @base64' ) ;
171
- do
172
- # ####################################
173
- # Get the info from the json object #
174
- # ####################################
175
- get_repo_info ()
176
- {
177
- echo ${repo} | base64 --decode | jq -r ${1}
178
- }
179
-
180
- # Get the info from the JSON object
181
- REPO_NAME=$( get_repo_info ' .name' )
182
- REPO_SIZE=$( get_repo_info ' .size' )
183
-
184
- # If repository has data, size will not be zero, therefore skip.
185
- if [[ ${REPO_SIZE} -ne 0 ]]; then
186
- continue ;
187
- fi
188
-
189
- # ###############################################
190
- # If we are NOT deleting repository, list them #
191
- # ###############################################
192
- if [[ ${EXECUTE} = " FALSE" ]]; then
193
- echo " ${ORG_NAME} :${REPO_NAME} "
194
-
195
- # Increment counter
196
- EMPTY_REPO_COUNTER=$(( EMPTY_REPO_COUNTER+ 1 ))
197
-
198
- # ################################################
199
- # EXECUTE is TRUE, we are deleting repositories #
200
- # ################################################
201
- elif [[ ${EXECUTE} = " TRUE" ]]; then
202
- echo " ${REPO_NAME} will be deleted from ${ORG_NAME} !"
203
-
204
- # ###########################
205
- # Call API to delete repos #
206
- # ###########################
207
- curl --request DELETE \
208
- -s \
209
- --url ${API_ROOT} /repos/${ORG_NAME} /${REPO_NAME} \
210
- --header " authorization: Bearer ${GITHUB_TOKEN} "
211
-
212
- echo " ${REPO_NAME} was deleted from ${ORG_NAME} successfully."
164
+ # #######################
165
+ # Check for any errors #
166
+ # #######################
167
+ if [ $REPO_RESPONSE_CODE != 200 ]; then
168
+ echo " "
169
+ echo " ERROR: Failed to get the list of repositories within ${ORG_NAME} "
170
+ echo " ${REPO_RESPONSE} "
171
+ echo " "
172
+ (( ERROR_COUNT++ ))
173
+ else
174
+ # #########################################################################
175
+ # Loop through every organization's repo to get repository name and size #
176
+ # #########################################################################
177
+ echo " Generating list of empty repositories."
178
+ echo " "
179
+ echo " -------------------"
180
+ echo " | Empty Repo List |"
181
+ echo " | Org : Repo Name |"
182
+ echo " -------------------"
183
+
184
+ for repo in $( echo " ${REPO_RESPONSE} " | jq -r ' .[] | @base64' ) ;
185
+ do
186
+ # ####################################
187
+ # Get the info from the json object #
188
+ # ####################################
189
+ get_repo_info ()
190
+ {
191
+ echo ${repo} | base64 --decode | jq -r ${1}
192
+ }
193
+
194
+ # Get the info from the JSON object
195
+ REPO_NAME=$( get_repo_info ' .name' )
196
+ REPO_SIZE=$( get_repo_info ' .size' )
197
+
198
+ # If repository has data, size will not be zero, therefore skip.
199
+ if [[ ${REPO_SIZE} -ne 0 ]]; then
200
+ continue ;
201
+ fi
202
+
203
+ # ###############################################
204
+ # If we are NOT deleting repository, list them #
205
+ # ###############################################
206
+ if [[ ${EXECUTE} = " FALSE" ]]; then
207
+ echo " ${ORG_NAME} :${REPO_NAME} "
213
208
214
209
# Increment counter
215
210
EMPTY_REPO_COUNTER=$(( EMPTY_REPO_COUNTER+ 1 ))
216
- fi
217
211
218
- done
212
+ # ################################################
213
+ # EXECUTE is TRUE, we are deleting repositories #
214
+ # ################################################
215
+ elif [[ ${EXECUTE} = " TRUE" ]]; then
216
+ echo " ${REPO_NAME} will be deleted from ${ORG_NAME} !"
217
+
218
+ # ###########################
219
+ # Call API to delete repos #
220
+ # ###########################
221
+ DELETE_RESPONSE=" $( curl --request DELETE \
222
+ -s \
223
+ --write-out response=%{http_code} \
224
+ --url ${API_ROOT} /repos/${ORG_NAME} /${REPO_NAME} \
225
+ --header " authorization: Bearer ${GITHUB_TOKEN} " ) "
226
+
227
+ DELETE_RESPONSE_CODE=$( echo " ${DELETE_RESPONSE} " | grep ' response=' | sed ' s/response=\(.*\)/\1/' )
228
+
229
+ # #######################
230
+ # Check for any errors #
231
+ # #######################
232
+ if [ $DELETE_RESPONSE_CODE != 204 ]; then
233
+ echo " "
234
+ echo " ERROR: Failed to delete ${REPO_NAME} from ${ORG_NAME} !"
235
+ echo " ${DELETE_RESPONSE} "
236
+ echo " "
237
+ (( ERROR_COUNT++ ))
238
+ else
239
+ echo " ${REPO_NAME} was deleted from ${ORG_NAME} successfully."
240
+ fi
241
+
242
+ # Increment counter
243
+ EMPTY_REPO_COUNTER=$(( EMPTY_REPO_COUNTER+ 1 ))
244
+ fi
245
+
246
+ done
247
+ fi
219
248
220
249
# #################
221
250
# Exit Messaging #
222
251
# #################
252
+ if [[ $ERROR_COUNT -gt 0 ]]; then
253
+ echo " -----------------------------------------------------"
254
+ echo " the script has completed, there were errors"
255
+ exit $ERROR_COUNT
256
+ fi
257
+
223
258
if [[ ${EXECUTE} = " TRUE" ]]; then
224
259
echo " "
225
260
echo " Successfully deleted ${EMPTY_REPO_COUNTER} empty repos from ${ORG_NAME} ."
0 commit comments