fix: list public repos

This commit is contained in:
Saravana Dhandapani 2025-05-21 22:58:55 -07:00
parent 6d9f4f39a8
commit 5c58149135

83
list_all_public_repos.sh Normal file → Executable file
View File

@ -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 [[ -z "$USER_PUBLIC_REPOS" ]]; then
echo "❌ No public repositories found for user."
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 "📂 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')
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$ORG/repos?per_page=100&type=public")
if [[ -z "$ORG_PUBLIC_REPOS" ]]; then
echo "🚫 No public repositories found in organization: $ORG"
# 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