Skip to content

Commit f1f270c

Browse files
armsnydergmessner
authored andcommitted
Adds a new updateMergeRequest method with more attributes (#206)
1 parent f75f0da commit f1f270c

File tree

3 files changed

+144
-18
lines changed

3 files changed

+144
-18
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
<jackson.version>2.9.3</jackson.version>
4545
<javaServlet.version>3.1.0</javaServlet.version>
4646
<junit.version>4.12</junit.version>
47+
<mockito.version>1.10.19</mockito.version>
48+
<hamcrest.version>1.3</hamcrest.version>
4749
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4850
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
4951
</properties>
@@ -244,6 +246,18 @@
244246
<version>${junit.version}</version>
245247
<scope>test</scope>
246248
</dependency>
249+
<dependency>
250+
<groupId>org.mockito</groupId>
251+
<artifactId>mockito-all</artifactId>
252+
<version>${mockito.version}</version>
253+
<scope>test</scope>
254+
</dependency>
255+
<dependency>
256+
<groupId>org.hamcrest</groupId>
257+
<artifactId>hamcrest-all</artifactId>
258+
<version>${hamcrest.version}</version>
259+
<scope>test</scope>
260+
</dependency>
247261
</dependencies>
248262

249263
</project>

src/main/java/org/gitlab4j/api/MergeRequestApi.java

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,21 +329,61 @@ public MergeRequest createMergeRequest(Integer projectId, String sourceBranch, S
329329
* @param stateEvent new state for the merge request, optional
330330
* @param labels comma separated list of labels, optional
331331
* @param milestoneId the ID of a milestone, optional
332+
* @param removeSourceBranch Flag indicating if a merge request should remove the source
333+
* branch when merging, optional
334+
* @param squash Squash commits into a single commit when merging, optional
335+
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
336+
* @param allowCollaboration Allow commits from members who can merge to the target branch,
337+
* optional
332338
* @return the updated merge request
333339
* @throws GitLabApiException if any exception occurs
334340
*/
341+
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid,
342+
String targetBranch, String title, Integer assigneeId, String description,
343+
StateEvent stateEvent, String labels, Integer milestoneId, Boolean removeSourceBranch,
344+
Boolean squash, Boolean discussionLocked, Boolean allowCollaboration)
345+
throws GitLabApiException {
346+
347+
Form formData = new GitLabApiForm()
348+
.withParam("target_branch", targetBranch)
349+
.withParam("title", title)
350+
.withParam("assignee_id", assigneeId)
351+
.withParam("description", description)
352+
.withParam("state_event", stateEvent)
353+
.withParam("labels", labels)
354+
.withParam("milestone_id", milestoneId)
355+
.withParam("remove_source_branch", removeSourceBranch)
356+
.withParam("squash", squash)
357+
.withParam("discussion_locked", discussionLocked)
358+
.withParam("allow_collaboration", allowCollaboration);
359+
360+
return updateMergeRequest(projectId, mergeRequestIid, formData);
361+
}
362+
363+
/**
364+
* Updates an existing merge request. You can change branches, title, or even close the MR.
365+
*
366+
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
367+
*
368+
* PUT /projects/:id/merge_requests/:merge_request_iid
369+
*
370+
* @param projectId the ID of a project
371+
* @param mergeRequestIid the internal ID of the merge request to update
372+
* @param targetBranch the target branch, optional
373+
* @param title the title for the merge request
374+
* @param assigneeId the Assignee user ID, optional
375+
* @param description the description of the merge request, optional
376+
* @param stateEvent new state for the merge request, optional
377+
* @param labels comma separated list of labels, optional
378+
* @param milestoneId the ID of a milestone, optional
379+
* @return the updated merge request
380+
* @throws GitLabApiException if any exception occurs
381+
*/
382+
@Deprecated
335383
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String targetBranch,
336384
String title, Integer assigneeId, String description, StateEvent stateEvent, String labels,
337385
Integer milestoneId) throws GitLabApiException {
338386

339-
if (projectId == null) {
340-
throw new RuntimeException("projectId cannot be null");
341-
}
342-
343-
if (mergeRequestIid == null) {
344-
throw new RuntimeException("mergeRequestIid cannot be null");
345-
}
346-
347387
Form formData = new GitLabApiForm()
348388
.withParam("target_branch", targetBranch)
349389
.withParam("title", title)
@@ -353,8 +393,7 @@ public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIi
353393
.withParam("labels", labels)
354394
.withParam("milestone_id", milestoneId);
355395

356-
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid);
357-
return (response.readEntity(MergeRequest.class));
396+
return updateMergeRequest(projectId, mergeRequestIid, formData);
358397
}
359398

360399
/**
@@ -379,6 +418,19 @@ public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIi
379418
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid, String sourceBranch, String targetBranch, String title, String description,
380419
Integer assigneeId) throws GitLabApiException {
381420

421+
Form formData = new Form();
422+
addFormParam(formData, "source_branch", sourceBranch, false);
423+
addFormParam(formData, "target_branch", targetBranch, false);
424+
addFormParam(formData, "title", title, false);
425+
addFormParam(formData, "description", description, false);
426+
addFormParam(formData, "assignee_id", assigneeId, false);
427+
428+
return updateMergeRequest(projectId, mergeRequestIid, formData);
429+
}
430+
431+
protected MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIid,
432+
Form formData) throws GitLabApiException {
433+
382434
if (projectId == null) {
383435
throw new RuntimeException("projectId cannot be null");
384436
}
@@ -387,14 +439,8 @@ public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestIi
387439
throw new RuntimeException("mergeRequestId cannot be null");
388440
}
389441

390-
Form formData = new Form();
391-
addFormParam(formData, "source_branch", sourceBranch, false);
392-
addFormParam(formData, "target_branch", targetBranch, false);
393-
addFormParam(formData, "title", title, false);
394-
addFormParam(formData, "description", description, false);
395-
addFormParam(formData, "assignee_id", assigneeId, false);
396-
397-
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestIid);
442+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId,
443+
"merge_requests", mergeRequestIid);
398444
return (response.readEntity(MergeRequest.class));
399445
}
400446

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.MergeRequest;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockito.ArgumentCaptor;
7+
import org.mockito.Captor;
8+
import org.mockito.Mock;
9+
import org.mockito.Mockito;
10+
11+
import javax.ws.rs.core.MultivaluedMap;
12+
import javax.ws.rs.core.Response;
13+
import java.util.Collections;
14+
15+
import static org.hamcrest.Matchers.hasEntry;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertThat;
18+
import static org.mockito.Matchers.any;
19+
import static org.mockito.Mockito.when;
20+
import static org.mockito.MockitoAnnotations.initMocks;
21+
22+
public class TestMergeRequestApi {
23+
24+
@Mock private GitLabApi gitLabApi;
25+
@Mock private GitLabApiClient gitLabApiClient;
26+
@Mock private Response response;
27+
28+
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
29+
30+
@Before
31+
public void setUp() throws Exception {
32+
initMocks(this);
33+
34+
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
35+
36+
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
37+
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>anyVararg()))
38+
.thenReturn(response);
39+
40+
when(response.getStatus()).thenReturn(200);
41+
when(response.getEntity()).thenReturn(new MergeRequest());
42+
}
43+
44+
@Test
45+
public void whenAllArgumentsNull_thenNoAttributesSent() throws Exception {
46+
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
47+
null, null, null, null, null);
48+
assertEquals(0, attributeCaptor.getValue().size());
49+
}
50+
51+
@Test
52+
public void falseBooleansAreSerializedCorrectly() throws Exception {
53+
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
54+
null, null, null, null, false);
55+
assertThat(attributeCaptor.getValue(),
56+
hasEntry("allow_collaboration", Collections.singletonList("false")));
57+
}
58+
59+
@Test
60+
public void trueBooleansAreSerializedCorrectly() throws Exception {
61+
new MergeRequestApi(gitLabApi).updateMergeRequest(1, 2, null, null, null, null, null, null,
62+
null, null, null, null, true);
63+
assertThat(attributeCaptor.getValue(),
64+
hasEntry("allow_collaboration", Collections.singletonList("true")));
65+
}
66+
}

0 commit comments

Comments
 (0)