Commit 0aa2b589 authored by Stel000's avatar Stel000 Committed by Greg Messner
Browse files

Add project milestones,issue notes and labels API. (#99)

* [Add]Add gitlab project milestone api.

* [Add]Check projectId not null.

* [Fix]Fix some bugs.

* [Add]Add gitlab issue notes api.

* [Add]Add gitlab labels api.

* [Imp]Add attr-required to Param.

* [Fix]Fix test error.
parent 3a9bd74a
......@@ -5,7 +5,7 @@
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<packaging>jar</packaging>
<version>4.6.10-SNAPSHOT</version>
<version>4.7.1-SNAPSHOT</version>
<name>GitLab API Java Client</name>
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API</description>
<url>http://www.messners.com/#gitlab4j-api/gitlab4j-api.html</url>
......
package org.gitlab4j.api;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
public interface Constants {
......@@ -214,6 +213,28 @@ public interface Constants {
}
}
public enum MilestoneState {
ACTIVE, CLOSED, ACTIVATE, CLOSE;
private static JacksonJsonEnumHelper<MilestoneState> enumHelper = new JacksonJsonEnumHelper<>(MilestoneState.class);
@JsonCreator
public static MilestoneState forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
/** Enum to use for specifying the event action_type. */
public enum ActionType {
......
package org.gitlab4j.api;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version;
import javax.ws.rs.core.Response;
import java.util.Map;
/**
* This class is provides a simplified interface to a GitLab API server, and divides the API up into
* a separate API class for each concern.
......@@ -35,6 +34,7 @@ public class GitLabApi {
private GroupApi groupApi;
private IssuesApi issuesApi;
private MergeRequestApi mergeRequestApi;
private MileStonesApi mileStonesApi;
private NamespaceApi namespaceApi;
private PipelineApi pipelineApi;
private ProjectApi projectApi;
......@@ -44,6 +44,7 @@ public class GitLabApi {
private SessionApi sessoinApi;
private UserApi userApi;
private JobApi jobApi;
private LabelsApi labelsApi;
private NotesApi notesApi;
private EventsApi eventsApi;
......@@ -321,7 +322,9 @@ public class GitLabApi {
groupApi = new GroupApi(this);
issuesApi = new IssuesApi(this);
jobApi = new JobApi(this);
labelsApi = new LabelsApi(this);
mergeRequestApi = new MergeRequestApi(this);
mileStonesApi = new MileStonesApi(this);
namespaceApi = new NamespaceApi(this);
notesApi = new NotesApi(this);
pipelineApi = new PipelineApi(this);
......@@ -530,6 +533,10 @@ public class GitLabApi {
return (jobApi);
}
public LabelsApi getLabelsApi() {
return labelsApi;
}
/**
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
* to perform all merge request related API calls.
......@@ -540,6 +547,10 @@ public class GitLabApi {
return (mergeRequestApi);
}
public MileStonesApi getMileStonesApi() {
return mileStonesApi;
}
/**
* Gets the NamespaceApi instance owned by this GitLabApi instance. The NamespaceApi is used
* to perform all namespace related API calls.
......
package org.gitlab4j.api;
import org.gitlab4j.api.models.Label;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
public class LabelsApi extends AbstractApi {
public LabelsApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
public List<Label> getLabels(Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(javax.ws.rs.core.Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "labels");
return (response.readEntity(new GenericType<List<Label>>() {
}));
}
public List<Label> getLabels(Integer projectId, int page, int perPage) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(javax.ws.rs.core.Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "labels");
return (response.readEntity(new GenericType<List<Label>>() {
}));
}
public Label createLabel(Integer projectId, String name, String color, String description) throws GitLabApiException {
return (createLabel(projectId, name, color, description, null));
}
public Label createLabel(Integer projectId, String name, String color) throws GitLabApiException {
return (createLabel(projectId, name, color, null, null));
}
public Label createLabel(Integer projectId, String name, String color, Integer priority) throws GitLabApiException {
return (createLabel(projectId, name, color, null, priority));
}
public Label createLabel(Integer projectId, String name, String color, String description, Integer priority) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("name", name, true)
.withParam("color", color, true)
.withParam("description", description)
.withParam("priority", priority);
Response response = post(Response.Status.OK, formData, "projects", projectId, "labels");
return (response.readEntity(Label.class));
}
public Label updateLabelName(Integer projectId, String name, String newName, String description, Integer priority) throws GitLabApiException {
return (updateLabel(projectId, name, newName, null, description, priority));
}
public Label updateLabelColor(Integer projectId, String name, String color, String description, Integer priority) throws GitLabApiException {
return (updateLabel(projectId, name, null, color, description, priority));
}
public Label updateLabel(Integer projectId, String name, String newName, String color, String description, Integer priority) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("name", name, true)
.withParam("new_name", newName)
.withParam("color", color)
.withParam("description", description)
.withParam("priority", priority);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "labels");
return (response.readEntity(Label.class));
}
public void deleteLabel(Integer projectId, String name) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("name", name, true);
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, formData.asMap(), "projects", projectId, "labels");
}
public Label subscribeLabel(Integer projectId, Integer labelId) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), "projects", projectId, "labels", labelId, "subscribe");
return (response.readEntity(Label.class));
}
public Label unsubscribeLabel(Integer projectId, Integer labelId) throws GitLabApiException {
Response response = post(Response.Status.NOT_MODIFIED, getDefaultPerPageParam(), "projects", projectId, "labels", labelId, "unsubscribe");
return (response.readEntity(Label.class));
}
}
package org.gitlab4j.api;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.Milestone;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.List;
/**
* This class implements the client side API for the GitLab groups calls.
*/
public class MileStonesApi extends AbstractApi {
public MileStonesApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
public List<Milestone> getMilestones(Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones");
return (response.readEntity(new GenericType<List<Milestone>>() {
}));
}
public List<Milestone> getMilestones(Integer projectId, int page, int perPage) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "milestones");
return (response.readEntity(new GenericType<List<Milestone>>() {
}));
}
public List<Milestone> getMilestones(Integer projectId, MilestoneState state) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
return (response.readEntity(new GenericType<List<Milestone>>() {
}));
}
public List<Milestone> getMilestones(Integer projectId, String search) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new GitLabApiForm().withParam("search", search).withParam(PER_PAGE_PARAM, getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
return (response.readEntity(new GenericType<List<Milestone>>() {
}));
}
public List<Milestone> getMilestones(Integer projectId, MilestoneState state, String search) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new GitLabApiForm().withParam("state", state).withParam("search", search).withParam(PER_PAGE_PARAM, getDefaultPerPage());
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones");
return (response.readEntity(new GenericType<List<Milestone>>() {
}));
}
public Milestone getMilestone(Integer projectId, int milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId);
return (response.readEntity(Milestone.class));
}
public List<Issue> getIssues(Integer projectId, Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId, "issues");
return (response.readEntity(new GenericType<List<Issue>>() {
}));
}
public List<MergeRequest> getMergeRequest(Integer projectId, Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId, "merge_requests");
return (response.readEntity(new GenericType<List<MergeRequest>>() {
}));
}
public Milestone createMilestone(Integer projectId, String title, String description, Date dueDate, Date startDate) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("description", description)
.withParam("due_date", dueDate)
.withParam("start_date", startDate);
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "milestones");
return (response.readEntity(Milestone.class));
}
public Milestone closeMilestone(Integer projectId, Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
if (milestoneId == null) {
throw new RuntimeException("milestoneId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm().withParam("state_event", MilestoneState.CLOSE);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
return (response.readEntity(Milestone.class));
}
public Milestone activateMilestone(Integer projectId, Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
if (milestoneId == null) {
throw new RuntimeException("milestoneId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm().withParam("state_event", MilestoneState.ACTIVATE);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
return (response.readEntity(Milestone.class));
}
public Milestone updateMilestone(Integer projectId, Integer milestoneId, String title, String description, Date dueDate, Date startDate, MilestoneState milestoneState) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
if (milestoneId == null) {
throw new RuntimeException("milestoneId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("description", description)
.withParam("due_date", dueDate)
.withParam("start_date", startDate)
.withParam("state_event", milestoneState);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "milestones", milestoneId);
return (response.readEntity(Milestone.class));
}
}
package org.gitlab4j.api;
import java.util.List;
import org.gitlab4j.api.models.Note;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Note;
import java.util.Date;
import java.util.List;
public class NotesApi extends AbstractApi {
......@@ -23,7 +23,7 @@ public class NotesApi extends AbstractApi {
* @return a list of the issues's notes
* @throws GitLabApiException if any exception occurs
*/
public List<Note> getNotes(Integer projectId, Integer issueIid) throws GitLabApiException {
public List<Note> getIssueNotes(Integer projectId, Integer issueIid) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "issues", issueIid, "notes");
return (response.readEntity(new GenericType<List<Note>>() {}));
}
......@@ -40,7 +40,7 @@ public class NotesApi extends AbstractApi {
* @return the list of notes in the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<Note> getNotes(Integer projectId, Integer issueIid, int page, int perPage) throws GitLabApiException {
public List<Note> getIssueNotes(Integer projectId, Integer issueIid, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "issues", issueIid, "notes");
return (response.readEntity(new GenericType<List<Note>>() {}));
}
......@@ -56,7 +56,51 @@ public class NotesApi extends AbstractApi {
* @return the list of notes in the specified range
* @throws GitLabApiException if any exception occurs
*/
public Pager<Note> getNotes(Integer projectId, Integer issueIid, int itemsPerPage) throws GitLabApiException {
public Pager<Note> getIssueNotes(Integer projectId, Integer issueIid, int itemsPerPage) throws GitLabApiException {
return (new Pager<Note>(this, Note.class, itemsPerPage, null, "projects", projectId, "issues", issueIid, "notes"));
}
public Note getIssueNote(Integer projectId, Integer issueIid, Integer noteId) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "issues", issueIid, "notes", noteId);
return (response.readEntity(Note.class));
}
public Note createIssueNote(Integer projectId, Integer issueIid, String body) throws GitLabApiException {
return (createIssueNote(projectId, issueIid, body, null));
}
public Note createIssueNote(Integer projectId, Integer issueIid, String body, Date createdAt) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("body", body, true)
.withParam("created_at", createdAt);
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "issues", issueIid, "notes");
return (response.readEntity(Note.class));
}
public Note updateIssueNote(Integer projectId, Integer issueIid, String body) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("body", body, true);
Response response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "notes");
return (response.readEntity(Note.class));
}
public void deleteIssueNote(Integer projectId, Integer issueIid, Integer noteId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
if (issueIid == null) {
throw new RuntimeException("issueIid cannot be null");
}
if (noteId == null) {
throw new RuntimeException("noteId cannot be null");
}
Response.Status expectedStatus = (isApiVersion(GitLabApi.ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "issues", issueIid, "notes", noteId);
}
}
package org.gitlab4j.api.models;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Label {
private Integer id;
private String name;
private String color;
private String description;
private Integer openIssuesCount;
private Integer closedIssuesCount;
private Integer openMergeRequestCount;
private boolean subscribed;
private Integer priority;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getOpenIssuesCount() {
return openIssuesCount;
}
public void setOpenIssuesCount(Integer openIssuesCount) {
this.openIssuesCount = openIssuesCount;
}
public Integer getClosedIssuesCount() {
return closedIssuesCount;
}
public void setClosedIssuesCount(Integer closedIssuesCount) {
this.closedIssuesCount = closedIssuesCount;
}
public Integer getOpenMergeRequestCount() {
return openMergeRequestCount;
}
public void setOpenMergeRequestCount(Integer openMergeRequestCount) {
this.openMergeRequestCount = openMergeRequestCount;
}
public boolean isSubscribed() {
return subscribed;
}
public void setSubscribed(boolean subscribed) {
this.subscribed = subscribed;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
}
package org.gitlab4j.api;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Note;
......@@ -15,6 +10,11 @@ import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
......@@ -85,7 +85,7 @@ public class TestNotesApi {
assertNotNull(project);
for (Issue issue : gitLabApi.getIssuesApi().getIssues(project.getId())) {
List<Note> notes = gitLabApi.getNotesApi().getNotes(project.getId(), issue.getIid());
List<Note> notes = gitLabApi.getNotesApi().getIssueNotes(project.getId(), issue.getIid());
assertNotNull(notes);
// This requires some issues in the project
// assertTrue(0 < notes.size());
......
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