Skip to content

Commit a3e1af7

Browse files
committed
fix!: fix error handling when querying enterprise orgs
added a few count scripts renamed codeowner usage script fixed various bugs/wording
1 parent 78df573 commit a3e1af7

11 files changed

+351
-147
lines changed

gh-cli/README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,19 +808,13 @@ Gets the count of apps in all organizations in a given enterprise
808808

809809
Gets a list of apps (and app information) in all organizations in a given enterprise
810810

811-
### get-organizations-codeowner-usage.sh
812-
813-
Gets the usage of CODEOWNERS files in all repositories in all organizations in a given enterprise (checks `HEAD` for `./`, `./.github`, and `./docs` and returns `TRUE` or `FALSE` for each repository)
814-
815811
### get-organizations-custom-repository-roles-count.sh
816812

817813
Gets the count of custom repository roles in all organizations in a given enterprise
818814

819815
### get-organizations-discussions-count.sh
820816

821-
Gets the usage of discussions in all repositories in all organizations in a given enterprise (org-wide discussions have to be created in a repository, so this covers that as well)
822-
823-
817+
Gets the count of discussions in all organizations in a given enterprise
824818

825819
### get-organizations-for-user.sh
826820

@@ -834,10 +828,25 @@ Gets the count of organization projects (classic projects) in all organizations
834828

835829
Gets the count of projects (ProjectsV2) in all organizations in a given enterprise
836830

831+
### get-organizations-repositories-codeowner-usage.sh
832+
833+
Gets the usage of CODEOWNERS files in all repositories in all organizations in a given enterprise (checks `HEAD` for `./`, `./.github`, and `./docs` and returns `TRUE` or `FALSE` for each repository)
834+
835+
### get-organizations-repositories-discussions-count.sh
836+
837+
Gets the usage of discussions in all repositories in all organizations in a given enterprise (org-wide discussions have to be created in a repository, so this covers that as well)
838+
837839
### get-organizations-settings.sh
838840

839841
Gets the settings for all organizations in an enterprise
840842

843+
### get-organizations-webhooks-count.sh
844+
845+
Gets a count of webhooks (and webhook information) in all organizations in an enterprise
846+
847+
> [!NOTE]
848+
> Requires a GitHub PAT instead of using the OAuth token with the `gh api` - the OAuth token can only retrieve webhooks it created
849+
841850
### get-organizations-webhooks.sh
842851

843852
Gets a list of webhooks (and webhook information) in all organizations in an enterprise

gh-cli/get-enterprise-organizations.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
#!/bin/bash
22

3+
# gets a list of organizations for a given enterprise
4+
5+
# need: `gh auth refresh -h github.com -s read:org -s read:enterprise`
6+
37
if [ -z "$1" ]; then
4-
echo "Usage: $(basename $0) <enterprise-slug>"
8+
echo "Usage: $(basename $0) <enterprise-slug> <hostname>"
59
exit 1
610
fi
711

812
enterpriseslug=$1
13+
hostname=${2:-"github.com"}
14+
export PAGER=""
915

10-
organizations=$(gh api graphql --paginate -f enterpriseName="$enterpriseslug" -f query='
16+
# Define color codes
17+
RED='\033[0;31m'
18+
NC='\033[0m' # No Color
19+
20+
organizations=$(gh api graphql --hostname $hostname --paginate -f enterpriseName="$enterpriseslug" -f query='
1121
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
1222
enterprise(slug: $enterpriseName) {
1323
organizations(first: 100, after: $endCursor) {
@@ -30,14 +40,10 @@ merged_organizations=$(echo "$organizations" | jq -s '{organizations: map(.organ
3040
echo "$merged_organizations" | jq .
3141

3242
# 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
43+
if [ -z "$organizations" ] || [[ "$organizations" == *"INSUFFICIENT_SCOPES"* ]]; then
4044
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}"
45+
echo -e "${RED} - Check that you have the proper scopes for enterprise with 'gh auth status' - you need at least 'read:enterprise'${NC}"
46+
echo -e "${RED} - You can run 'gh auth refresh -h github.com -s read:org -s read:enterprise' to add the scopes${NC}"
47+
echo -e "${RED} - Or you can run 'gh auth login -h github.com' and authenticate using a PAT with the proper scopes${NC}"
4248
exit 1
4349
fi

gh-cli/get-organization-webhooks.sh

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,24 @@ if [ $# -lt 1 ]
1515
fi
1616

1717
org=$1
18-
hostname=$2
19-
format=$3
18+
hostname=${2:-"github.com"}
19+
format=${3:-"tsv"}
2020
export PAGER=""
2121

22-
# set hostname to github.com by default
23-
if [ -z "$hostname" ]
24-
then
25-
hostname="github.com"
26-
fi
22+
# Define color codes
23+
RED='\033[0;31m'
24+
NC='\033[0m' # No Color
2725

2826
auth_status=$(gh auth token -h $hostname 2>&1)
2927

3028
if [[ $auth_status == gho_* ]]
3129
then
32-
echo "Token starts with gho_ - use "gh auth login" and authenticate with a PAT with read:org and admin:org_hook scope"
30+
echo -e "${RED}Token is an OAuth that starts with \"gho_\" which won't work for this request. To resolve, either:${NC}"
31+
echo -e "${RED} 1. use \"gh auth login\" and authenticate with a PAT with \"read:org\" and \"admin:org_hook\" scope${NC}"
32+
echo -e "${RED} 2. set an environment variable \"GITHUB_TOKEN=your_PAT\" using a PAT with \"read:org\" and \"admin:org_hook\" scope${NC}"
3333
exit 1
3434
fi
3535

36-
if [ -z "$format" ]
37-
then
38-
format="tsv"
39-
fi
40-
4136
if [ "$format" == "tsv" ]; then
4237
echo -e "Organization\tActive\tURL\tCreated At\tUpdated At\tEvents"
4338
fi

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# gets the settings for all organizations in an enterprise
3+
# gets the installed app count for all organizations in an enterprise
44

55
# need: `gh auth refresh -h github.com -s read:org -s read:enterprise`
66

@@ -14,13 +14,11 @@ fi
1414

1515
export PAGER=""
1616
enterpriseslug=$1
17-
hostname=$2
17+
hostname=${2:-"github.com"}
1818

19-
# set hostname to github.com by default
20-
if [ -z "$hostname" ]
21-
then
22-
hostname="github.com"
23-
fi
19+
# Define color codes
20+
RED='\033[0;31m'
21+
NC='\033[0m' # No Color
2422

2523
organizations=$(gh api graphql --paginate --hostname $hostname -f enterpriseName="$enterpriseslug" -f query='
2624
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
@@ -39,21 +37,30 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
3937
}' --jq '.data.enterprise.organizations.nodes[].login')
4038

4139
# 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
40+
if [ -z "$organizations" ] || [[ "$organizations" == *"INSUFFICIENT_SCOPES"* ]]; then
4941
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}"
42+
echo -e "${RED} - Check that you have the proper scopes for enterprise with 'gh auth status' - you need at least 'read:enterprise'${NC}"
43+
echo -e "${RED} - You can run 'gh auth refresh -h github.com -s read:org -s read:enterprise' to add the scopes${NC}"
44+
echo -e "${RED} - Or you can run 'gh auth login -h github.com' and authenticate using a PAT with the proper scopes${NC}"
5145
exit 1
5246
fi
5347

5448
echo -e "Org\tApp Count"
5549

50+
errors=""
51+
5652
for org in $organizations
5753
do
58-
gh api "orgs/$org/installations" --hostname $hostname --jq ". | [\"$org\", .total_count] | @tsv"
54+
output=$(gh api "orgs/$org/installations" --hostname $hostname --jq ". | [\"$org\", .total_count] | @tsv" 2>&1)
55+
56+
if [ $? -ne 0 ]; then
57+
errors="$errors\nError accessing organization: $org:\n$output"
58+
echo -e "$org\tn/a"
59+
else
60+
echo "$output"
61+
fi
5962
done
63+
64+
if [ -n "$errors" ]; then
65+
echo -e "${RED}\nErrors encountered:\n$errors${NC}"
66+
fi

gh-cli/get-organizations-apps.sh

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# gets the settings for all organizations in an enterprise
3+
# gets the installed apps and their details for all organizations in an enterprise
44

55
# need: `gh auth refresh -h github.com -s read:org -s read:enterprise`
66

@@ -14,20 +14,13 @@ if [ $# -lt 1 ]
1414
fi
1515

1616
enterpriseslug=$1
17-
hostname=$2
18-
format=$3
17+
hostname=${2:-"github.com"}
18+
format=${3:-"tsv"}
1919
export PAGER=""
2020

21-
# set hostname to github.com by default
22-
if [ -z "$hostname" ]
23-
then
24-
hostname="github.com"
25-
fi
26-
27-
if [ -z "$format" ]
28-
then
29-
format="tsv"
30-
fi
21+
# Define color codes
22+
RED='\033[0;31m'
23+
NC='\033[0m' # No Color
3124

3225
organizations=$(gh api graphql --paginate --hostname $hostname -f enterpriseName="$enterpriseslug" -f query='
3326
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
@@ -46,27 +39,35 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4639
}' --jq '.data.enterprise.organizations.nodes[].login')
4740

4841
# 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
42+
if [ -z "$organizations" ] || [[ "$organizations" == *"INSUFFICIENT_SCOPES"* ]]; then
5643
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}"
44+
echo -e "${RED} - Check that you have the proper scopes for enterprise with 'gh auth status' - you need at least 'read:enterprise'${NC}"
45+
echo -e "${RED} - You can run 'gh auth refresh -h github.com -s read:org -s read:enterprise' to add the scopes${NC}"
46+
echo -e "${RED} - Or you can run 'gh auth login -h github.com' and authenticate using a PAT with the proper scopes${NC}"
5847
exit 1
5948
fi
6049

50+
errors=""
51+
6152
if [ "$format" == "tsv" ]; then
6253
echo -e "Org\tApp Slug\tApp ID\tCreated At\tUpdated At\tPermissions\tEvents"
6354
fi
6455

6556
for org in $organizations
6657
do
6758
if [ "$format" == "tsv" ]; then
68-
gh api "orgs/$org/installations" --hostname $hostname --jq ".installations[] | [\"$org\", .app_slug, .app_id, .created_at, .updated_at, (.permissions | join(\",\")), (if .events | length == 0 then \"null\" else .events | join(\",\") end)] | @tsv"
59+
output=$(gh api "orgs/$org/installations" --hostname $hostname --jq ".installations[] | [\"$org\", .app_slug, .app_id, .created_at, .updated_at, (.permissions | join(\",\")), (if .events | length == 0 then \"null\" else .events | join(\",\") end)] | @tsv" 2>&1)
6960
else
70-
gh api "orgs/$org/installations" --hostname $hostname --jq '.installations[]'
61+
output=$(gh api "orgs/$org/installations" --hostname $hostname --jq '.installations[]' 2>&1)
62+
fi
63+
64+
if [ $? -ne 0 ]; then
65+
errors="$errors\nError accessing organization: $org:\n$output"
66+
elif [ -n "$output" ]; then
67+
echo "$output"
7168
fi
7269
done
70+
71+
if [ -n "$errors" ]; then
72+
echo -e "${RED}\nErrors encountered:\n$errors${NC}"
73+
fi
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# gets the custom repository roles for all organizations in an enterprise
3+
# gets the custom repository roles count for all organizations in an enterprise
44

55
# need: `gh auth refresh -h github.com -s read:org -s read:enterprise`
66

@@ -14,13 +14,11 @@ fi
1414

1515
export PAGER=""
1616
enterpriseslug=$1
17-
hostname=$2
17+
hostname=${2:-"github.com"}
1818

19-
# set hostname to github.com by default
20-
if [ -z "$hostname" ]
21-
then
22-
hostname="github.com"
23-
fi
19+
# Define color codes
20+
RED='\033[0;31m'
21+
NC='\033[0m' # No Color
2422

2523
organizations=$(gh api graphql --paginate --hostname $hostname -f enterpriseName="$enterpriseslug" -f query='
2624
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
@@ -39,21 +37,30 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
3937
}' --jq '.data.enterprise.organizations.nodes[].login')
4038

4139
# 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
40+
if [ -z "$organizations" ] || [[ "$organizations" == *"INSUFFICIENT_SCOPES"* ]]; then
4941
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}"
42+
echo -e "${RED} - Check that you have the proper scopes for enterprise with 'gh auth status' - you need at least 'read:enterprise'${NC}"
43+
echo -e "${RED} - You can run 'gh auth refresh -h github.com -s read:org -s read:enterprise' to add the scopes${NC}"
44+
echo -e "${RED} - Or you can run 'gh auth login -h github.com' and authenticate using a PAT with the proper scopes${NC}"
5145
exit 1
5246
fi
5347

54-
echo -e "Org\tCustoim Role Count"
48+
echo -e "Org\tCustom Role Count"
49+
50+
errors=""
5551

5652
for org in $organizations
5753
do
58-
gh api "orgs/$org/custom-repository-roles" --hostname $hostname --jq ". | [\"$org\", .total_count] | @tsv"
54+
output=$(gh api "orgs/$org/custom-repository-roles" --hostname $hostname --jq ". | [\"$org\", .total_count] | @tsv" 2>&1)
55+
56+
if [ $? -ne 0 ]; then
57+
errors="$errors\nError accessing organization: $org:\n$output"
58+
echo -e "$org\tn/a"
59+
elif [ -n "$output" ]; then
60+
echo "$output"
61+
fi
5962
done
63+
64+
if [ -n "$errors" ]; then
65+
echo -e "${RED}\nErrors encountered:\n$errors${NC}"
66+
fi

0 commit comments

Comments
 (0)