forked from openjdk/skara
/
GitHubHost.java
403 lines (352 loc) · 14.6 KB
/
GitHubHost.java
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.forge.github;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.*;
import org.openjdk.skara.json.*;
import org.openjdk.skara.network.*;
import org.openjdk.skara.vcs.*;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class GitHubHost implements Forge {
private final URI uri;
private final Pattern webUriPattern;
private final String webUriReplacement;
private final GitHubApplication application;
private final Credential pat;
private final RestRequest request;
private final RestRequest graphQL;
private final Duration searchInterval;
private HostUser currentUser;
private volatile Instant lastSearch = Instant.now();
private final Logger log = Logger.getLogger("org.openjdk.skara.forge.github");
private final Set<String> orgs;
public GitHubHost(URI uri, GitHubApplication application, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.application = application;
this.pat = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(3);
var baseApi = URIBuilder.base(uri)
.appendSubDomain("api")
.setPath("/")
.build();
request = new RestRequest(baseApi, application.authId(), () -> Arrays.asList(
"Authorization", "token " + getInstallationToken().orElseThrow(),
"Accept", "application/vnd.github.machine-man-preview+json",
"Accept", "application/vnd.github.antiope-preview+json"));
var graphQLAPI = URIBuilder.base(uri)
.appendSubDomain("api")
.setPath("/graphql")
.build();
graphQL = new RestRequest(graphQLAPI, application.authId(), () -> Arrays.asList(
"Authorization", "bearer " + getInstallationToken().orElseThrow(),
"Accept", "application/vnd.github.machine-man-preview+json",
"Accept", "application/vnd.github.antiope-preview+json",
"Accept", "application/vnd.github.shadow-cat-preview+json",
"Accept", "application/vnd.github.comfort-fade-preview+json"
));
}
RestRequest graphQL() {
if (graphQL == null) {
throw new IllegalStateException("Cannot use GraphQL API without authorization");
}
return graphQL;
}
public GitHubHost(URI uri, Credential pat, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.pat = pat;
this.application = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(3);
var baseApi = URIBuilder.base(uri)
.appendSubDomain("api")
.setPath("/")
.build();
request = new RestRequest(baseApi, pat.username(), () -> Arrays.asList(
"Authorization", "token " + getInstallationToken().orElseThrow()));
var graphQLAPI = URIBuilder.base(uri)
.appendSubDomain("api")
.setPath("/graphql")
.build();
graphQL = new RestRequest(graphQLAPI, pat.username(), () -> Arrays.asList(
"Authorization", "bearer " + getInstallationToken().orElseThrow(),
"Accept", "application/vnd.github.machine-man-preview+json",
"Accept", "application/vnd.github.antiope-preview+json",
"Accept", "application/vnd.github.shadow-cat-preview+json",
"Accept", "application/vnd.github.comfort-fade-preview+json"
));
}
GitHubHost(URI uri, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.pat = null;
this.application = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(10);
var baseApi = URIBuilder.base(uri)
.appendSubDomain("api")
.setPath("/")
.build();
request = new RestRequest(baseApi);
graphQL = null;
}
public URI getURI() {
return uri;
}
@Override
public String name() {
return "GitHub";
}
URI getWebURI(String endpoint) {
return getWebURI(endpoint, true);
}
URI getWebURI(String endpoint, boolean transform) {
var baseWebUri = URIBuilder.base(uri)
.setPath(endpoint)
.build();
if (webUriPattern == null || !transform) {
return baseWebUri;
}
var matcher = webUriPattern.matcher(baseWebUri.toString());
if (!matcher.matches()) {
return baseWebUri;
}
return URIBuilder.base(matcher.replaceAll(webUriReplacement)).build();
}
Optional<String> getInstallationToken() {
if (application != null) {
return Optional.of(application.getInstallationToken());
}
if (pat != null) {
return Optional.of(pat.password());
}
return Optional.empty();
}
Optional<String> authId() {
if (application != null) {
return Optional.of(application.authId());
}
if (pat != null) {
return Optional.of(pat.username());
}
return Optional.empty();
}
// Most GitHub API's return user information in this format
HostUser parseUserField(JSONValue json) {
return parseUserObject(json.get("user"));
}
HostUser parseUserObject(JSONValue json) {
return hostUser(json.get("id").asInt(), json.get("login").asString());
}
HostUser hostUser(int id, String username) {
return HostUser.builder()
.id(id)
.username(username)
.supplier(() -> user(username).orElseThrow())
.build();
}
HostUser hostUser(String username) {
return HostUser.builder()
.username(username)
.supplier(() -> user(username).orElseThrow())
.build();
}
@Override
public boolean isValid() {
try {
var endpoints = request.get("")
.executeUnparsed();
var parsed = JSON.parse(endpoints);
if (parsed != null && parsed.contains("current_user_url")) {
return true;
} else {
log.fine("Error during GitHub host validation: unexpected endpoint list: " + endpoints);
return false;
}
} catch (IOException e) {
log.fine("Error during GitHub host validation: " + e);
return false;
}
}
JSONObject getProjectInfo(String name) {
var project = request.get("repos/" + name)
.execute();
return project.asObject();
}
JSONObject runSearch(String category, String query) {
// Searches on GitHub uses a special rate limit, so make sure to wait between consecutive searches
while (true) {
synchronized (this) {
if (lastSearch.isBefore(Instant.now().minus(searchInterval))) {
lastSearch = Instant.now();
break;
}
}
log.fine("Searching too fast - waiting");
try {
Thread.sleep(Duration.ofMillis(500).toMillis());
} catch (InterruptedException ignored) {
}
}
var result = request.get("search/" + category)
.param("q", query)
.execute();
return result.asObject();
}
@Override
public Optional<HostedRepository> repository(String name) {
try {
return Optional.of(new GitHubRepository(this, name));
} catch (Throwable t) {
return Optional.empty();
}
}
@Override
public Optional<HostUser> user(String username) {
var details = request.get("users/" + URLEncoder.encode(username, StandardCharsets.UTF_8))
.onError(r -> Optional.of(JSON.of()))
.execute();
if (details.isNull()) {
return Optional.empty();
}
return Optional.of(toHostUser(details.asObject()));
}
private HostUser toHostUser(JSONObject details) {
// Always present
var login = details.get("login").asString();
var id = details.get("id").asInt();
var name = details.get("name").asString();
if (name == null) {
name = login;
}
var email = details.get("email").asString();
return HostUser.builder()
.id(id)
.username(login)
.fullName(name)
.email(email)
.build();
}
@Override
public HostUser currentUser() {
if (currentUser == null) {
if (application != null) {
var appDetails = application.getAppDetails();
var appName = appDetails.get("name").asString() + "[bot]";
currentUser = user(appName).get();
} else if (pat != null) {
// Cannot always trust username in PAT, e.g. Git Credential Manager
// on Windows always return "PersonalAccessToken" as username.
// Query GitHub for the username instead.
var details = request.get("user").execute().asObject();
currentUser = toHostUser(details);
} else {
throw new IllegalStateException("No credentials present");
}
}
return currentUser;
}
@Override
public boolean supportsReviewBody() {
return true;
}
@Override
public boolean isMemberOf(String groupId, HostUser user) {
long gid = 0L;
try {
gid = Long.parseLong(groupId);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Group id is not a number: " + groupId);
}
var username = URLEncoder.encode(user.username(), StandardCharsets.UTF_8);
var orgs = request.get("users/" + username + "/orgs").execute().asArray();
for (var org : orgs) {
if (org.get("id").asLong() == gid) {
return true;
}
}
return false;
}
CommitMetadata toCommitMetadata(JSONValue o) {
var hash = new Hash(o.get("sha").asString());
var parents = o.get("parents").stream()
.map(p -> new Hash(p.get("sha").asString()))
.collect(Collectors.toList());
var commit = o.get("commit").asObject();
var author = new Author(commit.get("author").get("name").asString(),
commit.get("author").get("email").asString());
var authored = ZonedDateTime.parse(commit.get("author").get("date").asString());
var committer = new Author(commit.get("committer").get("name").asString(),
commit.get("committer").get("email").asString());
var committed = ZonedDateTime.parse(commit.get("committer").get("date").asString());
var message = Arrays.asList(commit.get("message").asString().split("\n"));
return new CommitMetadata(hash, parents, author, authored, committer, committed, message);
}
Diff toDiff(Hash from, Hash to, JSONValue files) {
var patches = new ArrayList<Patch>();
for (var file : files.asArray()) {
var status = Status.from(file.get("status").asString().toUpperCase().charAt(0));
var targetPath = Path.of(file.get("filename").asString());
var sourcePath = status.isRenamed() || status.isCopied() ?
Path.of(file.get("previous_filename").asString()) :
targetPath;
var filetype = FileType.fromOctal("100644");
var diff = file.get("patch").asString().split("\n");
var hunks = UnifiedDiffParser.parseSingleFileDiff(diff);
patches.add(new TextualPatch(sourcePath, filetype, Hash.zero(),
targetPath, filetype, Hash.zero(),
status, hunks));
}
return new Diff(from, to, patches);
}
@Override
public Optional<HostedCommit> search(Hash hash) {
var orgsToSearch = orgs.stream().map(o -> "org:" + o).collect(Collectors.joining("+"));
if (!orgsToSearch.isEmpty()) {
orgsToSearch = "+" + orgsToSearch;
}
var result = runSearch("commits", "hash:" + hash.hex() + orgsToSearch);
var items = result.get("items").asArray();
if (items.isEmpty()) {
return Optional.empty();
}
var first = items.get(0);
var metadata = toCommitMetadata(first);
var diff = toDiff(metadata.parents().get(0), hash, first.get("files"));
var url = URI.create(first.get("url").asString());
return Optional.of(new HostedCommit(metadata, List.of(diff), url));
}
}