Skip to content

Commit 081e485

Browse files
authored
Merge pull request #773 from martinvanzijl/issue_444_unset_milestone
Add ability to unset the milestone of an issue
2 parents 4adf88d + 31e2b1b commit 081e485

20 files changed

+1219
-7
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ private void edit(String key, Object value) throws IOException {
228228
root.createRequest().with(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
229229
}
230230

231+
/**
232+
* Identical to edit(), but allows null for the value.
233+
*/
234+
private void editNullable(String key, Object value) throws IOException {
235+
root.createRequest().withNullable(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
236+
}
237+
231238
private void editIssue(String key, Object value) throws IOException {
232239
root.createRequest().with(key, value).method("PATCH").withUrlPath(getIssuesApiRoute()).send();
233240
}
@@ -277,15 +284,19 @@ public void setBody(String body) throws IOException {
277284
}
278285

279286
/**
280-
* Sets milestone.
287+
* Sets the milestone for this issue.
281288
*
282289
* @param milestone
283-
* the milestone
290+
* The milestone to assign this issue to. Use null to remove the milestone for this issue.
284291
* @throws IOException
285-
* the io exception
292+
* The io exception
286293
*/
287294
public void setMilestone(GHMilestone milestone) throws IOException {
288-
edit("milestone", milestone.getNumber());
295+
if (milestone == null) {
296+
editNullable("milestone", null);
297+
} else {
298+
edit("milestone", milestone.getNumber());
299+
}
289300
}
290301

291302
/**

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.io.IOException;
88
import java.util.Date;
99

10-
import static org.junit.Assert.assertEquals;
11-
1210
/**
1311
* @author Martin van Zijl
1412
*/
@@ -23,7 +21,8 @@ public void cleanUp() throws Exception {
2321
}
2422

2523
for (GHMilestone milestone : getRepository(getGitHubBeforeAfter()).listMilestones(GHIssueState.ALL)) {
26-
if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle())) {
24+
if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle())
25+
|| "Unset Test Milestone".equals(milestone.getTitle())) {
2726
milestone.delete();
2827
}
2928
}
@@ -54,6 +53,23 @@ public void testUpdateMilestone() throws Exception {
5453
assertEquals(OUTPUT_DUE_DATE, milestone.getDueOn());
5554
}
5655

56+
@Test
57+
public void testUnsetMilestone() throws IOException {
58+
GHRepository repo = getRepository();
59+
GHMilestone milestone = repo.createMilestone("Unset Test Milestone", "For testUnsetMilestone");
60+
GHIssue issue = repo.createIssue("Issue for testUnsetMilestone").create();
61+
62+
// set the milestone
63+
issue.setMilestone(milestone);
64+
issue = repo.getIssue(issue.getNumber()); // force reload
65+
assertEquals(milestone.getNumber(), issue.getMilestone().getNumber());
66+
67+
// remove the milestone
68+
issue.setMilestone(null);
69+
issue = repo.getIssue(issue.getNumber()); // force reload
70+
assertEquals(null, issue.getMilestone());
71+
}
72+
5773
protected GHRepository getRepository() throws IOException {
5874
return getRepository(gitHub);
5975
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/github-api-test-org",
6+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 25,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/github-api-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2015-04-20T00:42:30Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 147,
29+
"collaborators": 0,
30+
"billing_email": "[email protected]",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 15,
39+
"seats": 0
40+
}
41+
}

0 commit comments

Comments
 (0)