|
| 1 | +#!/bin/bash |
| 2 | +################ |
| 3 | +# collision.sh # |
| 4 | +################ |
| 5 | +######################################### |
| 6 | +# Collision detection script to verify # |
| 7 | +# That all Repos in an Org do NOT exist # |
| 8 | +# in the master Organization # |
| 9 | +# # |
| 10 | +# @admiralAwkbar # |
| 11 | +######################################### |
| 12 | + |
| 13 | +# |
| 14 | +# Legend: |
| 15 | +# This script is used to see if repo names from one organization |
| 16 | +# are found in another ogranization. This is helpful |
| 17 | +# when your consolidating organizations into a single Org |
| 18 | +# |
| 19 | +# To run the script: |
| 20 | +# - chmod +x script.sh |
| 21 | +# - export GITHUB_TOKEN=YourGithubTokenWithAccessToBothOrgs |
| 22 | +# - ./script OriginalOrg MasterOrg |
| 23 | +# |
| 24 | +# The script will come back with a list of any repos that have a |
| 25 | +# name collision that will cause errors in a migration process |
| 26 | +# |
| 27 | + |
| 28 | + |
| 29 | +######## |
| 30 | +# VARS # |
| 31 | +######## |
| 32 | +ORIG_ORG=$1 # Name of the users GitHub Organization |
| 33 | +MASTER_ORG=$2 # Name of the master Organization |
| 34 | +GITHUB_TOKEN='' # Token to authenticate into GitHub |
| 35 | + |
| 36 | +###################################### |
| 37 | +# Will be set when the script is ran # |
| 38 | +###################################### |
| 39 | +ORIG_ORG_REPOS= # Array of all the repositories in Organization |
| 40 | +MASTER_ORG_REPOS= # Array of all the repositories in Organization |
| 41 | +COLLISION_REPOS= # Repos that will have a collision |
| 42 | + |
| 43 | +################################################################################ |
| 44 | +######################## SUB ROUTINES BELOW #################################### |
| 45 | +################################################################################ |
| 46 | +################################################################################ |
| 47 | +#### Sub Routine ValidateInput ################################################# |
| 48 | +ValidateInput() |
| 49 | +{ |
| 50 | + ########################################### |
| 51 | + # Need to make sure we have all varaibles # |
| 52 | + ########################################### |
| 53 | + # Validate we have Original Organization Name |
| 54 | + if [[ -z $ORIG_ORG ]]; then |
| 55 | + echo "ERROR: No Original Organization given!" |
| 56 | + echo $0 <Original_Organization_Name> <Master_Organization_Name> |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + # Validate we have Master Organization Name |
| 61 | + if [[ -z $MASTER_ORG ]]; then |
| 62 | + echo "ERROR: No Master Organization given!" |
| 63 | + echo $0 <Original_Organization_Name> <Master_Organization_Name> |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + # Validate we have a token to connect to GitHub |
| 68 | + if [[ -z $GITHUB_TOKEN ]]; then |
| 69 | + echo "ERROR: No GitHub Token given!" |
| 70 | + echo "Please update script with GitHub token or place token in the environment" |
| 71 | + echo "Example: Comment out line GITHUB_TOKEN='' and then export GITHUB_TOKEN=YourToken" |
| 72 | + echo $0 <Original_Organization_Name> <Master_Organization_Name> |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +} |
| 76 | +################################################################################ |
| 77 | +#### Sub Routine Header ######################################################## |
| 78 | +Header() |
| 79 | +{ |
| 80 | + ############################### |
| 81 | + # Print the basic header info # |
| 82 | + ############################### |
| 83 | + echo "-----------------------------------------------------" |
| 84 | + echo "------ GitHub Repo Collision Detection Script -------" |
| 85 | + echo "-- Validating Repo name not found inside master Org -" |
| 86 | + echo "-----------------------------------------------------" |
| 87 | + echo "" |
| 88 | + echo "Original Organization:[$ORIG_ORG]" |
| 89 | + echo "Master Organization:[$MASTER_ORG]" |
| 90 | + echo "" |
| 91 | +} |
| 92 | +################################################################################ |
| 93 | +#### Sub Routine GetOrigOrgInfo ################################################ |
| 94 | +GetOrigOrgInfo() |
| 95 | +{ |
| 96 | + #################################### |
| 97 | + # Get all repositories in User Org # |
| 98 | + #################################### |
| 99 | + echo "-----------------------------------------------------" |
| 100 | + echo "Gathering all repos from Original Organization:[$ORIG_ORG]" |
| 101 | + ORIG_ORG_RESPONSE=$(curl -s --request GET \ |
| 102 | + --url https://api.github.com/orgs/$ORIG_ORG/repos \ |
| 103 | + --header "authorization: Bearer $GITHUB_TOKEN" \ |
| 104 | + --header 'content-type: application/json') |
| 105 | + |
| 106 | + ####################################################### |
| 107 | + # Loop through list of repos in original organization # |
| 108 | + ####################################################### |
| 109 | + echo "-----------------------------------------------------" |
| 110 | + echo "Parsing repo names from Original Organization:" |
| 111 | + for orig_repo in $(echo "${ORIG_ORG_RESPONSE}" | jq -r '.[] | @base64'); |
| 112 | + do |
| 113 | + get_orig_repo_name() |
| 114 | + { |
| 115 | + echo ${orig_repo} | base64 --decode | jq -r ${1} |
| 116 | + } |
| 117 | + |
| 118 | + # Get the name of the repo |
| 119 | + ORIG_REPO_NAME=$(get_orig_repo_name '.name') |
| 120 | + echo "Name:[$ORIG_REPO_NAME]" |
| 121 | + ORIG_ORG_REPOS+=($ORIG_REPO_NAME) |
| 122 | + done |
| 123 | +} |
| 124 | +################################################################################ |
| 125 | +#### Sub Routine GetMasterOrgInfo ############################################## |
| 126 | +GetMasterOrgInfo() |
| 127 | +{ |
| 128 | + ###################################### |
| 129 | + # Get all repositories in MASTER Org # |
| 130 | + ###################################### |
| 131 | + echo "-----------------------------------------------------" |
| 132 | + echo "Gathering all repos from Master Organization:[$MASTER_ORG]" |
| 133 | + MASTER_ORG_RESPONSE=$(curl -s --request GET \ |
| 134 | + --url https://api.github.com/orgs/$MASTER_ORG/repos \ |
| 135 | + --header "authorization: Bearer $GITHUB_TOKEN" \ |
| 136 | + --header 'content-type: application/json') |
| 137 | + |
| 138 | + ##################################################### |
| 139 | + # Loop through list of repos in master organization # |
| 140 | + ##################################################### |
| 141 | + echo "-----------------------------------------------------" |
| 142 | + echo "Parsing repo names from Master Organization:" |
| 143 | + for master_repo in $(echo "${MASTER_ORG_RESPONSE}" | jq -r '.[] | @base64'); |
| 144 | + do |
| 145 | + get_master_repo_name() |
| 146 | + { |
| 147 | + echo ${master_repo} | base64 --decode | jq -r ${1} |
| 148 | + } |
| 149 | + |
| 150 | + # Get the name of the repo |
| 151 | + MASTER_REPO_NAME=$(get_master_repo_name '.name') |
| 152 | + echo "Name:[$MASTER_REPO_NAME]" |
| 153 | + MASTER_ORG_REPOS+=($MASTER_REPO_NAME) |
| 154 | + done |
| 155 | +} |
| 156 | +################################################################################ |
| 157 | +#### Sub Routine CheckCollisions ############################################### |
| 158 | +CheckCollisions() |
| 159 | +{ |
| 160 | + ############################ |
| 161 | + # Check for any collisions # |
| 162 | + ############################ |
| 163 | + echo "-----------------------------------------------------" |
| 164 | + echo "Checking for collisions" |
| 165 | + for new_repo in ${ORIG_ORG_REPOS[@]}; |
| 166 | + do |
| 167 | + if [[ " ${MASTER_ORG_REPOS[@]} " =~ " ${new_repo} " ]]; then |
| 168 | + # We have found the name of the repo in the master org |
| 169 | + echo "ERROR: Collision detection repo:[$new_repo]" |
| 170 | + COLLISION_REPOS+=($new_repo) |
| 171 | + fi |
| 172 | + done |
| 173 | + |
| 174 | + ############################## |
| 175 | + # Print the collisions found # |
| 176 | + ############################## |
| 177 | + echo "-----------------------------------------------------" |
| 178 | + echo "COLLISION_REPOS:" |
| 179 | + for repo in ${COLLISION_REPOS[@]}; |
| 180 | + do |
| 181 | + echo "$repo" |
| 182 | + done |
| 183 | +} |
| 184 | +################################################################################ |
| 185 | +#### Sub Routine Footer ######################################################## |
| 186 | +Footer() |
| 187 | +{ |
| 188 | + ################################################### |
| 189 | + # Check to see if we exit with success or failure # |
| 190 | + ################################################### |
| 191 | + echo "-----------------------------------------------------" |
| 192 | + if [ ${#COLLISION_REPOS[@]} -eq 0 ]; then |
| 193 | + echo "No collisions detected" |
| 194 | + exit 0 |
| 195 | + else |
| 196 | + echo "ERROR: Collisions detected!" |
| 197 | + exit 1 |
| 198 | + fi |
| 199 | +} |
| 200 | +################################################################################ |
| 201 | +############################## MAIN ############################################ |
| 202 | +################################################################################ |
| 203 | + |
| 204 | +################## |
| 205 | +# Validate input # |
| 206 | +################## |
| 207 | +ValidateInput |
| 208 | + |
| 209 | +########## |
| 210 | +# Header # |
| 211 | +########## |
| 212 | +Header |
| 213 | + |
| 214 | +#################################### |
| 215 | +# Get all repositories in User Org # |
| 216 | +#################################### |
| 217 | +GetOrigOrgInfo |
| 218 | + |
| 219 | +###################################### |
| 220 | +# Get all repositories in MASTER Org # |
| 221 | +###################################### |
| 222 | +GetMasterOrgInfo |
| 223 | + |
| 224 | +############################################################################## |
| 225 | +# Check if original is already in master, if so add to COLLISION_REPOS array # |
| 226 | +############################################################################## |
| 227 | +CheckCollisions |
| 228 | + |
| 229 | +#################### |
| 230 | +# Print the footer # |
| 231 | +#################### |
| 232 | +Footer |
0 commit comments