Skip to content

Instantly share code, notes, and snippets.

@timabell
Created February 11, 2025 15:57
Show Gist options
  • Save timabell/23de04727c281875aec31ca4637cb373 to your computer and use it in GitHub Desktop.
Save timabell/23de04727c281875aec31ca4637cb373 to your computer and use it in GitHub Desktop.
Clone all git repos in an azure devops org/project
#!/bin/bash
# list and clone repos on azure devops
# Written by gpt with a bit of prompting https://chatgpt.com/share/67ab6f86-fc00-8006-b3f8-a1c9bd252fe7
# Check if Azure CLI is installed
check_az_cli() {
if ! command -v az &> /dev/null; then
echo "Error: Azure CLI is not installed. Please install it first."
exit 1
fi
}
# Display usage information
show_usage() {
cat << EOF
Usage: $0 <login|list|clone> <ORG_NAME> [PROJECT_NAME]
Commands:
login <ORG_NAME> Authenticate and open the PAT (Personal Access Token) page
list <ORG_NAME> <PROJECT_NAME> List all repositories in the specified project
clone <ORG_NAME> <PROJECT_NAME> Clone all repositories from the specified project
Examples:
# Login and authenticate
$0 login YOUR_ORG
# List all repositories in a project
$0 list YOUR_ORG YOUR_PROJECT
# Clone all repositories in a project
$0 clone YOUR_ORG YOUR_PROJECT
EOF
exit 1
}
# Open browser to generate a Personal Access Token (PAT) and log in
login_devops() {
local org_name=$1
local token_url="https://dev.azure.com/$org_name/_usersSettings/tokens"
echo "Opening Azure DevOps token page: $token_url"
if command -v xdg-open &> /dev/null; then
xdg-open "$token_url" # Linux
elif command -v open &> /dev/null; then
open "$token_url" # macOS
else
echo "Please open this URL in your browser: $token_url"
fi
echo "Logging into Azure DevOps..."
az devops login
}
# Fetch repository list from Azure DevOps
fetch_repos() {
local org_url="https://dev.azure.com/$ORG_NAME"
az devops configure --defaults organization="$org_url" project="$PROJECT_NAME"
# sshUrl / remoteUrl for ssh/http clone
az repos list --org "$org_url" --project "$PROJECT_NAME" --query "[].{name:name, url:sshUrl}" -o tsv
}
# List repositories
list_repos() {
local repos
repos=$(fetch_repos)
if [[ -z "$repos" ]]; then
echo "No repositories found in $ORG_NAME/$PROJECT_NAME."
exit 1
fi
echo "Repositories in $ORG_NAME/$PROJECT_NAME:"
echo "$repos" | awk '{print $1}'
}
# Clone repositories
clone_repos() {
local repos
repos=$(fetch_repos)
if [[ -z "$repos" ]]; then
echo "No repositories found in $ORG_NAME/$PROJECT_NAME."
exit 1
fi
echo "Cloning repositories into $(pwd)..."
while IFS=$'\t' read -r repo_name repo_url; do
echo "Cloning $repo_name..."
git clone "$repo_url"
done <<< "$repos"
echo "All repositories cloned successfully!"
}
# Main logic
main() {
check_az_cli
if [[ $# -lt 2 ]]; then
show_usage
fi
COMMAND=$1
ORG_NAME=$2
PROJECT_NAME=$3
case "$COMMAND" in
login) login_devops "$ORG_NAME" ;;
list) list_repos ;;
clone) clone_repos ;;
*) show_usage ;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment