forked from spotify/github-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullRequestClient.java
More file actions
482 lines (443 loc) · 17 KB
/
PullRequestClient.java
File metadata and controls
482 lines (443 loc) · 17 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.github.v3.clients;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static java.util.Objects.isNull;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.core.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.spotify.github.async.AsyncPage;
import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_REQUEST_TYPE_REFERENCE;
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_TYPE_REFERENCE;
import com.spotify.github.v3.prs.Comment;
import com.spotify.github.v3.prs.MergeParameters;
import com.spotify.github.v3.prs.PullRequest;
import com.spotify.github.v3.prs.PullRequestItem;
import com.spotify.github.v3.prs.RequestReviewParameters;
import com.spotify.github.v3.prs.Review;
import com.spotify.github.v3.prs.ReviewParameters;
import com.spotify.github.v3.prs.ReviewRequests;
import com.spotify.github.v3.prs.requests.PullRequestCreate;
import com.spotify.github.v3.prs.requests.PullRequestParameters;
import com.spotify.github.v3.prs.requests.PullRequestUpdate;
import com.spotify.github.v3.repos.CommitItem;
/** Pull call API client */
public class PullRequestClient {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String PR_TEMPLATE = "/repos/%s/%s/pulls";
private static final String PR_NUMBER_TEMPLATE = "/repos/%s/%s/pulls/%s";
private static final String PR_COMMITS_TEMPLATE = "/repos/%s/%s/pulls/%s/commits";
private static final String PR_REVIEWS_TEMPLATE = "/repos/%s/%s/pulls/%s/reviews";
private static final String PR_REVIEW_REQUESTS_TEMPLATE =
"/repos/%s/%s/pulls/%s/requested_reviewers";
private static final String PR_COMMENT_REPLIES_TEMPLATE =
"/repos/%s/%s/pulls/%s/comments/%s/replies";
private final GitHubClient github;
private final String owner;
private final String repo;
PullRequestClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.owner = owner;
this.repo = repo;
}
static PullRequestClient create(
final GitHubClient github, final String owner, final String repo) {
return new PullRequestClient(github, owner, repo);
}
/**
* List repository pull request.
*
* @return pull requests
*/
public CompletableFuture<List<PullRequestItem>> list() {
return list("");
}
/**
* List repository pull requests using given parameters.
*
* @param parameters request parameters
* @return pull requests
*/
public CompletableFuture<List<PullRequestItem>> list(final PullRequestParameters parameters) {
final String serial = parameters.serialize();
final String path = Strings.isNullOrEmpty(serial) ? "" : "?" + serial;
return list(path);
}
/**
* Get a specific pull request.
*
* @deprecated Use {@link #get(long)} instead
* @param prNumber pull request number
* @return pull request
*/
@Deprecated
public CompletableFuture<PullRequest> get(final int prNumber) {
return get((long) prNumber);
}
/**
* Get a specific pull request.
*
* @param prNumber pull request number
* @return pull request
*/
public CompletableFuture<PullRequest> get(final long prNumber) {
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, prNumber);
log.debug("Fetching pull request from " + path);
return github.request(path, PullRequest.class);
}
/**
* Create a pull request.
*
* @param request create request
* @return pull request
*/
public CompletableFuture<PullRequest> create(final PullRequestCreate request) {
final String path = String.format(PR_TEMPLATE, owner, repo);
return github.post(path, github.json().toJsonUnchecked(request), PullRequest.class);
}
/**
* Update given pull request.
*
* @deprecated Use {@link #update(long, PullRequestUpdate)} instead
* @param prNumber pull request number
* @param request update request
* @return pull request
*/
@Deprecated
public CompletableFuture<PullRequest> update(
final int prNumber, final PullRequestUpdate request) {
return update((long) prNumber, request);
}
/**
* Update given pull request.
*
* @param prNumber pull request number
* @param request update request
* @return pull request
*/
public CompletableFuture<PullRequest> update(
final long prNumber, final PullRequestUpdate request) {
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, prNumber);
return github.patch(path, github.json().toJsonUnchecked(request), PullRequest.class);
}
/**
* List pull request commits.
*
* @deprecated Use {@link #listCommits(long)} instead
* @param prNumber pull request number
* @return commits
*/
@Deprecated
public CompletableFuture<List<CommitItem>> listCommits(final int prNumber) {
return listCommits((long) prNumber);
}
/**
* List pull request commits.
*
* @param prNumber pull request number
* @return commits
*/
public CompletableFuture<List<CommitItem>> listCommits(final long prNumber) {
final String path = String.format(PR_COMMITS_TEMPLATE, owner, repo, prNumber);
log.debug("Fetching pull request commits from " + path);
return github.request(path, LIST_COMMIT_TYPE_REFERENCE);
}
/**
* List pull request reviews. Reviews are returned in chronological order.
*
* @deprecated Use {@link #listReviews(long)} instead
* @param prNumber pull request number
* @return list of reviews
*/
@Deprecated
public CompletableFuture<List<Review>> listReviews(final int prNumber) {
return listReviews((long) prNumber);
}
/**
* List pull request reviews. Reviews are returned in chronological order.
*
* @param prNumber pull request number
* @return list of reviews
*/
public CompletableFuture<List<Review>> listReviews(final long prNumber) {
final String path = String.format(PR_REVIEWS_TEMPLATE, owner, repo, prNumber);
log.debug("Fetching pull request reviews from " + path);
return github.request(path, LIST_REVIEW_TYPE_REFERENCE);
}
/**
* List pull request reviews paginated. Reviews are returned in chronological order.
*
* @deprecated Use {@link #listReviews(long,long)} instead
* @param prNumber pull request number
* @param itemsPerPage prNumber of items per page
* @return iterator of reviews
*/
@Deprecated
public Iterator<AsyncPage<Review>> listReviews(final int prNumber, final int itemsPerPage) {
return listReviews((long) prNumber, itemsPerPage);
}
/**
* List pull request reviews paginated. Reviews are returned in chronological order.
*
* @param prNumber pull request number
* @param itemsPerPage prNumber of items per page
* @return iterator of reviews
*/
public Iterator<AsyncPage<Review>> listReviews(final long prNumber, final long itemsPerPage) {
// FIXME Use itemsPerPage property
final String path = String.format(PR_REVIEWS_TEMPLATE, owner, repo, prNumber);
log.debug("Fetching pull request reviews from " + path);
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_REVIEW_TYPE_REFERENCE));
}
/**
* Creates a review for a pull request.
*
* @deprecated Use {@link #createReview(long,ReviewParameters)} instead
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as sha, body and event
* @see "https://developer.github.com/v3/pulls/reviews/#create-a-review-for-a-pull-request"
*/
@Deprecated
public CompletableFuture<Review> createReview(
final int prNumber, final ReviewParameters properties) {
return createReview((long) prNumber, properties);
}
/**
* Creates a review for a pull request.
*
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as sha, body and event
* @see "https://developer.github.com/v3/pulls/reviews/#create-a-review-for-a-pull-request"
*/
public CompletableFuture<Review> createReview(
final long prNumber, final ReviewParameters properties) {
final String path = String.format(PR_REVIEWS_TEMPLATE, owner, repo, prNumber);
final String jsonPayload = github.json().toJsonUnchecked(properties);
log.debug("Creating review for PR: " + path);
return github.post(path, jsonPayload, Review.class);
}
/**
* List pull request requested reviews.
*
* @deprecated Use {@link #listReviewRequests(long)} instead
* @param prNumber pull request number
* @return list of reviews
*/
@Deprecated
public CompletableFuture<ReviewRequests> listReviewRequests(final int prNumber) {
return listReviewRequests((long) prNumber);
}
/**
* List pull request requested reviews.
*
* @param prNumber pull request number
* @return list of reviews
*/
public CompletableFuture<ReviewRequests> listReviewRequests(final long prNumber) {
final String path = String.format(PR_REVIEW_REQUESTS_TEMPLATE, owner, repo, prNumber);
log.debug("Fetching pull request requested reviews from " + path);
return github.request(path, LIST_REVIEW_REQUEST_TYPE_REFERENCE);
}
/**
* Requests a review for a pull request.
*
* @deprecated Use {@link #requestReview(long,RequestReviewParameters)} instead
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as reviewers and team_reviewers.
* @see "https://docs.github.com/en/rest/reference/pulls#request-reviewers-for-a-pull-request"
*/
@Deprecated
public CompletableFuture<PullRequest> requestReview(
final int prNumber, final RequestReviewParameters properties) {
return requestReview((long) prNumber, properties);
}
/**
* Requests a review for a pull request.
*
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as reviewers and team_reviewers.
* @see "https://docs.github.com/en/rest/reference/pulls#request-reviewers-for-a-pull-request"
*/
public CompletableFuture<PullRequest> requestReview(
final long prNumber, final RequestReviewParameters properties) {
final String path = String.format(PR_REVIEW_REQUESTS_TEMPLATE, owner, repo, prNumber);
final String jsonPayload = github.json().toJsonUnchecked(properties);
log.debug("Requesting reviews for PR: " + path);
return github.post(path, jsonPayload, PullRequest.class);
}
/**
* Remove a request for review for a pull request.
*
* @deprecated Use {@link #removeRequestedReview(long,RequestReviewParameters)} instead
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as reviewers and team_reviewers.
* @see "https://docs.github.com/en/rest/reference/pulls#request-reviewers-for-a-pull-request"
*/
@Deprecated
public CompletableFuture<Void> removeRequestedReview(
final int prNumber, final RequestReviewParameters properties) {
return removeRequestedReview((long) prNumber, properties);
}
/**
* Remove a request for review for a pull request.
*
* @param prNumber pull request number
* @param properties properties for reviewing the PR, such as reviewers and team_reviewers.
* @see "https://docs.github.com/en/rest/reference/pulls#request-reviewers-for-a-pull-request"
*/
public CompletableFuture<Void> removeRequestedReview(
final long prNumber, final RequestReviewParameters properties) {
final String path = String.format(PR_REVIEW_REQUESTS_TEMPLATE, owner, repo, prNumber);
final String jsonPayload = github.json().toJsonUnchecked(properties);
log.debug("Removing requested reviews for PR: " + path);
return github.delete(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER);
}
/**
* Merges a pull request.
*
* @deprecated Use {@link #merge(long,MergeParameters)} instead
* @param prNumber pull request number
* @param properties the properties on merging the PR, such as title, message and sha
* @see "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button"
*/
@Deprecated
public CompletableFuture<Void> merge(final int prNumber, final MergeParameters properties) {
return merge((long) prNumber, properties);
}
/**
* Merges a pull request.
*
* @param prNumber pull request number
* @param properties the properties on merging the PR, such as title, message and sha
* @see "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button"
*/
public CompletableFuture<Void> merge(final long prNumber, final MergeParameters properties) {
final String path = String.format(PR_NUMBER_TEMPLATE + "/merge", owner, repo, prNumber);
final String jsonPayload = github.json().toJsonUnchecked(properties);
log.debug("Merging pr, running: {}", path);
return github.put(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER);
}
/**
* Fetches a pull request patch.
*
* @deprecated Use {@link #patch(long)} instead
* @param prNumber pull request number
* @return reader for the patch
*/
@Deprecated
public CompletableFuture<Reader> patch(final int prNumber) {
return patch((long) prNumber);
}
/**
* Fetches a pull request patch.
*
* @param prNumber pull request number
* @return reader for the patch
*/
public CompletableFuture<Reader> patch(final long prNumber) {
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, prNumber);
final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.patch");
log.debug("Fetching pull request patch from " + path);
return github
.request(path, extraHeaders)
.thenApply(
response -> {
final var body = response.body();
if (isNull(body)) {
return Reader.nullReader();
}
return new InputStreamReader(body);
});
}
/**
* Fetches a pull request diff.
*
* @deprecated Use {@link #diff(long)} instead
* @param prNumber pull request number
* @return reader for the diff
*/
@Deprecated
public CompletableFuture<Reader> diff(final int prNumber) {
return diff((long) prNumber);
}
/**
* Fetches a pull request diff.
*
* @param prNumber pull request number
* @return reader for the diff
*/
public CompletableFuture<Reader> diff(final long prNumber) {
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, prNumber);
final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.diff");
log.debug("Fetching pull diff from " + path);
return github
.request(path, extraHeaders)
.thenApply(
response -> {
final var body = response.body();
if (isNull(body)) {
return Reader.nullReader();
}
return new InputStreamReader(body);
});
}
/**
* List pull requests using given parameters.
*
* @param parameterPath request parameters
* @return pull requests
*/
private CompletableFuture<List<PullRequestItem>> list(final String parameterPath) {
final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo);
log.debug("Fetching pull requests from " + path);
return github.request(path, LIST_PR_TYPE_REFERENCE);
}
/**
* Creates a reply to a pull request review comment.
*
* @param prNumber pull request number
* @param commentId the ID of the comment to reply to
* @param body the reply message
* @return the created comment
* @see "https://docs.github.com/en/rest/pulls/comments#create-a-reply-for-a-review-comment"
*/
public CompletableFuture<Comment> createCommentReply(
final long prNumber, final long commentId, final String body) {
final String path =
String.format(PR_COMMENT_REPLIES_TEMPLATE, owner, repo, prNumber, commentId);
final Map<String, String> payload = ImmutableMap.of("body", body);
final String jsonPayload = github.json().toJsonUnchecked(payload);
log.debug("Creating reply to PR comment: " + path);
return github.post(path, jsonPayload, Comment.class);
}
}