Unverified Commit b642cfe2 authored by Greg Messner's avatar Greg Messner Committed by GitHub
Browse files

Added group labels API support - #477 (#478)

parent 3ad03863
...@@ -11,6 +11,7 @@ import javax.ws.rs.core.StreamingOutput; ...@@ -11,6 +11,7 @@ import javax.ws.rs.core.StreamingOutput;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Label;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
import org.gitlab4j.api.utils.UrlEncoder; import org.gitlab4j.api.utils.UrlEncoder;
...@@ -57,7 +58,6 @@ public abstract class AbstractApi implements Constants { ...@@ -57,7 +58,6 @@ public abstract class AbstractApi implements Constants {
throw (new RuntimeException("Cannot determine ID or path from provided Project instance")); throw (new RuntimeException("Cannot determine ID or path from provided Project instance"));
} else { } else {
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() + throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
" instance, must be Integer, String, or a Project instance")); " instance, must be Integer, String, or a Project instance"));
} }
...@@ -93,7 +93,6 @@ public abstract class AbstractApi implements Constants { ...@@ -93,7 +93,6 @@ public abstract class AbstractApi implements Constants {
throw (new RuntimeException("Cannot determine ID or path from provided Group instance")); throw (new RuntimeException("Cannot determine ID or path from provided Group instance"));
} else { } else {
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() + throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
" instance, must be Integer, String, or a Group instance")); " instance, must be Integer, String, or a Group instance"));
} }
...@@ -129,12 +128,46 @@ public abstract class AbstractApi implements Constants { ...@@ -129,12 +128,46 @@ public abstract class AbstractApi implements Constants {
throw (new RuntimeException("Cannot determine ID or username from provided User instance")); throw (new RuntimeException("Cannot determine ID or username from provided User instance"));
} else { } else {
throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() + throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() +
" instance, must be Integer, String, or a User instance")); " instance, must be Integer, String, or a User instance"));
} }
} }
/**
* Returns the label ID or name from the provided Integer, String, or Label instance.
*
* @param obj the object to determine the ID or name from
* @return the user ID or name from the provided Integer, String, or Label instance
* @throws GitLabApiException if any exception occurs during execution
*/
public Object getLabelIdOrName(Object obj) throws GitLabApiException {
if (obj == null) {
throw (new RuntimeException("Cannot determine ID or name from null object"));
} else if (obj instanceof Integer) {
return (obj);
} else if (obj instanceof String) {
return (urlEncode(((String) obj).trim()));
} else if (obj instanceof Label) {
Integer id = ((Label) obj).getId();
if (id != null && id.intValue() > 0) {
return (id);
}
String name = ((User) obj).getName();
if (name != null && name.trim().length() > 0) {
return (urlEncode(name.trim()));
}
throw (new RuntimeException("Cannot determine ID or name from provided Label instance"));
} else {
throw (new RuntimeException("Cannot determine ID or name from provided " + obj.getClass().getSimpleName() +
" instance, must be Integer, String, or a Label instance"));
}
}
protected ApiVersion getApiVersion() { protected ApiVersion getApiVersion() {
return (gitLabApi.getApiVersion()); return (gitLabApi.getApiVersion());
} }
......
package org.gitlab4j.api.models; package org.gitlab4j.api.models;
import org.gitlab4j.api.GitLabApiForm;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Label { public class Label {
private Integer id; private Integer id;
...@@ -30,6 +33,11 @@ public class Label { ...@@ -30,6 +33,11 @@ public class Label {
this.name = name; this.name = name;
} }
public Label withName(String name) {
this.name = name;
return (this);
}
public String getColor() { public String getColor() {
return color; return color;
} }
...@@ -38,6 +46,11 @@ public class Label { ...@@ -38,6 +46,11 @@ public class Label {
this.color = color; this.color = color;
} }
public Label withColor(String color) {
this.color = color;
return (this);
}
public String getDescription() { public String getDescription() {
return description; return description;
} }
...@@ -46,6 +59,11 @@ public class Label { ...@@ -46,6 +59,11 @@ public class Label {
this.description = description; this.description = description;
} }
public Label withDescription(String description) {
this.description = description;
return (this);
}
public Integer getOpenIssuesCount() { public Integer getOpenIssuesCount() {
return openIssuesCount; return openIssuesCount;
} }
...@@ -86,8 +104,35 @@ public class Label { ...@@ -86,8 +104,35 @@ public class Label {
this.priority = priority; this.priority = priority;
} }
public Label withPriority(Integer priority) {
this.priority = priority;
return (this);
}
@Override @Override
public String toString() { public String toString() {
return (JacksonJson.toJsonString(this)); return (JacksonJson.toJsonString(this));
} }
/**
* Get the form params specified by this instance.
*
* @param isCreate set to true if the params are for a create label call, false for an update
* @return a GitLabApiForm instance holding the form parameters for this LabelParams instance
*/
@JsonIgnore
public GitLabApiForm getForm(boolean isCreate) {
GitLabApiForm form = new GitLabApiForm()
.withParam("description", description)
.withParam("color", color, isCreate)
.withParam("priority", priority);
if (isCreate) {
form.withParam("name", name, true);
} else {
form.withParam("new_name", name);
}
return (form);
}
} }
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Label;
import org.gitlab4j.api.models.Project;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class TestLabelsApi extends AbstractIntegrationTest {
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
private static final String TEST_PROJECT_LABEL = "test-project-label";
private static final String TEST_GROUP_LABEL = "test-group-label";
private static final String TEST_PROJECT_LABEL_1 = "test-project-label-1";
private static final String TEST_GROUP_LABEL_1 = "test-group-label-1";
private static GitLabApi gitLabApi;
private static Project testProject;
private static Group testGroup;
public TestLabelsApi() {
super();
}
@BeforeClass
public static void testSetup() {
// Must setup the connection to the GitLab test server and get the test Project instance
gitLabApi = baseTestSetup();
testProject = getTestProject();
String problems = "";
if (gitLabApi != null) {
Optional<Group> group = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
if (group.isPresent()) {
testGroup = group.get();
} else {
problems += "Problem fetching test group\n";
}
}
if (!problems.isEmpty()) {
System.err.print(problems);
}
deleteTestLabels();
}
@AfterClass
public static void tearDown() {
deleteTestLabels();
}
private static final void deleteTestLabels() {
if (testProject != null) {
try {
gitLabApi.getLabelsApi().deleteProjectLabel(testProject, TEST_PROJECT_LABEL);
} catch (Exception ignore) {}
try {
gitLabApi.getLabelsApi().deleteGroupLabel(testGroup, TEST_GROUP_LABEL);
} catch (Exception ignore) {}
try {
gitLabApi.getLabelsApi().deleteProjectLabel(testProject, TEST_PROJECT_LABEL_1);
} catch (Exception ignore) {}
try {
gitLabApi.getLabelsApi().deleteGroupLabel(testGroup, TEST_GROUP_LABEL_1);
} catch (Exception ignore) {}
}
}
@Before
public void beforeMethod() {
assumeTrue(testProject != null);
}
@Test
public void testCreateAndDeleteProjectLabel() throws GitLabApiException {
Label labelConfig = new Label().withName(TEST_PROJECT_LABEL).withColor("#FF0000");
Label testLabel = gitLabApi.getLabelsApi().createProjectLabel(testProject, labelConfig);
assertNotNull(testLabel);
assertEquals(TEST_PROJECT_LABEL, testLabel.getName());
Optional<Label> optionalLabel = gitLabApi.getLabelsApi().getOptionalProjectLabel(testProject, TEST_PROJECT_LABEL);
assertTrue(optionalLabel.isPresent());
assertEquals(TEST_PROJECT_LABEL, optionalLabel.get().getName());
assertEquals("#FF0000", optionalLabel.get().getColor());
Stream<Label> labelsStream = gitLabApi.getLabelsApi().getProjectLabelsStream(testProject);
assertTrue(labelsStream.map(Label::getName).anyMatch(s -> TEST_PROJECT_LABEL.equals(s)));
gitLabApi.getLabelsApi().deleteProjectLabel(testProject, testLabel.getId());
optionalLabel = gitLabApi.getLabelsApi().getOptionalProjectLabel(testProject, TEST_PROJECT_LABEL);
assertFalse(optionalLabel.isPresent());
List<Label> labels = gitLabApi.getLabelsApi().getProjectLabels(testProject);
assertFalse(labels.stream().map(Label::getName).anyMatch(s -> TEST_PROJECT_LABEL.equals(s)));
}
@Test
public void testCreateAndUpdateProjectLabel() throws GitLabApiException {
Label labelConfig = new Label().withName(TEST_PROJECT_LABEL_1).withColor("#FF0000");
Label testLabel = gitLabApi.getLabelsApi().createProjectLabel(testProject, labelConfig);
assertNotNull(testLabel);
assertEquals(TEST_PROJECT_LABEL_1, testLabel.getName());
assertEquals("#FF0000", testLabel.getColor());
try {
labelConfig = new Label().withName(TEST_PROJECT_LABEL_1).withColor("#000000");
gitLabApi.getLabelsApi().updateProjectLabel(testProject, testLabel, labelConfig);
Optional<Label> optionalLabel = gitLabApi.getLabelsApi().getOptionalProjectLabel(testProject, TEST_PROJECT_LABEL_1);
assertTrue(optionalLabel.isPresent());
assertEquals("#000000", optionalLabel.get().getColor());
} finally {
try {
gitLabApi.getLabelsApi().deleteProjectLabel(testProject, testLabel.getId());
} catch (Exception ignore) {
}
}
}
@Test
public void testCreateAndDeleteGroupLabel() throws GitLabApiException {
Label labelConfig = new Label().withName(TEST_GROUP_LABEL).withColor("#FF0000");
Label testLabel = gitLabApi.getLabelsApi().createGroupLabel(testGroup, labelConfig);
assertNotNull(testLabel);
assertEquals(TEST_GROUP_LABEL, testLabel.getName());
Optional<Label> optionalLabel = gitLabApi.getLabelsApi().getOptionalGroupLabel(testGroup, TEST_GROUP_LABEL);
assertTrue(optionalLabel.isPresent());
assertEquals(TEST_GROUP_LABEL, optionalLabel.get().getName());
assertEquals("#FF0000", optionalLabel.get().getColor());
Stream<Label> labelsStream = gitLabApi.getLabelsApi().getGroupLabelsStream(testGroup);
assertTrue(labelsStream.map(Label::getName).anyMatch(s -> TEST_GROUP_LABEL.equals(s)));
gitLabApi.getLabelsApi().deleteGroupLabel(testGroup, testLabel.getId());
optionalLabel = gitLabApi.getLabelsApi().getOptionalGroupLabel(testGroup, TEST_GROUP_LABEL);
assertFalse(optionalLabel.isPresent());
List<Label> labels = gitLabApi.getLabelsApi().getGroupLabels(testGroup);
assertFalse(labels.stream().map(Label::getName).anyMatch(s -> TEST_GROUP_LABEL.equals(s)));
}
@Test
public void testCreateAndUpdateGroupLabel() throws GitLabApiException {
Label labelConfig = new Label().withName(TEST_GROUP_LABEL_1).withColor("#FF0000");
Label testLabel = gitLabApi.getLabelsApi().createGroupLabel(testGroup, labelConfig);
assertNotNull(testLabel);
assertEquals(TEST_GROUP_LABEL_1, testLabel.getName());
assertEquals("#FF0000", testLabel.getColor());
try {
labelConfig = new Label().withName(TEST_GROUP_LABEL_1).withColor("#000000");
gitLabApi.getLabelsApi().updateGroupLabel(testGroup, testLabel, labelConfig);
Optional<Label> optionalLabel = gitLabApi.getLabelsApi().getOptionalGroupLabel(testGroup, TEST_GROUP_LABEL_1);
assertTrue(optionalLabel.isPresent());
assertEquals("#000000", optionalLabel.get().getColor());
} finally {
try {
gitLabApi.getLabelsApi().deleteGroupLabel(testGroup, testLabel.getId());
} catch (Exception ignore) {
}
}
}
}
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