Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/org/gitlab4j/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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(). */
public enum ProjectOrderBy {

Expand Down
38 changes: 36 additions & 2 deletions src/main/java/org/gitlab4j/api/PackagesApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>This class implements the client side API for the GitLab Packages API.
Expand Down Expand Up @@ -88,8 +90,25 @@ public List<Package> getPackages(Object projectIdOrPath, int page, int perPage)
* @throws GitLabApiException if any exception occurs
*/
public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Package>(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.
*
* <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"));
}

/**
Expand All @@ -106,6 +125,21 @@ public Stream<Package> 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.
*
* <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.
*
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/org/gitlab4j/api/models/PackageFilter.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/gitlab4j/api/models/PackageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public enum PackageType {

MAVEN, NPM;
MAVEN, NPM, CONAN, PYPI, COMPOSER, NUGET, HELM, GOLANG, GENERIC;

private static JacksonJsonEnumHelper<PackageType> enumHelper = new JacksonJsonEnumHelper<>(PackageType.class);

Expand All @@ -25,4 +25,4 @@ public String toValue() {
public String toString() {
return (enumHelper.toString(this));
}
}
}
49 changes: 49 additions & 0 deletions src/test/java/org/gitlab4j/api/TestPackageApi.java
Original file line number Diff line number Diff line change
@@ -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<Package> lastPackage = packagesApi.getPackagesStream(testProject.getId(),filter).findAny();

assertTrue(lastPackage.isPresent());
}
}