-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmigrate-repos-in-org.sh
393 lines (364 loc) · 14.7 KB
/
migrate-repos-in-org.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/bash
#################################################
# Migrate All repos in One Org to a Master Org #
# Used to help consolidate users Orgs #
# Can run in debug mode to show list of actions #
# #
# @admiralAwkbar #
#################################################
#
# Legend:
# This script is used to migrate repos from one organization
# to a master organization. This is done in org consolidations.
# It will transfer ownership to the new org. It can also set
# teams access in master org when the transfer is complete.
# You just need to set the teams ids in the script
# To run the script:
#
# - Update variables section in script
# - chmod +x script.sh
# - export GITHUB_TOKEN=YourGitHubTokenWithAccess
# - ./script.sh UsersOrg
#
# Script can be ran in debug mode as well to show what repos
# will be migrated
#
##############
# Debug Flag #
##############
DEBUG=1 # Debug Flag 0=execute 1=report
########
# VARS #
########
ORIG_ORG=$1 # Name of the Original GitHub Organization
UPDATE_TEAMS=1 # Update Teams access 0=skip 1=execute
MASTER_ORG='' # Name of the master Organization
#GITHUB_TOKEN='' # Token to authenticate into GitHub
GITHUB_URL="https://api.github.com" # URL to GitHub
READ_TEAM='' # ID of the GitHub team with read access
WRITE_TEAM='' # Team to add with write access to the repos
ADMIN_TEAM='' # ID of the GitHub team with Admin access
#################################
# Vars Set During Run of Script #
#################################
ORIG_ORG_REPOS= # Array of all the repositories in Organization
TEAM_IDS="$READ_TEAM,$ADMIN_TEAM" # String of all team ids to add to repos
ERROR_COUNT='0' # Total errors found
################################################################################
####################### SUB ROUTINES BELOW #####################################
################################################################################
################################################################################
#### Function CheckVars ########################################################
CheckVars()
{
# Validate we have Original Org name
if [[ -z $ORIG_ORG ]]; then
echo "ERROR: No Original Organization given!"
echo $0 <OriginalOrganizationName>
exit 1
fi
# Validate we have Master Org name
if [[ -z $MASTER_ORG ]]; then
echo "ERROR: No MASTER Organization given!"
echo "Please update scripts internal Variables!"
exit 1
fi
# Validate we have a token to connect
if [[ -z $GITHUB_TOKEN ]]; then
echo "ERROR: No GitHub Token given!"
echo "Please update scripts internal Variables! Or set env var: export GITHUB_TOKEN=YourToken"
exit 1
fi
################################
# Check if were updating teams #
################################
if [ $UPDATE_TEAMS -eq 0 ]; then
echo "Skippinig the update of team permissions"
else
# Validate we have a team to grant read access
if [[ -z $READ_TEAM ]]; then
echo "ERROR: No Read access team given!"
echo "Please update scripts internal Variables!"
exit 1
fi
# Validate we have a team to grant write access
if [[ -z $WRITE_TEAM ]]; then
echo "ERROR: No Write access team given!"
echo "Please update scripts internal Variables!"
exit 1
fi
# Validate we have a team to grant admin access
if [[ -z $ADMIN_TEAM ]]; then
echo "ERROR: No Admin access team given!"
echo "Please update scripts internal Variables!"
exit 1
fi
fi
}
################################################################################
#### Function GetTeamIds #######################################################
GetTeamIds()
{
#################################################
# Need to get the team id from team name passed #
#################################################
REGEX='^[0-9]+$'
# Check if team was passed as number
if [[ $WRITE_TEAM =~ $REGEX ]]; then
echo "Team ID passed, adding to list"
TEAM_IDS+=",$WRITE_TEAM"
else
echo "Need to convert TeamName into ID"
TEAM_RESPONSE=$(curl --request GET \
--url $GITHUB_URL/orgs/$ORIG_ORG/teams \
--header 'accept: application/vnd.github.hellcat-preview+json' \
--header "authorization: token $GITHUB_TOKEN")
# Get the team id
get_team_id()
{
echo ${TEAM_RESPONSE} | base64 --decode | jq -r ${1}
#echo ${TEAM_RESPONSE} | base64 --decode --ignore-garbage | jq -r ${1} # Need ignore garbae on windows machines
}
# Get the id of the team
TEAM_ID=$(get_team_id '.id')
echo "TeamId:[$TEAM_ID]"
TEAM_IDS+=",$TEAM_ID"
# Reset the global to the id
WRITE_TEAM=$TEAM_ID
fi
}
################################################################################
#### Function UpdateTeamPermission #############################################
UpdateTeamPermission()
{
# need to add the teams permissions to the repo
REPO_TO_UPDATE_PERMS=$1
# https://developer.github.com/v3/teams/#edit-team
# PUT /teams/:team_id/repos/:owner/:repo
###################################
# Update the Read Permission Team #
###################################
echo "-----------------------------------------------------"
echo "Setting Read Team Permissions"
curl -s --request PUT \
--url $GITHUB_URL/teams/$READ_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json' \
--header 'application/vnd.github.hellcat-preview+json' \
--data \"{\"permission\": \"pull\"}\"
########################
# Check for any errors #
########################
if [ $? -ne 0 ]; then
echo "Error! Failed to set permission"
((ERROR_COUNT++))
fi
####################################
# Update the Write Permission Team #
####################################
echo "-----------------------------------------------------"
echo "Setting Write Team Permissions"
curl -s --request PUT \
--url $GITHUB_URL/teams/$WRITE_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json' \
--header 'application/vnd.github.hellcat-preview+json' \
--data \"{\"permission\": \"push\"}\"
########################
# Check for any errors #
########################
if [ $? -ne 0 ]; then
echo "Error! Failed to set permission"
((ERROR_COUNT++))
fi
####################################
# Update the Admin Permission Team #
####################################
echo "-----------------------------------------------------"
echo "Setting Admin Team Permissions"
curl -s --request PUT \
--url $GITHUB_URL/teams/$ADMIN_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json' \
--header 'application/vnd.github.hellcat-preview+json' \
--data \"{\"permission\": \"admin\"}\"
########################
# Check for any errors #
########################
if [ $? -ne 0 ]; then
echo "Error! Failed to set permission"
((ERROR_COUNT++))
fi
}
################################################################################
#### Function GetOrigOrgRepos ##################################################
GetOrigOrgRepos()
{
##############################
# Get response with all info #
##############################
echo "-----------------------------------------------------"
echo "Gathering all repos from Original Organization:[$ORIG_ORG]"
ORIG_ORG_RESPONSE=$(curl -s --request GET \
--url $GITHUB_URL/orgs/$ORIG_ORG/repos \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json')
#######################################################
# Loop through list of repos in original organization #
#######################################################
echo "-----------------------------------------------------"
echo "Parsing repo names from Original Organization:"
for orig_repo in $(echo "${ORIG_ORG_RESPONSE}" | jq -r '.[] | @base64');
do
# Pull the name of the repo out
get_orig_repo_name()
{
echo ${orig_repo} | base64 --decode | jq -r ${1}
#echo ${orig_repo} | base64 --decode --ignore-garbage | jq -r ${1} # Need ignore garbage on windows machines
}
# Get the name of the repo
ORIG_REPO_NAME=$(get_orig_repo_name '.name')
echo "Name:[$ORIG_REPO_NAME]"
ORIG_ORG_REPOS+=($ORIG_REPO_NAME)
done
}
################################################################################
#### Function MigrateRepos #####################################################
MigrateRepos()
{
########################################
# Migrate all the repos to the new org #
########################################
echo "-----------------------------------------------------"
echo "Migrating Reposities to master Organization:[$MASTER_ORG]"
for new_repo in ${ORIG_ORG_REPOS[@]};
do
#######################################
# Call the single repo to be migrated #
#######################################
if [ $DEBUG -eq 0 ]; then
if [ $UPDATE_TEAMS -eq 0 ]; then
##########################################
# Migrating repos without updating teams #
##########################################
echo "-----------------------------------------------------"
echo "Skipping updating teams"
echo "Migrating Repo:[$new_repo] to:[$MASTER_ORG/$new_repo]"
#####################################
# Call GitHub =API to transfer repo #
#####################################
curl -s --request POST \
--url $GITHUB_URL/repos/$ORIG_ORG/$new_repo/transfer \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json' \
--header 'application/vnd.github.nightshade-preview+json' \
--data \"{\"new_owner\": \"$MASTER_ORG\"}\"
########################
# Check for any errors #
########################
if [ $? -ne 0 ]; then
echo "Error! Failed to migrate repo"
((ERROR_COUNT++))
fi
else
######################################
# Migrating repos and updating teams #
######################################
echo "-----------------------------------------------------"
echo "Migrating Repo:[$new_repo] to:[$MASTER_ORG/$new_repo]"
#####################################
# Call GitHub =API to transfer repo #
#####################################
curl -s --request POST \
--url $GITHUB_URL/repos/$ORIG_ORG/$new_repo/transfer \
--header "authorization: Bearer $GITHUB_TOKEN" \
--header 'content-type: application/json' \
--header 'application/vnd.github.nightshade-preview+json' \
--data \"{\"new_owner\": \"$MASTER_ORG\", \"team_ids\": [ $TEAM_IDS ]}\"
########################
# Check for any errors #
########################
if [ $? -ne 0 ]; then
echo "Error! Failed to migrate repo"
((ERROR_COUNT++))
fi
###########################
# Update Team permissions #
###########################
UpdateTeamPermission $new_repo
fi
else
# Debug loop to print results
echo "DEBUG: Would have moved:[$new_repo] to:[$MASTER_ORG/$new_repo]"
fi
done
}
################################################################################
#### Function Footer ###########################################################
Footer()
{
####################
# Print the footer #
####################
echo "-----------------------------------------------------"
echo "the script has completed"
exit $ERROR_COUNT
}
################################################################################
#### Function Header ###########################################################
Header()
{
#####################
# Print Header Info #
#####################
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo "----- Migrate Repos from user Org to Master Org -----"
echo "-----------------------------------------------------"
echo "-----------------------------------------------------"
echo ""
echo "Migrating All Repositories from Org:[$ORIG_ORG]"
echo "Moving all Repositories to Org:[$MASTER_ORG]"
##############
# Debug info #
##############
if [ $DEBUG -eq 1 ]; then
echo "Running in DEBUG mode! Will only report Repositories that will be migrated"
else
echo "Running in Execute mode! Will migrate all repositories"
fi
#############
# Team Info #
#############
if [ $UPDATE_TEAMS -eq 1 ]; then
echo "Updating Repositories teams when migrating"
else
echo "No teams will be assigned during the migration process"
fi
echo ""
}
################################################################################
################################################################################
############################## MAIN ############################################
################################################################################
################################################################################
#######################################################
# Checking that all variables were passed in properly #
#######################################################
CheckVars
########################################
# Get all repositories in Original Org #
########################################
GetOrigOrgRepos
####################################################
# Get a list of all teamIds for the repo migration #
####################################################
GetTeamIds
###############################################
# Migrate Repositories to master organization #
###############################################
MigrateRepos
####################
# Print the footer #
####################
Footer