Last active
June 24, 2024 23:34
-
-
Save joost/ec18beaed76098b4bd266b1caaeb4f73 to your computer and use it in GitHub Desktop.
Clone or pull all your Azure DevOps repo's (for all projects)
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
export AZURE_PERSONAL_ACCESS_TOKEN=YOUR_AZURE_ACCESS_TOKEN | |
export AZURE_ORGANIZATION_URL=https://dev.azure.com/YOURORG |
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
#! /usr/local/bin/python3 | |
from azure.devops.connection import Connection | |
from msrest.authentication import BasicAuthentication | |
# https://github.com/theskumar/python-dotenv | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
# Fill in with your personal access token and org URL | |
personal_access_token = os.getenv("AZURE_PERSONAL_ACCESS_TOKEN") | |
organization_url = os.getenv("AZURE_ORGANIZATION_URL") | |
# Create a connection to the org | |
credentials = BasicAuthentication('', personal_access_token) | |
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() | |
def clone_or_pull_repos_for_project_id(project): | |
git_client = connection.clients.get_git_client() | |
repos = git_client.get_repositories(project.id) | |
os.system(f"mkdir -p {project.name}") | |
for repo in repos: | |
target_dir = os.path.join(os.getcwd(), project.name, repo.name) | |
print(f"{repo.id} : {repo.name} ({target_dir})") | |
if os.path.isdir(target_dir): | |
os.system(f"cd {target_dir};git pull") | |
else: | |
os.system(f"cd {project.name};git clone {repo.web_url} \"{target_dir}\"") | |
# # Get the first page of projects | |
get_projects_response = core_client.get_projects() | |
index = 0 | |
while get_projects_response is not None: | |
for project in get_projects_response.value: | |
print(f"[{str(index)}] ({project.id}) {project.name}") | |
index += 1 | |
clone_or_pull_repos_for_project_id(project) | |
if get_projects_response.continuation_token is not None and get_projects_response.continuation_token != "": | |
# Get the next page of projects | |
get_projects_response = core_client.get_projects(continuation_token=get_projects_response.continuation_token) | |
else: | |
# All projects have been retrieved | |
get_projects_response = None |
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
python-dotenv==0.10.3 | |
azure-devops==5.1.0b4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment