Skip to content

Commit d35148f

Browse files
committed
fix: add hostname and use --jq
1 parent 50a1c43 commit d35148f

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
#!/bin/bash
22

3+
# gets information for all webhooks for in an organization
4+
5+
# need: `gh auth login -h github.com` and auth with a PAT!
6+
# since the Oauth token can only receive results for hooks it created for this API call
7+
38
if [ $# -lt 1 ]
49
then
5-
echo "usage: $0 <org> <format: tsv|json>" > output.csv/json
10+
echo "usage: $0 <org> <hostname> <format: tsv|json> > output.tsv/json"
611
exit 1
712
fi
813

9-
# need: `gh auth login -h github.com` and auth with a PAT!
10-
# sine the Oauth token can only receive results for hooks it created for this API call
14+
org=$1
15+
hostname=$2
16+
format=$3
17+
export PAGER=""
1118

12-
auth_status=$(gh auth token 2>&1)
19+
# set hostname to github.com by default
20+
if [ -z "$hostname" ]
21+
then
22+
hostname="github.com"
23+
fi
24+
25+
auth_status=$(gh auth token -h $hostname 2>&1)
1326

1427
if [[ $auth_status == gho_* ]]
1528
then
1629
echo "Token starts with gho_ - use "gh auth login" and authenticate with a PAT with read:org and admin:org_hook scope"
1730
exit 1
1831
fi
1932

20-
export PAGER=""
21-
org=$1
22-
format=$2
2333
if [ -z "$format" ]
2434
then
2535
format="tsv"
@@ -30,7 +40,7 @@ if [ "$format" == "tsv" ]; then
3040
fi
3141

3242
if [ "$format" == "tsv" ]; then
33-
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | [$org,.active,.config.url, .created_at, .updated_at, (.events | join(","))] | @tsv'
43+
gh api "orgs/$org/hooks" --hostname $hostname --paginate --jq ".[] | [\"$org\",.active,.config.url, .created_at, .updated_at, (.events | join(\",\"))] | @tsv"
3444
else
35-
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | {organization: $org, active: .active, url: .config.url, created_at: .created_at, updated_at: .updated_at, events: .events}'
45+
gh api "orgs/$org/hooks" --hostname $hostname --paginate --jq ".[] | {organization: \"$org\", active: .active, url: .config.url, created_at: .created_at, updated_at: .updated_at, events: .events}"
3646
fi
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
#!/bin/bash
22

3-
if [ $# -ne 1 ]; then
4-
echo "usage: $0 <enterprise slug>"
3+
# gets the projects count for all organizations in an enterprise
4+
5+
# need: `gh auth refresh -h github.com -s read:org -s read:enterprise`
6+
7+
if [ $# -lt 1 ]; then
8+
echo "usage: $0 <enterprise slug> <hostname> > output.tsv"
59
exit 1
610
fi
711

812
enterprise=$1
13+
hostname=$2
14+
export PAGER=""
15+
16+
# set hostname to github.com by default
17+
if [ -z "$hostname" ]
18+
then
19+
hostname="github.com"
20+
fi
21+
22+
echo -e "Organization\tProjectv2 Count"
923

10-
gh api graphql -f enterprise="$enterprise" --paginate -f query='query($enterprise:String!, $endCursor: String) {
24+
gh api graphql -f enterprise="$enterprise" --paginate --hostname $hostname -f query='query($enterprise:String!, $endCursor: String) {
1125
enterprise(slug:$enterprise) {
1226
organizations(first:100, after: $endCursor) {
1327
pageInfo { hasNextPage endCursor }
@@ -17,4 +31,4 @@ gh api graphql -f enterprise="$enterprise" --paginate -f query='query($enterpris
1731
}
1832
}
1933
}
20-
}' --jq '.data.enterprise.organizations.nodes[] | [.name, .projectsV2.totalCount] | @tsv'
34+
}' --jq '.data.enterprise.organizations.nodes[] | [.name, .projectsV2.totalCount] | @tsv'

gh-cli/get-organizations-webhooks.sh

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

3+
# gets information for all webhooks for in an organization
4+
5+
# need: `gh auth login -h github.com` and auth with a PAT!
6+
# since the Oauth token can only receive results for hooks it created for this API call
7+
38
if [ $# -lt 1 ]
49
then
5-
echo "usage: $0 <enterprise-slug> <format: tsv|json>" > output.csv/json
10+
echo "usage: $0 <enterprise slug> <hostname> <format: tsv|json> > output.tsv/json"
611
exit 1
712
fi
813

9-
# need: `gh auth login -h github.com` and auth with a PAT!
10-
# sine the Oauth token can only receive results for hooks it created for this API call
14+
enterpriseslug=$1
15+
hostname=$2
16+
format=$3
17+
export PAGER=""
1118

12-
auth_status=$(gh auth token 2>&1)
19+
# set hostname to github.com by default
20+
if [ -z "$hostname" ]
21+
then
22+
hostname="github.com"
23+
fi
24+
25+
auth_status=$(gh auth token -h $hostname 2>&1)
1326

1427
if [[ $auth_status == gho_* ]]
1528
then
16-
echo "Token starts with gho_ - use "gh auth login" and authenticate with a PAT with read:enterprise, reaad:org, and admin:org_hook scope"
29+
echo "Token starts with gho_ - use "gh auth login" and authenticate with a PAT with read:org and admin:org_hook scope"
1730
exit 1
1831
fi
19-
20-
export PAGER=""
21-
enterpriseslug=$1
22-
format=$2
2332
if [ -z "$format" ]
2433
then
25-
format="tsv"fi
34+
format="tsv"
2635
fi
2736

28-
organizations=$(gh api graphql --paginate -f enterpriseName="$enterpriseslug" -f query='
37+
organizations=$(gh api graphql --hostname $hostname --paginate -f enterpriseName="$enterpriseslug" -f query='
2938
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
3039
enterprise(slug: $enterpriseName) {
3140
organizations(first: 100, after: $endCursor) {
@@ -48,8 +57,8 @@ fi
4857
for org in $organizations
4958
do
5059
if [ "$format" == "tsv" ]; then
51-
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | [$org,.active,.config.url, .created_at, .updated_at, (.events | join(","))] | @tsv'
60+
gh api "orgs/$org/hooks" --hostname $hostname --paginate --jq ".[] | [\"$org\",.active,.config.url, .created_at, .updated_at, (.events | join(\",\"))] | @tsv"
5261
else
53-
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | {organization: $org, active: .active, url: .config.url, created_at: .created_at, updated_at: .updated_at, events: .events}'
62+
gh api "orgs/$org/hooks" --hostname $hostname --paginate --jq ".[] | {organization: \"$org\", active: .active, url: .config.url, created_at: .created_at, updated_at: .updated_at, events: .events}"
5463
fi
5564
done

0 commit comments

Comments
 (0)