This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRepositoryCloneService.cs
More file actions
164 lines (146 loc) · 6.34 KB
/
Copy pathRepositoryCloneService.cs
File metadata and controls
164 lines (146 loc) · 6.34 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Extensions;
using GitHub.Helpers;
using GitHub.Logging;
using GitHub.Models;
using GitHub.Primitives;
using Microsoft.VisualStudio.Shell;
using Octokit.GraphQL;
using Octokit.GraphQL.Model;
using Rothko;
using Serilog;
using Task = System.Threading.Tasks.Task;
namespace GitHub.Services
{
/// <summary>
/// Service used to clone GitHub repositories. It wraps the
/// <see cref="Microsoft.TeamFoundation.Git.Controls.Extensibility.IGitRepositoriesExt"/> service provided
/// by Team Explorer.
/// </summary>
[Export(typeof(IRepositoryCloneService))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class RepositoryCloneService : IRepositoryCloneService
{
static readonly ILogger log = LogManager.ForContext<RepositoryCloneService>();
readonly IOperatingSystem operatingSystem;
readonly string defaultClonePath;
readonly IVSGitServices vsGitServices;
readonly IGraphQLClientFactory graphqlFactory;
readonly IUsageTracker usageTracker;
ICompiledQuery<ViewerRepositoriesModel> readViewerRepositories;
[ImportingConstructor]
public RepositoryCloneService(
IOperatingSystem operatingSystem,
IVSGitServices vsGitServices,
IGraphQLClientFactory graphqlFactory,
IUsageTracker usageTracker)
{
this.operatingSystem = operatingSystem;
this.vsGitServices = vsGitServices;
this.graphqlFactory = graphqlFactory;
this.usageTracker = usageTracker;
defaultClonePath = GetLocalClonePathFromGitProvider(operatingSystem.Environment.GetUserRepositoriesPath());
}
/// <inheritdoc/>
public async Task<ViewerRepositoriesModel> ReadViewerRepositories(HostAddress address)
{
if (readViewerRepositories == null)
{
var order = new RepositoryOrder
{
Field = RepositoryOrderField.Name,
Direction = OrderDirection.Asc
};
var affiliation = new RepositoryAffiliation?[]
{
RepositoryAffiliation.Owner, RepositoryAffiliation.Collaborator
};
var repositorySelection = new Fragment<Repository, RepositoryListItemModel>(
"repository",
repo => new RepositoryListItemModel
{
IsFork = repo.IsFork,
IsPrivate = repo.IsPrivate,
Name = repo.Name,
Owner = repo.Owner.Login,
Url = new Uri(repo.Url),
});
readViewerRepositories = new Query()
.Viewer
.Select(viewer => new ViewerRepositoriesModel
{
Owner = viewer.Login,
Repositories = viewer.Repositories(null, null, null, null, null, order, affiliation, null, null)
.AllPages()
.Select(repositorySelection).ToList(),
OrganizationRepositories = viewer.Organizations(null, null, null, null).AllPages().Select(org => new
{
org.Login,
Repositories = org.Repositories(null, null, null, null, null, order, null, null, null)
.AllPages()
.Select(repositorySelection).ToList()
}).ToDictionary(x => x.Login, x => (IReadOnlyList<RepositoryListItemModel>)x.Repositories),
}).Compile();
}
var graphql = await graphqlFactory.CreateConnection(address).ConfigureAwait(false);
var result = await graphql.Run(readViewerRepositories).ConfigureAwait(false);
return result;
}
/// <inheritdoc/>
public async Task CloneRepository(
string cloneUrl,
string repositoryPath,
object progress = null)
{
Guard.ArgumentNotEmptyString(cloneUrl, nameof(cloneUrl));
Guard.ArgumentNotEmptyString(repositoryPath, nameof(repositoryPath));
// Switch to a thread pool thread for IO then back to the main thread to call
// vsGitServices.Clone() as this must be called on the main thread.
await ThreadingHelper.SwitchToPoolThreadAsync();
operatingSystem.Directory.CreateDirectory(repositoryPath);
await ThreadingHelper.SwitchToMainThreadAsync();
try
{
await vsGitServices.Clone(cloneUrl, repositoryPath, true, progress);
await usageTracker.IncrementCounter(x => x.NumberOfClones);
var repositoryUrl = new UriString(cloneUrl).ToRepositoryUrl();
var isDotCom = HostAddress.IsGitHubDotComUri(repositoryUrl);
if (isDotCom)
{
await usageTracker.IncrementCounter(x => x.NumberOfGitHubClones);
}
else
{
// If it isn't a GitHub URL, assume it's an Enterprise URL
await usageTracker.IncrementCounter(x => x.NumberOfEnterpriseClones);
}
}
catch (Exception ex)
{
log.Error(ex, "Could not clone {CloneUrl} to {Path}", cloneUrl, repositoryPath);
throw;
}
}
/// <inheritdoc/>
public bool DestinationExists(string path) => Directory.Exists(path) || File.Exists(path);
string GetLocalClonePathFromGitProvider(string fallbackPath)
{
var ret = vsGitServices.GetLocalClonePathFromGitProvider();
return !string.IsNullOrEmpty(ret)
? operatingSystem.Environment.ExpandEnvironmentVariables(ret)
: fallbackPath;
}
public string DefaultClonePath { get { return defaultClonePath; } }
class OrganizationAdapter
{
public IReadOnlyList<RepositoryListItemModel> Repositories { get; set; }
}
}
}