Skip to content

Instantly share code, notes, and snippets.

@mdrokz
Created August 24, 2023 17:40
Show Gist options
  • Save mdrokz/1294708b54b82d4ff8fad5785721e16e to your computer and use it in GitHub Desktop.
Save mdrokz/1294708b54b82d4ff8fad5785721e16e to your computer and use it in GitHub Desktop.
Bash script to clone all the repos by organization by using the gh graphql API
#!/bin/bash
clone_repos_by_org() {
local orgName="$1"
local numRepos="$2"
local name=$(getReposByOrg "$orgName" "$numRepos" | jq "{nodes: .data.organization.repositories.nodes}")
cd ./repos
for ((i=0; i<numRepos; i++)); do
local repoName=$(echo $name | jq ".nodes[$i].name" | tr -d '"')
echo "Cloning $orgName/$repoName ..."
if [ ! "$name" == "null" ]; then
gh repo clone "$orgName/$repoName"
fi
done
}
getReposByOrg() {
local orgName="$1"
local numRepos="$2"
# Use the gh CLI to make the GraphQL query
gh api graphql -f query="query {
organization(login: \"$orgName\") {
repositories(first: $numRepos) {
nodes {
name
}
}
}
}"
}
# Call the function
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Please set the GITHUB_TOKEN environment variable."
exit 1
fi
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <organization_name> <number_of_repos_to_clone>"
exit 1
fi
clone_repos_by_org "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment