From e5c79cdb0ae8aa96ea68088f0162b7a0da37be94 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 1 Nov 2018 20:34:20 -0700 Subject: [PATCH 1/5] Initial commit (#268). --- .../gitlab4j/api/models/ProjectFilter.java | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 src/main/java/org/gitlab4j/api/models/ProjectFilter.java diff --git a/src/main/java/org/gitlab4j/api/models/ProjectFilter.java b/src/main/java/org/gitlab4j/api/models/ProjectFilter.java new file mode 100644 index 000000000..7534d41eb --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/ProjectFilter.java @@ -0,0 +1,219 @@ +package org.gitlab4j.api.models; + +import org.gitlab4j.api.Constants.ProjectOrderBy; +import org.gitlab4j.api.Constants.SortOrder; +import org.gitlab4j.api.Constants; +import org.gitlab4j.api.GitLabApiForm; + +/** + * This class is used to filter Projects when getting lists of projects for a specified user. + */ +public class ProjectFilter { + + private Boolean archived; + private Visibility visibility; + private ProjectOrderBy orderBy; + private SortOrder sort; + private String search; + private Boolean simple; + private Boolean owned; + private Boolean membership; + private Boolean starred; + private Boolean statistics; + private Boolean withCustomAttributes; + private Boolean withIssuesEnabled; + private Boolean withMergeRequestsEnabled; + private AccessLevel minAccessLevel; + + /** + * Limit by archived status. + * + * @param archived if true will only return archived projects + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withArchived(Boolean archived) { + this.archived = archived; + return (this); + } + + /** + * Limit by visibility public, internal, or private. + * + * @param visibility the visibility to match + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withVisibility(Visibility visibility) { + this.visibility = visibility; + return (this); + } + + /** + * Return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields. Default is created_at. + * + * @param orderBy specifies what field to order by + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withOrderBy(ProjectOrderBy orderBy) { + this.orderBy = orderBy; + return (this); + } + + /** + * Return projects sorted in asc or desc order. Default is desc. + * + * @param sort sort direction, ASC or DESC + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withSortOder(SortOrder sort) { + this.sort = sort; + return (this); + } + + /** + * Return list of projects matching the search criteria. + * + * @param search the search criteria + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withSearch(String search) { + this.search = search; + return (this); + } + + /** + * Return only limited fields for each project. This is a no-op without + * authentication as then only simple fields are returned. + * + * @param simple if true, return only limited fields for each project + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withSimple(Boolean simple) { + this.simple = simple; + return (this); + } + + /** + * Limit by projects explicitly owned by the current user + * + * @param owned if true, limit to projects explicitly owned by the current user + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withOwned(Boolean owned) { + this.owned = owned; + return (this); + } + + /** + * Limit by projects that the current user is a member of + * + * @param membership if true, limit by projects that the current user is a member of + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withMembership(Boolean membership) { + this.membership = membership; + return (this); + } + + /** + * Limit by projects starred by the current user. + * + * @param starred if true, limit by projects starred by the current user + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withStarred(Boolean starred) { + this.starred = starred; + return (this); + } + + /** + * Include project statistics. + * + * @param statistics if true, include project statistics + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withStatistics(Boolean statistics) { + this.statistics = statistics; + return (this); + } + + /** + * Include custom attributes in response (admins only). + * + * @param withCustomAttributes if true, include custom attributes in the repsonse + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withCustomAttributes(Boolean withCustomAttributes) { + this.withCustomAttributes = withCustomAttributes; + return (this); + } + + /** + * Limit by enabled issues feature + * + * @param withIssuesEnabled if true, limit by enabled issues feature + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withIssuesEnabled(Boolean withIssuesEnabled) { + this.withIssuesEnabled = withIssuesEnabled; + return (this); + } + + /** + * Limit by enabled merge requests feature + * + * @param withMergeRequestsEnabled if true, imit by enabled merge requests feature + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter withMergeRequestsEnabled(Boolean withMergeRequestsEnabled) { + this.withMergeRequestsEnabled = withMergeRequestsEnabled; + return (this); + } + + /** + * Limit by current user minimal access level + * + * @param minAccessLevel limit by current user minimal access level + * @return the reference to this ProjectFilter instance + */ + public ProjectFilter minAccessLevel(AccessLevel minAccessLevel) { + this.minAccessLevel = minAccessLevel; + return (this); + } + + /** + * Get the query params specified by this filter. + * + * @param page specifies the page number + * @param perPage specifies the number of items per page + * @return a GitLabApiForm instance holding the query parameters for this ProjectFilter instance + */ + public GitLabApiForm getQueryParams(int page, int perPage) { + return (getQueryParams() + .withParam(Constants.PAGE_PARAM, page) + .withParam(Constants.PER_PAGE_PARAM, perPage)); + } + + /** + * Get the query params specified by this filter. + * + * @return a GitLabApiForm instance holding the query parameters for this ProjectFilter instance + */ + public GitLabApiForm getQueryParams() { + return (new GitLabApiForm() + .withParam("archived", archived) + .withParam("visibility", visibility) + .withParam("order_by", orderBy) + .withParam("sort", sort) + .withParam("search", search) + .withParam("simple", simple) + .withParam("owned", owned) + .withParam("membership", membership) + .withParam("starred", starred) + .withParam("statistics", statistics) + .withParam("with_custom_attributes", withCustomAttributes) + .withParam("with_issues_enabled", withIssuesEnabled) + .withParam("with_merge_requests_enabled ", withMergeRequestsEnabled)) + .withParam("min_access_level ", (minAccessLevel != null ? minAccessLevel.toValue() : null) + ); + } +} From 4858f054504861db58843626efa3d42c1918a0eb Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 1 Nov 2018 20:35:01 -0700 Subject: [PATCH 2/5] Updated for release 4.8.54 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1fbae3b3..698f17d39 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep ```java dependencies { ... - compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.53' + compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.54' } ``` @@ -22,7 +22,7 @@ dependencies { org.gitlab4j gitlab4j-api - 4.8.53 + 4.8.54 ``` From f6d5cff81a1457818009ed122fde518d06315e6d Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 1 Nov 2018 20:35:55 -0700 Subject: [PATCH 3/5] Added support for listing projects for a specified user (#268). --- .../java/org/gitlab4j/api/AbstractApi.java | 37 ++++++++++++++ .../java/org/gitlab4j/api/ProjectApi.java | 51 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/main/java/org/gitlab4j/api/AbstractApi.java b/src/main/java/org/gitlab4j/api/AbstractApi.java index ecd31c3ae..9b770fd37 100644 --- a/src/main/java/org/gitlab4j/api/AbstractApi.java +++ b/src/main/java/org/gitlab4j/api/AbstractApi.java @@ -13,6 +13,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Project; +import org.gitlab4j.api.models.User; /** * This class is the base class for all the sub API classes. It provides implementations of @@ -98,6 +99,42 @@ public Object getGroupIdOrPath(Object obj) throws GitLabApiException { } } + /** + * Returns the user ID or path from the provided Integer, String, or User instance. + * + * @param obj the object to determine the ID or username from + * @return the user ID or username from the provided Integer, String, or User instance + * @throws GitLabApiException if any exception occurs during execution + */ + public Object getUserIdOrUsername(Object obj) throws GitLabApiException { + + if (obj == null) { + throw (new RuntimeException("Cannot determine ID or username from null object")); + } else if (obj instanceof Integer) { + return (obj); + } else if (obj instanceof String) { + return (urlEncode(((String) obj).trim())); + } else if (obj instanceof User) { + + Integer id = ((User) obj).getId(); + if (id != null && id.intValue() > 0) { + return (id); + } + + String username = ((User) obj).getUsername(); + if (username != null && username.trim().length() > 0) { + return (urlEncode(username.trim())); + } + + throw (new RuntimeException("Cannot determine ID or username from provided User instance")); + + } else { + + throw (new RuntimeException("Cannot determine ID or username from provided " + obj.getClass().getSimpleName() + + " instance, must be Integer, String, or a User instance")); + } + } + protected ApiVersion getApiVersion() { return (gitLabApi.getApiVersion()); } diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index 19902edac..6911e0266 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -42,6 +42,7 @@ import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Project; +import org.gitlab4j.api.models.ProjectFilter; import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectUser; import org.gitlab4j.api.models.PushRules; @@ -464,6 +465,56 @@ public Pager getStarredProjects(int itemsPerPage) throws GitLabApiExcep return (new Pager(this, Project.class, itemsPerPage, formData.asMap(), "projects")); } + /** + * Get a list of visible projects owned by the given user. + * + *
GET /users/:user_id/projects
+ * + * @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username + * @param filter the ProjectFilter instance holding the filter values for the query + * @return a list of visible projects owned by the given use + * @throws GitLabApiException if any exception occurs + */ + public List getUserProjects(Object userIdOrUsername, ProjectFilter filter) throws GitLabApiException { + return (getUserProjects(userIdOrUsername, filter, 1, getDefaultPerPage())); + } + + /** + * Get a list of visible projects owned by the given user in the specified page range. + * + *
GET /users/:user_id/projects
+ * + * @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username + * @param filter the ProjectFilter instance holding the filter values for the query + * @param page the page to get + * @param perPage the number of projects per page + * @return a list of visible projects owned by the given use + * @throws GitLabApiException if any exception occurs + */ + public List getUserProjects(Object userIdOrUsername, ProjectFilter filter, int page, int perPage) throws GitLabApiException { + GitLabApiForm formData = filter.getQueryParams(page, perPage); + Response response = get(Response.Status.OK, formData.asMap(), + "users", getUserIdOrUsername(userIdOrUsername), "projects"); + return (response.readEntity(new GenericType>() {})); + } + + /** + * Get a Pager of visible projects owned by the given user. + * + *
GET /users/:user_id/projects
+ * + * @param userIdOrUsername the user ID, username of the user, or a User instance holding the user ID or username + * @param filter the ProjectFilter instance holding the filter values for the query + * @param itemsPerPage the number of Project instances that will be fetched per page + * @return a Pager of visible projects owned by the given use + * @throws GitLabApiException if any exception occurs + */ + public Pager getUserProjects(Object userIdOrUsername, ProjectFilter filter, int itemsPerPage) throws GitLabApiException { + GitLabApiForm formData = filter.getQueryParams(); + return (new Pager(this, Project.class, itemsPerPage, formData.asMap(), + "users", getUserIdOrUsername(userIdOrUsername), "projects")); + } + /** * Get a specific project, which is owned by the authentication user. * From aa6ccbdbdafc132fea8ed5537ca85b22714fd6ad Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 1 Nov 2018 20:38:52 -0700 Subject: [PATCH 4/5] [maven-release-plugin] prepare release gitlab4j-api-4.8.54 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1e64581e7..42c080e6e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.gitlab4j gitlab4j-api jar - 4.8.54-SNAPSHOT + 4.8.54 GitLab API Java Client GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API. https://github.com/gmessner/gitlab4j-api @@ -55,7 +55,7 @@ git@github.com:gmessner/gitlab4j-api.git scm:git:git@github.com:gmessner/gitlab4j-api.git scm:git:git@github.com:gmessner/gitlab4j-api.git - HEAD + gitlab4j-api-4.8.54 From b7b2e3af0849e2810c800350d99d825b0c0ca9f2 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 1 Nov 2018 20:39:04 -0700 Subject: [PATCH 5/5] [maven-release-plugin] prepare for next development iteration --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 42c080e6e..2bb81a20f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.gitlab4j gitlab4j-api jar - 4.8.54 + 4.8.55-SNAPSHOT GitLab API Java Client GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API. https://github.com/gmessner/gitlab4j-api @@ -55,7 +55,7 @@ git@github.com:gmessner/gitlab4j-api.git scm:git:git@github.com:gmessner/gitlab4j-api.git scm:git:git@github.com:gmessner/gitlab4j-api.git - gitlab4j-api-4.8.54 + HEAD