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; package org.gitlab4j.api;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
...@@ -8,6 +9,12 @@ import javax.ws.rs.core.Response; ...@@ -8,6 +9,12 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Label; import org.gitlab4j.api.models.Label;
/**
* This class provides an entry point to all the GitLab API project and group label calls.
*
* @see <a href="https://docs.gitlab.com/ce/api/labels.html">Labels API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ce/api/group_labels.html">Group Labels API at GitLab</a>
*/
public class LabelsApi extends AbstractApi { public class LabelsApi extends AbstractApi {
public LabelsApi(GitLabApi gitLabApi) { public LabelsApi(GitLabApi gitLabApi) {
...@@ -21,6 +28,321 @@ public class LabelsApi extends AbstractApi { ...@@ -21,6 +28,321 @@ public class LabelsApi extends AbstractApi {
* @return a list of project's labels * @return a list of project's labels
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public List<Label> getProjectLabels(Object projectIdOrPath) throws GitLabApiException {
return (getProjectLabels(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a Pager of all labels of the specified project.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param itemsPerPage the number of items per page
* @return a list of project's labels in the specified range
* @throws GitLabApiException if any exception occurs
*/
public Pager<Label> getProjectLabels(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Label>(this, Label.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "labels"));
}
/**
* Get a Stream of all labels of the specified project.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a Stream of project's labels
* @throws GitLabApiException if any exception occurs
*/
public Stream<Label> getProjectLabelsStream(Object projectIdOrPath) throws GitLabApiException {
return (getProjectLabels(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Get a single project label.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return a Label instance holding the information for the group label
* @throws GitLabApiException if any exception occurs
*/
public Label getProjectLabel(Object projectIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = get(Response.Status.OK, null,
"projects", getProjectIdOrPath(projectIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
return (response.readEntity(Label.class));
}
/**
* Get a single project label as the value of an Optional.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return a Optional instance with a Label instance as its value
* @throws GitLabApiException if any exception occurs
*/
public Optional<Label> getOptionalProjectLabel(Object projectIdOrPath, Object labelIdOrName) throws GitLabApiException {
try {
return (Optional.ofNullable(getProjectLabel(projectIdOrPath, labelIdOrName)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a project label. A Label instance is used to set the label properties.
* withXXX() methods are provided to set the properties of the label to create:
* <pre><code>
* // name and color properties are required
* Label labelProperties = new Label()
* .withName("a-pink-project-label")
* .withColor("pink")
* .withDescription("A new pink project label")
* .withPriority(10);
* gitLabApi.getLabelsApi().createProjectLabel(projectId, labelProperties);
* </code></pre>
*
* <pre><code>GitLab Endpoint: POST /groups/:id/labels</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelProperties a Label instance holding the properties for the new group label
* @return the created Label instance
* @throws GitLabApiException if any exception occurs
*/
public Label createProjectLabel(Object projectIdOrPath, Label labelProperties) throws GitLabApiException {
GitLabApiForm formData = labelProperties.getForm(true);
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "labels");
return (response.readEntity(Label.class));
}
/**
* Update the specified project label. The name, color, and description can be updated.
* A Label instance is used to set the properties of the label to update,
* withXXX() methods are provided to set the properties to update:
* <pre><code>
* Label labelUpdates = new Label()
* .withName("a-new-name")
* .withColor("red")
* .withDescription("A red group label");
* gitLabApi.getLabelsApi().updateGroupLabel(projectId, labelId, labelUpdates);
* </code></pre>
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/labels/:label_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @param labelConfig a Label instance holding the label properties to update
* @return the updated Label instance
* @throws GitLabApiException if any exception occurs
*/
public Label updateProjectLabel(Object projectIdOrPath, Object labelIdOrName, Label labelConfig) throws GitLabApiException {
GitLabApiForm formData = labelConfig.getForm(false);
Response response = putWithFormData(Response.Status.OK, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
return (response.readEntity(Label.class));
}
/**
* Delete the specified project label.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @throws GitLabApiException if any exception occurs
*/
public void deleteProjectLabel(Object projectIdOrPath, Object labelIdOrName) throws GitLabApiException {
delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
}
/**
* Subscribe a specified project label.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs
*/
public Label subscribeProjectLabel(Object projectIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(),
"projects", getProjectIdOrPath(projectIdOrPath), "labels", getLabelIdOrName(labelIdOrName), "subscribe");
return (response.readEntity(Label.class));
}
/**
* Unsubscribe a specified project label.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs
*/
public Label unsubscribeProjectLabel(Object projectIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(),
"projects", getProjectIdOrPath(projectIdOrPath), "labels", getLabelIdOrName(labelIdOrName), "unsubscribe");
return (response.readEntity(Label.class));
}
/**
* Get all labels of the specified group.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @return a list of group's labels
* @throws org.gitlab4j.api.GitLabApiException if any exception occurs
*/
public List<Label> getGroupLabels(Object groupIdOrPath) throws GitLabApiException {
return (getGroupLabels(groupIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a Pager of all labels of the specified group.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param itemsPerPage the number of items per page
* @return a list of group's labels in the specified range
* @throws GitLabApiException if any exception occurs
*/
public Pager<Label> getGroupLabels(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Label>(this, Label.class, itemsPerPage, null,
"groups", getGroupIdOrPath(groupIdOrPath), "labels"));
}
/**
* Get a Stream of all labels of the specified group.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @return a Stream of group's labels
* @throws GitLabApiException if any exception occurs
*/
public Stream<Label> getGroupLabelsStream(Object groupIdOrPath) throws GitLabApiException {
return (getGroupLabels(groupIdOrPath, getDefaultPerPage()).stream());
}
/**
* Get a single group label.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return a Label instance holding the information for the group label
* @throws GitLabApiException if any exception occurs
*/
public Label getGroupLabel(Object groupIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = get(Response.Status.OK, null,
"groups", getGroupIdOrPath(groupIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
return (response.readEntity(Label.class));
}
/**
* Get a single group label as the value of an Optional.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return a Optional instance with a Label instance as its value
* @throws GitLabApiException if any exception occurs
*/
public Optional<Label> getOptionalGroupLabel(Object groupIdOrPath, Object labelIdOrName) throws GitLabApiException {
try {
return (Optional.ofNullable(getGroupLabel(groupIdOrPath, labelIdOrName)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a group label. A Label instance is used to set the label properties.
* withXXX() methods are provided to set the properties of the label to create:
* <pre><code>
* Label labelProperties = new Label()
* .withName("a-name")
* .withColor("green")
* .withDescription("A new green group label");
* gitLabApi.getLabelsApi().createGroupLabel(projectId, labelProperties);
* </code></pre>
*
* <pre><code>GitLab Endpoint: POST /groups/:id/labels</code></pre>
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelProperties a Label instance holding the properties for the new group label
* @return the created Label instance
* @throws GitLabApiException if any exception occurs
*/
public Label createGroupLabel(Object groupIdOrPath, Label labelProperties) throws GitLabApiException {
GitLabApiForm formData = labelProperties.getForm(true);
Response response = post(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "labels");
return (response.readEntity(Label.class));
}
/**
* Update the specified label. The name, color, and description can be updated.
* A Label instance is used to set the properties of the label to update,
* withXXX() methods are provided to set the properties to update:
* <pre><code>
* Label labelUpdates = new Label()
* .withName("a-new-name")
* .withColor("red")
* .withDescription("A red group label");
* gitLabApi.getLabelsApi().updateGroupLabel(projectId, labelId, labelUpdates);
* </code></pre>
*
* <pre><code>GitLab Endpoint: PUT /groups/:id/labels/:label_id</code></pre>
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @param labelConfig a Label instance holding the label properties to update
* @return the updated Label instance
* @throws GitLabApiException if any exception occurs
*/
public Label updateGroupLabel(Object groupIdOrPath, Object labelIdOrName, Label labelConfig) throws GitLabApiException {
GitLabApiForm formData = labelConfig.getForm(false);
Response response = putWithFormData(Response.Status.OK, formData,
"groups", getGroupIdOrPath(groupIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
return (response.readEntity(Label.class));
}
/**
* Delete the specified label
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @throws GitLabApiException if any exception occurs
*/
public void deleteGroupLabel(Object groupIdOrPath, Object labelIdOrName) throws GitLabApiException {
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "labels", getLabelIdOrName(labelIdOrName));
}
/**
* Subscribe a specified group label.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs
*/
public Label subscribeGroupLabel(Object groupIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(),
"groups", getGroupIdOrPath(groupIdOrPath), "labels", getLabelIdOrName(labelIdOrName), "subscribe");
return (response.readEntity(Label.class));
}
/**
* Unsubscribe a specified group label.
*
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
* @param labelIdOrName the label in the form of an Integer(ID), String(name), or Label instance
* @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs
*/
public Label unsubscribeGroupLabel(Object groupIdOrPath, Object labelIdOrName) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(),
"groups", getGroupIdOrPath(groupIdOrPath), "labels", getLabelIdOrName(labelIdOrName), "unsubscribe");
return (response.readEntity(Label.class));
}
/**
* Get all labels of the specified project.
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a list of project's labels
* @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #getProjectLabels(Object)} method.
*/
@Deprecated
public List<Label> getLabels(Object projectIdOrPath) throws GitLabApiException { public List<Label> getLabels(Object projectIdOrPath) throws GitLabApiException {
return (getLabels(projectIdOrPath, getDefaultPerPage()).all()); return (getLabels(projectIdOrPath, getDefaultPerPage()).all());
} }
...@@ -33,7 +355,9 @@ public class LabelsApi extends AbstractApi { ...@@ -33,7 +355,9 @@ public class LabelsApi extends AbstractApi {
* @param perPage the number of items per page * @param perPage the number of items per page
* @return a list of project's labels in the specified range * @return a list of project's labels in the specified range
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in the next major release (5.0.0)
*/ */
@Deprecated
public List<Label> getLabels(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { public List<Label> getLabels(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
Response response = get(javax.ws.rs.core.Response.Status.OK, getPageQueryParams(page, perPage), Response response = get(javax.ws.rs.core.Response.Status.OK, getPageQueryParams(page, perPage),
"projects", getProjectIdOrPath(projectIdOrPath), "labels"); "projects", getProjectIdOrPath(projectIdOrPath), "labels");
...@@ -47,7 +371,9 @@ public class LabelsApi extends AbstractApi { ...@@ -47,7 +371,9 @@ public class LabelsApi extends AbstractApi {
* @param itemsPerPage the number of items per page * @param itemsPerPage the number of items per page
* @return a list of project's labels in the specified range * @return a list of project's labels in the specified range
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #getProjectLabels(Object, int)} method.
*/ */
@Deprecated
public Pager<Label> getLabels(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { public Pager<Label> getLabels(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Label>(this, Label.class, itemsPerPage, null, return (new Pager<Label>(this, Label.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "labels")); "projects", getProjectIdOrPath(projectIdOrPath), "labels"));
...@@ -59,7 +385,9 @@ public class LabelsApi extends AbstractApi { ...@@ -59,7 +385,9 @@ public class LabelsApi extends AbstractApi {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a Stream of project's labels * @return a Stream of project's labels
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #getProjectLabelsStream(Object)} method.
*/ */
@Deprecated
public Stream<Label> getLabelsStream(Object projectIdOrPath) throws GitLabApiException { public Stream<Label> getLabelsStream(Object projectIdOrPath) throws GitLabApiException {
return (getLabels(projectIdOrPath, getDefaultPerPage()).stream()); return (getLabels(projectIdOrPath, getDefaultPerPage()).stream());
} }
...@@ -73,7 +401,9 @@ public class LabelsApi extends AbstractApi { ...@@ -73,7 +401,9 @@ public class LabelsApi extends AbstractApi {
* @param description the description for the label * @param description the description for the label
* @return the created Label instance * @return the created Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #createProjectLabel(Object, Label)} method.
*/ */
@Deprecated
public Label createLabel(Object projectIdOrPath, String name, String color, String description) throws GitLabApiException { public Label createLabel(Object projectIdOrPath, String name, String color, String description) throws GitLabApiException {
return (createLabel(projectIdOrPath, name, color, description, null)); return (createLabel(projectIdOrPath, name, color, description, null));
} }
...@@ -86,7 +416,9 @@ public class LabelsApi extends AbstractApi { ...@@ -86,7 +416,9 @@ public class LabelsApi extends AbstractApi {
* @param color the color for the label * @param color the color for the label
* @return the created Label instance * @return the created Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #createProjectLabel(Object, Label)} method.
*/ */
@Deprecated
public Label createLabel(Object projectIdOrPath, String name, String color) throws GitLabApiException { public Label createLabel(Object projectIdOrPath, String name, String color) throws GitLabApiException {
return (createLabel(projectIdOrPath, name, color, null, null)); return (createLabel(projectIdOrPath, name, color, null, null));
} }
...@@ -100,7 +432,9 @@ public class LabelsApi extends AbstractApi { ...@@ -100,7 +432,9 @@ public class LabelsApi extends AbstractApi {
* @param priority the priority for the label * @param priority the priority for the label
* @return the created Label instance * @return the created Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #createProjectLabel(Object, Label)} method.
*/ */
@Deprecated
public Label createLabel(Object projectIdOrPath, String name, String color, Integer priority) throws GitLabApiException { public Label createLabel(Object projectIdOrPath, String name, String color, Integer priority) throws GitLabApiException {
return (createLabel(projectIdOrPath, name, color, null, priority)); return (createLabel(projectIdOrPath, name, color, null, priority));
} }
...@@ -115,19 +449,18 @@ public class LabelsApi extends AbstractApi { ...@@ -115,19 +449,18 @@ public class LabelsApi extends AbstractApi {
* @param priority the priority for the label * @param priority the priority for the label
* @return the created Label instance * @return the created Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #createProjectLabel(Object, Label)} method.
*/ */
@Deprecated
public Label createLabel(Object projectIdOrPath, String name, String color, String description, Integer priority) throws GitLabApiException { public Label createLabel(Object projectIdOrPath, String name, String color, String description, Integer priority) throws GitLabApiException {
Label labelProperties = new Label()
GitLabApiForm formData = new GitLabApiForm() .withName(name)
.withParam("name", name, true) .withColor(color)
.withParam("color", color, true) .withDescription(description)
.withParam("description", description) .withPriority(priority);
.withParam("priority", priority); return (createProjectLabel(projectIdOrPath, labelProperties));
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "labels");
return (response.readEntity(Label.class));
} }
/** /**
* Update the specified label * Update the specified label
* *
...@@ -138,12 +471,13 @@ public class LabelsApi extends AbstractApi { ...@@ -138,12 +471,13 @@ public class LabelsApi extends AbstractApi {
* @param priority the priority for the label * @param priority the priority for the label
* @return the modified Label instance * @return the modified Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated @deprecated Replaced by the {@link #updateProjectLabel(Object, Object, Label)} method.
*/ */
@Deprecated
public Label updateLabelName(Object projectIdOrPath, String name, String newName, String description, Integer priority) throws GitLabApiException { public Label updateLabelName(Object projectIdOrPath, String name, String newName, String description, Integer priority) throws GitLabApiException {
return (updateLabel(projectIdOrPath, name, newName, null, description, priority)); return (updateLabel(projectIdOrPath, name, newName, null, description, priority));
} }
/** /**
* Update the specified label * Update the specified label
* *
...@@ -154,7 +488,9 @@ public class LabelsApi extends AbstractApi { ...@@ -154,7 +488,9 @@ public class LabelsApi extends AbstractApi {
* @param priority the priority for the label * @param priority the priority for the label
* @return the modified Label instance * @return the modified Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated @deprecated Replaced by the {@link #updateProjectLabel(Object, Object, Label)} method.
*/ */
@Deprecated
public Label updateLabelColor(Object projectIdOrPath, String name, String color, String description, Integer priority) throws GitLabApiException { public Label updateLabelColor(Object projectIdOrPath, String name, String color, String description, Integer priority) throws GitLabApiException {
return (updateLabel(projectIdOrPath, name, null, color, description, priority)); return (updateLabel(projectIdOrPath, name, null, color, description, priority));
} }
...@@ -170,9 +506,10 @@ public class LabelsApi extends AbstractApi { ...@@ -170,9 +506,10 @@ public class LabelsApi extends AbstractApi {
* @param priority the priority for the label * @param priority the priority for the label
* @return the modified Label instance * @return the modified Label instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated @deprecated Replaced by the {@link #updateProjectLabel(Object, Object, Label)} method.
*/ */
@Deprecated
public Label updateLabel(Object projectIdOrPath, String name, String newName, String color, String description, Integer priority) throws GitLabApiException { public Label updateLabel(Object projectIdOrPath, String name, String newName, String color, String description, Integer priority) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() GitLabApiForm formData = new GitLabApiForm()
.withParam("name", name, true) .withParam("name", name, true)
.withParam("new_name", newName) .withParam("new_name", newName)
...@@ -190,12 +527,12 @@ public class LabelsApi extends AbstractApi { ...@@ -190,12 +527,12 @@ public class LabelsApi extends AbstractApi {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param name the name for the label * @param name the name for the label
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #deleteProjectLabel(Object, Object)} method.
*/ */
@Deprecated
public void deleteLabel(Object projectIdOrPath, String name) throws GitLabApiException { public void deleteLabel(Object projectIdOrPath, String name) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("name", name, true); GitLabApiForm formData = new GitLabApiForm().withParam("name", name, true);
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "labels");
delete(expectedStatus, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "labels");
} }
/** /**
...@@ -205,11 +542,11 @@ public class LabelsApi extends AbstractApi { ...@@ -205,11 +542,11 @@ public class LabelsApi extends AbstractApi {
* @param labelId the label ID * @param labelId the label ID
* @return HttpStatusCode 503 * @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #subscribeProjectLabel(Object, Object)} method.
*/ */
@Deprecated
public Label subscribeLabel(Object projectIdOrPath, Integer labelId) throws GitLabApiException { public Label subscribeLabel(Object projectIdOrPath, Integer labelId) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), return (subscribeProjectLabel(projectIdOrPath, labelId));
"projects", getProjectIdOrPath(projectIdOrPath), "labels", labelId, "subscribe");
return (response.readEntity(Label.class));
} }
/** /**
...@@ -219,10 +556,10 @@ public class LabelsApi extends AbstractApi { ...@@ -219,10 +556,10 @@ public class LabelsApi extends AbstractApi {
* @param labelId the label ID * @param labelId the label ID
* @return HttpStatusCode 503 * @return HttpStatusCode 503
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @deprecated Replaced by the {@link #unsubscribeProjectLabel(Object, Object)} method.
*/ */
@Deprecated
public Label unsubscribeLabel(Object projectIdOrPath, Integer labelId) throws GitLabApiException { public Label unsubscribeLabel(Object projectIdOrPath, Integer labelId) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), return (unsubscribeProjectLabel(projectIdOrPath, labelId));
"projects", getProjectIdOrPath(projectIdOrPath), "labels", labelId, "unsubscribe");
return (response.readEntity(Label.class));
} }
} }
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