Skip to content

Instantly share code, notes, and snippets.

@duboisf
Last active February 17, 2023 21:41
Show Gist options
  • Save duboisf/76714b89b42d105abf87bbaa99dd510f to your computer and use it in GitHub Desktop.
Save duboisf/76714b89b42d105abf87bbaa99dd510f to your computer and use it in GitHub Desktop.
GitHub org collaborators csv report
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
if (( $# != 1 )); then
echo "This script takes 1 argument, the GitHub org name" >&2
exit 1
fi
ORG=$1
echo "Getting list of repositories for GitHub org $ORG..."
gh api graphql --paginate -F org=$ORG -f query='
query ($org: String!, $endCursor: String) {
organization(login: $org) {
repositories(first: 100, after: $endCursor) {
nodes {
name
}
pageInfo {
endCursor
hasNextPage
}
}
}
}' | jq -r .data.organization.repositories.nodes[].name > /tmp/repos
COLLABORATORS=/tmp/collaborators
if [[ -d $COLLABORATORS ]]; then
rm -rf $COLLABORATORS
fi
mkdir $COLLABORATORS
for REPO in $(cat /tmp/repos); do
echo "Getting collaborators for repo $REPO..."
gh api graphql --paginate -F org=$ORG -F repo=$REPO -f query='
query ($org: String!, $repo: String!, $endCursor: String) {
organization(login: $org) {
repository(name: $repo) {
name
collaborators(first: 100, after: $endCursor) {
pageInfo {
endCursor
hasNextPage
}
edges {
node {
login
}
permission
permissionSources {
permission
source {
__typename
... on Organization {
name: login
}
... on Repository {
name
}
... on Team {
name: slug
}
}
}
}
}
}
}
}
' | jq -r '
.data.organization.repository.name as $repo
| .data.organization.repository.collaborators.edges[]
| .node.login as $collaborator
| .permissionSources[]
| "\($repo),\($collaborator),\(.permission),\(.source["__typename"]),\(.source.name)"
' > $COLLABORATORS/$REPO \
|| true
done
echo "Sorting and merging results"
OUTPUT=/tmp/${ORG}_collaborators.csv
echo "Repository,Collaborator,Permission,Permission Source Type,Permission Source Name" > $OUTPUT
cat $COLLABORATORS/* | sort >> $OUTPUT
echo "Done! The result is in the file $OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment