#!/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}" # 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 } 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 "❌ Failed to fetch user repositories." fi echo "🔍 Fetching organizations..." RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ "https://api.github.com/user/orgs") # 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 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 "❌ Failed to fetch repositories for organization: $ORG" fi echo "--------------------------------------------" done