fix: list public repos

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

85
list_all_public_repos.sh Normal file → Executable file
View File

@ -1,39 +1,86 @@
#!/bin/bash #!/bin/bash
# Color codes using tput for better terminal compatibility
RED=$(tput setaf 1)
RESET=$(tput sgr0)
# Load environment variables # Load environment variables
GITHUB_TOKEN="${GITHUB_TOKEN:?❌ Error: GITHUB_TOKEN is not set in ~/.zshrc}" GITHUB_TOKEN="${GITHUB_TOKEN:?❌ Error: GITHUB_TOKEN is not set in ~/.zshrc}"
echo "🔍 Fetching all public repositories owned by user..." # Function to check for API errors
USER_PUBLIC_REPOS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ check_api_error() {
"https://api.github.com/user/repos?per_page=100&type=public" | jq -r '.[].full_name') 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 "🔍 Fetching all public repositories owned by user..."
echo "❌ No public repositories found for 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 else
echo "📂 Public Repositories (User-Owned):" echo "❌ Failed to fetch user repositories."
echo "$USER_PUBLIC_REPOS"
echo "--------------------------------------------"
fi fi
echo "🔍 Fetching organizations..." echo "🔍 Fetching organizations..."
ORG_LIST=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/user/orgs" | jq -r '.[].login') "https://api.github.com/user/orgs")
if [[ -z "$ORG_LIST" ]]; then # Check for API errors
echo "❌ No organizations found for user." 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 exit 1
fi fi
echo "🔍 Fetching all public repositories under organizations..." echo "🔍 Fetching all public repositories under organizations..."
for ORG in $ORG_LIST; do for ORG in $ORG_LIST; do
ORG_PUBLIC_REPOS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$ORG/repos?per_page=100&type=public" | jq -r '.[].full_name') "https://api.github.com/orgs/$ORG/repos?per_page=100&type=public")
if [[ -z "$ORG_PUBLIC_REPOS" ]]; then # Check for API errors
echo "🚫 No public repositories found in organization: $ORG" 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 else
echo "📂 Public Repositories ($ORG):" echo "❌ Failed to fetch repositories for organization: $ORG"
echo "$ORG_PUBLIC_REPOS"
fi fi
echo "--------------------------------------------" echo "--------------------------------------------"
done done