-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathupdate-enterprise-owner-organizational-role.sh
executable file
·63 lines (52 loc) · 1.94 KB
/
update-enterprise-owner-organizational-role.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
#!/bin/bash
# Adds the authenticated user (Enterprise Owner) to an organization in an enterprise
# Notes:
# - this script uses the enterprise owner's ability to add themselves to an organization in the enterprise
# - this script only adds the person running the script to the organization, and that person needs to be an enterprise owner
# - gh cli's token needs to be able to admin enterprise: gh auth refresh -h github.com -s admin:enterprise
# organizationRole
# - DIRECT_MEMBER: The user is a member of the organization.
# - OWNER: The user is an administrator/owner of the organization.
# - UNAFFILIATED: The user is a not a member of the organization. (leave the organization)
function print_usage {
echo "Usage: $0 <enterprise> <org> <role>"
echo "Example: ./get-enterprise-id.sh avocado-corp joshjohanning-org owner"
echo "Valid roles: DIRECT_MEMBER, OWNER, UNAFFILIATED (leave organization)"
exit 1
}
if [ -z "$3" ]; then
print_usage
fi
enterprise="$1"
org="$2"
role=$(echo "$3" | tr '[:lower:]' '[:upper:]')
case "$role" in
"DIRECT_MEMBER" | "OWNER" | "UNAFFILIATED")
;;
*)
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')
gh api graphql -f enterprise_id="$enterprise_id" -f organization_id="$org_id" -f organization_role="$role" -f query='
mutation ($enterprise_id: ID! $organization_id: ID! $organization_role: RoleInOrganization!) {
updateEnterpriseOwnerOrganizationRole(input: { enterpriseId: $enterprise_id organizationId: $organization_id organizationRole: $organization_role}) {
clientMutationId
message
}
}'