forked from Coderockr/coderockr-way-github-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coderockr-way-github-setup.bash
executable file
·145 lines (125 loc) · 3.67 KB
/
coderockr-way-github-setup.bash
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
function getHelp {
echo "Setup repository labels
--help, -h Show this help
--user, -u GitHub username
--password, -p GitHub password
--verbose, -v Details process
Usage:
$0 -u githubUser -p githubPassword owner/repo\n"
}
function github_api {
AUTHORIZATION="$GITHUB_USERNAME:$GITHUB_PASSWORD"
COMMAND='-u'
[ ! -z ${GITHUB_TOKEN+x} ] && {
AUTHORIZATION="Authorization: token $GITHUB_TOKEN"
COMMAND='-H'
}
curl $COMMAND "$AUTHORIZATION" -sL "https://api.github.com/repos/$GITHUB_REPO/$1" -X "$2" -d "$3"
}
VERBOSE=0
args=("$@")
for i in "$@"
do
if [ ! -z "$counter" ] && [[ "$counter" != "$readed_counter" ]]; then
counter=$[$counter + 1]
continue
fi
if [[ "$i" = "--help" ]] || [[ "$i" = "-h" ]]; then
printf "$(getHelp)"
exit 0
elif [[ $i == '--user' ]] || [[ $i == '-u' ]]; then
GITHUB_USERNAME=${args[$counter + 1]}
readed_counter=$[$readed_counter + 1]
elif [[ $i == '--password' ]] || [[ $i == '-p' ]]; then
GITHUB_PASSWORD=${args[$counter + 1]}
readed_counter=$[$readed_counter + 1]
elif [[ $i == '--verbose' ]] || [[ $i == '-v' ]]; then
VERBOSE=1
else
GITHUB_REPO=$i
fi
readed_counter=$[$readed_counter + 1]
counter=$[$counter + 1]
done
if [ -z "$GITHUB_REPO" ]; then
read -p "Type your Github repository name (owner/repo_name): " GITHUB_REPO
fi
if [ -z "$GITHUB_USERNAME" ] && [ -z $GITHUB_TOKEN ]; then
read -p "Type your Github username: " GITHUB_USERNAME
fi
if [ -z "$GITHUB_PASSWORD" ] && [ -z $GITHUB_TOKEN ]; then
read -p "Type your Github password (won't be shown): " -s GITHUB_PASSWORD
echo;
fi
if [ -z "$GITHUB_USERNAME" ] || [ -z "$GITHUB_REPO" ] && [ -z $GITHUB_TOKEN ]; then
>&2 echo "There are missing parameters !"
>&2 printf "$(getHelp)"
exit 1
fi
REMOVE_DEFAULT_LABELS='bug
documentation
duplicate
enhancement
good%20first%20issue
help%20wanted
invalid
question
wontfix'
LABELS='Category: Backend,c2e0c6
Category: Business/Meetings,0e8a16
Category: DevOps,fef2c0
Category: Frontend,bfdadc
Category: Infrastructure,f0e68c
Category: Report,40e0d0
Category: Unit test,ededed
Level: Easy,48d1cc
Level: Medium,20b2aa
Level: Hard,008b8b
Priority: High,fef2c0
Priority: Highest,b60205
Priority: Low,d4c5f9
Priority: Lowest,ededed
Priority: Medium,d4c5f9
Stage: Analysis,e6e6e6
Stage: Backlog,ededed
Stage: Cancelled,000000
Stage: In progress,fbca04
Stage: Review,0052cc
Stage: Testing,e616e6
Status: Blocked,d93f0b
Status: Duplicated,c5def5
Status: Impediment,b60205
Status: Needs Fixing,ff8c00
Type: Bug,fc2929
Type: Improvement,84b6eb
Type: New feature,0052cc
Type: Sub-task,ededed'
if [[ "$VERBOSE" == 1 ]]; then
echo "Removing default labels"
fi
while read -r label; do
response=$(github_api "labels/$label" DELETE)
if [[ "$response" == *"message"* ]]; then
if [[ ! "$response" == *"Not Found"* ]]; then
echo "Error removing \"$label\": $response"
fi
elif [[ "$VERBOSE" == 1 ]]; then
echo "Label \"$label\" removed"
fi
done <<< "$REMOVE_DEFAULT_LABELS"
if [[ "$VERBOSE" == 1 ]]; then
echo "Creating new labels"
fi
while read -r label; do
label_name=$(echo $label | cut -d , -f 1)
label_color=$(echo $label | cut -d , -f 2)
response=$(github_api labels POST "{\"name\": \"$label_name\", \"color\":\"$label_color\"}")
if [[ "$response" == *"errors"* ]]; then
if [[ ! "$response" == *"already_exists"* ]]; then
>&2 echo "Error on creating: $label_name, response: $response"
fi
elif [[ "$VERBOSE" == 1 ]]; then
echo "Label \"$label_name\" created"
fi
done <<< "$LABELS"