Commit 03f72a35 authored by 644262163's avatar 644262163 Committed by Greg Messner
Browse files

Add todo api (#441)

parent 44b0012e
...@@ -690,4 +690,73 @@ public interface Constants { ...@@ -690,4 +690,73 @@ public interface Constants {
return (enumHelper.toString(this)); return (enumHelper.toString(this));
} }
} }
/** Enum to use for specifying the action when doing a getTodos() with the TodoApi. */
public enum TodoAction {
ASSIGNED, MENTIONED, BUILD_FAILED, MARKED, APPROVAL_REQUIRED, UNMERGEABLE, DIRECTLY_ADDRESSED;
private static JacksonJsonEnumHelper<TodoAction> enumHelper = new JacksonJsonEnumHelper<>(TodoAction.class);
@JsonCreator
public static TodoAction 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 state when doing a getTodos() with the TodoApi. */
public enum TodoState {
PENDING, DONE;
private static JacksonJsonEnumHelper<TodoState> enumHelper = new JacksonJsonEnumHelper<>(TodoState.class);
@JsonCreator
public static TodoState 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 type when doing a getTodos() with the TodoApi. */
public enum TodoType {
ISSUE, MERGEREQUEST;
private static JacksonJsonEnumHelper<TodoType> enumHelper = new JacksonJsonEnumHelper<>(TodoType.class);
@JsonCreator
public static TodoType forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
} }
...@@ -89,6 +89,7 @@ public class GitLabApi { ...@@ -89,6 +89,7 @@ public class GitLabApi {
private TagsApi tagsApi; private TagsApi tagsApi;
private UserApi userApi; private UserApi userApi;
private WikisApi wikisApi; private WikisApi wikisApi;
private TodosApi todosApi;
/** /**
* Get the GitLab4J shared Logger instance. * Get the GitLab4J shared Logger instance.
...@@ -1655,4 +1656,21 @@ public class GitLabApi { ...@@ -1655,4 +1656,21 @@ public class GitLabApi {
return wikisApi; return wikisApi;
} }
/**
* Gets the TodosApi instance owned by this GitLabApi instance. The TodosApi is used to perform all todo related API calls.
*
* @return the TodosApi instance owned by this GitLabApi instance
*/
public TodosApi getTodosApi() {
if (todosApi == null) {
synchronized (this) {
if (todosApi == null) {
todosApi = new TodosApi(this);
}
}
}
return todosApi;
}
} }
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.gitlab4j.api;
import org.gitlab4j.api.models.Todo;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.stream.Stream;
/**
* This class implements the client side API for the GitLab Todos API.
*/
public class TodosApi extends AbstractApi {
public TodosApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of all pending todos for the current user.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @return a list of pages in todo
* @throws GitLabApiException if any exception occurs
*/
public List<Todo> getTodos() throws GitLabApiException {
return (getTodos(null, null, null, null, null, null, getDefaultPerPage()).all());
}
/**
* Get a list of pages in all pending todos for the current user.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @param page the page to get
* @param perPage the number of todo-pages per page
* @return a list of pages in todo for the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<Todo> getTodos(int page, int perPage) throws GitLabApiException {
return (getTodos(null, null, null, null, null, null, page, perPage));
}
/**
* Get a list of all todos for the current user.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @param itemsPerPage the number of todo that will be fetched per page
* @return a Pager containing the Todos for the user
* @throws GitLabApiException if any exception occurs
*/
public Pager<Todo> getTodos(int itemsPerPage) throws GitLabApiException {
return (getTodos(null, null, null, null, null, null, itemsPerPage));
}
/**
* Returns a list of todos. When no filter is applied,
* it returns all pending todos for the current user.
* Different filters allow the user to precise the request.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @param action the action to be filtered. Can be assigned, mentioned, build_failed, marked, approval_required, unmergeable or directly_addressed.
* @param authorId the ID of an author
* @param projectId the ID of a project
* @param groupId the ID of a group
* @param state the state of the todo. Can be either pending or done
* @param type the type of a todo. Can be either Issue or MergeRequest
* @param itemsPerPage the number of todo that will be fetched per page
* @return a list of pages in todo for the specified range
* @throws GitLabApiException if any exception occurs
*/
public Pager<Todo> getTodos(TodoAction action, Integer authorId, Integer projectId, Integer groupId, TodoState state, TodoType type, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("action", action, false)
.withParam("author_id", authorId, false)
.withParam("project_id", projectId, false)
.withParam("group_id", groupId, false)
.withParam("state", state, false)
.withParam("type", type, false);
return (new Pager<Todo>(this, Todo.class, itemsPerPage, formData.asMap(), "todos"));
}
/**
* Returns a list of todos. When no filter is applied,
* it returns all pending todos for the current user.
* Different filters allow the user to precise the request.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @param action the action to be filtered. Can be assigned, mentioned, build_failed, marked, approval_required, unmergeable or directly_addressed.
* @param authorId the ID of an author
* @param projectId the ID of a project
* @param groupId the ID of a group
* @param state the state of the todo. Can be either pending or done
* @param type the type of a todo. Can be either Issue or MergeRequest
* @param page the page to get
* @param perPage the number of todo-pages per page
* @return a list of pages in todo for the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<Todo> getTodos(TodoAction action, Integer authorId, Integer projectId, Integer groupId, TodoState state, TodoType type, int page, int perPage) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm(page, perPage)
.withParam("action", action, false)
.withParam("author_id", authorId, false)
.withParam("project_id", projectId, false)
.withParam("group_id", groupId, false)
.withParam("state", state, false)
.withParam("type", type, false);
Response response = get(Response.Status.OK, formData.asMap(), "todos");
return (response.readEntity(new GenericType<List<Todo>>() {
}));
}
/**
* Get a Stream of all todos for the current user.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @return Stream of Todos
* @throws GitLabApiException if any exception occurs
*/
public Stream<Todo> getTodoStream() throws GitLabApiException {
return (getTodos(null, null, null, null, null, null, getDefaultPerPage()).stream());
}
/**
* Get a Stream of all todos for the current user with pagination support.
*
* <pre><code>GitLab Endpoint: GET /todos</code></pre>
*
* @param action the action to be filtered. Can be assigned, mentioned, build_failed, marked, approval_required, unmergeable or directly_addressed.
* @param authorId the ID of an author
* @param projectId the ID of a project
* @param groupId the ID of a group
* @param state the state of the todo. Can be either pending or done
* @param type the type of a todo. Can be either Issue or MergeRequest
* @return Stream of Todos
* @throws GitLabApiException if any exception occurs
*/
public Stream<Todo> getTodoStream(TodoAction action, Integer authorId, Integer projectId, Integer groupId, TodoState state, TodoType type) throws GitLabApiException {
return (getTodos(action, authorId, projectId, groupId, state, type, getDefaultPerPage()).stream());
}
/**
* Marks a single pending todo given by its ID for the current user as done.
* The todo marked as done is returned in the response.
*
* <pre><code>GitLab Endpoint: POST /todos/:id/mark_as_done</code></pre>
*
* @param todoId the ID of a todi
* @return todo instance with info on the created page
* @throws GitLabApiException if any exception occurs
*/
public Todo markAsDone(Integer todoId) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm();
Response response = post(Response.Status.OK, formData, "todos", todoId, "mark_as_done");
return (response.readEntity(Todo.class));
}
/**
* Marks all pending todos for the current user as done.
*
* <pre><code>GitLab Endpoint: POST /todos/mark_as_done</code></pre>
*
* @throws GitLabApiException if any exception occurs
*/
public void markAsDone() throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm();
Response response = post(Response.Status.NO_CONTENT, formData, "todos", "mark_as_done");
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Greg Messner <greg@messners.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.gitlab4j.api.models;
import org.gitlab4j.api.utils.JacksonJson;
import java.util.Date;
public class Todo {
public static class Project {
private Integer id;
private String name;
private String nameWithNamespace;
private String path;
private String pathWithNamespace;
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 getNameWithNamespace() {
return nameWithNamespace;
}
public void setNameWithNamespace(String nameWithNamespace) {
this.nameWithNamespace = nameWithNamespace;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getPathWithNamespace() {
return pathWithNamespace;
}
public void setPathWithNamespace(String pathWithNamespace) {
this.pathWithNamespace = pathWithNamespace;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
public class Target {
private Integer id;
private Integer iid;
private Integer projectId;
private String title;
private String description;
private String state;
private Date createdAt;
private Date updatedAt;
private String targetBranch;
private String sourceBranch;
private Integer upvotes;
private Integer downvotes;
private Author author;
private Author assignee;
private Integer sourceProjectId;
private Integer targetProjectId;
private String[] labels;
private Boolean workInProgress;
private Milestone milestone;
private Boolean mergeWhenPipelineSucceeds;
private String mergeStatus;
private Boolean subscribed;
private Integer userNotesCount;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIid() {
return iid;
}
public void setIid(Integer iid) {
this.iid = iid;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public String getTargetBranch() {
return targetBranch;
}
public void setTargetBranch(String targetBranch) {
this.targetBranch = targetBranch;
}
public String getSourceBranch() {
return sourceBranch;
}
public void setSourceBranch(String sourceBranch) {
this.sourceBranch = sourceBranch;
}
public Integer getUpvotes() {
return upvotes;
}
public void setUpvotes(Integer upvotes) {
this.upvotes = upvotes;
}
public Integer getDownvotes() {
return downvotes;
}
public void setDownvotes(Integer downvotes) {
this.downvotes = downvotes;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Author getAssignee() {
return assignee;
}
public void setAssignee(Author assignee) {
this.assignee = assignee;
}
public Integer getSourceProjectId() {
return sourceProjectId;
}
public void setSourceProjectId(Integer sourceProjectId) {
this.sourceProjectId = sourceProjectId;
}
public Integer getTargetProjectId() {
return targetProjectId;
}
public void setTargetProjectId(Integer targetProjectId) {
this.targetProjectId = targetProjectId;
}
public String[] getLabels() {
return labels;
}
public void setLabels(String[] labels) {
this.labels = labels;
}
public Boolean getWorkInProgress() {
return workInProgress;
}
public void setWorkInProgress(Boolean workInProgress) {
this.workInProgress = workInProgress;
}
public Milestone getMilestone() {
return milestone;
}
public void setMilestone(Milestone milestone) {
this.milestone = milestone;
}
public Boolean getMergeWhenPipelineSucceeds() {
return mergeWhenPipelineSucceeds;
}
public void setMergeWhenPipelineSucceeds(Boolean mergeWhenPipelineSucceeds) {
this.mergeWhenPipelineSucceeds = mergeWhenPipelineSucceeds;
}
public String getMergeStatus() {
return mergeStatus;
}
public void setMergeStatus(String mergeStatus) {
this.mergeStatus = mergeStatus;
}
public Boolean getSubscribed() {
return subscribed;
}
public void setSubscribed(Boolean subscribed) {
this.subscribed = subscribed;
}
public Integer getUserNotesCount() {
return userNotesCount;
}
public void setUserNotesCount(Integer userNotesCount) {
this.userNotesCount = userNotesCount;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
private Integer id;
private Project project;
private Author author;
private String actionName;
private String targetType;
private String targetUrl;
private Target target;
private String body;
private String state;
private Date createdAt;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getTargetType() {
return targetType;
}
public void setTargetType(String targetType) {
this.targetType = targetType;
}
public String getTargetUrl() {
return targetUrl;
}
public void setTargetUrl(String targetUrl) {
this.targetUrl = targetUrl;
}
public Target getTarget() {
return target;
}
public void setTarget(Target target) {
this.target = target;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.gitlab4j.api;
import org.gitlab4j.api.models.Todo;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeNotNull;
/**
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
* <p>
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* <p>
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Category(IntegrationTest.class)
public class TestTodosApi extends AbstractIntegrationTest {
private static GitLabApi gitLabApi;
public TestTodosApi() {
super();
}
@BeforeClass
public static void setup() {
// Must setup the connection to the GitLab test server and get the test Project instance
gitLabApi = baseTestSetup();
}
@Before
public void beforeMethod() {
assumeNotNull(gitLabApi);
}
@Test
public void testGetTodos() throws GitLabApiException {
List<Todo> todos = gitLabApi.getTodosApi().getTodos();
assertNotNull(todos);
}
@Test
public void testMarkAsDone() throws GitLabApiException {
gitLabApi.getTodosApi().markAsDone();
List<Todo> todos = gitLabApi.getTodosApi().getTodos();
assertNotNull(todos);
assertEquals(0, todos.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