Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ public boolean canMaintainerModify() throws IOException {
return maintainerCanModify;
}

/**
* Converts a pull request from ready for review to draft
*
* @throws IOException
* the io exception
* @throws IllegalStateException
* if the pull request is not a draft
*/
public void convertToDraft() throws IOException {
if (draft) {
throw new IllegalStateException("Pull request is already a draft");
}

StringBuilder inputBuilder = new StringBuilder();
addParameter(inputBuilder, "pullRequestId", this.getNodeId());

String graphqlBody = "mutation ConvertToDraft { convertPullRequestToDraft(input: {" + inputBuilder
+ "}) { pullRequest { id } } }";

root().createGraphQLRequest(graphqlBody).sendGraphQL();

refresh();
}

/**
* Create review gh pull request review builder.
*
Expand Down Expand Up @@ -275,6 +299,10 @@ public GHCommitPointer getBase() {
return base;
}

//
// details that are only available via get with ID
//

/**
* Gets changed files.
*
Expand All @@ -287,10 +315,6 @@ public int getChangedFiles() throws IOException {
return changedFiles;
}

//
// details that are only available via get with ID
//

/**
* Gets the closed by.
*
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/kohsuke/github/GHPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,49 @@ public void commentsInPullRequestReviewBuilder() {
assertThat(multilineDraftReviewComment.getLine(), equalTo(2));
}

/**
* Test marking a draft pull request as ready for review.
*
* @throws Exception
* the exception
*/
@Test
public void convertToDraft() throws Exception {
// Create a draft PR first
GHPullRequest p = getRepository()
.createPullRequest("convertToDraft", "test/stable", "main", "## test", false, false);
assertThat(p.isDraft(), is(false));

// Mark it ready for review
p.convertToDraft();

// Verify it's no longer a draft
assertThat(p.isDraft(), is(true));
}

/**
* Test marking a draft pull request as ready for review.
*
* @throws Exception
* the exception
*/
@Test
public void convertToDraftFromDraft() throws Exception {
// Create a draft PR first
GHPullRequest p = getRepository()
.createPullRequest("convertToDraft", "test/stable", "main", "## test", false, true);
assertThat(p.isDraft(), is(true));

// Mark it ready for review
try {
p.convertToDraft();
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("Pull request is already a draft"));
}

}

/**
* Creates the draft pull request.
*
Expand Down
Loading