From 3bd5260fc3848a0c28f4d4fd3ca9e884e57d25d3 Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Sat, 8 Feb 2025 22:07:20 -0800 Subject: [PATCH] feat: colloborators --- list_repos_contributors_by_user.sh | 41 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index dca54a0..886772a 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -1,33 +1,36 @@ #!/bin/bash -# Fetch all private repositories where you are an owner or collaborator -REPOS_DATA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ +# 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") # Extract repository names -REPOS=$(echo "$REPOS_DATA" | jq -r '.[].name') +REPO_LIST=$(echo "$REPO_DATA" | jq -r '.[].name') -if [[ -z "$REPOS" ]]; then - echo "❌ No private repositories found or token missing 'repo' scope." +# Exit if no repositories found +if [[ -z "$REPO_LIST" ]]; then + echo "❌ No private repositories found or token is missing the 'repo' scope." exit 1 fi -echo "Fetching contributors and owners from all private repositories..." -for REPO in $REPOS; do - # Extract the repo owner (useful if it belongs to an org) - OWNER=$(echo "$REPOS_DATA" | jq -r --arg REPO "$REPO" '.[] | select(.name==$REPO) | .owner.login') +echo "🔍 Fetching collaborators for all private repositories..." +for REPO in $REPO_LIST; do + # Determine the actual owner (useful for org-owned repos) + REPO_OWNER=$(echo "$REPO_DATA" | jq -r --arg REPO "$REPO" '.[] | select(.name==$REPO) | .owner.login') - echo "🔍 Scanning $REPO (Owner: $OWNER)..." - - # List contributors - CONTRIBUTORS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/repos/$OWNER/$REPO/contributors" | jq -r '.[].login') + echo "📂 Repository: $REPO (Owner: $REPO_OWNER)" - if [[ -z "$CONTRIBUTORS" ]]; then - echo "🚫 No contributors found. (Owner: $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') + + # Display collaborators or notify if none found + if [[ -z "$COLLABORATORS" ]]; then + echo "🚫 No collaborators found." else - echo "📌 Owner: $OWNER" - echo "$CONTRIBUTORS" + echo "👥 Collaborators:" + echo "$COLLABORATORS" fi -done + echo "--------------------------------------------" +done