Skip to content

Instantly share code, notes, and snippets.

@jmhublar
Created November 17, 2024 22:42
Show Gist options
  • Save jmhublar/a4eed481fb015a438cdca9d2aea91d7f to your computer and use it in GitHub Desktop.
Save jmhublar/a4eed481fb015a438cdca9d2aea91d7f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Ensure you have authenticated with the GitHub CLI
if ! gh auth status &>/dev/null; then
echo "Please authenticate with GitHub CLI using 'gh auth login'."
exit 1
fi
# Get the current authenticated username
username=$(gh api user --jq '.login')
# Fetch all repositories for the authenticated user
repos=$(gh repo list "$username" --visibility public --json name,description,url --jq '.[] | @base64')
# Check if no repositories are found
if [[ -z "$repos" ]]; then
echo "No public repositories found."
exit 0
fi
# Iterate through the repositories
echo "Scanning your public repositories..."
for repo_data in $repos; do
# Decode the base64 JSON object
repo=$(echo "$repo_data" | base64 --decode)
# Extract name, description, and URL
repo_name=$(echo "$repo" | jq -r '.name')
repo_description=$(echo "$repo" | jq -r '.description')
repo_url=$(echo "$repo" | jq -r '.url')
# Display repository details
echo -e "\nRepository: $repo_name"
echo "URL: $repo_url"
echo "Description: ${repo_description:-No description provided.}"
# Prompt for deletion
read -p "Do you want to delete this repository? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
gh repo delete "$username/$repo_name" --confirm
echo "Repository '$repo_name' has been deleted."
else
echo "Skipped repository '$repo_name'."
fi
done
echo -e "\nFinished scanning repositories."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment