2
2
#
3
3
# GitHub Management Script
4
4
#
5
- # Usage: ./create_repos.sh [--apply ] [--debug]
5
+ # Usage: ./create_repos.sh --org <organization_name> [--dry-run ] [--debug]
6
6
#
7
7
# Parameters:
8
- # --apply: Apply changes to GitHub (default is dry-run mode) .
9
- # --debug: Enable debug mode for additional information .
8
+ # --org: The name of the organization on GitHub .
9
+ # --dry-run: Optional flag to simulate script execution without making changes .
10
10
#
11
11
# Description:
12
12
# This Bash script automates GitHub repository and team management based on a YAML configuration file.
13
13
# It uses GitHub CLI (gh) and yq for interaction and configuration parsing, respectively.
14
- # The script can create, transfer, and synchronize repositories and teams, and it supports dry-run mode.
14
+ # The script can create and synchronize repositories and teams, and it supports dry-run mode.
15
15
16
16
cd " $( dirname " $0 " ) "
17
17
IFS=$' \n ' # keep whitespace when iterating with for loops
@@ -26,12 +26,18 @@ if ! command -v yq &> /dev/null || ! command -v gh &> /dev/null; then
26
26
fi
27
27
28
28
# Parse command line parameters
29
- DRY_RUN=true
29
+ ORG_NAME=" "
30
+ DRY_RUN=false
30
31
DEBUG=false
32
+
31
33
while [ $# -gt 0 ]; do
32
34
case " $1 " in
33
- --apply)
34
- DRY_RUN=false
35
+ --org)
36
+ shift
37
+ ORG_NAME=$1
38
+ ;;
39
+ --dry-run)
40
+ DRY_RUN=true
35
41
;;
36
42
--debug)
37
43
DEBUG=true
@@ -88,29 +94,6 @@ create_repo() {
88
94
}
89
95
90
96
91
- # Function to transfer a GitHub repository from one organization to another
92
- transfer_repo () {
93
- local name=$1
94
-
95
- if [ " $DRY_RUN " = true ]; then
96
- DRY_RUN_MESSAGES+=" ~ Would transfer repository $name from allianz-incubator to allianz.\n"
97
- else
98
- local response=$( gh api \
99
- --method POST \
100
- -H " Accept: application/vnd.github+json" \
101
- -H " X-GitHub-Api-Version: 2022-11-28" \
102
- repos/allianz-incubator/$name /transfer \
103
- -f new_owner=allianz)
104
-
105
- if [ $? -eq 0 ] && [ " $( echo $response | jq -r ' .id' ) " != " null" ]; then
106
- echo -e " \e[32m✓\e[0m Repository '$name ' successfully transfered to organization allianz."
107
- else
108
- echo " Error transfering repo '$name ' at line $LINENO . $response ." >&2 ; exit 1;
109
- fi
110
- fi
111
- }
112
-
113
-
114
97
# Function to create a new GitHub team and set up team synchronization
115
98
create_team () {
116
99
local name=$1
@@ -159,7 +142,7 @@ create_team() {
159
142
if [ " $DRY_RUN " = true ]; then
160
143
DRY_RUN_MESSAGES+=" + Would setup team sync: team '$name ' with AD Group '$giam_name '.\n"
161
144
else
162
- load_teams # Update cache to include new team slug
145
+ load_teams $org # Update cache to include new team slug
163
146
local slug_name=$( get_team_slug $name ) || exit 1
164
147
local response=$( echo $ad_group | gh api \
165
148
--method PATCH \
@@ -271,13 +254,11 @@ load_repositories() {
271
254
}
272
255
273
256
274
- # Function to load existing teams from GitHub
257
+ # Function to load existing teams from GitHub and cache the result
275
258
load_teams () {
276
- CACHED_ALLIANZ_TEAMS=$( gh api -H " Accept: application/vnd.github+json" -H " X-GitHub-Api-Version: 2022-11-28" /orgs/allianz/teams) || {
277
- echo " Error fetching teams for allianz at line $LINENO . $CACHED_ALLIANZ_TEAMS ." ; exit 1; }
278
-
279
- CACHED_ALLIANZ_INCUBATOR_TEAMS=$( gh api -H " Accept: application/vnd.github+json" -H " X-GitHub-Api-Version: 2022-11-28" /orgs/allianz-incubator/teams) || {
280
- echo " Error fetching teams for allianz-incubator at line $LINENO . $CACHED_ALLIANZ_INCUBATOR_TEAMS ." ; exit 1; }
259
+ local org=" $1 "
260
+ CACHED_TEAMS=$( gh api -H " Accept: application/vnd.github+json" -H " X-GitHub-Api-Version: 2022-11-28" /orgs/$org /teams) || {
261
+ echo " Error fetching teams for allianz at line $LINENO . $CACHED_TEAMS ." ; exit 1; }
281
262
}
282
263
283
264
@@ -297,12 +278,7 @@ load_team_permissions(){
297
278
# Function to get the list of teams for a given organization
298
279
get_teams (){
299
280
local org=" $1 "
300
-
301
- if [ " $org " == " allianz" ]; then
302
- echo " $CACHED_ALLIANZ_TEAMS "
303
- else
304
- echo " $CACHED_ALLIANZ_INCUBATOR_TEAMS "
305
- fi
281
+ echo " $CACHED_TEAMS "
306
282
}
307
283
308
284
@@ -311,14 +287,11 @@ get_team_slug(){
311
287
local name=" $1 "
312
288
313
289
# Search for the team in both organizations
314
- local slug_allianz=$( jq -r ' .[] | select(.name == "' " $name " ' ") | .slug' <<< " $CACHED_ALLIANZ_TEAMS" ) || exit 1
315
- local slug_incubator=$( jq -r ' .[] | select(.name == "' " $name " ' ") | .slug' <<< " $CACHED_ALLIANZ_INCUBATOR_TEAMS" ) || exit 1
316
-
290
+ local slug=$( jq -r ' .[] | select(.name == "' " $name " ' ") | .slug' <<< " $CACHED_TEAMS" ) || exit 1
291
+
317
292
# Return the first non-empty slug found
318
- if [ -n " $slug_allianz " ]; then
319
- echo " $slug_allianz "
320
- elif [ -n " $slug_incubator " ]; then
321
- echo " $slug_incubator "
293
+ if [ -n " $slug " ]; then
294
+ echo " $slug "
322
295
else
323
296
echo " Error: team slug not found for $name " >&2 ; exit 1
324
297
fi
@@ -334,53 +307,31 @@ get_team_slug(){
334
307
# warnings for inconsistent repository configurations.
335
308
#
336
309
process_repos () {
310
+ local org=" $1 "
337
311
echo " READING REPOSITORIES..."
338
312
339
313
# Status
340
- local existing_main_repos=$( load_repositories allianz) || exit 1
341
- local existing_incubator_repos=$( load_repositories allianz-incubator) || exit 1
342
- local desired_main_repos=$( yq eval ' .repositories[] | select(.stage == "allianz") | .name' " $YAML_FILE " | sort -u) || exit 1
343
- local desired_incubator_repos=$( yq eval ' .repositories[] | select(.stage == "allianz-incubator") | .name' " $YAML_FILE " | sort -u) || exit 1
344
-
314
+ local existing_repos=$( load_repositories $org ) || exit 1
315
+ local desired_repos=$( yq eval ' .repositories[] | select(.stage == "' " $org " ' ") | .name' " $YAML_FILE " | sort -u) || exit 1
316
+
345
317
# # calculate changes
346
- local repos_to_add_in_incubator=$( comm -23 <( echo " $desired_incubator_repos " ) <( echo " $existing_incubator_repos " ) ) || exit 1
347
- local repos_to_add_in_main=$( comm -23 <( comm -23 <( echo " $desired_main_repos " ) <( echo " $existing_main_repos " ) ) <( echo " $existing_incubator_repos " ) ) || exit 1
348
- local repos_to_transfer_to_main=$( comm -12 <( comm -23 <( echo " $desired_main_repos " ) <( echo " $existing_main_repos " ) ) <( echo " $existing_incubator_repos " ) ) || exit 1
349
-
318
+ local repos_to_add=$( comm -23 <( echo " $desired_repos " ) <( echo " $existing_repos " ) ) || exit 1
319
+
350
320
# Debug
351
321
print_debug
352
322
print_debug " Existing Repositories in allianz:"
353
- print_debug " $existing_main_repos " | sed ' s/^/ /'
323
+ print_debug " $existing_repos " | sed ' s/^/ /'
354
324
print_debug
355
325
print_debug " Desired Repositories in allianz:"
356
- print_debug " $desired_main_repos " | sed ' s/^/ /'
326
+ print_debug " $desired_repos " | sed ' s/^/ /'
357
327
print_debug
358
328
print_debug " Repositories to Add in allianz:"
359
- print_debug " $repos_to_add_in_main " | sed ' s/^/ /'
360
- print_debug
361
- print_debug " Repositories to Transfer to allianz:"
362
- print_debug " $repos_to_transfer_to_main " | sed ' s/^/ /'
363
- print_debug
364
- print_debug " Existing Repositories in allianz-incubator:"
365
- print_debug " $existing_incubator_repos " | sed ' s/^/ /'
366
- print_debug
367
- print_debug " Desired Repositories in allianz-incubator:"
368
- print_debug " $desired_incubator_repos " | sed ' s/^/ /'
329
+ print_debug " $repos_to_add " | sed ' s/^/ /'
369
330
print_debug
370
- print_debug " Repositories to Add in allianz-incubator:"
371
- print_debug " $repos_to_add_in_incubator " | sed ' s/^/ /'
372
- print_debug
373
-
374
331
375
332
# Iterate over changes
376
- for repo in $repos_to_add_in_incubator ; do
377
- create_repo $repo " allianz-incubator"
378
- done
379
- for repo in $repos_to_add_in_main ; do
380
- create_repo $repo " allianz"
381
- done
382
- for repo in $repos_to_transfer_to_main ; do
383
- transfer_repo $repo
333
+ for repo in $repos_to_add ; do
334
+ create_repo $repo $org
384
335
done
385
336
}
386
337
@@ -478,10 +429,9 @@ process_teams() {
478
429
479
430
# Run
480
431
validate_yaml
481
- process_repos
482
- load_teams
483
- process_teams allianz
484
- process_teams allianz-incubator
432
+ process_repos $ORG_NAME
433
+ load_teams $ORG_NAME
434
+ process_teams $ORG_NAME
485
435
486
436
# Print warnings
487
437
if [ -n " $warning_messages " ]; then
0 commit comments