Skip to content

Commit 60b35f7

Browse files
authored
Merge pull request #210 from kaltepeter/delete-error-handling
'delete-empty-repos.sh' error handling
2 parents 6f484ff + 4e7d472 commit 60b35f7

File tree

1 file changed

+89
-54
lines changed

1 file changed

+89
-54
lines changed

api/bash/delete-empty-repos.sh

Lines changed: 89 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ echo ""
7979
API_ROOT="https://<your-domain>/api/v3"
8080
EXECUTE="FALSE"
8181
EMPTY_REPO_COUNTER=0
82+
ERROR_COUNT=0 # Total errors found
8283

8384
##################################
8485
# Parse options/flags passed in. #
@@ -154,72 +155,106 @@ echo "Getting a list of the repositories within "${ORG_NAME}
154155
REPO_RESPONSE="$(curl --request GET \
155156
--url ${API_ROOT}/orgs/${ORG_NAME}/repos \
156157
-s \
158+
--write-out response=%{http_code} \
157159
--header "authorization: Bearer ${GITHUB_TOKEN}" \
158160
--header "content-type: application/json")"
159161

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/')
169163

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}"
213208

214209
# Increment counter
215210
EMPTY_REPO_COUNTER=$((EMPTY_REPO_COUNTER+1))
216-
fi
217211

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
219248

220249
##################
221250
# Exit Messaging #
222251
##################
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+
223258
if [[ ${EXECUTE} = "TRUE" ]]; then
224259
echo ""
225260
echo "Successfully deleted ${EMPTY_REPO_COUNTER} empty repos from ${ORG_NAME}."

0 commit comments

Comments
 (0)