-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-repo.sh
253 lines (227 loc) · 9.4 KB
/
create-repo.sh
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env bash
usage() {
echo "Create repo under this org"
echo
echo "Usage: $PROGNAME [options]..."
echo
echo "Options:"
echo
echo " -h, --help"
echo " This help text."
echo
echo " -r , --repo <repo name in the form of org/repo >"
echo
echo " -o , --owner <GitHub Owner ID such as Jane-Doe_ghub>"
echo
echo " -g , --owner-org <GitHub Org ID such as BY-Product-Development>"
echo
echo " -t , --token <GitHub Dev token>"
echo
echo " -s , --create-team-set <Create team set from the prefix>"
echo
echo " -d , --team-description-prefix <The prefix of the team description>"
echo
echo " -v , --verbose <Run in verbose mode>"
echo
echo " --"
echo " Do not interpret any more arguments as options."
echo
}
VERBOSiTY="-sS"
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
usage
exit 0
;;
-r|--repo)
REPO_NAME="$2"
;;
-d|--team-description-prefix)
TEAM_PREFIX="$2"
;;
-o|--owner)
REQUESTOR_USER="$2"
;;
-g|--organization)
ORG="$2"
;;
-t|--token)
TOKEN="$2"
;;
-s|--team-slug-prefix)
CREATE_TEAMSET=true
;;
-v|--verbose)
VERBOSiTY="-v"
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
github_cmd(){
local TOKEN=$1
local URL=$2
local PAYLOAD=$3
if [[ -z ${PAYLOAD} ]]
then
RESULT=$(curl ${VERBOSiTY} -H "Authorization: token ${TOKEN}" $URL)
else
RESULT=$(curl ${VERBOSiTY} -H "Authorization: token ${TOKEN}" -d "$PAYLOAD" $URL)
fi
echo ${RESULT}
}
github_post_cmd(){
local TOKEN=$1
local URL=$2
local PAYLOAD=$3
if [[ -z ${PAYLOAD} ]]
then
RESULT=$(curl ${VERBOSiTY} -X POST -H "Authorization: token ${TOKEN}" $URL)
else
RESULT=$(curl ${VERBOSiTY} -X POST -H "Accept: application/vnd.github.luke-cage-preview+json" -H "Authorization: token ${TOKEN}" -d "$PAYLOAD" $URL)
fi
echo ${RESULT}
}
github_update_cmd(){
local TOKEN=$1
local URL=$2
local PAYLOAD=$3
if [[ -z ${PAYLOAD} ]]
then
RESULT=$(curl ${VERBOSiTY} -X PUT -H "Authorization: token ${TOKEN}" $URL)
else
RESULT=$(curl ${VERBOSiTY} -X PUT -H "Accept: application/vnd.github.luke-cage-preview+json" -H "Authorization: token ${TOKEN}" -d "$PAYLOAD" $URL)
fi
echo ${RESULT}
}
github_delete_cmd(){
local TOKEN=$1
local URL=$2
local PAYLOAD=$3
if [[ -z ${PAYLOAD} ]]
then
RESULT=$(curl ${VERBOSiTY} -X DELETE -H "Authorization: token ${TOKEN}" $URL)
else
RESULT=$(curl ${VERBOSiTY} -X DELETE -H "Authorization: token ${TOKEN}" -d "$PAYLOAD" $URL)
fi
echo ${RESULT}
}
ADMIN_TEAM="$TEAM_PREFIX Admins"
TEAM_SLUG_PREFIX=$(echo $TEAM_PREFIX | sed 's/ /-/g;s/.*/\L&/g')
ADMIN_TEAM_SLUG_QRY="${TEAM_SLUG_PREFIX}-admins"
COLLABORATORS_TEAM="$TEAM_PREFIX Collaborators"
COLLABORATORS_TEAM_SLUG_QRY="${TEAM_SLUG_PREFIX}-collaborators"
CONTRIBUTORS_TEAM="$TEAM_PREFIX Contributors"
CONTRIBUTORS_TEAM_SLUG_QRY="${TEAM_SLUG_PREFIX}-contributors"
echo ADMIN_TEAM=$ADMIN_TEAM
echo COLLABORATORS_TEAM=$COLLABORATORS_TEAM
echo CONTRIBUTORS_TEAM=$CONTRIBUTORS_TEAM
echo TEAM_SLUG_PREFIX=$TEAM_SLUG_PREFIX
ORG="BY-Product-Development"
EMPLOYEES_TEAM="associates-pd"
PROXIED_USER=`github_cmd $TOKEN https://api.github.com/user | jq -r '.login'`
echo PROXIED_USER=$REQUESTOR_USER
echo REPO_NAME=$REPO_NAME
# Delete the repository
# github_delete_cmd $TOKEN https://api.github.com/repos/${ORG}/${REPO_NAME}
# Create repository
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/repos '{"auto_init":true,"private":true,"name":"'"$REPO_NAME"'"}'
# Apply branch protection
REPO_ID=$(echo $RESULT | jq '.id')
if [[ "$REPO_ID" = "null" ]]
then
>&2 echo "Repository ${REPO_NAME} already exists"
return 1
fi
REPO_URL=$(echo $RESULT | jq -r '.html_url')
echo RESULT=$RESULT
echo REPO_ID=$REPO_ID
# Create teams
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/everyone
EVERYONE_TEAM_ID=$(echo $RESULT | jq '.id')
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/associates-pd
EMPLOYEES_TEAM_ID=$(echo $RESULT | jq '.id')
echo EVERYONE_TEAM_ID=$EVERYONE_TEAM_ID
echo EMPLOYEES_TEAM_ID=$EMPLOYEES_TEAM_ID
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams
if [[ "$CREATE_TEAMSET" != true ]]
then
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG_QRY}
else
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams '{"name":"'"$CONTRIBUTORS_TEAM"'","parent_team_id":'$EVERYONE_TEAM_ID',"permission":"pull"}'
fi
CONTRIBUTORS_TEAM_ID=$(echo $RESULT | jq -r '.id | select (.!=null)')
CONTRIBUTORS_TEAM_SLUG="$(echo $RESULT | jq -r '.slug')"
if [[ -z ${CONTRIBUTORS_TEAM_ID} ]]
then
echo "$TEAM_PREFIX" teamset not found. Please choose an approve teamset.
exit 1;
fi
echo CONTRIBUTORS_TEAM_ID=$CONTRIBUTORS_TEAM_ID
echo CONTRIBUTORS_TEAM_SLUG="$CONTRIBUTORS_TEAM_SLUG"
if [[ "$CREATE_TEAMSET" != true ]]
then
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG_QRY}
else
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams '{"name":"'"$COLLABORATORS_TEAM"'","parent_team_id":'$CONTRIBUTORS_TEAM_ID',"permission":"pull"}'
fi
COLLABORATORS_TEAM_ID=$(echo $RESULT | jq '.id')
COLLABORATORS_TEAM_SLUG="$(echo $RESULT | jq -r '.slug')"
echo COLLABORATORS_TEAM_ID=$COLLABORATORS_TEAM_ID
echo COLLABORATORS_TEAM_SLUG="$COLLABORATORS_TEAM_SLUG"
if [[ "$CREATE_TEAMSET" != true ]]
then
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG_QRY}
else
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams '{"name":"'"$ADMIN_TEAM"'","parent_team_id":'$COLLABORATORS_TEAM_ID',"permission":"pull"}'
fi
ADMIN_TEAM_ID=$(echo $RESULT | jq '.id')
ADMIN_TEAM_SLUG="$(echo $RESULT | jq -r '.slug')"
echo ADMIN_TEAM_ID=$ADMIN_TEAM_ID
echo ADMIN_TEAM_SLUG="$ADMIN_TEAM_SLUG"
# Make sure all employees can create pull requests
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${EMPLOYEES_TEAM}/repos/${ORG}/${REPO_NAME} '{"permission":"triage"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG}/repos/${ORG}/${REPO_NAME} '{"permission":"admin"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG}/repos/${ORG}/${REPO_NAME} '{"permission":"maintain"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG}/repos/${ORG}/${REPO_NAME} '{"permission":"push"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${EMPLOYEES_TEAM}/repositories/${REPO_NAME} '{"permission":"triage"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG}/repositories/${REPO_NAME} '{"permission":"admin"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG}/repositories/${REPO_NAME} '{"permission":"maintain"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG}/repositories/${REPO_NAME} '{"permission":"push"}'
echo +++ Get currenrt protection +++
#github_cmd $TOKEN https://api.github.com/repos/${ORG}/${REPO_NAME}/branches/main/protection
github_update_cmd $TOKEN https://api.github.com/repos/${ORG}/${REPO_NAME}/branches/main/protection "{\"restrictions\":{\"teams\":[\"${COLLABORATORS_TEAM_SLUG}\"],\"users\":[]}, \"enforce_admins\":true,\"required_status_checks\":null,\"required_pull_request_reviews\":{\"dismiss_stale_reviews\":true,\"require_code_owner_reviews\":true,\"required_approving_review_count\":1,\"restrictions\":{\"users\":[],\"teams\":[\"${COLLABORATORS_TEAM_SLUG}\"]}}"
github_update_cmd $TOKEN https://api.github.com/repos/${ORG}/${REPO_NAME}/branches/main/protection/restrictions/teams "[\"${COLLABORATORS_TEAM_SLUG}\"]"
if [[ "$CREATE_TEAMSET" = true ]]
then
# Make requester owner
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG}/memberships/${REQUESTOR_USER} '{"role":"maintainer"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG}/memberships/${REQUESTOR_USER} '{"role":"maintainer"}'
github_update_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG}/memberships/${REQUESTOR_USER} '{"role":"maintainer"}'
# Remove proxied user
github_delete_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG}/memberships/${PROXIED_USER}
github_delete_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG}/memberships/${PROXIED_USER}
github_delete_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG}/memberships/${PROXIED_USER}
fi
# Get maintainers
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${CONTRIBUTORS_TEAM_SLUG_QRY}/members?role=maintainer
export CONTIRIBUTORS_MAINTAINERS=$(echo $(echo $RESULT | jq -r '.[] | .login' ) | tr ' ' ', ')
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${COLLABORATORS_TEAM_SLUG_QRY}/members?role=maintainer
export COLLABORATORS_MAINTAINERS=$(echo $(echo $RESULT | jq -r '.[] | .login' ) | tr ' ' ', ')
github_cmd $TOKEN https://api.github.com/orgs/${ORG}/teams/${ADMIN_TEAM_SLUG_QRY}/members?role=maintainer
export ADMIN_MAINTAINERS=$(echo $(echo $RESULT | jq -r '.[] | .login' ) | tr ' ' ', ')
echo CONTIRIBUTORS_MAINTAINERS=$CONTIRIBUTORS_MAINTAINERS
echo COLLABORATORS_MAINTAINERS=$COLLABORATORS_MAINTAINERS
echo ADMIN_MAINTAINERS=$ADMIN_MAINTAINERS
export CREATE_REPO_RESULT=${REPO_URL}
echo -e "Repo URL is \n${REPO_URL}"