From e2bc48c4cb0a758a9986faa23ed5828d2d18aaf8 Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Sat, 8 Feb 2025 22:13:47 -0800 Subject: [PATCH] feat: add whitelist --- list_repos_contributors_by_user.sh | 35 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index 886772a..1897c6a 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Define the whitelist of allowed collaborators +WHITELIST=("saravanakumardb" "saravanange" "abhinaisai2002" "sandho" "akshaj-us" "saravanakumardb1" "bytelyst-ai" "umadev0931") + # Fetch all private repositories where the user is an owner or collaborator REPO_DATA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ "https://api.github.com/user/repos?per_page=100&affiliation=owner,collaborator&visibility=private") @@ -13,24 +16,28 @@ if [[ -z "$REPO_LIST" ]]; then exit 1 fi -echo "🔍 Fetching collaborators for all private repositories..." +echo "🔍 Fetching repositories with non-whitelisted collaborators..." for REPO in $REPO_LIST; do - # Determine the actual owner (useful for org-owned repos) + # Determine the actual owner (useful if it belongs to an org) REPO_OWNER=$(echo "$REPO_DATA" | jq -r --arg REPO "$REPO" '.[] | select(.name==$REPO) | .owner.login') - echo "📂 Repository: $REPO (Owner: $REPO_OWNER)" - # Fetch all collaborators (includes users even if they haven't committed) - COLLABORATORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators" | jq -r '.[].login') + ALL_COLLABORATORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO/collaborators" | jq -r '.[].login') - # Display collaborators or notify if none found - if [[ -z "$COLLABORATORS" ]]; then - echo "🚫 No collaborators found." - else - echo "👥 Collaborators:" - echo "$COLLABORATORS" + # Identify non-whitelisted collaborators + NON_WHITELISTED_COLLABS=() + for COLLAB in $ALL_COLLABORATORS; do + if [[ ! " ${WHITELIST[@]} " =~ " ${COLLAB} " ]]; then + NON_WHITELISTED_COLLABS+=("$COLLAB") + fi + done + + # Only show repositories where non-whitelisted collaborators exist + if [[ ${#NON_WHITELISTED_COLLABS[@]} -gt 0 ]]; then + echo "🚨 Repository: $REPO (Owner: $REPO_OWNER)" + echo "❌ Non-Whitelisted Collaborators:" + printf '%s\n' "${NON_WHITELISTED_COLLABS[@]}" + echo "--------------------------------------------" fi - - echo "--------------------------------------------" done