From 7062eea3d42223e8372d5ba0b59810f07e41baec Mon Sep 17 00:00:00 2001 From: Aditya Bansal Date: Mon, 14 Mar 2022 12:07:33 -0700 Subject: [PATCH 1/2] Adding getContributors sort by asc / desc support --- .../java/org/gitlab4j/api/RepositoryApi.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/org/gitlab4j/api/RepositoryApi.java b/src/main/java/org/gitlab4j/api/RepositoryApi.java index 620781ec8..735cf41dc 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryApi.java @@ -663,6 +663,33 @@ public List getContributors(Object projectIdOrPath, int page, int p return (response.readEntity(new GenericType>() { })); } + /** + * Get a list of contributors from a project and in the specified page range, sorted by specified param. + * + *
GitLab Endpoint: GET /projects/:id/repository/contributors
+ * + * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance + * @param page the page to get + * @param perPage the number of projects per page + * @param sort optional param to sort the list of contributors by + * @return a List containing the contributors for the specified project ID + * @throws GitLabApiException if any exception occurs + */ + public List getContributors(Object projectIdOrPath, int page, int perPage, String sort) throws GitLabApiException { + if (sort != null && !(sort.equals("asc") || sort.equals("desc")) ) { + throw new RuntimeException("Sort must be asc or desc"); + } + + GitLabApiForm formData = new GitLabApiForm().withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage); + if (sort != null) { + formData.withParam("sort", sort, false); + } + + Response response = get(Response.Status.OK, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "contributors"); + return (response.readEntity(new GenericType>() { })); + } + /** * Get a Pager of contributors from a project. * From e825044b9148afbbd0d7584119dcd115c6ff7908 Mon Sep 17 00:00:00 2001 From: Aditya Bansal Date: Mon, 14 Mar 2022 18:46:50 -0700 Subject: [PATCH 2/2] Comments and order_by support --- src/main/java/org/gitlab4j/api/Constants.java | 23 +++++++++++++++++++ .../java/org/gitlab4j/api/RepositoryApi.java | 15 ++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index bb786c54b..f96945c57 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -311,6 +311,29 @@ public String toString() { } } + /** Enum to use for ordering the results of getContibutors(). */ + public enum ContributorOrderBy { + + NAME, EMAIL, COMMITS; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(ContributorOrderBy.class); + + @JsonCreator + public static ContributorOrderBy 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 scope when calling getPipelines(). */ public enum PipelineScope { diff --git a/src/main/java/org/gitlab4j/api/RepositoryApi.java b/src/main/java/org/gitlab4j/api/RepositoryApi.java index 735cf41dc..e2aa3d3ed 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryApi.java @@ -671,18 +671,19 @@ public List getContributors(Object projectIdOrPath, int page, int p * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance * @param page the page to get * @param perPage the number of projects per page - * @param sort optional param to sort the list of contributors by + * @param orderBy (optional param) returns contributors ordered by NAME, EMAIL, or COMMITS. Default is COMMITS + * @param sortOrder (optional param) returns contributors sorted in ASC or DESC order. Default is ASC * @return a List containing the contributors for the specified project ID * @throws GitLabApiException if any exception occurs */ - public List getContributors(Object projectIdOrPath, int page, int perPage, String sort) throws GitLabApiException { - if (sort != null && !(sort.equals("asc") || sort.equals("desc")) ) { - throw new RuntimeException("Sort must be asc or desc"); + public List getContributors(Object projectIdOrPath, int page, int perPage, ContributorOrderBy orderBy, SortOrder sortOrder) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm().withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage); + if (sortOrder != null) { + formData.withParam("sort", sortOrder, false); } - GitLabApiForm formData = new GitLabApiForm().withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage); - if (sort != null) { - formData.withParam("sort", sort, false); + if (orderBy != null) { + formData.withParam("order_by", orderBy, false); } Response response = get(Response.Status.OK, formData.asMap(),