Created
July 10, 2023 20:03
-
-
Save azigler/ea04b0f1b18339b4c763a3477bb72899 to your computer and use it in GitHub Desktop.
Starfetcher ⭐️ fetch all GitHub stars for an organization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Starfetcher | |
# This script uses 'jq', a command-line JSON processor. | |
# Installation instructions: | |
# - On Ubuntu or other Debian-based systems: sudo apt-get install jq | |
# - On CentOS, Fedora, or RHEL: sudo yum install jq | |
# - On macOS: brew install jq | |
# - On Windows, you can download it from https://stedolan.github.io/jq/download/ | |
# Use the following flags: | |
# -d: Enable debug mode (optional) | |
# -n: Provide the name of the GitHub organization (required) | |
# -a: Provide your GitHub Personal Access Token (optional, but recommended to avoid rate limits) | |
# -f: Include archived repositories in the star count (optional, by default archived repositories are not counted) | |
TOKEN="" | |
org_name="" | |
total_stars=0 | |
filter_archived=true | |
page=1 | |
debug=false | |
# Check flags | |
while getopts ":fda:n:" option; do | |
case "${option}" in | |
f) | |
filter_archived=false | |
;; | |
d) | |
debug=true | |
;; | |
a) | |
TOKEN=${OPTARG} | |
;; | |
n) | |
org_name=${OPTARG} | |
;; | |
esac | |
done | |
# Validate that organization name has been provided | |
if [[ -z "${org_name}" ]]; then | |
echo "Organization name is required. Use -n flag to provide it." | |
exit 1 | |
fi | |
# Setting up the Authorization header if TOKEN is provided | |
if [[ -n "${TOKEN}" ]]; then | |
AUTH_HEADER="Authorization: token ${TOKEN}" | |
else | |
AUTH_HEADER="" | |
fi | |
# Stylized starting message | |
echo "🚀 Starfetcher 🌟" | |
echo "Fetching star count for the '${org_name}' GitHub organization..." | |
if [ "$debug" = true ] ; then | |
echo "Debug mode: ON" | |
echo "Include archived repos: $filter_archived" | |
fi | |
while : ; do | |
# Get all repos for the organization | |
if [ "$debug" = true ] ; then | |
echo " Fetching page ${page} for organization: ${org_name}..." | |
fi | |
response=$(curl -s -H "${AUTH_HEADER}" "https://api.github.com/users/${org_name}/repos?per_page=100&page=${page}") | |
if [ "$filter_archived" = true ] ; then | |
repos=$(echo "${response}" | jq -r '.[] | select(.archived == false) | .name') | |
else | |
repos=$(echo "${response}" | jq -r '.[] | .name') | |
fi | |
# If no repo names are returned, we've reached the end of the pagination | |
if [ -z "$repos" ]; then | |
break | |
fi | |
for repo in $repos; do | |
if [ "$debug" = true ] ; then | |
echo " Processing repository: ${repo}..." | |
fi | |
# Fetch stargazers_count for each repo and add it to total | |
if [ "$debug" = true ] ; then | |
echo " Fetching star count for repository: ${repo}..." | |
fi | |
stargazers_count=$(curl -s -H "${AUTH_HEADER}" "https://api.github.com/repos/${org_name}/${repo}" | jq -r '.stargazers_count') | |
if [ "$debug" = true ] ; then | |
echo " Star count for repository ${repo}: ${stargazers_count}" | |
fi | |
total_stars=$((total_stars + stargazers_count)) | |
done | |
# Move to the next page | |
page=$((page + 1)) | |
done | |
echo "✨ Total stars for the '${org_name}' organization: ${total_stars}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment