Skip to content

Commit 36415e0

Browse files
adding script to migrate repos
1 parent 7148223 commit 36415e0

File tree

1 file changed

+391
-0
lines changed

1 file changed

+391
-0
lines changed

api/bash/migrate-repos-in-org.sh

Lines changed: 391 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,391 @@
1+
#!/usr/bin/bash
2+
3+
#################################################
4+
# Migrate All repos in One Org to a Master Org #
5+
# Used to help consolidate users Orgs #
6+
# Can run in debug mode to show list of actions #
7+
# #
8+
# @admiralAwkbar #
9+
#################################################
10+
11+
#
12+
# Legend:
13+
# This script is used to migrate repos from one organization
14+
# to a master organization. This is done in org consolidations.
15+
# It will transfer ownership to the new org. It can be ran in debug
16+
# mode to show what repos will be migrated. It can also set
17+
# teams access in master org when the transfer is complete.
18+
# You Just need to set the teams ids in the script
19+
# To run the script:
20+
#
21+
# - Update vareiables section in script
22+
# - chmod +x script.sh
23+
# - export GITHUB_TOKEN=YourGitHubTokenWithAccess
24+
# - ./script.sh UsersOrg
25+
#
26+
# Script can be ran in debug mode as well to show what repos
27+
# will be migrated
28+
#
29+
30+
##############
31+
# Debug Flag #
32+
##############
33+
DEBUG=1 # Debug Flag 0=execute 1=report
34+
35+
########
36+
# VARS #
37+
########
38+
ORIG_ORG=$1 # Name of the users GitHub Organization
39+
UPDATE_TEAMS=1 # UPdate Teams access 0=skip 1=execute
40+
MASTER_ORG='' # Name of the master Organization
41+
GITHUB_TOKEN='' # Token to authenticate into GitHub
42+
GITHUB_URL="https://api.github.com" # URL to GitHub
43+
READ_TEAM='' # ID of the GitHub team with read access
44+
WRITE_TEAM='' # Team to add with write access to the repos
45+
ADMIN_TEAM='' # ID of the GitHub team with Admin access
46+
47+
#################################
48+
# Vars Set During Run of Script #
49+
#################################
50+
ORIG_ORG_REPOS= # Array of all the repositories in Organization
51+
TEAM_IDS="$READ_TEAM,$ADMIN_TEAM" # String of all team ids to add to repos
52+
ERROR_COUNT='0' # Total errors found
53+
54+
################################################################################
55+
####################### SUB ROUTINES BELOW #####################################
56+
################################################################################
57+
################################################################################
58+
#### Function CheckVars ########################################################
59+
CheckVars()
60+
{
61+
# Validate we have Original Org name
62+
if [[ -z $ORIG_ORG ]]; then
63+
echo "ERROR: No Original Organization given!"
64+
echo $0 <OriginalOrganizationName>
65+
exit 1
66+
fi
67+
68+
# Validate we have Master Org name
69+
if [[ -z $MASTER_ORG ]]; then
70+
echo "ERROR: No MASTER Organization given!"
71+
echo "Please update scripts internal Variables!"
72+
exit 1
73+
fi
74+
75+
# Validate we have a token to connect
76+
if [[ -z $GITHUB_TOKEN ]]; then
77+
echo "ERROR: No GitHub Token given!"
78+
echo "Please update scripts internal Variables! Or set env var: export GITHUB_TOKEN=YourToken"
79+
exit 1
80+
fi
81+
82+
################################
83+
# Check if were updating teams #
84+
################################
85+
if [ $UPDATE_TEAMS -eq 0 ]; then
86+
echo "Skippinig the update of team permissions"
87+
else
88+
# Validate we have a team to grant read access
89+
if [[ -z $READ_TEAM ]]; then
90+
echo "ERROR: No Read access team given!"
91+
echo "Please update scripts internal Variables!"
92+
exit 1
93+
fi
94+
95+
# Validate we have a team to grant write access
96+
if [[ -z $WRITE_TEAM ]]; then
97+
echo "ERROR: No Write access team given!"
98+
echo "Please update scripts internal Variables!"
99+
exit 1
100+
fi
101+
102+
# Validate we have a team to grant admin access
103+
if [[ -z $ADMIN_TEAM ]]; then
104+
echo "ERROR: No Admin access team given!"
105+
echo "Please update scripts internal Variables!"
106+
exit 1
107+
fi
108+
fi
109+
}
110+
################################################################################
111+
#### Function GetTeamIds #######################################################
112+
GetTeamIds()
113+
{
114+
#################################################
115+
# Need to get the team id from team name passed #
116+
#################################################
117+
REGEX='^[0-9]+$'
118+
# Check if team was passed as number
119+
if [[ $WRITE_TEAM =~ $REGEX ]]; then
120+
echo "Team ID passed, adding to list"
121+
TEAM_IDS+=",$WRITE_TEAM"
122+
else
123+
echo "Need to convert TeamName into ID"
124+
TEAM_RESPONSE=$(curl --request GET \
125+
--url $GITHUB_URL/orgs/$ORIG_ORG/teams \
126+
--header 'accept: application/vnd.github.hellcat-preview+json' \
127+
--header "authorization: token $GITHUB_TOKEN")
128+
129+
# Get the team id
130+
get_team_id()
131+
{
132+
echo ${TEAM_RESPONSE} | base64 --decode --ignore-garbage | jq -r ${1}
133+
}
134+
135+
# Get the id of the team
136+
TEAM_ID=$(get_team_id '.id')
137+
echo "TeamId:[$TEAM_ID]"
138+
TEAM_IDS+=",$TEAM_ID"
139+
# Reset the global to the id
140+
WRITE_TEAM=$TEAM_ID
141+
fi
142+
}
143+
################################################################################
144+
#### Function UpdateTeamPermission #############################################
145+
UpdateTeamPermission()
146+
{
147+
# need to add the teams permissions to the repo
148+
REPO_TO_UPDATE_PERMS=$1
149+
# PUT /teams/:team_id/repos/:owner/:repo
150+
151+
###################################
152+
# Update the Read Permission Team #
153+
###################################
154+
echo "-----------------------------------------------------"
155+
echo "Setting Read Team Permissions"
156+
curl -s --request PUT \
157+
--url $GITHUB_URL/teams/$READ_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
158+
--header "authorization: Bearer $GITHUB_TOKEN" \
159+
--header 'content-type: application/json' \
160+
--header 'application/vnd.github.hellcat-preview+json' \
161+
--data \"{\"permission\": \"pull\"}\"
162+
163+
########################
164+
# Check for any errors #
165+
########################
166+
if [ $? -ne 0 ]; then
167+
echo "Error! Failed to set permission"
168+
((ERROR_COUNT++))
169+
fi
170+
171+
####################################
172+
# Update the Write Permission Team #
173+
####################################
174+
echo "-----------------------------------------------------"
175+
echo "Setting Write Team Permissions"
176+
curl -s --request PUT \
177+
--url $GITHUB_URL/teams/$WRITE_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
178+
--header "authorization: Bearer $GITHUB_TOKEN" \
179+
--header 'content-type: application/json' \
180+
--header 'application/vnd.github.hellcat-preview+json' \
181+
--data \"{\"permission\": \"push\"}\"
182+
183+
########################
184+
# Check for any errors #
185+
########################
186+
if [ $? -ne 0 ]; then
187+
echo "Error! Failed to set permission"
188+
((ERROR_COUNT++))
189+
fi
190+
191+
####################################
192+
# Update the Admin Permission Team #
193+
####################################
194+
echo "-----------------------------------------------------"
195+
echo "Setting Admin Team Permissions"
196+
curl -s --request PUT \
197+
--url $GITHUB_URL/teams/$ADMIN_TEAM/repos/$MASTER_ORG/$REPO_TO_UPDATE_PERMS \
198+
--header "authorization: Bearer $GITHUB_TOKEN" \
199+
--header 'content-type: application/json' \
200+
--header 'application/vnd.github.hellcat-preview+json' \
201+
--data \"{\"permission\": \"admin\"}\"
202+
203+
########################
204+
# Check for any errors #
205+
########################
206+
if [ $? -ne 0 ]; then
207+
echo "Error! Failed to set permission"
208+
((ERROR_COUNT++))
209+
fi
210+
}
211+
################################################################################
212+
#### Function GetOrigOrgRepos ##################################################
213+
GetOrigOrgRepos()
214+
{
215+
##############################
216+
# Get response with all info #
217+
##############################
218+
echo "-----------------------------------------------------"
219+
echo "Gathering all repos from Original Organization:[$ORIG_ORG]"
220+
ORIG_ORG_RESPONSE=$(curl -s --request GET \
221+
--url $GITHUB_URL/orgs/$ORIG_ORG/repos \
222+
--header "authorization: Bearer $GITHUB_TOKEN" \
223+
--header 'content-type: application/json')
224+
225+
#######################################################
226+
# Loop through list of repos in original organization #
227+
#######################################################
228+
echo "-----------------------------------------------------"
229+
echo "Parsing repo names from Original Organization:"
230+
for orig_repo in $(echo "${ORIG_ORG_RESPONSE}" | jq -r '.[] | @base64');
231+
do
232+
# Pull the name of the repo out
233+
get_orig_repo_name()
234+
{
235+
echo ${orig_repo} | base64 --decode --ignore-garbage | jq -r ${1}
236+
}
237+
238+
# Get the name of the repo
239+
ORIG_REPO_NAME=$(get_orig_repo_name '.name')
240+
echo "Name:[$ORIG_REPO_NAME]"
241+
ORIG_ORG_REPOS+=($ORIG_REPO_NAME)
242+
done
243+
}
244+
################################################################################
245+
#### Function MigrateRepos #####################################################
246+
MigrateRepos()
247+
{
248+
########################################
249+
# Migrate all the repos to the new org #
250+
########################################
251+
echo "-----------------------------------------------------"
252+
echo "Migrating Reposities to master Organization:[$MASTER_ORG]"
253+
for new_repo in ${ORIG_ORG_REPOS[@]};
254+
do
255+
#######################################
256+
# Call the single repo to be migrated #
257+
#######################################
258+
if [ $DEBUG -eq 0 ]; then
259+
if [ $UPDATE_TEAMS -eq 0 ]; then
260+
##########################################
261+
# Migrating repos without updating teams #
262+
##########################################
263+
echo "-----------------------------------------------------"
264+
echo "Skipping updating teams"
265+
echo "Migrating Repo:[$new_repo] to:[$MASTER_ORG/$new_repo]"
266+
#####################################
267+
# Call GitHub =API to transfer repo #
268+
#####################################
269+
curl -s --request POST \
270+
--url $GITHUB_URL/repos/$ORIG_ORG/$new_repo/transfer \
271+
--header "authorization: Bearer $GITHUB_TOKEN" \
272+
--header 'content-type: application/json' \
273+
--header 'application/vnd.github.nightshade-preview+json' \
274+
--data \"{\"new_owner\": \"$MASTER_ORG\"}\"
275+
276+
########################
277+
# Check for any errors #
278+
########################
279+
if [ $? -ne 0 ]; then
280+
echo "Error! Failed to migrate repo"
281+
((ERROR_COUNT++))
282+
fi
283+
else
284+
######################################
285+
# Migrating repos and updating teams #
286+
######################################
287+
echo "-----------------------------------------------------"
288+
echo "Migrating Repo:[$new_repo] to:[$MASTER_ORG/$new_repo]"
289+
#####################################
290+
# Call GitHub =API to transfer repo #
291+
#####################################
292+
curl -s --request POST \
293+
--url $GITHUB_URL/repos/$ORIG_ORG/$new_repo/transfer \
294+
--header "authorization: Bearer $GITHUB_TOKEN" \
295+
--header 'content-type: application/json' \
296+
--header 'application/vnd.github.nightshade-preview+json' \
297+
--data \"{\"new_owner\": \"$MASTER_ORG\", \"team_ids\": [ $TEAM_IDS ]}\"
298+
299+
########################
300+
# Check for any errors #
301+
########################
302+
if [ $? -ne 0 ]; then
303+
echo "Error! Failed to migrate repo"
304+
((ERROR_COUNT++))
305+
fi
306+
307+
###########################
308+
# Update Team permissions #
309+
###########################
310+
UpdateTeamPermission $new_repo
311+
fi
312+
else
313+
# Debug loop to print results
314+
echo "DEBUG: Would have moved:[$new_repo] to:[$MASTER_ORG/$new_repo]"
315+
fi
316+
done
317+
}
318+
################################################################################
319+
#### Function Footer ###########################################################
320+
Footer()
321+
{
322+
####################
323+
# Print the footer #
324+
####################
325+
echo "-----------------------------------------------------"
326+
echo "the script has completed"
327+
exit $ERROR_COUNT
328+
}
329+
################################################################################
330+
#### Function Header ###########################################################
331+
Header()
332+
{
333+
#####################
334+
# Print Header Info #
335+
#####################
336+
echo "-----------------------------------------------------"
337+
echo "-----------------------------------------------------"
338+
echo "----- Migrate Repos from user Org to Master Org -----"
339+
echo "-----------------------------------------------------"
340+
echo "-----------------------------------------------------"
341+
echo ""
342+
echo "Migrating All Repositories from Org:[$ORIG_ORG]"
343+
echo "Moving all Repositories to Org:[$MASTER_ORG]"
344+
##############
345+
# Debug info #
346+
##############
347+
if [ $DEBUG -eq 1 ]; then
348+
echo "Running in DEBUG mode! Will only report Repositories that will be migrated"
349+
else
350+
echo "Running in Execute mode! Will migrate all repositories"
351+
fi
352+
#############
353+
# Team Info #
354+
#############
355+
if [ $UPDATE_TEAMS -eq 1 ]; then
356+
echo "Updating Repositories teams when migrating"
357+
else
358+
echo "No teams will be set during the migration process"
359+
fi
360+
echo ""
361+
}
362+
################################################################################
363+
################################################################################
364+
############################## MAIN ############################################
365+
################################################################################
366+
################################################################################
367+
368+
#######################################################
369+
# Checking that all variables were passed in properly #
370+
#######################################################
371+
CheckVars
372+
373+
########################################
374+
# Get all repositories in Original Org #
375+
########################################
376+
GetOrigOrgRepos
377+
378+
####################################################
379+
# Get a list of all teamIds for the repo migration #
380+
####################################################
381+
GetTeamIds
382+
383+
###############################################
384+
# Migrate Repositories to master organization #
385+
###############################################
386+
MigrateRepos
387+
388+
####################
389+
# Print the footer #
390+
####################
391+
Footer

0 commit comments

Comments
 (0)