forked from microsoft/PullRequestQuantifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (66 loc) · 2.78 KB
/
Program.cs
File metadata and controls
72 lines (66 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
namespace PullRequestQuantifier.Tools.CloneAdoRepo
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using LibGit2Sharp;
using PullRequestQuantifier.Tools.Common;
using PullRequestQuantifier.Tools.Common.Model;
using YamlDotNet.Serialization;
using Repository = LibGit2Sharp.Repository;
[ExcludeFromCodeCoverage]
public static class Program
{
public static async Task Main(string[] args)
{
var commandLine = new CommandLine(args);
var organizations = new DeserializerBuilder().Build().Deserialize<List<Organization>>(await File.ReadAllTextAsync(commandLine.ConfigFile));
await Clone(
organizations,
commandLine.ClonePath,
commandLine.User,
commandLine.Pat);
}
private static async Task Clone(
IEnumerable<Organization> organizations,
string clonePath,
string userName,
string pat)
{
var credentials = new NetworkCredential(userName, pat);
var cloneOptions = new CloneOptions();
cloneOptions.FetchOptions.CredentialsProvider = (url, user, cred) => new SecureUsernamePasswordCredentials
{
Username = credentials.UserName,
Password = credentials.SecurePassword
};
foreach (var organization in organizations)
{
foreach (var project in organization.Projects)
{
foreach (var repository in project.Repositories)
{
var path = Path.Combine(clonePath, repository.Name);
var repoRoot = Repository.Discover(path);
// don't crash when there is no repo to this path, return empty changes
if (repoRoot != null)
{
Console.WriteLine($"Repo {repository.Name} already exists! Continue with other.");
continue;
}
Console.WriteLine($"Cloning {repository.Name} repository!");
Directory.CreateDirectory(path);
await Task.Run(() => Repository.Clone(
$"https://{Uri.EscapeDataString(organization.Name)}@dev.azure.com/{Uri.EscapeDataString(organization.Name)}/" +
$"{Uri.EscapeDataString(project.Name)}/_git/{Uri.EscapeDataString(repository.Name)}",
path,
cloneOptions));
}
}
}
}
}
}