Skip to content

Commit e90e8ec

Browse files
authored
Merge pull request #757 from tubbynl/issue-755
add support for PackageFilter in PackagesApi
2 parents 1c5d956 + 08da485 commit e90e8ec

File tree

5 files changed

+245
-4
lines changed

5 files changed

+245
-4
lines changed

src/main/java/org/gitlab4j/api/Constants.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,52 @@ public String toString() {
131131
}
132132
}
133133

134+
/** Enum to use for ordering the results of getPackages(). */
135+
public enum PackageOrderBy {
136+
137+
NAME, CREATED_AT, VERSION, TYPE, PROJECT_PATH;
138+
139+
private static JacksonJsonEnumHelper<PackageOrderBy> enumHelper = new JacksonJsonEnumHelper<>(PackageOrderBy.class);
140+
141+
@JsonCreator
142+
public static PackageOrderBy forValue(String value) {
143+
return enumHelper.forValue(value);
144+
}
145+
146+
@JsonValue
147+
public String toValue() {
148+
return (enumHelper.toString(this));
149+
}
150+
151+
@Override
152+
public String toString() {
153+
return (enumHelper.toString(this));
154+
}
155+
}
156+
157+
/** Enum to use for filtering the results of getPackages(). */
158+
public enum PackageStatus {
159+
160+
DEFAULT, HIDDEN, PROCESSING;
161+
162+
private static JacksonJsonEnumHelper<PackageStatus> enumHelper = new JacksonJsonEnumHelper<>(PackageStatus.class);
163+
164+
@JsonCreator
165+
public static PackageStatus forValue(String value) {
166+
return enumHelper.forValue(value);
167+
}
168+
169+
@JsonValue
170+
public String toValue() {
171+
return (enumHelper.toString(this));
172+
}
173+
174+
@Override
175+
public String toString() {
176+
return (enumHelper.toString(this));
177+
}
178+
}
179+
134180
/** Enum to use for ordering the results of getProjects(). */
135181
public enum ProjectOrderBy {
136182

src/main/java/org/gitlab4j/api/PackagesApi.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.util.stream.Stream;
2828

2929
import javax.ws.rs.core.GenericType;
30+
import javax.ws.rs.core.MultivaluedMap;
3031
import javax.ws.rs.core.Response;
3132

3233
import org.gitlab4j.api.models.Package;
3334
import org.gitlab4j.api.models.PackageFile;
35+
import org.gitlab4j.api.models.PackageFilter;
3436

3537
/**
3638
* <p>This class implements the client side API for the GitLab Packages API.
@@ -88,8 +90,25 @@ public List<Package> getPackages(Object projectIdOrPath, int page, int perPage)
8890
* @throws GitLabApiException if any exception occurs
8991
*/
9092
public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
91-
return (new Pager<Package>(this, Package.class, itemsPerPage, null,
92-
"projects", getProjectIdOrPath(projectIdOrPath), "packages"));
93+
return getPackages(projectIdOrPath,null,itemsPerPage);
94+
}
95+
96+
/**
97+
* Get a Pager of project packages. Both Maven and NPM packages are included in results.
98+
* When accessed without authentication, only packages of public projects are returned.
99+
*
100+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
101+
*
102+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
103+
* @param filter the PackageFilter instance holding the filter values for the query
104+
* @param itemsPerPage the number of Package instances per page
105+
* @return a Pager of project packages for the specified range
106+
* @throws GitLabApiException if any exception occurs
107+
*/
108+
public Pager<Package> getPackages(Object projectIdOrPath, PackageFilter filter, int itemsPerPage) throws GitLabApiException {
109+
MultivaluedMap query = filter!=null?filter.getQueryParams().asMap():null;
110+
return (new Pager<Package>(this, Package.class, itemsPerPage, query,
111+
"projects", getProjectIdOrPath(projectIdOrPath), "packages"));
93112
}
94113

95114
/**
@@ -106,6 +125,21 @@ public Stream<Package> getPackagesStream(Object projectIdOrPath) throws GitLabAp
106125
return (getPackages(projectIdOrPath, getDefaultPerPage()).stream());
107126
}
108127

128+
/**
129+
* Get a Stream of project packages. Both Maven and NPM packages are included in results.
130+
* When accessed without authentication, only packages of public projects are returned.
131+
*
132+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
133+
*
134+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
135+
* @param filter the PackageFilter instance holding the filter values for the query
136+
* @return a Stream of pages in the project's packages
137+
* @throws GitLabApiException if any exception occurs
138+
*/
139+
public Stream<Package> getPackagesStream(Object projectIdOrPath, PackageFilter filter) throws GitLabApiException {
140+
return (getPackages(projectIdOrPath, filter, getDefaultPerPage()).stream());
141+
}
142+
109143
/**
110144
* Get a single project package.
111145
*
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.Constants.PackageStatus;
4+
import org.gitlab4j.api.Constants.PackageOrderBy;
5+
import org.gitlab4j.api.Constants.SortOrder;
6+
import org.gitlab4j.api.GitLabApiForm;
7+
8+
/**
9+
* This class is used to filter Projects when getting lists of projects for a specified group.
10+
*/
11+
public class PackageFilter {
12+
13+
private Boolean excludeSubgroups;
14+
private PackageOrderBy orderBy;
15+
private SortOrder sort;
16+
private PackageType packageType;
17+
private String packageName;
18+
private Boolean includeVersionless;
19+
private PackageStatus status;
20+
21+
/**
22+
* Exclude Subgroups.
23+
*
24+
* @param excludeSubgroups if true, packages from projects from subgroups are not listed.
25+
* @return the reference to this ProjectFilter instance
26+
*/
27+
public PackageFilter withExcludeSubgroups(Boolean excludeSubgroups) {
28+
this.excludeSubgroups = excludeSubgroups;
29+
return (this);
30+
}
31+
32+
/**
33+
* Return projects ordered by created_at, name, version, type, or project_path
34+
*
35+
* @param orderBy specifies what field to order by
36+
* @return the reference to this ProjectFilter instance
37+
*/
38+
public PackageFilter withOrderBy(PackageOrderBy orderBy) {
39+
this.orderBy = orderBy;
40+
return (this);
41+
}
42+
43+
/**
44+
* Return projects sorted in asc or desc order. Default is desc.
45+
*
46+
* @param sort sort direction, ASC or DESC
47+
* @return the reference to this ProjectFilter instance
48+
*/
49+
public PackageFilter withSortOder(SortOrder sort) {
50+
this.sort = sort;
51+
return (this);
52+
}
53+
54+
/**
55+
* Filter the returned packages by type.
56+
*
57+
* @param packageType One of conan, maven, npm, pypi, composer, nuget, helm, generic or golang
58+
* @return the reference to this ProjectFilter instance
59+
*/
60+
public PackageFilter withPackageType(PackageType packageType) {
61+
this.packageType = packageType;
62+
return (this);
63+
}
64+
65+
/**
66+
* Filter the project packages with a fuzzy search by name
67+
*
68+
* @param packageName
69+
* @return the reference to this ProjectFilter instance
70+
*/
71+
public PackageFilter withPackageName(String packageName) {
72+
this.packageName = packageName;
73+
return (this);
74+
}
75+
76+
/**
77+
* @param includeVersionless if true, versionless packages are included in the response
78+
* @return the reference to this ProjectFilter instance
79+
*/
80+
public PackageFilter withIncludeVersionless(Boolean includeVersionless) {
81+
this.includeVersionless = includeVersionless;
82+
return (this);
83+
}
84+
85+
/**
86+
* Filter the returned packages by status.
87+
*
88+
* @param status One of default (default), hidden, or processing
89+
* @return the reference to this ProjectFilter instance
90+
*/
91+
public PackageFilter withStatus(PackageStatus status) {
92+
this.status = status;
93+
return (this);
94+
}
95+
96+
/**
97+
* Get the query params specified by this filter.
98+
*
99+
* @return a GitLabApiForm instance holding the query parameters for this ProjectFilter instance
100+
*/
101+
public GitLabApiForm getQueryParams() {
102+
return (new GitLabApiForm()
103+
.withParam("order_by", orderBy)
104+
.withParam("sort", sort)
105+
.withParam("exclude_subgroups", excludeSubgroups)
106+
.withParam("package_type", packageType)
107+
.withParam("package_name", packageName)
108+
.withParam("include_versionless", includeVersionless)
109+
.withParam("status", status)
110+
);
111+
}
112+
}

src/main/java/org/gitlab4j/api/models/PackageType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public enum PackageType {
99

10-
MAVEN, NPM;
10+
MAVEN, NPM, CONAN, PYPI, COMPOSER, NUGET, HELM, GOLANG, GENERIC;
1111

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

@@ -25,4 +25,4 @@ public String toValue() {
2525
public String toString() {
2626
return (enumHelper.toString(this));
2727
}
28-
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.*;
4+
import org.gitlab4j.api.models.Package;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
10+
import java.util.*;
11+
import static org.junit.Assert.*;
12+
import static org.junit.Assume.assumeNotNull;
13+
14+
@Category(IntegrationTest.class)
15+
public class TestPackageApi extends AbstractIntegrationTest {
16+
17+
private static GitLabApi gitLabApi;
18+
private static Project testProject;
19+
20+
public TestPackageApi() {
21+
super();
22+
}
23+
24+
@BeforeClass
25+
public static void setup() {
26+
gitLabApi = baseTestSetup();
27+
testProject = getTestProject();
28+
}
29+
30+
@Before
31+
public void beforeMethod() {
32+
assumeNotNull(gitLabApi);
33+
assumeNotNull(testProject);
34+
}
35+
36+
@Test
37+
public void getPackagesStream() throws GitLabApiException {
38+
PackagesApi packagesApi = gitLabApi.getPackagesApi();
39+
PackageFilter filter = new PackageFilter()
40+
.withOrderBy(Constants.PackageOrderBy.CREATED_AT)
41+
.withSortOder(Constants.SortOrder.DESC);
42+
43+
44+
45+
Optional<Package> lastPackage = packagesApi.getPackagesStream(testProject.getId(),filter).findAny();
46+
47+
assertTrue(lastPackage.isPresent());
48+
}
49+
}

0 commit comments

Comments
 (0)