Created
August 14, 2023 14:51
-
-
Save caseyw/caad209f01a35d6af530121b387ff93f to your computer and use it in GitHub Desktop.
Clone repos in an organization with gh/jq/bash
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 | |
# Verify ghcli is installed. | |
if ! command -v gh >/dev/null 2>&1; then | |
echo "Install gh first" | |
exit 1 | |
fi | |
# Verify jq is installed. | |
if ! command -v jq >/dev/null 2>&1; then | |
echo "Install jq first" | |
exit 1 | |
fi | |
# Verify we're logged in. | |
if ! gh auth status >/dev/null 2>&1; then | |
echo "You need to login: gh auth login" | |
exit 1 | |
fi | |
# Check if the organization name is provided. | |
if [ -z "$1" ]; then | |
echo "Please provide the organization name." | |
exit 1 | |
fi | |
organization_name="$1" | |
limit=${2:-100} | |
# Get non-archived repositories from the specified organization. | |
repos=$(gh repo list "$organization_name" -L"$limit" --no-archived --json name,sshUrl | jq -r '.[] | (.name + " " + .sshUrl)') | |
# Iterate through the repositories and clone them if the directory doesn't exist. | |
while read -r line; do | |
name=$(echo "$line" | awk '{print $1}') | |
sshUrl=$(echo "$line" | awk '{print $2}') | |
if [ ! -d "$name" ]; then | |
echo "Cloning $name from $sshUrl..." | |
git clone "$sshUrl" "$name" | |
else | |
echo "Directory $name already exists from $sshUrl. Skipping..." | |
fi | |
echo | |
done <<< "$repos" | |
echo "Fin" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment