forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubGateway.cs
More file actions
321 lines (273 loc) · 10.7 KB
/
GithubGateway.cs
File metadata and controls
321 lines (273 loc) · 10.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using ServiceStack.DataAnnotations;
namespace ServiceStack.WebHost.Endpoints.Tests.Support
{
public class GithubRepo
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Homepage { get; set; }
public string Language { get; set; }
public int Watchers_Count { get; set; }
public int Stargazes_Count { get; set; }
public int Forks_Count { get; set; }
public int Open_Issues_Count { get; set; }
public int Size { get; set; }
public string Full_Name { get; set; }
public DateTime Created_at { get; set; }
public DateTime? Pushed_At { get; set; }
public DateTime? Updated_At { get; set; }
public bool Has_issues { get; set; }
public bool Has_Downloads { get; set; }
public bool Has_Wiki { get; set; }
public bool Has_Pages { get; set; }
public bool Fork { get; set; }
public GithubUser Owner { get; set; }
public string Svn_Url { get; set; }
public string Mirror_Url { get; set; }
public string Url { get; set; }
public string Ssh_Url { get; set; }
public string Html_Url { get; set; }
public string Clone_Url { get; set; }
public string Git_Url { get; set; }
public bool Private { get; set; }
}
public abstract class GithubUser
{
public int Id { get; set; }
public string Login { get; set; }
public string Avatar_Url { get; set; }
public string Url { get; set; }
public int? Followers { get; set; }
public int? Following { get; set; }
public string Type { get; set; }
public int? Public_Gists { get; set; }
public string Location { get; set; }
public string Company { get; set; }
public string Html_Url { get; set; }
public int? Public_Repos { get; set; }
public DateTime? Created_At { get; set; }
public string Blog { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public bool? Hireable { get; set; }
public string Gravatar_Id { get; set; }
public string Bio { get; set; }
}
public class GithubOrg
{
public int Id { get; set; }
public string Avatar_Url { get; set; }
public string Url { get; set; }
public string Login { get; set; }
}
public class GithubByUser
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime Date { get; set; }
}
public class GithubCommitResult
{
public string Sha { get; set; }
public GithubCommit Commit { get; set; }
public GithubUser Author { get; set; }
public GithubUser Committer { get; set; }
}
public class GithubCommit
{
public string Id { get; set; }
public string Message { get; set; }
public DateTime Date { get; set; }
public string Name { get; set; }
public int Comment_Count { get; set; }
public GithubByUser Committer { get; set; }
public GithubByUser Author { get; set; }
public bool? ShouldSerialize(string fieldName)
{
return fieldName != "Committer" && fieldName != "Author";
}
}
public class GithubContent
{
[PrimaryKey]
public string Sha { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public int Size { get; set; }
public string Download_Url { get; set; }
}
public class GithubContributor
{
public int Id { get; set; }
public string Login { get; set; }
public int Contributions { get; set; }
public string Type { get; set; }
public string Avatar_Url { get; set; }
public string Gravatar_Id { get; set; }
public string Url { get; set; }
public bool? Site_Admin { get; set; }
}
public class GithubSubscriber
{
public int Id { get; set; }
public string Login { get; set; }
public int Contributions { get; set; }
public string Type { get; set; }
public string Avatar_Url { get; set; }
public string Gravatar_Id { get; set; }
public string Url { get; set; }
public bool? Site_Admin { get; set; }
}
public class GithubComment
{
public int Id { get; set; }
public string Body { get; set; }
public DateTime Created_At { get; set; }
public DateTime Updated_At { get; set; }
public GithubUser User { get; set; }
public string Url { get; set; }
public string Commit_Id { get; set; }
public string Position { get; set; }
public string Line { get; set; }
public string Path { get; set; }
}
public class GithubRelease
{
public int Id { get; set; }
public string Name { get; set; }
public string Tag_Name { get; set; }
public string Target_Commitish { get; set; }
public string Body { get; set; }
public bool Draft { get; set; }
public bool PreRelease { get; set; }
public DateTime Created_At { get; set; }
public DateTime Published_At { get; set; }
public GithubUser Author { get; set; }
public string Url { get; set; }
public string Tarball_Url { get; set; }
public string Zipball_Url { get; set; }
}
public partial class GithubGateway
{
public const string GithubApiBaseUrl = "https://api.github.com/";
public static string UserAgent = typeof(GithubGateway).Namespace.SplitOnFirst('.').First();
public string Username { get; set; }
public string Password { get; set; }
public List<GithubOrg> GetUserOrgs(string githubUsername)
{
return GetJson<List<GithubOrg>>("users/{0}/orgs", githubUsername);
}
public List<GithubRepo> GetUserRepos(string githubUsername)
{
return GetJson<List<GithubRepo>>("users/{0}/repos", githubUsername);
}
public List<GithubRepo> GetOrgRepos(string githubOrgName)
{
return GetJson<List<GithubRepo>>("orgs/{0}/repos", githubOrgName);
}
public List<GithubRepo> GetAllUserAndOrgsReposFor(string githubUsername)
{
var map = new Dictionary<int, GithubRepo>();
GetUserRepos(githubUsername).ForEach(x => map[x.Id] = x);
GetUserOrgs(githubUsername).ForEach(org =>
GetOrgRepos(org.Login)
.ForEach(repo => map[repo.Id] = repo));
return map.Values.ToList();
}
public IEnumerable<GithubCommitResult> GetRepoCommits(string githubUser, string githubRepo)
{
return StreamJsonCollection<GithubCommitResult>("repos/{0}/{1}/commits", githubUser, githubRepo);
}
public List<GithubContent> GetRepoContents(string githubUser, string githubRepo)
{
return GetJson<List<GithubContent>>("repos/{0}/{1}/contents", githubUser, githubRepo);
}
public List<GithubContributor> GetRepoContributors(string githubUser, string githubRepo)
{
return GetJson<List<GithubContributor>>("repos/{0}/{1}/contributors", githubUser, githubRepo);
}
public List<GithubSubscriber> GetRepoSubscribers(string githubUser, string githubRepo)
{
return GetJson<List<GithubSubscriber>>("repos/{0}/{1}/subscribers", githubUser, githubRepo);
}
public List<GithubComment> GetRepoComments(string githubUser, string githubRepo)
{
return GetJson<List<GithubComment>>("repos/{0}/{1}/comments", githubUser, githubRepo);
}
public List<GithubRelease> GetRepoReleases(string githubUser, string githubRepo)
{
return GetJson<List<GithubRelease>>("repos/{0}/{1}/releases", githubUser, githubRepo);
}
protected virtual void RequestFilter(HttpWebRequest req)
{
req.SetUserAgent(UserAgent);
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
req.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(Username + ":" + Password));
}
}
public T GetJson<T>(string route, params object[] routeArgs)
{
return GithubApiBaseUrl.CombineWith(route.Fmt(routeArgs))
.GetJsonFromUrl(RequestFilter)
.FromJson<T>();
}
public IEnumerable<T> StreamJsonCollection<T>(string route, params object[] routeArgs)
{
List<T> results;
var nextUrl = GithubApiBaseUrl.CombineWith(route.Fmt(routeArgs));
do
{
results = nextUrl
.GetJsonFromUrl(
RequestFilter,
responseFilter: res => {
var links = ParseLinkUrls(res.Headers["Link"]);
links.TryGetValue("next", out nextUrl);
})
.FromJson<List<T>>();
foreach (var result in results)
{
yield return result;
}
} while (results.Count > 0 && nextUrl != null);
}
public static Dictionary<string, string> ParseLinkUrls(string linkHeader)
{
var map = new Dictionary<string, string>();
var links = linkHeader;
while (!string.IsNullOrEmpty(links))
{
var urlStartPos = links.IndexOf('<');
var urlEndPos = links.IndexOf('>');
if (urlStartPos == -1 || urlEndPos == -1)
break;
var url = links.Substring(urlStartPos + 1, urlEndPos - urlStartPos - 1);
var parts = links.Substring(urlEndPos).SplitOnFirst(',');
var relParts = parts[0].Split(';');
foreach (var relPart in relParts)
{
var keyValueParts = relPart.SplitOnFirst('=');
if (keyValueParts.Length < 2)
continue;
var name = keyValueParts[0].Trim();
var value = keyValueParts[1].Trim().Trim('"');
if (name == "rel")
{
map[value] = url;
}
}
links = parts.Length > 1 ? parts[1] : null;
}
return map;
}
}
}