-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathadd-enterprise-organization-member.sh
executable file
·77 lines (66 loc) · 1.95 KB
/
add-enterprise-organization-member.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
# Adds a user to an organization in an enterprise
# Notes:
# - for EMU, this currently only works for adding:
# 1. Enterprise owners
# 2. Members who are already added to at least (1) org in the enterprise
# - gh cli's token needs to be able to admin enterprise: gh auth refresh -h github.com -s admin:enterprise
# role
# - MEMBER: The user is a member of the organization.
# - ADMIN: The user is an administrator/owner of the organization.
function print_usage {
echo "Usage: $0 <enterprise> <org> <user> <role>"
echo "Example: ./add-enterprise-organization-member.sh avocado-corp joshjohanning-org joshjohanning admin"
echo "Valid roles: MEMBER, ADMIN"
exit 1
}
if [ -z "$4" ]; then
print_usage
fi
enterprise="$1"
org="$2"
user="$3"
role=$(echo "$4" | tr '[:lower:]' '[:upper:]')
case "$role" in
"MEMBER" | "ADMIN")
;;
*)
print_usage
;;
esac
enterprise_id=$(gh api graphql -H X-Github-Next-Global-ID:1 -f enterprise="$enterprise" -f query='
query ($enterprise: String!)
{ enterprise(slug: $enterprise) {
id
}
}
' --jq '.data.enterprise.id')
org_id=$(gh api graphql -H X-Github-Next-Global-ID:1 -f organization="$org" -f query='
query ($organization: String!)
{ organization(login: $organization) {
login
name
id
}
}
' --jq '.data.organization.id')
user_id=$(gh api graphql -H X-Github-Next-Global-ID:1 -f user="$user" -f query='
query ($user: String!)
{ user(login: $user) {
login
name
id
}
}
' --jq '.data.user.id')
gh api graphql -H X-Github-Next-Global-ID:1 -f enterpriseId="$enterprise_id" -f organizationId="$org_id" -f userIds="$user_id" -f role=$role -f query='
mutation($enterpriseId: ID!, $organizationId: ID!, $userIds: [ID!]!, $role: OrganizationMemberRole) {
addEnterpriseOrganizationMember(input: {enterpriseId: $enterpriseId, organizationId: $organizationId, userIds: $userIds, role: $role}) {
clientMutationId
users {
login
name
id
}
}
}'