forked from joshjohanning/github-misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-enterprise-organizations-for-user.sh
More file actions
executable file
·37 lines (32 loc) · 1.1 KB
/
get-enterprise-organizations-for-user.sh
File metadata and controls
executable file
·37 lines (32 loc) · 1.1 KB
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
#!/bin/bash
# this returns a list of organizations in an enterprise the user is a member of
# gh cli's token needs to be able to admin enterprise - run this first if it can't
# gh auth refresh -h github.com -s admin:enterprise
if [ -z "$2" ]; then
echo "Usage: $0 <enterprise> <user>"
echo "Example: ./get-enterprise-organizations-for-user.sh avocado-corp joshjohanning"
exit 1
fi
enterprise="$1"
user="$2"
gh api graphql --paginate -f enterpriseSlug=$enterprise -f login=$user -f query='
query ($enterpriseSlug: String!, $login: String!, $endCursor: String) {
enterprise(slug: $enterpriseSlug) {
members(query: $login, first: 1) {
nodes {
... on EnterpriseUserAccount {
login
organizations(first: 100, after: $endCursor) {
nodes {
login
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
}
}' | jq -s -r '{login: .[0].data.enterprise.members.nodes[0].login, organizations: [.[].data.enterprise.members.nodes[0].organizations.nodes[].login]}'