Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for getting and setting a PRs milestone #51

Merged
merged 4 commits into from
Jan 28, 2019
Merged
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ issueUrl | `String` | false
title | `String` | **true**
body | `String` | **true**
locked | `Boolean` | **true** | Accepts `true`, `false` or `'true'`, `'false'`
milestone | `Integer` | **true**
milestone | `Milestone` | **true** | Setter accepts int or Milestone class.
head | `String` | false | Revision (SHA) of the head commit of this pull request
headRef | `String` | false | Name of the branch this pull request is created for
base | `String` | **true** | Name of the base branch in the current repository this pull request targets
Expand Down Expand Up @@ -364,6 +364,27 @@ state | `String` | One of APPROVED, PENDING, CHANGES_REQUESTED, DISMISSED, COMME
### Methods
None.

## Milestone
### Properties
Name | Type | Setter | Description
-----|------|----------|------------
number | `Integer` | false
createdAt | `Date` | false
dueOn | `Date` | false
updatedAt | `Date` | false
closedAt | `Date` | false
closedIssues | `Integer` | false
openIssues | `Integer` | false
description | `String` | false
state | `String` | false
title | `String` | false
url | `String` | false
creator | `String` | false

### Methods
None.


# Examples

## Pull Requests
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<dependency>
<groupId>org.eclipse.mylyn.github</groupId>
<artifactId>org.eclipse.egit.github.core</artifactId>
<version>5.0.0.201806131550-r</version>
<version>5.2.0.201812061821-r</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.jenkinsci.plugins.pipeline.github;

import groovy.lang.GroovyObjectSupport;
import org.eclipse.egit.github.core.Milestone;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMilestone;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;

import java.io.Serializable;
import java.util.Date;

/**
* Groovy wrapper over {@link Milestone}
*
* @author Aaron Whiteside
* @see Milestone
*/
public class MilestoneGroovyObject extends GroovyObjectSupport implements Serializable {
private static final long serialVersionUID = 1L;

private final ExtendedMilestone milestone;

MilestoneGroovyObject(final ExtendedMilestone milestone) {
this.milestone = milestone;
}

@Whitelisted
public Date getCreatedAt() {
return milestone.getCreatedAt();
}

@Whitelisted
public Date getDueOn() {
return milestone.getDueOn();
}

@Whitelisted
public int getClosedIssues() {
return milestone.getClosedIssues();
}

@Whitelisted
public int getNumber() {
return milestone.getNumber();
}

@Whitelisted
public int getOpenIssues() {
return milestone.getOpenIssues();
}

@Whitelisted
public String getDescription() {
return milestone.getDescription();
}

@Whitelisted
public String getState() {
return milestone.getState();
}

@Whitelisted
public String getTitle() {
return milestone.getTitle();
}

@Whitelisted
public String getUrl() {
return milestone.getUrl();
}

@Whitelisted
public String getCreator() {
return milestone.getCreator().getLogin();
}

@Whitelisted
public Date getUpdatedAt() {
return milestone.getUpdatedAt();
}

@Whitelisted
public Date getClosedAt() {
return milestone.getClosedAt();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.CommitStatus;
import org.eclipse.egit.github.core.Label;
import org.eclipse.egit.github.core.Milestone;
import org.eclipse.egit.github.core.PullRequestMarker;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.User;
Expand All @@ -16,6 +17,7 @@
import org.jenkinsci.plugins.pipeline.github.client.ExtendedGitHubClient;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedIssueService;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMergeStatus;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedMilestoneService;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedPullRequest;
import org.jenkinsci.plugins.pipeline.github.client.ExtendedPullRequestService;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
Expand Down Expand Up @@ -61,6 +63,7 @@ public class PullRequestGroovyObject extends GroovyObjectSupport implements Seri
private final ExtendedPullRequestService pullRequestService;
private final ExtendedIssueService issueService;
private final ExtendedCommitService commitService;
private final ExtendedMilestoneService milestoneService;
private ExtendedPullRequest pullRequest;

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

Expand Down Expand Up @@ -133,8 +137,12 @@ public boolean isLocked() {
}

@Whitelisted
public int getMilestone() {
return pullRequest.getMilestone().getNumber();
public MilestoneGroovyObject getMilestone() {
return Optional.ofNullable(pullRequest.getMilestone())
.map(Milestone::getNumber)
.map(m -> milestoneService.getMilestone(base, m))
.map(MilestoneGroovyObject::new)
.orElse(null);
}

@Whitelisted
Expand Down Expand Up @@ -335,8 +343,24 @@ public Iterable<CommitFileGroovyObject> getFiles() {
}
}

@Whitelisted
public void setMilestone(final int milestoneNumber) {
// todo
pullRequest.setMilestone(
issueService.setMilestone(base, pullRequest.getNumber(), milestoneNumber)
.getMilestone());
}

@Whitelisted
public void setMilestone(final MilestoneGroovyObject milestone) {
if (milestone == null) {
// call setMilestone because the caller might not have the right permissions to remove
// the milestone and it'll return the current milestone.
pullRequest.setMilestone(
issueService.setMilestone(base, pullRequest.getNumber(), null)
.getMilestone());
} else {
setMilestone(milestone.getNumber());
}
}

@Whitelisted
Expand All @@ -359,11 +383,7 @@ public void setTitle(final String title) {
ExtendedPullRequest edit = new ExtendedPullRequest();
edit.setNumber(pullRequest.getNumber());
edit.setTitle(title);
try {
pullRequest = pullRequestService.editPullRequest(base, edit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.editPullRequest(base, edit);
}

@Whitelisted
Expand All @@ -373,11 +393,7 @@ public void setBody(final String body) {
ExtendedPullRequest edit = new ExtendedPullRequest();
edit.setNumber(pullRequest.getNumber());
edit.setBody(body);
try {
pullRequest = pullRequestService.editPullRequest(base, edit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.editPullRequest(base, edit);
}

@Whitelisted
Expand All @@ -387,11 +403,7 @@ public void setState(final String state) {
ExtendedPullRequest edit = new ExtendedPullRequest();
edit.setNumber(pullRequest.getNumber());
edit.setState(state);
try {
pullRequest = pullRequestService.editPullRequest(base, edit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.editPullRequest(base, edit);
}

@Whitelisted
Expand All @@ -401,23 +413,15 @@ public void setBase(final String newBase) {
ExtendedPullRequest edit = new ExtendedPullRequest();
edit.setNumber(pullRequest.getNumber());
edit.setBase(new PullRequestMarker().setRef(newBase));
try {
pullRequest = pullRequestService.editPullRequest(base, edit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.editPullRequest(base, edit);
}

@Whitelisted
public void setMaintainerCanModify(final boolean value) {
ExtendedPullRequest edit = new ExtendedPullRequest();
edit.setNumber(pullRequest.getNumber());
edit.setMaintainerCanModify(value);
try {
pullRequest = pullRequestService.editPullRequest(base, edit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.editPullRequest(base, edit);
}

@Whitelisted
Expand Down Expand Up @@ -684,11 +688,7 @@ public String merge(final String commitTitle,

@Whitelisted
public void refresh() {
try {
pullRequest = pullRequestService.getPullRequest(base, pullRequest.getNumber());
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
pullRequest = pullRequestService.getPullRequest(base, pullRequest.getNumber());
}

@Whitelisted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.jenkinsci.plugins.pipeline.github.client;

import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.GitHubResponse;
import org.eclipse.egit.github.core.client.RequestException;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;

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

public <V> V patch(final String uri, final Object params, final Type type) throws IOException {
public <V> V patch(final String uri, final Object params, final Type type) {
return patch(uri, params, type, null);
}

public <V> V patch(final String uri, final Object params, final Type type, final String accept) throws IOException {
HttpURLConnection request = this.createPatch(uri);
if (accept != null) {
request.setRequestProperty("Accept", accept);
public <V> V patch(final String uri, final Object params, final Type type, final String accept) {
try {
final HttpURLConnection request = createPatch(uri);
if (accept != null) {
request.setRequestProperty("Accept", accept);
}
return this.sendJson(request, params, type);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
return this.sendJson(request, params, type);
}

protected HttpURLConnection createPatch(final String uri) throws IOException {
Expand Down Expand Up @@ -84,4 +91,14 @@ private <V> V sendJson(final HttpURLConnection request, final Object params, fin
throw this.createException(this.getStream(request), code, request.getResponseMessage());
}
}

// UncheckedIOException version of get(GitHubRequest)
public GitHubResponse getUnchecked(final GitHubRequest request) {
try {
return get(request);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public void removeAssignees(final IRepositoryIdProvider repository,
getClient().delete(uri.toString(), params);
}

public Issue setMilestone(final IRepositoryIdProvider repository,
final int issueNumber,
final Integer milestoneNumber) {
String repoId = this.getId(repository);
StringBuilder uri = new StringBuilder("/repos");
uri.append('/').append(repoId);
uri.append("/issues");
uri.append('/').append(issueNumber);
Map<Object, Object> params = new HashMap<>(1, 1.0F);
params.put("milestone", milestoneNumber);
return getClient().patch(uri.toString(), params, Issue.class);
}

public void setAssignees(final IRepositoryIdProvider repository,
final int issueNumber,
final List<String> assignees) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jenkinsci.plugins.pipeline.github.client;

import org.eclipse.egit.github.core.Milestone;
import org.eclipse.egit.github.core.util.DateUtils;

import java.util.Date;

/**
* @author Aaron Whiteside
*/
public class ExtendedMilestone extends Milestone {
private static final long serialVersionUID = 8017385076255266092L;

private Date updatedAt;
private Date closedAt;

public Date getUpdatedAt() {
return DateUtils.clone(this.updatedAt);
}

public void setUpdatedAt(final Date updatedAt) {
this.updatedAt = DateUtils.clone(updatedAt);
}

public Date getClosedAt() {
return DateUtils.clone(this.closedAt);
}

public void setClosedAt(final Date closedAt) {
this.closedAt = DateUtils.clone(closedAt);
}
}
Loading