Skip to content

Commit 621795c

Browse files
Improve support for getting and setting a PRs milestone
ExtendedGitHubClient.java ExtendedPullRequestService.java - expanded the UncheckedIOException love to more places. ExtendedIssueService.java - added a method to set an issues milestone ExtendedMilestone.java ExtendedMilestoneService.java - added to support updatedAt and closedAt fields MilestoneGroovyObject.java - wrapper over ExtendedMilestone PullRequestGroovyObject.java - getMilestone() now return MilestoneGroovyObject - setMilestone() is now @Whitelisted - overloaded setMilestone to take both int and MilestoneGroovyObject - cleaned up some IOException uglyness
1 parent 320b075 commit 621795c

File tree

8 files changed

+256
-36
lines changed

8 files changed

+256
-36
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ issueUrl | `String` | false
177177
title | `String` | **true**
178178
body | `String` | **true**
179179
locked | `Boolean` | **true** | Accepts `true`, `false` or `'true'`, `'false'`
180-
milestone | `Integer` | **true**
180+
milestone | `Milestone` | **true** | Setter accepts int or Milestone class.
181181
head | `String` | false | Revision (SHA) of the head commit of this pull request
182182
headRef | `String` | false | Name of the branch this pull request is created for
183183
base | `String` | **true** | Name of the base branch in the current repository this pull request targets
@@ -364,6 +364,27 @@ state | `String` | One of APPROVED, PENDING, CHANGES_REQUESTED, DISMISSED, COMME
364364
### Methods
365365
None.
366366

367+
## Milestone
368+
### Properties
369+
Name | Type | Setter | Description
370+
-----|------|----------|------------
371+
number | `Integer` | false
372+
createdAt | `Date` | false
373+
dueOn | `Date` | false
374+
updatedAt | `Date` | false
375+
closedAt | `Date` | false
376+
closedIssues | `Integer` | false
377+
openIssues | `Integer` | false
378+
description | `String` | false
379+
state | `String` | false
380+
title | `String` | false
381+
url | `String` | false
382+
creator | `String` | false
383+
384+
### Methods
385+
None.
386+
387+
367388
# Examples
368389

369390
## Pull Requests
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.jenkinsci.plugins.pipeline.github;
2+
3+
import groovy.lang.GroovyObjectSupport;
4+
import org.eclipse.egit.github.core.Milestone;
5+
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMilestone;
6+
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
7+
8+
import java.io.Serializable;
9+
import java.util.Date;
10+
11+
/**
12+
* Groovy wrapper over {@link Milestone}
13+
*
14+
* @author Aaron Whiteside
15+
* @see Milestone
16+
*/
17+
public class MilestoneGroovyObject extends GroovyObjectSupport implements Serializable {
18+
private static final long serialVersionUID = 1L;
19+
20+
private final ExtendedMilestone milestone;
21+
22+
MilestoneGroovyObject(final ExtendedMilestone milestone) {
23+
this.milestone = milestone;
24+
}
25+
26+
@Whitelisted
27+
public Date getCreatedAt() {
28+
return milestone.getCreatedAt();
29+
}
30+
31+
@Whitelisted
32+
public Date getDueOn() {
33+
return milestone.getDueOn();
34+
}
35+
36+
@Whitelisted
37+
public int getClosedIssues() {
38+
return milestone.getClosedIssues();
39+
}
40+
41+
@Whitelisted
42+
public int getNumber() {
43+
return milestone.getNumber();
44+
}
45+
46+
@Whitelisted
47+
public int getOpenIssues() {
48+
return milestone.getOpenIssues();
49+
}
50+
51+
@Whitelisted
52+
public String getDescription() {
53+
return milestone.getDescription();
54+
}
55+
56+
@Whitelisted
57+
public String getState() {
58+
return milestone.getState();
59+
}
60+
61+
@Whitelisted
62+
public String getTitle() {
63+
return milestone.getTitle();
64+
}
65+
66+
@Whitelisted
67+
public String getUrl() {
68+
return milestone.getUrl();
69+
}
70+
71+
@Whitelisted
72+
public String getCreator() {
73+
return milestone.getCreator().getLogin();
74+
}
75+
76+
@Whitelisted
77+
public Date getUpdatedAt() {
78+
return milestone.getUpdatedAt();
79+
}
80+
81+
@Whitelisted
82+
public Date getClosedAt() {
83+
return milestone.getClosedAt();
84+
}
85+
86+
}

src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGroovyObject.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.egit.github.core.Comment;
88
import org.eclipse.egit.github.core.CommitStatus;
99
import org.eclipse.egit.github.core.Label;
10+
import org.eclipse.egit.github.core.Milestone;
1011
import org.eclipse.egit.github.core.PullRequestMarker;
1112
import org.eclipse.egit.github.core.RepositoryId;
1213
import org.eclipse.egit.github.core.User;
@@ -16,6 +17,7 @@
1617
import org.jenkinsci.plugins.pipeline.github.client.ExtendedGitHubClient;
1718
import org.jenkinsci.plugins.pipeline.github.client.ExtendedIssueService;
1819
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMergeStatus;
20+
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMilestoneService;
1921
import org.jenkinsci.plugins.pipeline.github.client.ExtendedPullRequest;
2022
import org.jenkinsci.plugins.pipeline.github.client.ExtendedPullRequestService;
2123
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
@@ -61,6 +63,7 @@ public class PullRequestGroovyObject extends GroovyObjectSupport implements Seri
6163
private final ExtendedPullRequestService pullRequestService;
6264
private final ExtendedIssueService issueService;
6365
private final ExtendedCommitService commitService;
66+
private final ExtendedMilestoneService milestoneService;
6467
private ExtendedPullRequest pullRequest;
6568

6669
PullRequestGroovyObject(@Nonnull final CpsScript script) throws Exception {
@@ -79,6 +82,7 @@ public class PullRequestGroovyObject extends GroovyObjectSupport implements Seri
7982
this.pullRequestService = new ExtendedPullRequestService(gitHubClient);
8083
this.issueService = new ExtendedIssueService(gitHubClient);
8184
this.commitService = new ExtendedCommitService(gitHubClient);
85+
this.milestoneService = new ExtendedMilestoneService(gitHubClient);
8286
this.pullRequest = pullRequestService.getPullRequest(base, pullRequestHead.getNumber());
8387
}
8488

@@ -133,8 +137,12 @@ public boolean isLocked() {
133137
}
134138

135139
@Whitelisted
136-
public int getMilestone() {
137-
return pullRequest.getMilestone().getNumber();
140+
public MilestoneGroovyObject getMilestone() {
141+
return Optional.ofNullable(pullRequest.getMilestone())
142+
.map(Milestone::getNumber)
143+
.map(m -> milestoneService.getMilestone(base, m))
144+
.map(MilestoneGroovyObject::new)
145+
.orElse(null);
138146
}
139147

140148
@Whitelisted
@@ -335,8 +343,24 @@ public Iterable<CommitFileGroovyObject> getFiles() {
335343
}
336344
}
337345

346+
@Whitelisted
338347
public void setMilestone(final int milestoneNumber) {
339-
// todo
348+
pullRequest.setMilestone(
349+
issueService.setMilestone(base, pullRequest.getNumber(), milestoneNumber)
350+
.getMilestone());
351+
}
352+
353+
@Whitelisted
354+
public void setMilestone(final MilestoneGroovyObject milestone) {
355+
if (milestone == null) {
356+
// call setMilestone because the caller might not have the right permissions to remove
357+
// the milestone and it'll return the current milestone.
358+
pullRequest.setMilestone(
359+
issueService.setMilestone(base, pullRequest.getNumber(), null)
360+
.getMilestone());
361+
} else {
362+
setMilestone(milestone.getNumber());
363+
}
340364
}
341365

342366
@Whitelisted
@@ -359,11 +383,7 @@ public void setTitle(final String title) {
359383
ExtendedPullRequest edit = new ExtendedPullRequest();
360384
edit.setNumber(pullRequest.getNumber());
361385
edit.setTitle(title);
362-
try {
363-
pullRequest = pullRequestService.editPullRequest(base, edit);
364-
} catch (final IOException e) {
365-
throw new UncheckedIOException(e);
366-
}
386+
pullRequest = pullRequestService.editPullRequest(base, edit);
367387
}
368388

369389
@Whitelisted
@@ -373,11 +393,7 @@ public void setBody(final String body) {
373393
ExtendedPullRequest edit = new ExtendedPullRequest();
374394
edit.setNumber(pullRequest.getNumber());
375395
edit.setBody(body);
376-
try {
377-
pullRequest = pullRequestService.editPullRequest(base, edit);
378-
} catch (final IOException e) {
379-
throw new UncheckedIOException(e);
380-
}
396+
pullRequest = pullRequestService.editPullRequest(base, edit);
381397
}
382398

383399
@Whitelisted
@@ -387,11 +403,7 @@ public void setState(final String state) {
387403
ExtendedPullRequest edit = new ExtendedPullRequest();
388404
edit.setNumber(pullRequest.getNumber());
389405
edit.setState(state);
390-
try {
391-
pullRequest = pullRequestService.editPullRequest(base, edit);
392-
} catch (final IOException e) {
393-
throw new UncheckedIOException(e);
394-
}
406+
pullRequest = pullRequestService.editPullRequest(base, edit);
395407
}
396408

397409
@Whitelisted
@@ -401,23 +413,15 @@ public void setBase(final String newBase) {
401413
ExtendedPullRequest edit = new ExtendedPullRequest();
402414
edit.setNumber(pullRequest.getNumber());
403415
edit.setBase(new PullRequestMarker().setRef(newBase));
404-
try {
405-
pullRequest = pullRequestService.editPullRequest(base, edit);
406-
} catch (final IOException e) {
407-
throw new UncheckedIOException(e);
408-
}
416+
pullRequest = pullRequestService.editPullRequest(base, edit);
409417
}
410418

411419
@Whitelisted
412420
public void setMaintainerCanModify(final boolean value) {
413421
ExtendedPullRequest edit = new ExtendedPullRequest();
414422
edit.setNumber(pullRequest.getNumber());
415423
edit.setMaintainerCanModify(value);
416-
try {
417-
pullRequest = pullRequestService.editPullRequest(base, edit);
418-
} catch (final IOException e) {
419-
throw new UncheckedIOException(e);
420-
}
424+
pullRequest = pullRequestService.editPullRequest(base, edit);
421425
}
422426

423427
@Whitelisted

src/main/java/org/jenkinsci/plugins/pipeline/github/client/ExtendedGitHubClient.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.jenkinsci.plugins.pipeline.github.client;
22

33
import org.eclipse.egit.github.core.client.GitHubClient;
4+
import org.eclipse.egit.github.core.client.GitHubRequest;
5+
import org.eclipse.egit.github.core.client.GitHubResponse;
46
import org.eclipse.egit.github.core.client.RequestException;
57

68
import java.io.IOException;
9+
import java.io.UncheckedIOException;
710
import java.lang.reflect.Type;
811
import java.net.HttpURLConnection;
912

@@ -23,16 +26,20 @@ public ExtendedGitHubClient(final String hostname, final int port, final String
2326
super(hostname, port, scheme);
2427
}
2528

26-
public <V> V patch(final String uri, final Object params, final Type type) throws IOException {
29+
public <V> V patch(final String uri, final Object params, final Type type) {
2730
return patch(uri, params, type, null);
2831
}
2932

30-
public <V> V patch(final String uri, final Object params, final Type type, final String accept) throws IOException {
31-
HttpURLConnection request = this.createPatch(uri);
32-
if (accept != null) {
33-
request.setRequestProperty("Accept", accept);
33+
public <V> V patch(final String uri, final Object params, final Type type, final String accept) {
34+
try {
35+
final HttpURLConnection request = createPatch(uri);
36+
if (accept != null) {
37+
request.setRequestProperty("Accept", accept);
38+
}
39+
return this.sendJson(request, params, type);
40+
} catch (final IOException e) {
41+
throw new UncheckedIOException(e);
3442
}
35-
return this.sendJson(request, params, type);
3643
}
3744

3845
protected HttpURLConnection createPatch(final String uri) throws IOException {
@@ -84,4 +91,14 @@ private <V> V sendJson(final HttpURLConnection request, final Object params, fin
8491
throw this.createException(this.getStream(request), code, request.getResponseMessage());
8592
}
8693
}
94+
95+
// UncheckedIOException version of get(GitHubRequest)
96+
public GitHubResponse getUnchecked(final GitHubRequest request) {
97+
try {
98+
return get(request);
99+
} catch (final IOException e) {
100+
throw new UncheckedIOException(e);
101+
}
102+
}
103+
87104
}

src/main/java/org/jenkinsci/plugins/pipeline/github/client/ExtendedIssueService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ public void removeAssignees(final IRepositoryIdProvider repository,
7575
getClient().delete(uri.toString(), params);
7676
}
7777

78+
public Issue setMilestone(final IRepositoryIdProvider repository,
79+
final int issueNumber,
80+
final Integer milestoneNumber) {
81+
String repoId = this.getId(repository);
82+
StringBuilder uri = new StringBuilder("/repos");
83+
uri.append('/').append(repoId);
84+
uri.append("/issues");
85+
uri.append('/').append(issueNumber);
86+
Map<Object, Object> params = new HashMap<>(1, 1.0F);
87+
params.put("milestone", milestoneNumber);
88+
return getClient().patch(uri.toString(), params, Issue.class);
89+
}
90+
7891
public void setAssignees(final IRepositoryIdProvider repository,
7992
final int issueNumber,
8093
final List<String> assignees) throws IOException {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jenkinsci.plugins.pipeline.github.client;
2+
3+
import org.eclipse.egit.github.core.Milestone;
4+
5+
import java.util.Date;
6+
7+
/**
8+
* @author Aaron Whiteside
9+
*/
10+
public class ExtendedMilestone extends Milestone {
11+
private static final long serialVersionUID = 8017385076255266092L;
12+
13+
private Date updatedAt;
14+
private Date closedAt;
15+
16+
public Date getUpdatedAt() {
17+
return updatedAt;
18+
}
19+
20+
public void setUpdatedAt(Date updatedAt) {
21+
this.updatedAt = updatedAt;
22+
}
23+
24+
public Date getClosedAt() {
25+
return closedAt;
26+
}
27+
28+
public void setClosedAt(Date closedAt) {
29+
this.closedAt = closedAt;
30+
}
31+
}

0 commit comments

Comments
 (0)