Skip to content

Commit

Permalink
Merge pull request #75 from joshjohanning/add-org-webhook-scripts
Browse files Browse the repository at this point in the history
feat: add scripts for retrieving organization webhooks
  • Loading branch information
joshjohanning committed May 16, 2024
2 parents 21d2b23 + 6cd634d commit 8433d41
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gh-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,13 @@ Gets the members of a team
Gets a team
### get-organization-webhooks.sh
Gets a list of webhooks in an organization
> [!NOTE]
> Requires a GitHub PAT instead of using the OAuth token with the `gh api` command - the OAuth token can only retrieve webhooks it created
### get-organizations-for-user.sh
Gets the list of organizations a user is a member of. This only returns organizations accessible to the person running the script, i.e.: organizations they are also a member of, or public organizations
Expand All @@ -797,6 +804,13 @@ Gets the list of organizations a user is a member of. This only returns organiza
Gets the count of projects (ProjectsV2) in all organizations in a given enterprise
### get-organizations-webhooks-in-enterprise.sh
Gets a list of webhooks in all organizations in an enterprise
> [!NOTE]
> Requires a GitHub PAT instead of using the OAuth token with the `gh api` - the OAuth token can only retrieve webhooks it created
### get-outside-collaborators-added-to-repository.sh
Get outside collaborators added to a repository
Expand Down
36 changes: 36 additions & 0 deletions gh-cli/get-organization-webhooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

if [ $# -lt 1 ]
then
echo "usage: $0 <org> <format: tsv|json>" > output.csv/json
exit 1
fi

# need: `gh auth login -h github.com` and auth with a PAT!
# sine the Oauth token can only receive results for hooks it created for this API call

auth_status=$(gh auth token 2>&1)

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

export PAGER=""
org=$1
format=$2
if [ -z "$format" ]
then
format="tsv"
fi

if [ "$format" == "tsv" ]; then
echo -e "Organization\tActive\tURL\tCreated At\tUpdated At\tEvents"
fi

if [ "$format" == "tsv" ]; then
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | [$org,.active,.config.url, .created_at, .updated_at, (.events | join(","))] | @tsv'
else
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}'
fi
55 changes: 55 additions & 0 deletions gh-cli/get-organizations-webhooks-in-enterprise.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

if [ $# -lt 1 ]
then
echo "usage: $0 <enterprise-slug> <format: tsv|json>" > output.csv/json
exit 1
fi

# need: `gh auth login -h github.com` and auth with a PAT!
# sine the Oauth token can only receive results for hooks it created for this API call

auth_status=$(gh auth token 2>&1)

if [[ $auth_status == gho_* ]]
then
echo "Token starts with gho_ - use "gh auth login" and authenticate with a PAT with read:enterprise, reaad:org, and admin:org_hook scope"
exit 1
fi

export PAGER=""
enterpriseslug=$1
format=$2
if [ -z "$format" ]
then
format="tsv"fi
fi

organizations=$(gh api graphql --paginate -f enterpriseName="$enterpriseslug" -f query='
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
enterprise(slug: $enterpriseName) {
organizations(first: 100, after: $endCursor) {
nodes {
id
login
}
pageInfo {
endCursor
hasNextPage
}
}
}
}' --jq '.data.enterprise.organizations.nodes[].login')

if [ "$format" == "tsv" ]; then
echo -e "Organization\tActive\tURL\tCreated At\tUpdated At\tEvents"
fi

for org in $organizations
do
if [ "$format" == "tsv" ]; then
gh api "orgs/$org/hooks" --paginate | jq -r --arg org "$org" '.[] | [$org,.active,.config.url, .created_at, .updated_at, (.events | join(","))] | @tsv'
else
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}'
fi
done

0 comments on commit 8433d41

Please sign in to comment.