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
25 changes: 25 additions & 0 deletions gitlab4j-api/src/main/java/org/gitlab4j/api/EnvironmentsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.ws.rs.core.Response;

import org.gitlab4j.api.models.Environment;
import org.gitlab4j.api.models.EnvironmentFilter;

/**
* This class provides an entry point to all the GitLab API Environments API calls.
Expand Down Expand Up @@ -65,6 +66,30 @@ public Pager<Environment> getEnvironments(Object projectIdOrPath, int itemsPerPa
"environments"));
}

/**
* Get a Pager of all environments for a given project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/environments</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param itemsPerPage the number of Environment instances that will be fetched per page
* @param filter Environment filters
* @return a Pager of Environment instances
* @throws GitLabApiException if any exception occurs
*/
public Pager<Environment> getEnvironments(Object projectIdOrPath, int itemsPerPage, EnvironmentFilter filter)
throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm(filter.getQueryParams());
return (new Pager<Environment>(
this,
Environment.class,
itemsPerPage,
formData.asMap(),
"projects",
getProjectIdOrPath(projectIdOrPath),
"environments"));
}

/**
* Get a specific environment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Stream;

import org.gitlab4j.api.models.Environment;
import org.gitlab4j.api.models.EnvironmentFilter;
import org.gitlab4j.api.models.Project;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -136,4 +137,22 @@ public void testOptionalEnvironment() throws GitLabApiException {
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
}

@Test
public void testFilterEnvironmentsByName() throws GitLabApiException {

final String uniqueName = getUniqueName();
final Environment env =
gitLabApi.getEnvironmentsApi().createEnvironment(testProject, uniqueName, EXTERNAL_URL, TIER);
final String uniqueName2 = getUniqueName();
final Environment env2 =
gitLabApi.getEnvironmentsApi().createEnvironment(testProject, uniqueName2, EXTERNAL_URL, TIER);

EnvironmentFilter filter = new EnvironmentFilter().withName(uniqueName);
Pager<Environment> envs = gitLabApi.getEnvironmentsApi().getEnvironments(testProject, 1, filter);
assertEquals(envs.first().get(0).getName(), uniqueName2);

gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.gitlab4j.api.models;

import java.io.Serializable;

import org.gitlab4j.models.GitLabForm;

public class EnvironmentFilter implements Serializable {
private static final long serialVersionUID = 1L;

private String name;
private String search;
private String states;

public EnvironmentFilter withName(String name) {
this.name = name;
return this;
}

public EnvironmentFilter withSearch(String search) {
this.search = search;
return this;
}

public EnvironmentFilter withStates(String states) {
this.states = states;
return this;
}

/**
* Get the query params specified by this filter.
*
* @return a GitLabApiForm instance holding the query parameters for this EnvironmentFilter instance
*/
public GitLabForm getQueryParams() {
return new GitLabForm()
.withParam("name", name)
.withParam("search", search)
.withParam("states", states);
}
}
Loading