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

Merge pull request #721 from gitlab4j/issue-621-deployment-api

Fix #621 : Add deployment API
parents bae822a4 cc37d216
......@@ -243,6 +243,28 @@ public interface Constants {
}
}
/** Enum to use for ordering the results of getDeployments. */
public static enum DeploymentOrderBy {
ID, IID, CREATED_AT, UPDATED_AT, REF;
private static JacksonJsonEnumHelper<DeploymentOrderBy> enumHelper = new JacksonJsonEnumHelper<>(DeploymentOrderBy.class);
@JsonCreator
public static DeploymentOrderBy 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 specifying the scope when calling getPipelines(). */
public enum PipelineScope {
......@@ -783,7 +805,9 @@ public interface Constants {
/** Enum to use for specifying the status of a deployment. */
public enum DeploymentStatus {
/**
* After some tests, {@link #CREATED} value is not a valid value.
*/
CREATED, RUNNING, SUCCESS, FAILED, CANCELED;
private static JacksonJsonEnumHelper<DeploymentStatus> enumHelper = new JacksonJsonEnumHelper<>(DeploymentStatus.class);
......
package org.gitlab4j.api;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Deployment;
import org.gitlab4j.api.models.DeploymentFilter;
import org.gitlab4j.api.models.MergeRequest;
/**
* This class implements the client side API for the GitLab Deployments API calls.
* See https://docs.gitlab.com/ee/api/deployments.html
*
*/
public class DeploymentsApi extends AbstractApi {
public DeploymentsApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a list of Deployments
* @throws GitLabApiException if any exception occurs
*/
public List<Deployment> getProjectDeployments(Object projectIdOrPath) throws GitLabApiException {
return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).all());
}
/**
* Get a Pager of all deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param itemsPerPage the number of Deployments instances that will be fetched per page
* @return a Pager of Deployment
* @throws GitLabApiException if any exception occurs
*/
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (getProjectDeployments(projectIdOrPath, null, itemsPerPage));
}
/**
* Get a Pager of all deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
* @return a Pager of Deployment
* @throws GitLabApiException if any exception occurs
*/
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException {
return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage()));
}
/**
* Get a Pager of all deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
* @param itemsPerPage the number of Deployments instances that will be fetched per page
* @return a Pager of Deployment
* @throws GitLabApiException if any exception occurs
*/
public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = (filter != null ? filter.getQueryParams() : new GitLabApiForm());
return (new Pager<Deployment>(this, Deployment.class, itemsPerPage, formData.asMap(),
"projects", getProjectIdOrPath(projectIdOrPath), "deployments"));
}
/**
* Get a Stream of all deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a list of Deployment
* @throws GitLabApiException if any exception occurs
*/
public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath) throws GitLabApiException {
return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).stream());
}
/**
* Get a Stream of all deployments for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings
* @return a list of Deployment
* @throws GitLabApiException if any exception occurs
*/
public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException {
return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage()).stream());
}
/**
* Get a specific deployment.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId the ID of a project's deployment
* @return the specified Deployment instance
* @throws GitLabApiException if any exception occurs
*/
public Deployment getDeployment(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
return (response.readEntity(Deployment.class));
}
/**
* Get a specific deployment as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId the ID of a project's deployment
* @return the specified Deployment as an Optional instance
*/
public Optional<Deployment> getOptionalDeployment(Object projectIdOrPath, Integer deploymentId) {
try {
return (Optional.ofNullable(getDeployment(projectIdOrPath, deploymentId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Creates a new deployment for a project.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/deployments</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param environment The name of the environment to create the deployment for, required
* @param sha The SHA of the commit that is deployed, required
* @param ref The name of the branch or tag that is deployed, required
* @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false), required
* @param status The status to filter deployments by, required
* @return a Deployment instance with info on the added deployment
* @throws GitLabApiException if any exception occurs
*/
public Deployment addDeployment(Object projectIdOrPath, String environment, String sha, String ref, Boolean tag, DeploymentStatus status) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("environment", environment, true)
.withParam("sha", sha, true)
.withParam("ref", ref, true)
.withParam("tag", tag, true)
.withParam("status", status, true);
Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "deployments");
return (response.readEntity(Deployment.class));
}
/**
* Updates an existing project deploy key.
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/deployments/:key_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId The ID of the deployment to update, required
* @param status The new status of the deployment, required
* @return an updated Deployment instance
* @throws GitLabApiException if any exception occurs
*/
public Deployment updateDeployment(Object projectIdOrPath, Integer deploymentId, DeploymentStatus status) throws GitLabApiException {
if (deploymentId == null) {
throw new RuntimeException("deploymentId cannot be null");
}
final Deployment deployment = new Deployment();
deployment.setStatus(status);
final Response response = put(Response.Status.OK, deployment,
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
return (response.readEntity(Deployment.class));
}
/**
* Get a list of Merge Requests shipped with a given deployment.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId The ID of the deployment to update, required
* @return a list containing the MergeRequest instances shipped with a given deployment
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).all());
}
/**
* Get a Pager of Merge Requests shipped with a given deployment.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId The ID of the deployment to update, required
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager containing the MergeRequest instances shipped with a given deployment
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId, int itemsPerPage) throws GitLabApiException {
return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, null,
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", deploymentId, "merge_requests"));
}
/**
* Get a Stream of Merge Requests shipped with a given deployment.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId The ID of the deployment to update, required
* @return a Stream containing the MergeRequest instances shipped with a given deployment
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Stream<MergeRequest> getMergeRequestsStream(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException {
return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).stream());
}
}
......@@ -58,6 +58,7 @@ public class GitLabApi implements AutoCloseable {
private ContainerRegistryApi containerRegistryApi;
private DiscussionsApi discussionsApi;
private DeployKeysApi deployKeysApi;
private DeploymentsApi deploymentsApi;
private DeployTokensApi deployTokensApi;
private EnvironmentsApi environmentsApi;
private EpicsApi epicsApi;
......@@ -961,6 +962,25 @@ public class GitLabApi implements AutoCloseable {
return (deployKeysApi);
}
/**
* Gets the DeployKeysApi instance owned by this GitLabApi instance. The DeploymentsApi is used
* to perform all deployment related API calls.
*
* @return the DeploymentsApi instance owned by this GitLabApi instance
*/
public DeploymentsApi getDeploymentsApi() {
if (deploymentsApi == null) {
synchronized (this) {
if (deploymentsApi == null) {
deploymentsApi = new DeploymentsApi(this);
}
}
}
return (deploymentsApi);
}
/**
* Gets the DeployTokensApi instance owned by this GitLabApi instance. The DeployTokensApi is used
* to perform all deploy token related API calls.
......
package org.gitlab4j.api.models;
import java.util.Date;
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.GitLabApiForm;
import org.gitlab4j.api.Constants.DeploymentOrderBy;
import org.gitlab4j.api.Constants.DeploymentStatus;
import org.gitlab4j.api.Constants.SortOrder;
import org.gitlab4j.api.utils.ISO8601;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class DeploymentFilter {
/**
* Return deployments ordered by either one of id, iid, created_at, updated_at or ref fields. Default is id.
*/
private DeploymentOrderBy orderBy;
/**
* Return deployments sorted in asc or desc order. Default is asc.
*/
private SortOrder sortOrder;
/**
* Return deployments updated after the specified date. Expected in ISO 8601 format (2019-03-15T08:00:00Z).
*/
private Date finishedAfter;
/**
* Return deployments updated before the specified date. Expected in ISO 8601 format (2019-03-15T08:00:00Z).
*/
private Date finishedBefore;
/**
* The name of the environment to filter deployments by.
*/
private String environment;
/**
* The status to filter deployments by.
*/
private DeploymentStatus status;
public DeploymentOrderBy getOrderBy() {
return orderBy;
}
public void setOrderBy(DeploymentOrderBy orderBy) {
this.orderBy = orderBy;
}
public SortOrder getSortOrder() {
return sortOrder;
}
public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}
public Date getFinishedAfter() {
return finishedAfter;
}
public void setFinishedAfter(Date finishedAfter) {
this.finishedAfter = finishedAfter;
}
public Date getFinishedBefore() {
return finishedBefore;
}
public void setFinishedBefore(Date finishedBefore) {
this.finishedBefore = finishedBefore;
}
public String getEnvironment() {
return environment;
}
public void setEnvironment(String environment) {
this.environment = environment;
}
public DeploymentStatus getStatus() {
return status;
}
public void setStatus(DeploymentStatus status) {
this.status = status;
}
public DeploymentFilter withOrderBy(DeploymentOrderBy orderBy) {
this.orderBy = orderBy;
return (this);
}
public DeploymentFilter withSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
return (this);
}
public DeploymentFilter withFinishedAfter(Date finishedAfter) {
this.finishedAfter = finishedAfter;
return (this);
}
public DeploymentFilter withFinishedBefore(Date finishedBefore) {
this.finishedBefore = finishedBefore;
return (this);
}
public DeploymentFilter withEnvironment(String environment) {
this.environment = environment;
return (this);
}
public DeploymentFilter withStatus(DeploymentStatus status) {
this.status = status;
return (this);
}
@JsonIgnore
public GitLabApiForm getQueryParams(int page, int perPage) {
return (getQueryParams()
.withParam(Constants.PAGE_PARAM, page)
.withParam(Constants.PER_PAGE_PARAM, perPage));
}
@JsonIgnore
public GitLabApiForm getQueryParams() {
return (new GitLabApiForm()
.withParam("order_by", orderBy)
.withParam("sort", sortOrder)
.withParam("finished_after", ISO8601.toString(finishedAfter, false))
.withParam("finished_before", ISO8601.toString(finishedBefore, false))
.withParam("environment", environment)
.withParam("status", status));
}
}
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.gitlab4j.api.Constants.DeploymentStatus;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Deployment;
import org.gitlab4j.api.models.DeploymentFilter;
import org.gitlab4j.api.models.Project;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* In order for these tests to run you must set the following properties in
* test-gitlab4j.properties
*
* TEST_HOST_URL TEST_PRIVATE_TOKEN TEST_USERNAME
*
* If any of the above are NULL, all tests in this class will be skipped.
*
*/
@Category(IntegrationTest.class)
public class TestDeploymentsApi extends AbstractIntegrationTest {
// The following needs to be set to your test repository
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
private static GitLabApi gitLabApi;
private static Project testProject;
public TestDeploymentsApi() {
super();
}
@BeforeClass
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
testProject = getTestProject();
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
System.err.println("TEST_USER_NAME cannot be empty");
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
assumeTrue(testProject != null);
}
@Test
public void testCreateDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.RUNNING);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
}
@Test
public void testUpdateDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.RUNNING);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
Deployment updatedDeployment = gitLabApi.getDeploymentsApi().updateDeployment(testProject, deployment.getId(),
DeploymentStatus.SUCCESS);
assertNotNull(updatedDeployment);
assertEquals(environment, updatedDeployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), updatedDeployment.getSha());
assertEquals(testProject.getDefaultBranch(), updatedDeployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, updatedDeployment.getStatus());
}
@Test
public void testGetDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.SUCCESS);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, deployment.getStatus());
Deployment getDeployment = gitLabApi.getDeploymentsApi().getDeployment(testProject, deployment.getId());
assertNotNull(getDeployment);
assertEquals(environment, getDeployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), getDeployment.getSha());
assertEquals(testProject.getDefaultBranch(), getDeployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, getDeployment.getStatus());
assertEquals(deployment.getCreatedAt(), getDeployment.getCreatedAt());
Optional<Deployment> optionalDeployment = gitLabApi.getDeploymentsApi().getOptionalDeployment(testProject,
getDeployment.getId());
optionalDeployment.ifPresent(d -> {
assertEquals(environment, d.getEnvironment().getName());
assertEquals(commits.get(0).getId(), d.getSha());
assertEquals(testProject.getDefaultBranch(), d.getRef());
assertEquals(DeploymentStatus.SUCCESS, d.getStatus());
assertEquals(deployment.getCreatedAt(), d.getCreatedAt());
});
if (!optionalDeployment.isPresent()) {
fail("A deployment should be present.");
}
}
@Test
public void testGetDeployments() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
for (int i = 0; i < 20; i++) {
gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.SUCCESS);
}
Pager<Deployment> pager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, 2);
while (pager.hasNext()) {
pager.next();
assertTrue(pager.current().size() == 1 || pager.current().size() == 2);
}
List<Deployment> deployments = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject);
int unfilteredeploymentNb = deployments.size();
assertTrue(unfilteredeploymentNb >= 10);
DeploymentFilter deploymentFilter = new DeploymentFilter();
deploymentFilter.setEnvironment(environment);
Pager<Deployment> filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
while (filteredPager.hasNext()) {
filteredPager.next();
assertTrue(filteredPager.current().size() > 1 && filteredPager.current().size() < unfilteredeploymentNb);
}
deploymentFilter.setEnvironment("none");
filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
if (filteredPager.hasNext()) {
filteredPager.next();
assertTrue("Should be no deployments for environment `none`", filteredPager.current().size() == 0);
}
Stream<Deployment> projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject);
assertTrue(projectDeploymentsStream.count() >= 10);
projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject, deploymentFilter);
assertEquals(0L, projectDeploymentsStream.count());
}
}
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