Created
March 30, 2024 11:30
-
-
Save rikwatson/e41a33ef5023325a71ae6828763032c4 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
from azure.devops.connection import Connection | |
from msrest.authentication import BasicAuthentication | |
import os | |
import git | |
''' | |
pip install azure-devops | |
pip install gitpython | |
''' | |
def clone_all_repos(organization: str, project_name: str, PAT: str, clone_directory: str): | |
# Azure DevOps organization URL | |
organization_url = f'https://dev.azure.com/{organization}' | |
# Authenticate | |
credentials = BasicAuthentication('', PAT) | |
connection = Connection(base_url=organization_url, creds=credentials) | |
# Get a client (the "core" client provides access to projects, teams, etc) | |
core_client = connection.clients.get_core_client() | |
# Get the project | |
project = core_client.get_project(project_name) | |
# Get a client for Git repositories | |
git_client = connection.clients.get_git_client() | |
# Get all repositories in the project | |
repos = git_client.get_repositories(project.id) | |
# Iterate through each repository and clone it | |
for repo in repos: | |
clone_url = repo.remote_url | |
repo_name = repo.name | |
repo_path = os.path.join(clone_directory, repo_name) | |
if not os.path.exists(repo_path): | |
os.makedirs(repo_path) | |
print(f"Cloning {repo_name}...") | |
try: | |
git.Repo.clone_from(clone_url, repo_path) | |
print(f"Repository '{repo_name}' cloned successfully.") | |
except git.exc.GitCommandError as e: | |
print(f"Failed to clone repository '{repo_name}': {e}") | |
def main(): | |
ORG = os.getenv("ORG") | |
if ORG is None: | |
print("Error: Environment variable ORG is not set.") | |
return | |
PROJECT = os.getenv("PROJECT") | |
if PROJECT is None: | |
print("Error: Environment variable PROJECT is not set.") | |
return | |
PAT = os.getenv("PAT") | |
if PAT is None: | |
print("Error: Environment variable PAT is not set.") | |
return | |
GIT = os.getenv("GIT") | |
if GIT is None: | |
print("Error: Environment variable GIT is not set.") | |
return | |
clone_all_repos(ORG, PROJECT, PAT, GIT) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment