Skip to content

Commit 9f2a6a0

Browse files
committed
Support add_labels / remove_labels in update Merge Request
1 parent 6478b39 commit 9f2a6a0

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

gitlab4j-models/src/main/java/org/gitlab4j/api/models/MergeRequestParams.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class MergeRequestParams implements Serializable {
2121
private List<Long> reviewerIds;
2222
private Long milestoneId;
2323
private List<String> labels;
24+
private List<String> addLabels;
25+
private List<String> removeLabels;
2426
private String description;
2527
private Long targetProjectId;
2628
private StateEvent stateEvent;
@@ -132,6 +134,56 @@ public MergeRequestParams withLabels(String[] labels) {
132134
return (this);
133135
}
134136

137+
/**
138+
* Add labels to the merge request (without affecting existing labels).
139+
* If a label does not already exist, this creates a new project label and assigns it to the merge request.
140+
* This is for merge request updates only.
141+
*
142+
* @param addLabels the List of labels to add
143+
* @return the reference to this MergeRequestParams instance
144+
*/
145+
public MergeRequestParams withAddLabels(List<String> addLabels) {
146+
this.addLabels = addLabels;
147+
return (this);
148+
}
149+
150+
/**
151+
* Add labels to the merge request (without affecting existing labels).
152+
* If a label does not already exist, this creates a new project label and assigns it to the merge request.
153+
* This is for merge request updates only.
154+
*
155+
* @param addLabels the array of labels to add
156+
* @return the reference to this MergeRequestParams instance
157+
*/
158+
public MergeRequestParams withAddLabels(String[] addLabels) {
159+
this.addLabels = (addLabels != null ? Arrays.asList(addLabels) : null);
160+
return (this);
161+
}
162+
163+
/**
164+
* Remove labels from the merge request (without affecting other labels).
165+
* This is for merge request updates only.
166+
*
167+
* @param removeLabels the List of labels to remove
168+
* @return the reference to this MergeRequestParams instance
169+
*/
170+
public MergeRequestParams withRemoveLabels(List<String> removeLabels) {
171+
this.removeLabels = removeLabels;
172+
return (this);
173+
}
174+
175+
/**
176+
* Remove labels from the merge request (without affecting other labels).
177+
* This is for merge request updates only.
178+
*
179+
* @param removeLabels the array of labels to remove
180+
* @return the reference to this MergeRequestParams instance
181+
*/
182+
public MergeRequestParams withRemoveLabels(String[] removeLabels) {
183+
this.removeLabels = (removeLabels != null ? Arrays.asList(removeLabels) : null);
184+
return (this);
185+
}
186+
135187
/**
136188
* Set the description of the merge request. Limited to 1,048,576 characters.
137189
*
@@ -266,7 +318,10 @@ public GitLabForm getForm(boolean isCreate) {
266318
.withParam("target_project_id", targetProjectId)
267319
.withParam("approvals_before_merge", approvalsBeforeMerge);
268320
} else {
269-
form.withParam("state_event", stateEvent).withParam("discussion_locked", discussionLocked);
321+
form.withParam("state_event", stateEvent)
322+
.withParam("discussion_locked", discussionLocked)
323+
.withParam("add_labels", (addLabels != null ? String.join(",", addLabels) : null))
324+
.withParam("remove_labels", (removeLabels != null ? String.join(",", removeLabels) : null));
270325
}
271326

272327
return (form);

gitlab4j-models/src/test/java/org/gitlab4j/api/models/TestMergeRequestParams.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.junit.jupiter.api.Assertions.assertNotNull;
55
import static org.junit.jupiter.api.Assertions.assertNull;
66

7+
import java.util.Arrays;
8+
79
import org.gitlab4j.models.GitLabForm;
810
import org.gitlab4j.models.GitLabFormValue;
911
import org.junit.jupiter.api.Test;
@@ -58,4 +60,32 @@ public void testNoDraftWithNullTitle() {
5860
assertNotNull(titleFormValue);
5961
assertNull(titleFormValue.getValue());
6062
}
63+
64+
@Test
65+
public void testAddLabels() {
66+
MergeRequestParams params = new MergeRequestParams().withAddLabels(Arrays.asList("bug", "urgent"));
67+
68+
GitLabForm form = params.getForm(false);
69+
assertEquals("bug,urgent", form.getFormValues().get("add_labels").getValue());
70+
}
71+
72+
@Test
73+
public void testRemoveLabels() {
74+
MergeRequestParams params = new MergeRequestParams().withRemoveLabels(new String[] {"wontfix"});
75+
76+
GitLabForm form = params.getForm(false);
77+
assertEquals("wontfix", form.getFormValues().get("remove_labels").getValue());
78+
}
79+
80+
@Test
81+
public void testNoAddRemoveLabelsOnCreate() {
82+
MergeRequestParams params = new MergeRequestParams()
83+
.withTitle("T")
84+
.withAddLabels(Arrays.asList("bug"))
85+
.withRemoveLabels(Arrays.asList("wontfix"));
86+
87+
GitLabForm form = params.getForm(true);
88+
assertNull(form.getFormValues().get("add_labels"));
89+
assertNull(form.getFormValues().get("remove_labels"));
90+
}
6191
}

0 commit comments

Comments
 (0)