From cc1fe90794f6644653b23d7641a0ccc0813ad952 Mon Sep 17 00:00:00 2001 From: Brian Lin Date: Thu, 6 Jan 2022 12:21:16 -0600 Subject: [PATCH] Add script to scan for repos where a user has collab access --- check-gh-collab.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 check-gh-collab.sh diff --git a/check-gh-collab.sh b/check-gh-collab.sh new file mode 100755 index 0000000..b9a0871 --- /dev/null +++ b/check-gh-collab.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +usage () { + echo "Usage: check-collab.sh " + exit 1 +} + +[[ $# -ne 2 ]] && usage + +ORG=$1 +USER=$2 + +REPOS_PER_PAGE=100 + +# TODO: figure out a way to find the last page containing repos +for i in $(seq 5); do + repos+=$(curl --stderr /dev/null \ + -H "Authorization: token $(cat ~/.github/oauth2)" \ + -H "Accept: application/vnd.github.v3+json" "https://api.github.com/orgs/$ORG/repos?per_page=$REPOS_PER_PAGE&page=$i" \ + | jq ".[].full_name" \ + | tr -d '"') +done + +echo "$USER is a collaborator on the following repositories:" + +for repo in ${repos[*]}; do + response=$(curl -o /dev/null \ + --stderr /dev/null \ + -H "Authorization: token $(cat ~/.github/oauth2)" \ + -w "%{http_code}" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$repo/collaborators/$USER") + # https://docs.github.com/en/rest/reference/collaborators#check-if-a-user-is-a-repository-collaborator + if [[ $response == "204" ]]; then + echo "- $repo" + fi +done