-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcreate-teams-from-list.sh
executable file
·77 lines (60 loc) · 2.21 KB
/
create-teams-from-list.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
#!/bin/bash
# Creates teams in an organization from a CSV input list
# Need to run this to be able to create teams: gh auth refresh -h github.com -s admin:org
# Usage:
# Step 1: Create a list of teams in a csv file, 1 per line, with a trailing empty line at the end of the file
# - child teams should have a slash in the name, e.g. test1-team/test1-1-team
# - build out the parent structure in the input file before creating the child teams;
# e.g. have the 'test1-team' come before 'test1-team/test1-1-team' in the file
# - DO NOT REMOVE TRAILING NEW LINE IN THE INPUT CSV FILE
# Step 2: ./create-teams-from-list.sh teams.csv <org>
# Example input file:
#
# test11-team
# test22-team
# test11-team/test11111-team
# test11-team/test11111-team/textxxx-team
#
if [ $# -lt "2" ]; then
echo "Usage: $0 <teams-file-name> <org>"
exit 1
fi
if [ ! -f "$1" ]; then
echo "File $1 does not exist"
exit 1
fi
filename="$1"
org="$2"
while read -r repofull ;
do
read -ra data <<< "$repofull"
team=${data[0]}
echo -e "\nCreating team: $team ..."
# reset the parent_param
parent_param=""
# if the team is a child team, need to get the parent team id
if [[ $team == *"/"* ]]; then
child_team=$(echo "$team" | rev | cut -d'/' -f1 | rev)
parent_team=$(echo "$team" | rev | cut -d'/' -f2 | rev)
team=$child_team
echo " - parent team: $parent_team"
echo " - child team: $child_team"
echo " - ...getting parent team id..."
parent_team_id=$(gh api \
--method GET \
-H "Accept: application/vnd.github+json" \
"/orgs/$org/teams/$parent_team" \
-q '.id')
parent_param="-F parent_team_id=$parent_team_id"
echo " - ...okay now creating $team with parent of $parent_team (parent id: $parent_team_id)..."
fi
# we want to pass the parent_param as (potentially multiple arguments, so we don't double quote it)
# shellcheck disable=SC2086
response=$(gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
"/orgs/$org/teams" \
-f name="$team" -f privacy='closed' \
$parent_param)
echo -e "\n$response"
done < "$filename"