From 5c581491357631e74817e923c33ae0a320390c51 Mon Sep 17 00:00:00 2001 From: Saravana Dhandapani Date: Wed, 21 May 2025 22:58:55 -0700 Subject: [PATCH] fix: list public repos --- list_all_public_repos.sh | 85 +++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 19 deletions(-) mode change 100644 => 100755 list_all_public_repos.sh diff --git a/list_all_public_repos.sh b/list_all_public_repos.sh old mode 100644 new mode 100755 index e094219..f696732 --- a/list_all_public_repos.sh +++ b/list_all_public_repos.sh @@ -1,39 +1,86 @@ #!/bin/bash +# Color codes using tput for better terminal compatibility +RED=$(tput setaf 1) +RESET=$(tput sgr0) + # Load environment variables GITHUB_TOKEN="${GITHUB_TOKEN:?❌ Error: GITHUB_TOKEN is not set in ~/.zshrc}" -echo "🔍 Fetching all public repositories owned by user..." -USER_PUBLIC_REPOS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/user/repos?per_page=100&type=public" | jq -r '.[].full_name') +# Function to check for API errors +check_api_error() { + local response="$1" + # First check if it's an object with a message field (error response) + local error_message=$(echo "$response" | jq -r 'if type == "object" and has("message") then .message else empty end') + + if [[ -n "$error_message" ]]; then + echo "❌ GitHub API Error: $error_message" + return 1 + fi + + # Then check if it's an array (expected response type) + local is_array=$(echo "$response" | jq -r 'if type == "array" then "true" else "false" end') + if [[ "$is_array" != "true" ]]; then + echo "❌ GitHub API Error: Unexpected response format" + return 1 + fi + + return 0 +} -if [[ -z "$USER_PUBLIC_REPOS" ]]; then - echo "❌ No public repositories found for user." +echo "🔍 Fetching all public repositories owned by user..." +RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/user/repos?per_page=100&type=public") + +# Check for API errors +if check_api_error "$RESPONSE"; then + USER_PUBLIC_REPOS=$(echo "$RESPONSE" | jq -r '.[].full_name') + + if [[ -z "$USER_PUBLIC_REPOS" ]]; then + echo "❌ No public repositories found for user." + else + printf "${RED}=> 📂 Public Repositories%s${RESET}\n" " (User-Owned)" > /dev/tty + echo "$USER_PUBLIC_REPOS" + echo "--------------------------------------------" + fi else - echo "📂 Public Repositories (User-Owned):" - echo "$USER_PUBLIC_REPOS" - echo "--------------------------------------------" + echo "❌ Failed to fetch user repositories." fi echo "🔍 Fetching organizations..." -ORG_LIST=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/user/orgs" | jq -r '.[].login') +RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/user/orgs") -if [[ -z "$ORG_LIST" ]]; then - echo "❌ No organizations found for user." +# Check for API errors +if check_api_error "$RESPONSE"; then + ORG_LIST=$(echo "$RESPONSE" | jq -r '.[].login') + + if [[ -z "$ORG_LIST" ]]; then + echo "❌ No organizations found for user." + exit 0 + fi +else + echo "❌ Failed to fetch organizations." exit 1 fi echo "🔍 Fetching all public repositories under organizations..." for ORG in $ORG_LIST; do - ORG_PUBLIC_REPOS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - "https://api.github.com/orgs/$ORG/repos?per_page=100&type=public" | jq -r '.[].full_name') - - if [[ -z "$ORG_PUBLIC_REPOS" ]]; then - echo "🚫 No public repositories found in organization: $ORG" + RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ + "https://api.github.com/orgs/$ORG/repos?per_page=100&type=public") + + # Check for API errors + if check_api_error "$RESPONSE"; then + ORG_PUBLIC_REPOS=$(echo "$RESPONSE" | jq -r '.[].full_name') + + if [[ -z "$ORG_PUBLIC_REPOS" ]]; then + echo "🚫 No public repositories found in organization: $ORG" + else + printf "${RED}=> 📂 Public Repositories%s${RESET}\n" " ($ORG)" > /dev/tty + echo "$ORG_PUBLIC_REPOS" + fi else - echo "📂 Public Repositories ($ORG):" - echo "$ORG_PUBLIC_REPOS" + echo "❌ Failed to fetch repositories for organization: $ORG" fi echo "--------------------------------------------" done