Commit f1f270c5 authored by Adam Snyder's avatar Adam Snyder Committed by Greg Messner
Browse files

Adds a new updateMergeRequest method with more attributes (#206)

parent f75f0da0
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
<jackson.version>2.9.3</jackson.version> <jackson.version>2.9.3</jackson.version>
<javaServlet.version>3.1.0</javaServlet.version> <javaServlet.version>3.1.0</javaServlet.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<hamcrest.version>1.3</hamcrest.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> </properties>
...@@ -244,6 +246,18 @@ ...@@ -244,6 +246,18 @@
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -329,21 +329,61 @@ public class MergeRequestApi extends AbstractApi { ...@@ -329,21 +329,61 @@ public class MergeRequestApi extends AbstractApi {
* @param stateEvent new state for the merge request, optional * @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional * @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional * @param milestoneId the ID of a milestone, optional
* @param removeSourceBranch Flag indicating if a merge request should remove the source
* branch when merging, optional
* @param squash Squash commits into a single commit when merging, optional
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
* @param allowCollaboration Allow commits from members who can merge to the target branch,
* optional
* @return the updated merge request * @return the updated merge request
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid,
String targetBranch, String title, Integer assigneeId, String description,
StateEvent stateEvent, String labels, Integer milestoneId, Boolean removeSourceBranch,
Boolean squash, Boolean discussionLocked, Boolean allowCollaboration)
throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("target_branch", targetBranch)
.withParam("title", title)
.withParam("assignee_id", assigneeId)
.withParam("description", description)
.withParam("state_event", stateEvent)
.withParam("labels", labels)
.withParam("milestone_id", milestoneId)
.withParam("remove_source_branch", removeSourceBranch)
.withParam("squash", squash)
.withParam("discussion_locked", discussionLocked)
.withParam("allow_collaboration", allowCollaboration);
return updateMergeRequest(projectId, mergeRequestIid, formData);
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* PUT /projects/:id/merge_requests/:merge_request_iid
*
* @param projectId the ID of a project
* @param mergeRequestIid the internal ID of the merge request to update
* @param targetBranch the target branch, optional
* @param title the title for the merge request
* @param assigneeId the Assignee user ID, optional
* @param description the description of the merge request, optional
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
*/
@Deprecated
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String targetBranch, public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String targetBranch,
String title, Integer assigneeId, String description, StateEvent stateEvent, String labels, String title, Integer assigneeId, String description, StateEvent stateEvent, String labels,
Integer milestoneId) throws GitLabApiException { Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
if (mergeRequestIid == null) {
throw new RuntimeException("mergeRequestIid cannot be null");
}
Form formData = new GitLabApiForm() Form formData = new GitLabApiForm()
.withParam("target_branch", targetBranch) .withParam("target_branch", targetBranch)
.withParam("title", title) .withParam("title", title)
...@@ -353,8 +393,7 @@ public class MergeRequestApi extends AbstractApi { ...@@ -353,8 +393,7 @@ public class MergeRequestApi extends AbstractApi {
.withParam("labels", labels) .withParam("labels", labels)
.withParam("milestone_id", milestoneId); .withParam("milestone_id", milestoneId);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid); return updateMergeRequest(projectId, mergeRequestIid, formData);
return (response.readEntity(MergeRequest.class));
} }
/** /**
...@@ -379,6 +418,19 @@ public class MergeRequestApi extends AbstractApi { ...@@ -379,6 +418,19 @@ public class MergeRequestApi extends AbstractApi {
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String sourceBranch, String targetBranch, String title, String description, public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String sourceBranch, String targetBranch, String title, String description,
Integer assigneeId) throws GitLabApiException { Integer assigneeId) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, false);
addFormParam(formData, "target_branch", targetBranch, false);
addFormParam(formData, "title", title, false);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
return updateMergeRequest(projectId, mergeRequestIid, formData);
}
protected MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid,
Form formData) throws GitLabApiException {
if (projectId == null) { if (projectId == null) {
throw new RuntimeException("projectId cannot be null"); throw new RuntimeException("projectId cannot be null");
} }
...@@ -387,14 +439,8 @@ public class MergeRequestApi extends AbstractApi { ...@@ -387,14 +439,8 @@ public class MergeRequestApi extends AbstractApi {
throw new RuntimeException("mergeRequestId cannot be null"); throw new RuntimeException("mergeRequestId cannot be null");
} }
Form formData = new Form(); Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId,
addFormParam(formData, "source_branch", sourceBranch, false); "merge_requests", mergeRequestIid);
addFormParam(formData, "target_branch", targetBranch, false);
addFormParam(formData, "title", title, false);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid);
return (response.readEntity(MergeRequest.class)); return (response.readEntity(MergeRequest.class));
} }
......
package org.gitlab4j.api;
import org.gitlab4j.api.models.MergeRequest;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.util.Collections;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class TestMergeRequestApi {
@Mock private GitLabApi gitLabApi;
@Mock private GitLabApiClient gitLabApiClient;
@Mock private Response response;
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
@Before
public void setUp() throws Exception {
initMocks(this);
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>anyVararg()))
.thenReturn(response);
when(response.getStatus()).thenReturn(200);
when(response.getEntity()).thenReturn(new MergeRequest());
}
@Test
public void whenAllArgumentsNull_thenNoAttributesSent() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, null);
assertEquals(0, attributeCaptor.getValue().size());
}
@Test
public void falseBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, false);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("false")));
}
@Test
public void trueBooleansAreSerializedCorrectly() throws Exception {
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
null, null, null, null, true);
assertThat(attributeCaptor.getValue(),
hasEntry("allow_collaboration", Collections.singletonList("true")));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment