Unverified Commit e90e8ec5 authored by Gautier de Saint Martin Lacaze's avatar Gautier de Saint Martin Lacaze Committed by GitHub
Browse files

Merge pull request #757 from tubbynl/issue-755

add support for PackageFilter in PackagesApi
parents 1c5d956a 08da485a
...@@ -131,6 +131,52 @@ public interface Constants { ...@@ -131,6 +131,52 @@ public interface Constants {
} }
} }
/** Enum to use for ordering the results of getPackages(). */
public enum PackageOrderBy {
NAME, CREATED_AT, VERSION, TYPE, PROJECT_PATH;
private static JacksonJsonEnumHelper<PackageOrderBy> 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<PackageStatus> 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(). */ /** Enum to use for ordering the results of getProjects(). */
public enum ProjectOrderBy { public enum ProjectOrderBy {
......
...@@ -27,10 +27,12 @@ import java.util.List; ...@@ -27,10 +27,12 @@ import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Package; import org.gitlab4j.api.models.Package;
import org.gitlab4j.api.models.PackageFile; import org.gitlab4j.api.models.PackageFile;
import org.gitlab4j.api.models.PackageFilter;
/** /**
* <p>This class implements the client side API for the GitLab Packages API. * <p>This class implements the client side API for the GitLab Packages API.
...@@ -88,8 +90,25 @@ public class PackagesApi extends AbstractApi { ...@@ -88,8 +90,25 @@ public class PackagesApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Package>(this, Package.class, itemsPerPage, null, return getPackages(projectIdOrPath,null,itemsPerPage);
"projects", getProjectIdOrPath(projectIdOrPath), "packages")); }
/**
* 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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
*
* @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<Package> getPackages(Object projectIdOrPath, PackageFilter filter, int itemsPerPage) throws GitLabApiException {
MultivaluedMap query = filter!=null?filter.getQueryParams().asMap():null;
return (new Pager<Package>(this, Package.class, itemsPerPage, query,
"projects", getProjectIdOrPath(projectIdOrPath), "packages"));
} }
/** /**
...@@ -106,6 +125,21 @@ public class PackagesApi extends AbstractApi { ...@@ -106,6 +125,21 @@ public class PackagesApi extends AbstractApi {
return (getPackages(projectIdOrPath, getDefaultPerPage()).stream()); 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.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
*
* @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<Package> getPackagesStream(Object projectIdOrPath, PackageFilter filter) throws GitLabApiException {
return (getPackages(projectIdOrPath, filter, getDefaultPerPage()).stream());
}
/** /**
* Get a single project package. * Get a single project package.
* *
......
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)
);
}
}
...@@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonValue; ...@@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
public enum PackageType { public enum PackageType {
MAVEN, NPM; MAVEN, NPM, CONAN, PYPI, COMPOSER, NUGET, HELM, GOLANG, GENERIC;
private static JacksonJsonEnumHelper<PackageType> enumHelper = new JacksonJsonEnumHelper<>(PackageType.class); private static JacksonJsonEnumHelper<PackageType> enumHelper = new JacksonJsonEnumHelper<>(PackageType.class);
...@@ -25,4 +25,4 @@ public enum PackageType { ...@@ -25,4 +25,4 @@ public enum PackageType {
public String toString() { public String toString() {
return (enumHelper.toString(this)); return (enumHelper.toString(this));
} }
} }
\ No newline at end of file
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<Package> lastPackage = packagesApi.getPackagesStream(testProject.getId(),filter).findAny();
assertTrue(lastPackage.isPresent());
}
}
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