-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoved.sh
More file actions
100 lines (90 loc) · 3.08 KB
/
removed.sh
File metadata and controls
100 lines (90 loc) · 3.08 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
reset=`tput sgr0`
COUNT=1;
linkPermission='https://github.com/settings/tokens/new?scopes=delete_repo&description=Mass+remove+repository'
access_token=''
repositoriesFile='repositories.txt'
savedTokenFile='token.txt'
countSuccessfully=0;
countFailed=0;
numOfLinesRepositoryList=0
promptAccessToken() {
read -p "${yellow}Access Token${reset}: " access_token
}
checkToken() {
while true; do
if [[ $(curl -H "Authorization: token $access_token" -s "https://api.github.com/user" -I | grep -i "HTTP/1.1 200 OK") != "" ]]
then
if [ ! -f $savedTokenFile ];
then
echo "${green}Wow. Token is correct. I write your token to ${reset}${yellow}'$savedTokenFile'${reset}"
echo $access_token > $savedTokenFile
else
echo "${green}Token is correct.${reset}"
fi
echo "${green}I begin remove repository:${reset}";
break
else
echo "${red}Bad access_token. Try again.${reset}"
promptAccessToken
fi
done
}
printf "\n"
echo "---------------------"
echo "${yellow} Youre running script to remove all github repositories listed in the file :D${reset}"
echo "---------------------"
printf "\n"
if [ ! -f $repositoriesFile ];
then
echo "${red}You have not create file '${repositoriesFile}'.${reset}"
exit
else
numOfLinesRepositoryList=$(wc -l < ${repositoriesFile})
fi
if [[ $numOfLinesRepositoryList == 0 ]]
then
echo "${yellow}File '${repositoriesFile}' the number of rows is equal to zero. Just sad :c ${reset}"
exit
else
if [ ! -f $savedTokenFile ];
then
echo "${yellow}Head to ${reset}${linkPermission} ${yellow}to retrieve a token.${reset}"
echo "${green}Please enter your GitHub token from removed repositories:${reset}"
printf "\n"
promptAccessToken
checkToken
else
while IFS= read -r token || [ -n "$token" ]; do
access_token=$token
checkToken
done < $savedTokenFile
fi
fi
while IFS= read -r repo || [ -n "$repo" ]; do
if [[ $(curl -X DELETE -H "Authorization: token $access_token" -s "https://api.github.com/repos/$repo" -I | grep -i "HTTP/1.1 404 Not Found") != "" ]]
then
printf " $COUNT) $yellow Repository: $reset[$repo]$red\n * [404] Repository not found.$reset\n\n";
countFailed=$(( $countFailed + 1 ))
elif [[ $(curl -X DELETE -H "Authorization: token $access_token" -s "https://api.github.com/repos/$repo" -I | grep -i "HTTP/1.1 403 Forbidden") != "" ]]
then
printf " $COUNT) $yellow Repository: $reset[$repo]$red\n * [403] You are not have permission.$reset\n\n";
countFailed=$(( $countFailed + 1 ))
else
printf " $COUNT) $yellow Repository: $reset[$repo]$green\n * [200] Successfully deleted.$reset\n\n";
countSuccessfully=$(( $countSuccessfully + 1 ))
fi
COUNT=$(( $COUNT + 1 ))
sed -i '1,1 d' ${repositoriesFile}
done < $repositoriesFile
echo "---------------------"
echo "${yellow} Script has completed execution.${reset}"
echo "---------------------"
echo " Statistics: "
printf "$green - Successfully removed:$reset $countSuccessfully"
printf "$red\n - Failed removed:$reset $countFailed\n"
echo "---------------------"
printf "\n"