Skip to content

Instantly share code, notes, and snippets.

@lukepearson
Last active August 10, 2022 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepearson/4c9a289151dc3a4dc27b7b80c7743d3e to your computer and use it in GitHub Desktop.
Save lukepearson/4c9a289151dc3a4dc27b7b80c7743d3e to your computer and use it in GitHub Desktop.
Clone all git repositories within an Azure DevOps Organisation
const { execSync } = require('child_process');
const { existsSync } = require('fs');
const { escape } = require('querystring');
const accessToken="PERSONAL_ACCESS_TOKEN";
const organisation="ORG_NAME";
const headers = {
Authorization: `Basic ${btoa(`:${accessToken}`)}`,
'Content-Type': 'application/json',
};
const clone = async () => {
// Fetch all projects for the organisation
const projects = await fetch(`https://dev.azure.com/${organisation}/_apis/projects?api-version=7.0`, {
headers,
}).then(response => response.json())
console.log(projects);
for (var i=0; i<projects.value.length; i++) {
const project = projects.value[i];
const projectName = project.name;
const repos = await fetch(`https://dev.azure.com/${organisation}/${projectName}/_apis/git/repositories?api-version=7.0`, {
headers,
}).then(response => response.json());
console.log(repos);
// Fetch all repositories for the project
for (var j=0; j<repos.value.length; j++) {
const repo = repos.value[j];
const repoName = repo.name;
const targetDirectory = `${projectName}/${repoName}`;
if (existsSync(targetDirectory)) {
console.log(`${targetDirectory} already exists`);
continue;
}
if (repo.isDisabled) {
console.log(`${repoName} is disabled`);
continue;
}
const cloneUri = `${organisation}@vs-ssh.visualstudio.com:v3/${escape(organisation)}/${escape(projectName)}/${escape(repoName)}`;
console.log(`Cloning ${cloneUri} to ${targetDirectory}`);
// Clone project
try {
const result = execSync(`git clone "${cloneUri}" "${targetDirectory}"`).toString();
console.log(result + '\n');
} catch (error) {
console.log(error.toString());
}
}
}
}
clone();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment