This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRepositoryProvider.cs
More file actions
287 lines (246 loc) · 11.8 KB
/
Copy pathRepositoryProvider.cs
File metadata and controls
287 lines (246 loc) · 11.8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using GitHubExtension.Client;
using GitHubExtension.Helpers;
using Microsoft.Windows.DevHome.SDK;
using Octokit;
using Serilog;
using Windows.Foundation;
using Windows.Storage.Streams;
namespace GitHubExtension.Providers;
public class RepositoryProvider : IRepositoryProvider
{
private static readonly Lazy<ILogger> _log = new(() => Serilog.Log.ForContext("SourceContext", nameof(RepositoryProvider)));
private static readonly ILogger Log = _log.Value;
public string DisplayName => Resources.GetResource(@"RepositoryProviderDisplayName");
public IRandomAccessStreamReference Icon
{
get; private set;
}
public RepositoryProvider()
{
Icon = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///GitHubExtension/Assets/GitHubLogo_Dark.png"));
}
public IAsyncOperation<RepositoryUriSupportResult> IsUriSupportedAsync(Uri uri)
{
return IsUriSupportedAsync(uri, null);
}
public IAsyncOperation<RepositoryUriSupportResult> IsUriSupportedAsync(Uri uri, IDeveloperId? developerId)
{
return Task.Run(() =>
{
if (!Validation.IsValidGitHubURL(uri))
{
return new RepositoryUriSupportResult(false);
}
var owner = Validation.ParseOwnerFromGitHubURL(uri);
if (string.IsNullOrEmpty(owner))
{
return new RepositoryUriSupportResult(false);
}
var repoName = Validation.ParseRepositoryFromGitHubURL(uri);
if (string.IsNullOrEmpty(repoName))
{
return new RepositoryUriSupportResult(false);
}
return new RepositoryUriSupportResult(true);
}).AsAsyncOperation();
}
private Octokit.GitHubClient GetClient(IDeveloperId developerId)
{
if (developerId != null)
{
var loggedInDeveloperId = DeveloperId.DeveloperIdProvider.GetInstance().GetDeveloperIdInternal(developerId);
return loggedInDeveloperId.GitHubClient;
}
else
{
return GitHubClientProvider.Instance.GetClient();
}
}
IAsyncOperation<RepositoriesResult> IRepositoryProvider.GetRepositoriesAsync(IDeveloperId developerId)
{
return Task.Run(async () =>
{
// This is fetching all repositories available to the specified DevId.
// We are not using the datastore cache for this query, it will always go to GitHub directly.
var repositoryList = new List<IRepository>();
try
{
ApiOptions apiOptions = new()
{
PageSize = 100,
PageCount = 1,
};
// Authenticate as the specified developer Id.
var client = GetClient(developerId);
var request = new RepositoryRequest
{
Sort = RepositorySort.Updated,
Direction = SortDirection.Descending,
Affiliation = RepositoryAffiliation.Owner,
// Gets only public repos for the owned repos.
Visibility = RepositoryRequestVisibility.Public,
};
var getPublicReposTask = client.Repository.GetAllForCurrent(request, apiOptions);
// this is getting private org and user repos.
request.Visibility = RepositoryRequestVisibility.Private;
var getPrivateReposTask = client.Repository.GetAllForCurrent(request, apiOptions);
// This gets all org repos.
request.Visibility = RepositoryRequestVisibility.All;
request.Affiliation = RepositoryAffiliation.CollaboratorAndOrganizationMember;
var getAllOrgReposTask = client.Repository.GetAllForCurrent(request, apiOptions);
var publicRepos = await getPublicReposTask;
publicRepos = publicRepos.OrderByDescending(x => x.UpdatedAt).ToList();
var privateRepos = await getPrivateReposTask;
privateRepos = privateRepos.OrderByDescending(x => x.UpdatedAt).ToList();
var orgRepos = await getAllOrgReposTask;
orgRepos = orgRepos.OrderByDescending(x => x.UpdatedAt).ToList();
var allRepos = publicRepos.Union(privateRepos).Union(orgRepos);
foreach (var repository in allRepos)
{
repositoryList.Add(new DevHomeRepository(repository));
}
}
catch (Exception ex)
{
// Any failures should be thrown so the core app can catch the failures.
Log.Error(ex, "Failed getting list of repositories.");
return new RepositoriesResult(ex, $"Something went wrong. HResult: {ex.HResult}");
}
return new RepositoriesResult(repositoryList.AsEnumerable());
}).AsAsyncOperation();
}
public IAsyncOperation<RepositoryResult> GetRepositoryFromUriAsync(Uri uri)
{
return GetRepositoryFromUriAsync(uri, null);
}
public IAsyncOperation<RepositoryResult> GetRepositoryFromUriAsync(Uri uri, IDeveloperId? developerId)
{
return Task.Run(() =>
{
if (!Validation.IsValidGitHubURL(uri))
{
var exception = new ArgumentException("Uri is invalid.");
return new RepositoryResult(exception, $"{exception.Message} HResult: {exception.HResult}");
}
Octokit.Repository? octokitRepo = null;
var owner = Validation.ParseOwnerFromGitHubURL(uri);
var repoName = Validation.ParseRepositoryFromGitHubURL(uri);
try
{
GitHubClient gitHubClient;
if (developerId != null)
{
var loggedInDeveloperId = DeveloperId.DeveloperIdProvider.GetInstance().GetDeveloperIdInternal(developerId);
gitHubClient = loggedInDeveloperId.GitHubClient;
}
else
{
gitHubClient = GitHubClientProvider.Instance.GetClient();
}
octokitRepo = gitHubClient.Repository.Get(owner, repoName).Result;
}
catch (AggregateException e)
{
var innerException = e.InnerException;
if (innerException is Octokit.NotFoundException)
{
Log.Error($"Can't find {owner}/{repoName}");
return new RepositoryResult(innerException, $"Can't find {owner}/{repoName}. HResult: {innerException.HResult}");
}
if (innerException is Octokit.ForbiddenException)
{
Log.Error($"Forbidden access to {owner}/{repoName}");
return new RepositoryResult(innerException, $"Forbidden access to {owner}/{repoName}. HResult: {innerException.HResult}");
}
if (innerException is Octokit.RateLimitExceededException)
{
Log.Error(innerException, "Rate limit exceeded.");
return new RepositoryResult(innerException, $"Rate limit exceeded. HResult: {innerException.HResult}");
}
}
catch (Exception e)
{
Log.Error(e, "Unspecified error.");
return new RepositoryResult(e, $"Unspecified error when cloning a repo. HResult: {e.HResult}");
}
if (octokitRepo == null)
{
return new RepositoryResult(new ArgumentException("Repo is still null"), "Repo is still null");
}
else
{
return new RepositoryResult(new DevHomeRepository(octokitRepo));
}
}).AsAsyncOperation();
}
public IAsyncOperation<ProviderOperationResult> CloneRepositoryAsync(IRepository repository, string cloneDestination)
{
return CloneRepositoryAsync(repository, cloneDestination, null);
}
public IAsyncOperation<ProviderOperationResult> CloneRepositoryAsync(IRepository repository, string cloneDestination, IDeveloperId? developerId)
{
return Task.Run(() =>
{
var cloneOptions = new LibGit2Sharp.CloneOptions
{
Checkout = true,
RecurseSubmodules = true,
};
if (developerId != null)
{
var loggedInDeveloperId = DeveloperId.DeveloperIdProvider.GetInstance().GetDeveloperIdInternal(developerId);
try
{
cloneOptions.FetchOptions.CredentialsProvider = (url, user, cred) => new LibGit2Sharp.UsernamePasswordCredentials
{
// Password is a PAT unique to GitHub.
Username = loggedInDeveloperId.GetCredential().Password,
Password = string.Empty,
};
}
catch (Exception e)
{
Log.Error(e, "Could not get credentials.");
return new ProviderOperationResult(ProviderOperationStatus.Failure, e, "Could not get credentials.", e.Message);
}
}
try
{
// Exceptions happen.
LibGit2Sharp.Repository.Clone(repository.RepoUri.OriginalString, cloneDestination, cloneOptions);
}
catch (LibGit2Sharp.RecurseSubmodulesException recurseException)
{
Log.Error(recurseException, "Could not clone all submodules.");
return new ProviderOperationResult(ProviderOperationStatus.Failure, recurseException, "Could not clone all submodules.", recurseException.Message);
}
catch (LibGit2Sharp.UserCancelledException userCancelledException)
{
Log.Error(userCancelledException, "The user stopped the clone operation.");
return new ProviderOperationResult(ProviderOperationStatus.Failure, userCancelledException, "User cancelled the clone operation.", userCancelledException.Message);
}
catch (LibGit2Sharp.NameConflictException nameConflictException)
{
Log.Error(nameConflictException, "Name conflict");
return new ProviderOperationResult(ProviderOperationStatus.Failure, nameConflictException, "The destination location is non-empty.", nameConflictException.Message);
}
catch (LibGit2Sharp.LibGit2SharpException libGitTwoException)
{
Log.Error(libGitTwoException, $"Either no logged in account has access to this repository, or the repository can't be found.");
return new ProviderOperationResult(ProviderOperationStatus.Failure, libGitTwoException, "LibGit2 library threw an exception.", "LibGit2 library threw an exception.");
}
catch (Exception e)
{
Log.Error(e, "Could not clone the repository.");
return new ProviderOperationResult(ProviderOperationStatus.Failure, e, "Something happened when cloning the repository.", "Something happened when cloning the repository.");
}
return new ProviderOperationResult(ProviderOperationStatus.Success, new ArgumentException("Nothing wrong"), "Nothing wrong", "Nothing wrong");
}).AsAsyncOperation();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}