-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGithub.cs
211 lines (195 loc) · 5.33 KB
/
Github.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Octokit.GraphQL;
using Octokit.GraphQL.Model;
namespace SponsorBoi
{
internal static class Github
{
public class Account
{
public string id = "";
public string name = "";
}
public class Sponsor
{
public Account sponsor = new Account();
public int dollarAmount = 0;
}
public class Issue
{
public string description = "";
public Account author = new Account();
}
private static ProductHeaderValue productInformation = new ProductHeaderValue(SponsorBoi.APPLICATION_NAME, SponsorBoi.GetVersion());
private static Connection connection = null;
private static ICompiledQuery<IEnumerable<Sponsor>> sponsorQuery = null;
private static ICompiledQuery<IEnumerable<Issue>> issueQuery = null;
public static void Initialize()
{
sponsorQuery = new Query() // Needs 'read:org' permission (And maybe 'read:user'?)
.Viewer
.SponsorshipsAsMaintainer()
.AllPages().Select(x => new Sponsor
{
sponsor = x.SponsorEntity.Switch<Account>(when => when
.User(user => new Account { id = user.Id.Value, name = user.Login })
.Organization(org => new Account { id = org.Id.Value, name = org.Login })),
dollarAmount = x.Tier.MonthlyPriceInDollars
})
.Compile();
issueQuery = new Query() // Does not require any specific permissions
.Viewer
.Repository(Config.repositoryName)
.Issues(
first: 3,
after: null,
last: null,
before: null,
filterBy: null,
labels: null,
orderBy: new IssueOrder { Field = IssueOrderField.CreatedAt, Direction = OrderDirection.Desc },
states: null)
.Nodes
.Select(i => new Issue
{
description = i.BodyText,
author = i.Author.Cast<User>().Select(user => new Account { id = user.Id.Value, name = user.Login } ).Single()
})
.Compile();
connection = new Connection(productInformation, Config.githubToken);
}
public static async Task<List<Sponsor>> GetSponsors()
{
try
{
return (await connection?.Run(sponsorQuery))?.ToList();
}
catch (HttpRequestException e)
{
LogException(new AggregateException(e));
return new List<Sponsor>();
}
catch (Octokit.GraphQL.Core.Deserializers.ResponseDeserializerException e)
{
LogException(new AggregateException(e));
return new List<Sponsor>();
}
catch (AggregateException e)
{
LogException(e);
return new List<Sponsor>();
}
}
public static async Task<List<Issue>> GetIssues()
{
try
{
return (await connection?.Run(issueQuery))?.ToList();
}
catch (HttpRequestException e)
{
LogException(new AggregateException(e));
return new List<Issue>();
}
catch (Octokit.GraphQL.Core.Deserializers.ResponseDeserializerException e)
{
LogException(new AggregateException(e));
return new List<Issue>();
}
catch (AggregateException e)
{
LogException(e);
return new List<Issue>();
}
}
public static async Task<Account> GetUserByUsername(string username)
{
try
{
ICompiledQuery<Account> userQuery = new Query() // Does not require any specific permissions
.User(username)
.Select(user => new Account { id = user.Id.Value, name = user.Login })
.Compile();
return await connection?.Run(userQuery);
}
catch (HttpRequestException e)
{
LogException(new AggregateException(e));
return null;
}
catch (Octokit.GraphQL.Core.Deserializers.ResponseDeserializerException e)
{
LogException(new AggregateException(e));
return null;
}
catch (AggregateException e)
{
LogException(e);
return null;
}
}
public static async Task<Account> GetUserByID(string id)
{
try
{
ICompiledQuery<Account> userQuery = new Query() // Does not require any specific permissions
.Node(new ID(id)).Cast<User>()
.Select(user => new Account { id = user.Id.Value, name = user.Login })
.Compile();
return await connection?.Run(userQuery);
}
catch (HttpRequestException e)
{
LogException(new AggregateException(e));
return null;
}
catch (Octokit.GraphQL.Core.Deserializers.ResponseDeserializerException e)
{
LogException(new AggregateException(e));
return null;
}
catch (AggregateException e)
{
LogException(e);
return null;
}
}
private static void LogException(AggregateException e)
{
e.Handle(ex =>
{
switch (ex)
{
case Octokit.GraphQL.Core.Deserializers.ResponseDeserializerException:
Logger.Error("Error occured when reading Github response:\n" + ex);
return true;
case HttpRequestException except:
LogHTTPRequestException(except);
return true;
default:
return false;
}
});
}
private static void LogHTTPRequestException(HttpRequestException e)
{
switch (e.StatusCode)
{
case System.Net.HttpStatusCode.BadGateway:
Logger.Error("Could not connect to Github API (Bad Gateway)");
break;
case System.Net.HttpStatusCode.Unauthorized:
case System.Net.HttpStatusCode.Forbidden:
Logger.Error("Github refused request, make sure your personal access token is valid and has the right permissions.");
break;
default:
Logger.Error("Unknown error occured requesting from the Github API: " + e.Message);
break;
}
}
}
}