From 5e57e9fbf0564788599373a60497b16df8d86291 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Sun, 10 Jun 2018 13:08:29 -0700 Subject: [PATCH] Added support fopr filtering when fetching merge requests (#187, #193). --- src/main/java/org/gitlab4j/api/Constants.java | 44 +++++++++++++++++ .../java/org/gitlab4j/api/GitLabApiForm.java | 4 +- .../org/gitlab4j/api/MergeRequestApi.java | 48 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index 3ebc1560..5e15bf4f 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -104,6 +104,29 @@ public interface Constants { } } + /** Enum to use for ordering the results of getMergeRequests(). */ + public enum MergeRequestOrderBy { + + CREATED_AT, UPDATED_AT; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(MergeRequestOrderBy.class); + + @JsonCreator + public static MergeRequestOrderBy 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 ordering the results of getGroups() and getSubGroups(). */ public enum GroupOrderBy { @@ -191,6 +214,27 @@ public interface Constants { } } + /** Enum to use for specifying the scope for getMergeRequests methods. */ + public enum MergeRequestScope { + + CREATED_BY_ME, ASSIGNED_TO_ME, ALL; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(MergeRequestScope.class); + + @JsonCreator + public static MergeRequestScope 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 querying the state of a MergeRequest */ public enum MergeRequestState { diff --git a/src/main/java/org/gitlab4j/api/GitLabApiForm.java b/src/main/java/org/gitlab4j/api/GitLabApiForm.java index e15bc7f6..8abf540c 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApiForm.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiForm.java @@ -23,7 +23,7 @@ public class GitLabApiForm extends Form { * @param value the value of the field/attribute to add * @return this GitLabAPiForm instance */ - protected GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException { + public GitLabApiForm withParam(String name, Object value) throws IllegalArgumentException { return (withParam(name, value, false)); } @@ -37,7 +37,7 @@ public class GitLabApiForm extends Form { * @return this GitLabAPiForm instance * @throws IllegalArgumentException if a required parameter is null or empty */ - protected GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException { + public GitLabApiForm withParam(String name, Object value, boolean required) throws IllegalArgumentException { if (value == null) { diff --git a/src/main/java/org/gitlab4j/api/MergeRequestApi.java b/src/main/java/org/gitlab4j/api/MergeRequestApi.java index fdf62cfb..67b386ff 100644 --- a/src/main/java/org/gitlab4j/api/MergeRequestApi.java +++ b/src/main/java/org/gitlab4j/api/MergeRequestApi.java @@ -5,11 +5,13 @@ import java.util.Optional; import javax.ws.rs.core.Form; import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.MergeRequest; +import org.gitlab4j.api.models.MergeRequestFilter; import org.gitlab4j.api.models.Participant; /** @@ -21,6 +23,52 @@ public class MergeRequestApi extends AbstractApi { super(gitLabApi); } + /** + * Get all merge requests matching the filter. + * + * GET /merge_requests + * + * @param filter a MergeRequestFilter instance with the filter settings + * @return all merge requests for the specified project matching the filter + * @throws GitLabApiException if any exception occurs + */ + public List getMergeRequests(MergeRequestFilter filter) throws GitLabApiException { + return (getMergeRequests(filter, 1, getDefaultPerPage())); + } + + /** + * Get all merge requests matching the filter. + * + * GET /merge_requests + * + * @param filter a MergeRequestFilter instance with the filter settings + * @param page the page to get + * @param perPage the number of MergeRequest instances per page + * @return all merge requests for the specified project matching the filter + * @throws GitLabApiException if any exception occurs + */ + public List getMergeRequests(MergeRequestFilter filter, int page, int perPage) throws GitLabApiException { + MultivaluedMap queryParams = (filter != null ? + filter.getQueryParams(page, perPage).asMap() : getPageQueryParams(page, perPage)); + Response response = get(Response.Status.OK, queryParams, "merge_requests"); + return (response.readEntity(new GenericType>() {})); + } + + /** + * Get all merge requests matching the filter. + * + * GET /merge_requests + * + * @param filter a MergeRequestFilter instance with the filter settings + * @param itemsPerPage the number of MergeRequest instances that will be fetched per page + * @return all merge requests for the specified project matching the filter + * @throws GitLabApiException if any exception occurs + */ + public Pager getMergeRequests(MergeRequestFilter filter, int itemsPerPage) throws GitLabApiException { + MultivaluedMap queryParams = (filter != null ? filter.getQueryParams().asMap() : null); + return (new Pager(this, MergeRequest.class, itemsPerPage, queryParams, "merge_requests")); + } + /** * Get all merge requests for the specified project. * -- GitLab