This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathApiClient.cs
More file actions
333 lines (277 loc) · 11.9 KB
/
Copy pathApiClient.cs
File metadata and controls
333 lines (277 loc) · 11.9 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reactive;
using System.Reactive.Linq;
using System.Security.Cryptography;
using System.Text;
using GitHub.Extensions;
using GitHub.Logging;
using GitHub.Primitives;
using Octokit;
using Octokit.Reactive;
using Serilog;
namespace GitHub.Api
{
public partial class ApiClient : IApiClient
{
const string ScopesHeader = "X-OAuth-Scopes";
const string ProductName = Info.ApplicationInfo.ApplicationDescription;
static readonly ILogger log = LogManager.ForContext<ApiClient>();
readonly IObservableGitHubClient gitHubClient;
string ClientId { get; set; }
string ClientSecret { get; set; }
public ApiClient(HostAddress hostAddress, IObservableGitHubClient gitHubClient)
{
ClientId = ApiClientConfiguration.ClientId;
ClientSecret = ApiClientConfiguration.ClientSecret;
HostAddress = hostAddress;
this.gitHubClient = gitHubClient;
}
partial void Configure();
public IGitHubClient GitHubClient => new GitHubClient(gitHubClient.Connection);
public IObservable<Repository> CreateRepository(NewRepository repository, string login, bool isUser)
{
Guard.ArgumentNotEmptyString(login, nameof(login));
var client = gitHubClient.Repository;
return (isUser ? client.Create(repository) : client.Create(login, repository));
}
public IObservable<Repository> ForkRepository(string owner, string name, NewRepositoryFork repository)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
Guard.ArgumentNotNull(repository, nameof(repository));
var client = gitHubClient.Repository.Forks;
return client.Create(owner, name, repository);
}
public IObservable<PullRequestReview> PostPullRequestReview(
string owner,
string name,
int number,
string commitId,
string body,
PullRequestReviewEvent e)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
var review = new PullRequestReviewCreate
{
Body = body,
CommitId = commitId,
Event = e,
};
return gitHubClient.PullRequest.Review.Create(owner, name, number, review);
}
public IObservable<PullRequestReviewComment> CreatePullRequestReviewComment(
string owner,
string name,
int number,
string body,
string commitId,
string path,
int position)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
Guard.ArgumentNotEmptyString(body, nameof(body));
Guard.ArgumentNotEmptyString(commitId, nameof(commitId));
Guard.ArgumentNotEmptyString(path, nameof(path));
var comment = new PullRequestReviewCommentCreate(body, commitId, path, position);
return gitHubClient.PullRequest.ReviewComment.Create(owner, name, number, comment);
}
public IObservable<PullRequestReviewComment> CreatePullRequestReviewComment(
string owner,
string name,
int number,
string body,
int inReplyTo)
{
var comment = new PullRequestReviewCommentReplyCreate(body, inReplyTo);
return gitHubClient.PullRequest.ReviewComment.CreateReply(owner, name, number, comment);
}
public IObservable<PullRequestReviewComment> EditPullRequestReviewComment(
string owner,
string name,
int number,
string body)
{
var pullRequestReviewCommentEdit = new PullRequestReviewCommentEdit(body);
return gitHubClient.PullRequest.ReviewComment.Edit(owner, name, number, pullRequestReviewCommentEdit);
}
public IObservable<Unit> DeletePullRequestReviewComment(
string owner,
string name,
int number)
{
return gitHubClient.PullRequest.ReviewComment.Delete(owner, name, number);
}
public IObservable<Gist> CreateGist(NewGist newGist)
{
return gitHubClient.Gist.Create(newGist);
}
public IObservable<Repository> GetForks(string owner, string name)
{
return gitHubClient.Repository.Forks.GetAll(owner, name);
}
public IObservable<User> GetUser()
{
return gitHubClient.User.Current();
}
public IObservable<User> GetUser(string login)
{
return gitHubClient.User.Get(login);
}
public IObservable<Organization> GetOrganizations()
{
// Organization.GetAllForCurrent doesn't return all of the information we need (we
// need information about the plan the organization is on in order to enable/disable
// the "Private Repository" checkbox in the "Create Repository" dialog). To get this
// we have to do an Organization.Get on each repository received.
return gitHubClient.Organization
.GetAllForCurrent()
.Select(x => gitHubClient.Organization.Get(x.Login))
.Merge();
}
public IObservable<Repository> GetUserRepositories(RepositoryType repositoryType)
{
var request = new RepositoryRequest
{
Type = repositoryType,
Direction = SortDirection.Ascending,
Sort = RepositorySort.FullName
};
return gitHubClient.Repository.GetAllForCurrent(request);
}
public IObservable<string> GetGitIgnoreTemplates()
{
return gitHubClient.Miscellaneous.GetAllGitIgnoreTemplates();
}
public IObservable<LicenseMetadata> GetLicenses()
{
return gitHubClient.Miscellaneous.GetAllLicenses();
}
public HostAddress HostAddress { get; }
static string GetFingerprint()
{
var fingerprint = ProductName + ":" + GetMachineIdentifier();
return fingerprint.GetSha256Hash();
}
static string GetMachineNameSafe()
{
try
{
return Dns.GetHostName();
}
catch (Exception e)
{
log.Warning(e, "Failed to retrieve host name using `DNS.GetHostName`");
try
{
return Environment.MachineName;
}
catch (Exception ex)
{
log.Warning(ex, "Failed to retrieve host name using `Environment.MachineName`");
return "(unknown)";
}
}
}
static string GetMachineIdentifier()
{
try
{
// adapted from http://stackoverflow.com/a/1561067
var fastedValidNetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
.OrderBy(nic => nic.Speed)
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Select(nic => nic.GetPhysicalAddress().ToString())
.FirstOrDefault(address => address.Length > 12);
return fastedValidNetworkInterface ?? GetMachineNameSafe();
}
catch (Exception e)
{
log.Warning(e, "Could not retrieve MAC address. Fallback to using machine name");
return GetMachineNameSafe();
}
}
public IObservable<Repository> GetRepositoriesForOrganization(string organization)
{
Guard.ArgumentNotEmptyString(organization, nameof(organization));
return gitHubClient.Repository.GetAllForOrg(organization);
}
public IObservable<Unit> DeleteApplicationAuthorization(int id, string twoFactorAuthorizationCode)
{
Guard.ArgumentNotEmptyString(twoFactorAuthorizationCode, nameof(twoFactorAuthorizationCode));
return gitHubClient.Authorization.Delete(id, twoFactorAuthorizationCode);
}
public IObservable<IssueComment> GetIssueComments(string owner, string name, int number)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
return gitHubClient.Issue.Comment.GetAllForIssue(owner, name, number);
}
public IObservable<PullRequest> GetPullRequest(string owner, string name, int number)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
return gitHubClient.PullRequest.Get(owner, name, number);
}
public IObservable<PullRequestFile> GetPullRequestFiles(string owner, string name, int number)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
return gitHubClient.PullRequest.Files(owner, name, number);
}
public IObservable<PullRequestReviewComment> GetPullRequestReviewComments(string owner, string name, int number)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
return gitHubClient.PullRequest.ReviewComment.GetAll(owner, name, number);
}
public IObservable<PullRequest> GetPullRequestsForRepository(string owner, string name)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
return gitHubClient.PullRequest.GetAllForRepository(owner, name,
new PullRequestRequest
{
State = ItemStateFilter.All,
SortProperty = PullRequestSort.Updated,
SortDirection = SortDirection.Descending
});
}
public IObservable<PullRequest> CreatePullRequest(NewPullRequest pullRequest, string owner, string repo)
{
Guard.ArgumentNotNull(pullRequest, nameof(pullRequest));
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(repo, nameof(repo));
return gitHubClient.PullRequest.Create(owner, repo, pullRequest);
}
public IObservable<Repository> GetRepositories()
{
return gitHubClient.Repository.GetAllForCurrent();
}
public IObservable<Branch> GetBranches(string owner, string repo)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(repo, nameof(repo));
return gitHubClient.Repository.Branch.GetAll(owner, repo);
}
public IObservable<Repository> GetRepository(string owner, string repo)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(repo, nameof(repo));
return gitHubClient.Repository.Get(owner, repo);
}
public IObservable<RepositoryContent> GetFileContents(string owner, string name, string reference, string path)
{
Guard.ArgumentNotEmptyString(owner, nameof(owner));
Guard.ArgumentNotEmptyString(name, nameof(name));
Guard.ArgumentNotEmptyString(reference, nameof(reference));
Guard.ArgumentNotEmptyString(path, nameof(path));
return gitHubClient.Repository.Content.GetAllContentsByRef(owner, name, reference, path);
}
}
}