Skip to content

Instantly share code, notes, and snippets.

@angelbarrera92
Last active February 1, 2021 12:40
Show Gist options
  • Save angelbarrera92/cd6d1db7e1d424b5543e9e8a95c59e02 to your computer and use it in GitHub Desktop.
Save angelbarrera92/cd6d1db7e1d424b5543e9e8a95c59e02 to your computer and use it in GitHub Desktop.
Download repositories from the GitHub api of an entire organization
#!/bin/bash
# Download repositories from the GitHub api of an entire organization
# It downloads the default branch of each repository
# Creating .tar file
# Usage: ./download_organization_repositories.sh k8spin k8spin_repositories ${GITHUB_PERSONAL_ACCESS_TOKEN}
# Requires curl, jq and bash
org_name="$1"
output_dir="$2"
token="$3" # GitHub personal access token with repo scope
mkdir -p ${output_dir}
PAGE=1
curl -L -u "$token:" -s -o repos.json "https://api.github.com/orgs/${org_name}/repos?per_page=100&page=${PAGE}"
REPOS=$(jq length repos.json)
while [ $REPOS -gt 0 ]
do
echo "PAGE: ${PAGE}"
echo "REPOSITORIES: $REPOS"
for (( r=0; r<${REPOS}; r++ ))
do
echo "--${r}/${REPOS}--"
repo_name=$(jq -r '.['"${r}"'].name' repos.json)
default_branch=$(jq -r '.['"${r}"'].default_branch' repos.json)
echo $repo_name
echo $default_branch
repo_url="https://api.github.com/repos/${org_name}/${repo_name}/tarball/${default_branch}"
echo $repo_url
curl -L -o "./${output_dir}/${repo_name}.tar" -s -u "$token:" ${repo_url}
echo "--NEXT--"
done
PAGE=$(( $PAGE + 1 ))
curl -L -u "$token:" -s -o repos.json "https://api.github.com/orgs/${org_name}/repos?per_page=100&page=${PAGE}"
REPOS=$(jq length repos.json)
done
rm -rf repos.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment