Skip to content

Commit 1ee6d9a

Browse files
Merge pull request #83 from joshjohanning/improve-enterprise-org-error-handling
feat: improve error handling when querying enterprise orgs
2 parents 65e958b + 00b9882 commit 1ee6d9a

8 files changed

+112
-2
lines changed

gh-cli/get-enterprise-organizations.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fi
77

88
enterpriseslug=$1
99

10-
gh api graphql --paginate -f enterpriseName="$enterpriseslug" -f query='
10+
organizations=$(gh api graphql --paginate -f enterpriseName="$enterpriseslug" -f query='
1111
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
1212
enterprise(slug: $enterpriseName) {
1313
organizations(first: 100, after: $endCursor) {
@@ -21,4 +21,23 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
2121
}
2222
}
2323
}
24-
}' --jq '{organizations: [.data.enterprise.organizations.nodes[].login]}'
24+
}' --jq '{organizations: [.data.enterprise.organizations.nodes[].login]}')
25+
26+
# Slurp and merge JSON objects
27+
merged_organizations=$(echo "$organizations" | jq -s '{organizations: map(.organizations) | add}')
28+
29+
# Print the consolidated JSON object
30+
echo "$merged_organizations" | jq .
31+
32+
# check to see if organizations is null - null error message is confusing otherwise
33+
if [ -z "$organizations" ]
34+
then
35+
# Define color codes
36+
RED='\033[0;31m'
37+
NC='\033[0m' # No Color
38+
39+
# Print colored messages
40+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
41+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
42+
exit 1
43+
fi

gh-cli/get-organizations-apps-count.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
3838
}
3939
}' --jq '.data.enterprise.organizations.nodes[].login')
4040

41+
# check to see if organizations is null - null error message is confusing otherwise
42+
if [ -z "$organizations" ] || [ $? -ne 0 ]
43+
then
44+
# Define color codes
45+
RED='\033[0;31m'
46+
NC='\033[0m' # No Color
47+
48+
# Print colored messages
49+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
50+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
51+
exit 1
52+
fi
53+
4154
echo -e "Org\tApp Count"
4255

4356
for org in $organizations

gh-cli/get-organizations-apps.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4545
}
4646
}' --jq '.data.enterprise.organizations.nodes[].login')
4747

48+
# check to see if organizations is null - null error message is confusing otherwise
49+
if [ -z "$organizations" ] || [ $? -ne 0 ]
50+
then
51+
# Define color codes
52+
RED='\033[0;31m'
53+
NC='\033[0m' # No Color
54+
55+
# Print colored messages
56+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
57+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
58+
exit 1
59+
fi
60+
4861
if [ "$format" == "tsv" ]; then
4962
echo -e "Org\tApp Slug\tApp ID\tCreated At\tUpdated At\tPermissions\tEvents"
5063
fi

gh-cli/get-organizations-codeowner-usage.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4040
}
4141
}' --jq '.data.enterprise.organizations.nodes[].login')
4242

43+
# check to see if organizations is null - null error message is confusing otherwise
44+
if [ -z "$organizations" ] || [ $? -ne 0 ]
45+
then
46+
# Define color codes
47+
RED='\033[0;31m'
48+
NC='\033[0m' # No Color
49+
50+
# Print colored messages
51+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
52+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
53+
exit 1
54+
fi
55+
4356
for org in $organizations
4457
do
4558
gh api graphql --paginate --hostname $hostname -f orgName="$org" -f query='

gh-cli/get-organizations-custom-repository-roles-count.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
3838
}
3939
}' --jq '.data.enterprise.organizations.nodes[].login')
4040

41+
# check to see if organizations is null - null error message is confusing otherwise
42+
if [ -z "$organizations" ] || [ $? -ne 0 ]
43+
then
44+
# Define color codes
45+
RED='\033[0;31m'
46+
NC='\033[0m' # No Color
47+
48+
# Print colored messages
49+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
50+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
51+
exit 1
52+
fi
53+
4154
echo -e "Org\tCustoim Role Count"
4255

4356
for org in $organizations

gh-cli/get-organizations-discussions-count.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4040
}
4141
}' --jq '.data.enterprise.organizations.nodes[].login')
4242

43+
# check to see if organizations is null - null error message is confusing otherwise
44+
if [ -z "$organizations" ] || [ $? -ne 0 ]
45+
then
46+
# Define color codes
47+
RED='\033[0;31m'
48+
NC='\033[0m' # No Color
49+
50+
# Print colored messages
51+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
52+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
53+
exit 1
54+
fi
55+
4356
for org in $organizations
4457
do
4558
gh api graphql --paginate --hostname $hostname -f orgName="$org" -f query='

gh-cli/get-organizations-settings.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4545
}
4646
}' --jq '.data.enterprise.organizations.nodes[].login')
4747

48+
# check to see if organizations is null - null error message is confusing otherwise
49+
if [ -z "$organizations" ] || [ $? -ne 0 ]
50+
then
51+
# Define color codes
52+
RED='\033[0;31m'
53+
NC='\033[0m' # No Color
54+
55+
# Print colored messages
56+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
57+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
58+
exit 1
59+
fi
60+
4861
if [ "$format" == "tsv" ]; then
4962
echo -e "Org Login\tOrg Name\tOrg Desc\tDefault Repo Permission\tMembers Can Create Repos\t\tMembers Allowed Repos Creation Type\tMembers Can Create Public Repos\tMembers Can Create Private Repos\tMembers Can Create Internal Repos\tMembers Can Fork Private Repos"
5063
fi

gh-cli/get-organizations-webhooks.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
5353
}
5454
}' --jq '.data.enterprise.organizations.nodes[].login')
5555

56+
# check to see if organizations is null - null error message is confusing otherwise
57+
if [ -z "$organizations" ] || [ $? -ne 0 ]
58+
then
59+
# Define color codes
60+
RED='\033[0;31m'
61+
NC='\033[0m' # No Color
62+
63+
# Print colored messages
64+
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
65+
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
66+
exit 1
67+
fi
68+
5669
if [ "$format" == "tsv" ]; then
5770
echo -e "Organization\tActive\tURL\tCreated At\tUpdated At\tEvents"
5871
fi

0 commit comments

Comments
 (0)