Skip to content

Commit 45c541f

Browse files
committed
Add convertToDraft for pull requests
fixes #2197
1 parent 9152b37 commit 45c541f

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ public boolean canMaintainerModify() throws IOException {
160160
return maintainerCanModify;
161161
}
162162

163+
/**
164+
* Converts a pull request from ready for review to draft
165+
*
166+
* @throws IOException
167+
* the io exception
168+
* @throws IllegalStateException
169+
* if the pull request is not a draft
170+
*/
171+
public void convertToDraft() throws IOException {
172+
if (draft) {
173+
throw new IllegalStateException("Pull request is already a draft");
174+
}
175+
176+
StringBuilder inputBuilder = new StringBuilder();
177+
addParameter(inputBuilder, "pullRequestId", this.getNodeId());
178+
179+
String graphqlBody = "mutation ConvertToDraft { convertPullRequestToDraft(input: {" + inputBuilder
180+
+ "}) { pullRequest { id } } }";
181+
182+
root().createGraphQLRequest(graphqlBody).sendGraphQL();
183+
184+
refresh();
185+
}
186+
163187
/**
164188
* Create review gh pull request review builder.
165189
*
@@ -275,6 +299,10 @@ public GHCommitPointer getBase() {
275299
return base;
276300
}
277301

302+
//
303+
// details that are only available via get with ID
304+
//
305+
278306
/**
279307
* Gets changed files.
280308
*
@@ -287,10 +315,6 @@ public int getChangedFiles() throws IOException {
287315
return changedFiles;
288316
}
289317

290-
//
291-
// details that are only available via get with ID
292-
//
293-
294318
/**
295319
* Gets the closed by.
296320
*

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,49 @@ public void commentsInPullRequestReviewBuilder() {
225225
assertThat(multilineDraftReviewComment.getLine(), equalTo(2));
226226
}
227227

228+
/**
229+
* Test marking a draft pull request as ready for review.
230+
*
231+
* @throws Exception
232+
* the exception
233+
*/
234+
@Test
235+
public void convertToDraft() throws Exception {
236+
// Create a draft PR first
237+
GHPullRequest p = getRepository()
238+
.createPullRequest("convertToDraft", "test/stable", "main", "## test", false, false);
239+
assertThat(p.isDraft(), is(false));
240+
241+
// Mark it ready for review
242+
p.convertToDraft();
243+
244+
// Verify it's no longer a draft
245+
assertThat(p.isDraft(), is(true));
246+
}
247+
248+
/**
249+
* Test marking a draft pull request as ready for review.
250+
*
251+
* @throws Exception
252+
* the exception
253+
*/
254+
@Test
255+
public void convertToDraftFromDraft() throws Exception {
256+
// Create a draft PR first
257+
GHPullRequest p = getRepository()
258+
.createPullRequest("convertToDraft", "test/stable", "main", "## test", false, true);
259+
assertThat(p.isDraft(), is(true));
260+
261+
// Mark it ready for review
262+
try {
263+
p.convertToDraft();
264+
fail("Expected IllegalStateException");
265+
} catch (IllegalStateException e) {
266+
assertThat(e.getMessage(), equalTo("Pull request is already a draft"));
267+
}
268+
269+
}
270+
228271
/**
229272
* Creates the draft pull request.
230273
*

0 commit comments

Comments
 (0)