-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcopy-organization-members.sh
executable file
·65 lines (54 loc) · 1.66 KB
/
copy-organization-members.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
#!/bin/bash
if [ $# -ne 2 ]
then
echo "usage: $0 <source org> <target org>"
exit 1
fi
if [ -z "$SOURCE_TOKEN" ]
then
echo "SOURCE_TOKEN must be set"
exit 1
fi
if [ -z "$TARGET_TOKEN" ]
then
echo "TARGET_TOKEN must be set"
exit 1
fi
source_org=$1
target_org=$2
if [ "$source_org" = "$target_org" ]
then
echo "source org and target org must be different"
exit 1
fi
if [ -z "$MAP_USER_SCRIPT" ]; then
echo "WARNING: MAP_USER_SCRIPT is not set. No mapping will be performed."
echo "Add a script to the environment variable MAP_USER_SCRIPT to map users from $source_org to $target_org"
else
if [ ! -f "$MAP_USER_SCRIPT" ]; then
echo "MAP_USER_SCRIPT is set to $MAP_USER_SCRIPT"
echo "ERROR: MAP_USER_SCRIPT is not a file"
exit 1
fi
fi
GH_TOKEN=$SOURCE_TOKEN gh api graphql --paginate -F owner="$source_org" -f query='query membersWithRole($owner: String! $endCursor: String) {
organization(login: $owner) {
membersWithRole(first: 100, after: $endCursor) {
pageInfo { hasNextPage endCursor }
edges {
role
node { login }
}
}
}
}' --jq '.data.organization.membersWithRole.edges[] | [.node.login, .role] | @tsv' | while read -r login role; do
if [ -n "$MAP_USER_SCRIPT" ]; then
login=$($MAP_USER_SCRIPT "$login")
fi
login=$($MAP_USER_SCRIPT "$login")
role=$(echo "$role" | tr '[:upper:]' '[:lower:]')
echo "Adding $login with role $role to $target_org"
GH_TOKEN=$TARGET_TOKEN gh api \
--method PUT -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
"orgs/$target_org/memberships/$login" -f role="$role" --silent
done