Skip to content

Commit 72d4b9b

Browse files
authored
Merge pull request hub4j#525 from vbehar/draft-pr
Add support for draft pull requests
2 parents e7348df + 81ea138 commit 72d4b9b

44 files changed

Lines changed: 31147 additions & 30 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/org/kohsuke/github/GHPullRequest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.Date;
3434
import java.util.List;
3535

36+
import static org.kohsuke.github.Previews.SHADOW_CAT;
37+
3638
/**
3739
* A pull request.
3840
*
@@ -54,6 +56,8 @@ public class GHPullRequest extends GHIssue implements Refreshable {
5456
private GHUser merged_by;
5557
private int review_comments, additions, commits;
5658
private boolean merged, maintainer_can_modify;
59+
// making these package private to all for testing
60+
boolean draft;
5761
private Boolean mergeable;
5862
private int deletions;
5963
private String mergeable_state;
@@ -190,6 +194,11 @@ public boolean canMaintainerModify() throws IOException {
190194
return maintainer_can_modify;
191195
}
192196

197+
public boolean isDraft() throws IOException {
198+
populate();
199+
return draft;
200+
}
201+
193202
/**
194203
* Is this PR mergeable?
195204
*
@@ -262,7 +271,9 @@ public void refresh() throws IOException {
262271
if (root.isOffline()) {
263272
return; // cannot populate, will have to live with what we have
264273
}
265-
root.retrieve().to(url, this).wrapUp(owner);
274+
root.retrieve()
275+
.withPreview(SHADOW_CAT)
276+
.to(url, this).wrapUp(owner);
266277
}
267278

268279
/**

src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.kohsuke.github;
22

3+
import static org.kohsuke.github.Previews.SHADOW_CAT;
4+
35
/**
46
* Lists up pull requests with some filtering and sorting.
57
*
@@ -46,7 +48,9 @@ public GHPullRequestQueryBuilder direction(GHDirection d) {
4648

4749
@Override
4850
public PagedIterable<GHPullRequest> list() {
49-
return req.asPagedIterable(
51+
return req
52+
.withPreview(SHADOW_CAT)
53+
.asPagedIterable(
5054
repo.getApiTailUrl("pulls"),
5155
GHPullRequest[].class,
5256
item -> item.wrapUp(repo) );

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,9 @@ public GHRepository forkTo(GHOrganization org) throws IOException {
745745
* Retrieves a specified pull request.
746746
*/
747747
public GHPullRequest getPullRequest(int i) throws IOException {
748-
return root.retrieve().to(getApiTailUrl("pulls/" + i), GHPullRequest.class).wrapUp(this);
748+
return root.retrieve()
749+
.withPreview(SHADOW_CAT)
750+
.to(getApiTailUrl("pulls/" + i), GHPullRequest.class).wrapUp(this);
749751
}
750752

751753
/**
@@ -814,11 +816,39 @@ public GHPullRequest createPullRequest(String title, String head, String base, S
814816
*/
815817
public GHPullRequest createPullRequest(String title, String head, String base, String body,
816818
boolean maintainerCanModify) throws IOException {
817-
return new Requester(root).with("title",title)
819+
return createPullRequest(title, head, base, body, maintainerCanModify, false);
820+
}
821+
822+
/**
823+
* Creates a new pull request. Maintainer's permissions and draft aware.
824+
*
825+
* @param title
826+
* Required. The title of the pull request.
827+
* @param head
828+
* Required. The name of the branch where your changes are implemented.
829+
* For cross-repository pull requests in the same network,
830+
* namespace head with a user like this: username:branch.
831+
* @param base
832+
* Required. The name of the branch you want your changes pulled into.
833+
* This should be an existing branch on the current repository.
834+
* @param body
835+
* The contents of the pull request. This is the markdown description
836+
* of a pull request.
837+
* @param maintainerCanModify
838+
* Indicates whether maintainers can modify the pull request.
839+
* @param draft
840+
* Indicates whether to create a draft pull request or not.
841+
*/
842+
public GHPullRequest createPullRequest(String title, String head, String base, String body,
843+
boolean maintainerCanModify, boolean draft) throws IOException {
844+
return new Requester(root)
845+
.withPreview(SHADOW_CAT)
846+
.with("title",title)
818847
.with("head",head)
819848
.with("base",base)
820849
.with("body",body)
821850
.with("maintainer_can_modify", maintainerCanModify)
851+
.with("draft", draft)
822852
.to(getApiTailUrl("pulls"),GHPullRequest.class)
823853
.wrapUp(this);
824854
}

src/main/java/org/kohsuke/github/Previews.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@
99
*/
1010
/*package*/ class Previews {
1111

12-
/**
13-
* Require multiple approving reviews
14-
*
15-
* @see <a href="https://developer.github.com/v3/previews/#require-multiple-approving-reviews">GitHub API Previews</a>
16-
*/
17-
static final String LUKE_CAGE = "application/vnd.github.luke-cage-preview+json";
18-
19-
/**
20-
* Reactions
21-
*
22-
* @see <a href="https://developer.github.com/v3/previews/#reactions">GitHub API Previews</a>
23-
*/
24-
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
25-
2612
/**
2713
* Commit Search
2814
*
@@ -31,11 +17,11 @@
3117
static final String CLOAK = "application/vnd.github.cloak-preview+json";
3218

3319
/**
34-
* Require signed commits
20+
* Owners of GitHub Apps can now uninstall an app using the Apps API
3521
*
36-
* @see <a href="https://developer.github.com/v3/previews/#require-signed-commits">GitHub API Previews</a>
22+
* @see <a href="https://developer.github.com/v3/previews/#uninstall-a-github-app">GitHub API Previews</a>
3723
*/
38-
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
24+
static final String GAMBIT = "application/vnd.github.gambit-preview+json";
3925

4026
/**
4127
* Manage projects
@@ -44,6 +30,13 @@
4430
*/
4531
static final String INERTIA = "application/vnd.github.inertia-preview+json";
4632

33+
/**
34+
* Require multiple approving reviews
35+
*
36+
* @see <a href="https://developer.github.com/v3/previews/#require-multiple-approving-reviews">GitHub API Previews</a>
37+
*/
38+
static final String LUKE_CAGE = "application/vnd.github.luke-cage-preview+json";
39+
4740
/**
4841
* Manage integrations through the API
4942
*
@@ -52,16 +45,30 @@
5245
static final String MACHINE_MAN = "application/vnd.github.machine-man-preview+json";
5346

5447
/**
55-
* Owners of GitHub Apps can now uninstall an app using the Apps API
48+
* Draft pull requests
5649
*
57-
* @see <a href="https://developer.github.com/v3/previews/#uninstall-a-github-app">GitHub API Previews</a>
50+
* @see <a href="https://developer.github.com/v3/previews/#draft-pull-requests">GitHub API Previews</a>
5851
*/
59-
static final String GAMBIT = "application/vnd.github.gambit-preview+json";
52+
static final String SHADOW_CAT = "application/vnd.github.shadow-cat-preview+json";
53+
54+
/**
55+
* Reactions
56+
*
57+
* @see <a href="https://developer.github.com/v3/previews/#reactions">GitHub API Previews</a>
58+
*/
59+
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
6060

6161
/**
6262
* Label emoji, search, and descriptions
6363
*
6464
* @see <a href="https://developer.github.com/v3/previews/#label-emoji-search-and-descriptions">GitHub API Previews</a>
6565
*/
6666
static final String SYMMETRA = "application/vnd.github.symmetra-preview+json";
67+
68+
/**
69+
* Require signed commits
70+
*
71+
* @see <a href="https://developer.github.com/v3/previews/#require-signed-commits">GitHub API Previews</a>
72+
*/
73+
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
6774
}

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,12 @@ public void tryHook() throws Exception {
435435
}
436436
}
437437

438-
@Ignore("Needs mocking check")
439438
@Test
440439
public void testEventApi() throws Exception {
441440
for (GHEventInfo ev : gitHub.getEvents()) {
442-
System.out.println(ev);
443441
if (ev.getType() == GHEvent.PULL_REQUEST) {
444442
GHEventPayload.PullRequest pr = ev.getPayload(GHEventPayload.PullRequest.class);
445-
System.out.println(pr.getNumber());
446-
System.out.println(pr.getPullRequest());
443+
assertThat(pr.getNumber(), is(pr.getPullRequest().getNumber()));
447444
}
448445
}
449446
}

src/test/java/org/kohsuke/github/GHPullRequestTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,42 @@ public void cleanUp() throws Exception {
3232
@Test
3333
public void createPullRequest() throws Exception {
3434
String name = "createPullRequest";
35-
GHPullRequest p = getRepository().createPullRequest(name, "test/stable", "master", "## test");
36-
System.out.println(p.getUrl());
35+
GHRepository repo = getRepository();
36+
GHPullRequest p = repo.createPullRequest(name, "test/stable", "master", "## test");
3737
assertEquals(name, p.getTitle());
38+
assertThat(p.canMaintainerModify(), is(false));
39+
assertThat(p.isDraft(), is(false));
40+
}
41+
42+
@Test
43+
public void createDraftPullRequest() throws Exception {
44+
String name = "createDraftPullRequest";
45+
GHRepository repo = getRepository();
46+
GHPullRequest p = repo.createPullRequest(name, "test/stable", "master", "## test", false, true);
47+
assertEquals(name, p.getTitle());
48+
assertThat(p.canMaintainerModify(), is(false));
49+
assertThat(p.isDraft(), is(true));
50+
51+
// There are multiple paths to get PRs and each needs to read draft correctly
52+
p.draft = false;
53+
p.refresh();
54+
assertThat(p.isDraft(), is(true));
55+
56+
GHPullRequest p2 = repo.getPullRequest(p.getNumber());
57+
assertThat(p2.getNumber(), is(p.getNumber()));
58+
assertThat(p2.isDraft(), is(true));
59+
60+
p = repo.queryPullRequests()
61+
.state(GHIssueState.OPEN)
62+
.head("test/stable")
63+
.list().asList().get(0);
64+
assertThat(p2.getNumber(), is(p.getNumber()));
65+
assertThat(p.isDraft(), is(true));
66+
67+
68+
69+
70+
3871
}
3972

4073
@Test

0 commit comments

Comments
 (0)