24
24
import static com .spotify .github .v3 .clients .GitHubClient .LIST_BRANCHES ;
25
25
import static com .spotify .github .v3 .clients .GitHubClient .LIST_COMMIT_TYPE_REFERENCE ;
26
26
import static com .spotify .github .v3 .clients .GitHubClient .LIST_FOLDERCONTENT_TYPE_REFERENCE ;
27
+ import static com .spotify .github .v3 .clients .GitHubClient .LIST_PR_TYPE_REFERENCE ;
27
28
import static com .spotify .github .v3 .clients .GitHubClient .LIST_REPOSITORY ;
28
29
import static com .spotify .github .v3 .clients .GitHubClient .LIST_STATUS_TYPE_REFERENCE ;
29
30
34
35
import com .spotify .github .v3 .exceptions .RequestNotOkException ;
35
36
import com .spotify .github .v3 .git .Tree ;
36
37
import com .spotify .github .v3 .hooks .requests .WebhookCreate ;
38
+ import com .spotify .github .v3 .prs .PullRequestItem ;
37
39
import com .spotify .github .v3 .repos .Branch ;
38
40
import com .spotify .github .v3 .repos .Commit ;
39
41
import com .spotify .github .v3 .repos .CommitComparison ;
49
51
import java .lang .invoke .MethodHandles ;
50
52
import java .util .Iterator ;
51
53
import java .util .List ;
54
+ import java .util .Map ;
52
55
import java .util .Optional ;
53
56
import java .util .concurrent .CompletableFuture ;
54
57
import java .util .concurrent .CompletionException ;
58
+ import javax .ws .rs .core .HttpHeaders ;
55
59
import org .slf4j .Logger ;
56
60
import org .slf4j .LoggerFactory ;
57
61
@@ -69,6 +73,7 @@ public class RepositoryClient {
69
73
public static final String STATUS_URI_TEMPLATE = "/repos/%s/%s/statuses/%s" ;
70
74
private static final String COMMITS_URI_TEMPLATE = "/repos/%s/%s/commits" ;
71
75
private static final String COMMIT_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s" ;
76
+ private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s/pulls" ;
72
77
private static final String COMMIT_STATUS_URI_TEMPLATE = "/repos/%s/%s/commits/%s/status" ;
73
78
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s" ;
74
79
private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s" ;
@@ -272,6 +277,22 @@ public CompletableFuture<List<CommitItem>> listCommits() {
272
277
return github .request (path , LIST_COMMIT_TYPE_REFERENCE );
273
278
}
274
279
280
+ /**
281
+ * List pull requests that contain the given commit.
282
+ *
283
+ * @param sha commit sha
284
+ * @return pull requests
285
+ */
286
+ public CompletableFuture <List <PullRequestItem >> listPullRequestsForCommit (final String sha ) {
287
+ final String path = String .format (COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE , owner , repo , sha );
288
+
289
+ // As of GHE 3.2, this feature is still in preview, so we need to add the extra header.
290
+ // https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/
291
+ final Map <String , String > extraHeaders =
292
+ ImmutableMap .of (HttpHeaders .ACCEPT , "application/vnd.github.groot-preview+json" );
293
+ return github .request (path , LIST_PR_TYPE_REFERENCE , extraHeaders );
294
+ }
295
+
275
296
/**
276
297
* Get a repository commit.
277
298
*
0 commit comments