diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index a627b1361..5cd923a78 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -131,6 +131,52 @@ public String toString() { } } + /** Enum to use for ordering the results of getPackages(). */ + public enum PackageOrderBy { + + NAME, CREATED_AT, VERSION, TYPE, PROJECT_PATH; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(PackageOrderBy.class); + + @JsonCreator + public static PackageOrderBy 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 filtering the results of getPackages(). */ + public enum PackageStatus { + + DEFAULT, HIDDEN, PROCESSING; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(PackageStatus.class); + + @JsonCreator + public static PackageStatus 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 getProjects(). */ public enum ProjectOrderBy { diff --git a/src/main/java/org/gitlab4j/api/PackagesApi.java b/src/main/java/org/gitlab4j/api/PackagesApi.java index eead42331..3876c751e 100644 --- a/src/main/java/org/gitlab4j/api/PackagesApi.java +++ b/src/main/java/org/gitlab4j/api/PackagesApi.java @@ -27,10 +27,12 @@ import java.util.stream.Stream; import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import org.gitlab4j.api.models.Package; import org.gitlab4j.api.models.PackageFile; +import org.gitlab4j.api.models.PackageFilter; /** *

This class implements the client side API for the GitLab Packages API. @@ -88,8 +90,25 @@ public List getPackages(Object projectIdOrPath, int page, int perPage) * @throws GitLabApiException if any exception occurs */ public Pager getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { - return (new Pager(this, Package.class, itemsPerPage, null, - "projects", getProjectIdOrPath(projectIdOrPath), "packages")); + return getPackages(projectIdOrPath,null,itemsPerPage); + } + + /** + * Get a Pager of project packages. Both Maven and NPM packages are included in results. + * When accessed without authentication, only packages of public projects are returned. + * + *

GitLab Endpoint: GET /projects/:id/packages
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param filter the PackageFilter instance holding the filter values for the query + * @param itemsPerPage the number of Package instances per page + * @return a Pager of project packages for the specified range + * @throws GitLabApiException if any exception occurs + */ + public Pager getPackages(Object projectIdOrPath, PackageFilter filter, int itemsPerPage) throws GitLabApiException { + MultivaluedMap query = filter!=null?filter.getQueryParams().asMap():null; + return (new Pager(this, Package.class, itemsPerPage, query, + "projects", getProjectIdOrPath(projectIdOrPath), "packages")); } /** @@ -106,6 +125,21 @@ public Stream getPackagesStream(Object projectIdOrPath) throws GitLabAp return (getPackages(projectIdOrPath, getDefaultPerPage()).stream()); } + /** + * Get a Stream of project packages. Both Maven and NPM packages are included in results. + * When accessed without authentication, only packages of public projects are returned. + * + *
GitLab Endpoint: GET /projects/:id/packages
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param filter the PackageFilter instance holding the filter values for the query + * @return a Stream of pages in the project's packages + * @throws GitLabApiException if any exception occurs + */ + public Stream getPackagesStream(Object projectIdOrPath, PackageFilter filter) throws GitLabApiException { + return (getPackages(projectIdOrPath, filter, getDefaultPerPage()).stream()); + } + /** * Get a single project package. * diff --git a/src/main/java/org/gitlab4j/api/models/PackageFilter.java b/src/main/java/org/gitlab4j/api/models/PackageFilter.java new file mode 100644 index 000000000..36a5b18eb --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/PackageFilter.java @@ -0,0 +1,112 @@ +package org.gitlab4j.api.models; + +import org.gitlab4j.api.Constants.PackageStatus; +import org.gitlab4j.api.Constants.PackageOrderBy; +import org.gitlab4j.api.Constants.SortOrder; +import org.gitlab4j.api.GitLabApiForm; + +/** + * This class is used to filter Projects when getting lists of projects for a specified group. + */ +public class PackageFilter { + + private Boolean excludeSubgroups; + private PackageOrderBy orderBy; + private SortOrder sort; + private PackageType packageType; + private String packageName; + private Boolean includeVersionless; + private PackageStatus status; + + /** + * Exclude Subgroups. + * + * @param excludeSubgroups if true, packages from projects from subgroups are not listed. + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withExcludeSubgroups(Boolean excludeSubgroups) { + this.excludeSubgroups = excludeSubgroups; + return (this); + } + + /** + * Return projects ordered by created_at, name, version, type, or project_path + * + * @param orderBy specifies what field to order by + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withOrderBy(PackageOrderBy 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 PackageFilter withSortOder(SortOrder sort) { + this.sort = sort; + return (this); + } + + /** + * Filter the returned packages by type. + * + * @param packageType One of conan, maven, npm, pypi, composer, nuget, helm, generic or golang + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withPackageType(PackageType packageType) { + this.packageType = packageType; + return (this); + } + + /** + * Filter the project packages with a fuzzy search by name + * + * @param packageName + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withPackageName(String packageName) { + this.packageName = packageName; + return (this); + } + + /** + * @param includeVersionless if true, versionless packages are included in the response + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withIncludeVersionless(Boolean includeVersionless) { + this.includeVersionless = includeVersionless; + return (this); + } + + /** + * Filter the returned packages by status. + * + * @param status One of default (default), hidden, or processing + * @return the reference to this ProjectFilter instance + */ + public PackageFilter withStatus(PackageStatus status) { + this.status = status; + return (this); + } + + /** + * 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("order_by", orderBy) + .withParam("sort", sort) + .withParam("exclude_subgroups", excludeSubgroups) + .withParam("package_type", packageType) + .withParam("package_name", packageName) + .withParam("include_versionless", includeVersionless) + .withParam("status", status) + ); + } +} diff --git a/src/main/java/org/gitlab4j/api/models/PackageType.java b/src/main/java/org/gitlab4j/api/models/PackageType.java index c2d1b4967..bb2cb8212 100644 --- a/src/main/java/org/gitlab4j/api/models/PackageType.java +++ b/src/main/java/org/gitlab4j/api/models/PackageType.java @@ -7,7 +7,7 @@ public enum PackageType { - MAVEN, NPM; + MAVEN, NPM, CONAN, PYPI, COMPOSER, NUGET, HELM, GOLANG, GENERIC; private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(PackageType.class); @@ -25,4 +25,4 @@ public String toValue() { public String toString() { return (enumHelper.toString(this)); } -} \ No newline at end of file +} diff --git a/src/test/java/org/gitlab4j/api/TestPackageApi.java b/src/test/java/org/gitlab4j/api/TestPackageApi.java new file mode 100644 index 000000000..27e92188f --- /dev/null +++ b/src/test/java/org/gitlab4j/api/TestPackageApi.java @@ -0,0 +1,49 @@ +package org.gitlab4j.api; + +import org.gitlab4j.api.models.*; +import org.gitlab4j.api.models.Package; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.util.*; +import static org.junit.Assert.*; +import static org.junit.Assume.assumeNotNull; + +@Category(IntegrationTest.class) +public class TestPackageApi extends AbstractIntegrationTest { + + private static GitLabApi gitLabApi; + private static Project testProject; + + public TestPackageApi() { + super(); + } + + @BeforeClass + public static void setup() { + gitLabApi = baseTestSetup(); + testProject = getTestProject(); + } + + @Before + public void beforeMethod() { + assumeNotNull(gitLabApi); + assumeNotNull(testProject); + } + + @Test + public void getPackagesStream() throws GitLabApiException { + PackagesApi packagesApi = gitLabApi.getPackagesApi(); + PackageFilter filter = new PackageFilter() + .withOrderBy(Constants.PackageOrderBy.CREATED_AT) + .withSortOder(Constants.SortOrder.DESC); + + + + Optional lastPackage = packagesApi.getPackagesStream(testProject.getId(),filter).findAny(); + + assertTrue(lastPackage.isPresent()); + } +}