From fe40d1be7f4309ed4f281a9f331bf8e613a34e95 Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Sat, 8 Feb 2025 21:17:55 -0800 Subject: [PATCH] fix: contributers --- list_repos_contributors_by_user.sh | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/list_repos_contributors_by_user.sh b/list_repos_contributors_by_user.sh index 29db340..dca54a0 100644 --- a/list_repos_contributors_by_user.sh +++ b/list_repos_contributors_by_user.sh @@ -1,13 +1,33 @@ #!/bin/bash -# List all repositories for the user -REPOS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/users/$GITHUB_USER/repos?per_page=100&visibility=private" | jq -r '.[].name') +# Fetch all private repositories where you are an owner or collaborator +REPOS_DATA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/user/repos?per_page=100&affiliation=owner,collaborator&visibility=private") -echo "Fetching contributors from all repositories..." +# Extract repository names +REPOS=$(echo "$REPOS_DATA" | jq -r '.[].name') + +if [[ -z "$REPOS" ]]; then + echo "❌ No private repositories found or token missing 'repo' scope." + exit 1 +fi + +echo "Fetching contributors and owners from all private repositories..." for REPO in $REPOS; do - echo "🔍 Scanning $REPO..." - curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/repos/$GITHUB_USER/$REPO/contributors" | jq -r '.[].login' + # 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 "🔍 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') + + if [[ -z "$CONTRIBUTORS" ]]; then + echo "🚫 No contributors found. (Owner: $OWNER)" + else + echo "📌 Owner: $OWNER" + echo "$CONTRIBUTORS" + fi done