Skip to content

Commit 731d22d

Browse files
committed
Add environment filters #1129
1 parent dff8509 commit 731d22d

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/EnvironmentsApi.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jakarta.ws.rs.core.Response;
88

99
import org.gitlab4j.api.models.Environment;
10+
import org.gitlab4j.api.models.EnvironmentFilter;
1011

1112
/**
1213
* This class provides an entry point to all the GitLab API Environments API calls.
@@ -65,6 +66,29 @@ public Pager<Environment> getEnvironments(Object projectIdOrPath, int itemsPerPa
6566
"environments"));
6667
}
6768

69+
/**
70+
* Get a Pager of all environments for a given project.
71+
*
72+
* <pre><code>GitLab Endpoint: GET /projects/:id/environments</code></pre>
73+
*
74+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
75+
* @param itemsPerPage the number of Environment instances that will be fetched per page
76+
* @param filter Environment filters
77+
* @return a Pager of Environment instances
78+
* @throws GitLabApiException if any exception occurs
79+
*/
80+
public Pager<Environment> getEnvironments(Object projectIdOrPath, int itemsPerPage, EnvironmentFilter filter) throws GitLabApiException {
81+
GitLabApiForm formData = new GitLabApiForm(filter.getQueryParams());
82+
return (new Pager<Environment>(
83+
this,
84+
Environment.class,
85+
itemsPerPage,
86+
formData.asMap(),
87+
"projects",
88+
getProjectIdOrPath(projectIdOrPath),
89+
"environments"));
90+
}
91+
6892
/**
6993
* Get a specific environment.
7094
*

gitlab4j-api/src/test/java/org/gitlab4j/api/TestEnvironmentsApi.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.stream.Stream;
1313

1414
import org.gitlab4j.api.models.Environment;
15+
import org.gitlab4j.api.models.EnvironmentFilter;
1516
import org.gitlab4j.api.models.Project;
1617
import org.junit.jupiter.api.AfterAll;
1718
import org.junit.jupiter.api.BeforeAll;
@@ -136,4 +137,22 @@ public void testOptionalEnvironment() throws GitLabApiException {
136137
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
137138
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
138139
}
140+
141+
@Test
142+
public void testFilterEnvironmentsByName() throws GitLabApiException {
143+
144+
final String uniqueName = getUniqueName();
145+
final Environment env =
146+
gitLabApi.getEnvironmentsApi().createEnvironment(testProject, uniqueName, EXTERNAL_URL, TIER);
147+
final String uniqueName2 = getUniqueName();
148+
final Environment env2 =
149+
gitLabApi.getEnvironmentsApi().createEnvironment(testProject, uniqueName2, EXTERNAL_URL, TIER);
150+
151+
EnvironmentFilter filter = new EnvironmentFilter().withName(uniqueName);
152+
Pager<Environment> envs = gitLabApi.getEnvironmentsApi().getEnvironments(testProject, 1, filter);
153+
assertEquals(envs.first().get(0).getName(), uniqueName2);
154+
155+
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
156+
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
157+
}
139158
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.models.GitLabForm;
4+
5+
import java.io.Serializable;
6+
7+
public class EnvironmentFilter implements Serializable {
8+
private static final long serialVersionUID = 1L;
9+
10+
private String name;
11+
private String search;
12+
private String states;
13+
14+
public EnvironmentFilter withName(String name) {
15+
this.name = name;
16+
return this;
17+
}
18+
19+
public EnvironmentFilter withSearch(String search) {
20+
this.search = search;
21+
return this;
22+
}
23+
24+
public EnvironmentFilter withStates(String states) {
25+
this.states = states;
26+
return this;
27+
}
28+
29+
/**
30+
* Get the query params specified by this filter.
31+
*
32+
* @return a GitLabApiForm instance holding the query parameters for this GroupFilter instance
33+
*/
34+
public GitLabForm getQueryParams() {
35+
return new GitLabForm()
36+
.withParam("name", name)
37+
.withParam("search", search)
38+
.withParam("states", states);
39+
}
40+
}

0 commit comments

Comments
 (0)