-
-
Save KyMidd/6aa4dd5e1ed9dfb189126b1dbc7d5f39 to your computer and use it in GitHub Desktop.
This file contains 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
get_org_repos() { | |
### | |
### Now that we have more than 1k repos, need to use paginated REST call to get all of them (search API hard limit of 1k) | |
### | |
# Grab Org info to get repo counts | |
ORG_INFO=$(curl -sL \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer $GITHUB_TOKEN"\ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/orgs/$GH_ORG) | |
# Filter org info to get repo counts | |
PRIVATE_REPO_COUNT=$(echo $ORG_INFO | jq -r '.owned_private_repos') | |
PUBLIC_REPO_COUNT=$(echo $ORG_INFO | jq -r '.public_repos') | |
TOTAL_REPO_COUNT=$(($PRIVATE_REPO_COUNT + $PUBLIC_REPO_COUNT)) | |
# Calculate number of pages needed to get all repos | |
REPOS_PER_PAGE=100 | |
PAGES_NEEDED=$(($TOTAL_REPO_COUNT / $REPOS_PER_PAGE)) | |
if [ $(($TOTAL_REPO_COUNT % $REPOS_PER_PAGE)) -gt 0 ]; then | |
PAGES_NEEDED=$(($PAGES_NEEDED + 1)) | |
fi | |
# Get all repos | |
for PAGE_NUMBER in $(seq $PAGES_NEEDED); do | |
echo "Getting repos page $PAGE_NUMBER of $PAGES_NEEDED" | |
# Could replace this with graphql call (would likely be faster, more efficient), but this works for now | |
PAGINATED_REPOS=$(curl -sL \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer $GITHUB_TOKEN"\ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"https://api.github.com/orgs/$GH_ORG/repos?per_page=$REPOS_PER_PAGE&sort=pushed&page=$PAGE_NUMBER" | jq -r ".[] | select(.topics[] | contains(\"$TOPIC\")) | .name") | |
# Combine all pages of repos into one variable | |
# Extra return added since last item in list doesn't have newline (would otherwise combine two repos on one line) | |
ALL_REPOS="${ALL_REPOS}"$'\n'"${PAGINATED_REPOS}" | |
done | |
# Find archived repos | |
ARCHIVED_REPOS=$(gh repo list $GH_ORG -L 1000 --archived | cut -d "/" -f 2 | cut -f 1) | |
ARCHIVED_REPOS_COUNT=$(echo "$ARCHIVED_REPOS" | wc -l | xargs) | |
# Remove archived repos from ALL_REPOS | |
echo "Skipping $ARCHIVED_REPOS_COUNT archived repos, they are read only" | |
for repo in $ARCHIVED_REPOS; do | |
ALL_REPOS=$(echo "$ALL_REPOS" | grep -Ev "^$repo$") | |
done | |
# Remove any empty lines | |
ALL_REPOS=$(echo "$ALL_REPOS" | awk 'NF') | |
# Get repo count | |
ALL_REPOS_COUNT=$(echo "$ALL_REPOS" | wc -l | xargs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment