|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import javax.ws.rs.core.Response; |
| 8 | + |
| 9 | +import org.gitlab4j.api.models.Deployment; |
| 10 | +import org.gitlab4j.api.models.DeploymentFilter; |
| 11 | +import org.gitlab4j.api.models.MergeRequest; |
| 12 | + |
| 13 | +/** |
| 14 | + * This class implements the client side API for the GitLab Deployments API calls. |
| 15 | + * See https://docs.gitlab.com/ee/api/deployments.html |
| 16 | + * |
| 17 | + */ |
| 18 | +public class DeploymentsApi extends AbstractApi { |
| 19 | + |
| 20 | + public DeploymentsApi(GitLabApi gitLabApi) { |
| 21 | + super(gitLabApi); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Get a list of deployments for the specified project. |
| 26 | + * |
| 27 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 28 | + * |
| 29 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 30 | + * @return a list of Deployments |
| 31 | + * @throws GitLabApiException if any exception occurs |
| 32 | + */ |
| 33 | + public List<Deployment> getProjectDeployments(Object projectIdOrPath) throws GitLabApiException { |
| 34 | + return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).all()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get a Pager of all deployments for the specified project. |
| 39 | + * |
| 40 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 41 | + * |
| 42 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 43 | + * @param itemsPerPage the number of Deployments instances that will be fetched per page |
| 44 | + * @return a Pager of Deployment |
| 45 | + * @throws GitLabApiException if any exception occurs |
| 46 | + */ |
| 47 | + public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 48 | + return (getProjectDeployments(projectIdOrPath, null, itemsPerPage)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get a Pager of all deployments for the specified project. |
| 53 | + * |
| 54 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 55 | + * |
| 56 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 57 | + * @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings |
| 58 | + * @return a Pager of Deployment |
| 59 | + * @throws GitLabApiException if any exception occurs |
| 60 | + */ |
| 61 | + public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException { |
| 62 | + return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage())); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get a Pager of all deployments for the specified project. |
| 67 | + * |
| 68 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 69 | + * |
| 70 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 71 | + * @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings |
| 72 | + * @param itemsPerPage the number of Deployments instances that will be fetched per page |
| 73 | + * @return a Pager of Deployment |
| 74 | + * @throws GitLabApiException if any exception occurs |
| 75 | + */ |
| 76 | + public Pager<Deployment> getProjectDeployments(Object projectIdOrPath, DeploymentFilter filter, int itemsPerPage) throws GitLabApiException { |
| 77 | + GitLabApiForm formData = (filter != null ? filter.getQueryParams() : new GitLabApiForm()); |
| 78 | + return (new Pager<Deployment>(this, Deployment.class, itemsPerPage, formData.asMap(), |
| 79 | + "projects", getProjectIdOrPath(projectIdOrPath), "deployments")); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get a Stream of all deployments for the specified project. |
| 84 | + * |
| 85 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 86 | + * |
| 87 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 88 | + * @return a list of Deployment |
| 89 | + * @throws GitLabApiException if any exception occurs |
| 90 | + */ |
| 91 | + public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath) throws GitLabApiException { |
| 92 | + return (getProjectDeployments(projectIdOrPath, null, getDefaultPerPage()).stream()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get a Stream of all deployments for the specified project. |
| 97 | + * |
| 98 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments</code></pre> |
| 99 | + * |
| 100 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 101 | + * @param filter {@link DeploymentFilter} a DeploymentFilter instance with the filter settings |
| 102 | + * @return a list of Deployment |
| 103 | + * @throws GitLabApiException if any exception occurs |
| 104 | + */ |
| 105 | + public Stream<Deployment> getProjectDeploymentsStream(Object projectIdOrPath, DeploymentFilter filter) throws GitLabApiException { |
| 106 | + return (getProjectDeployments(projectIdOrPath, filter, getDefaultPerPage()).stream()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get a specific deployment. |
| 111 | + * |
| 112 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre> |
| 113 | + * |
| 114 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 115 | + * @param deploymentId the ID of a project's deployment |
| 116 | + * @return the specified Deployment instance |
| 117 | + * @throws GitLabApiException if any exception occurs |
| 118 | + */ |
| 119 | + public Deployment getDeployment(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException { |
| 120 | + Response response = get(Response.Status.OK, getDefaultPerPageParam(), |
| 121 | + "projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId); |
| 122 | + return (response.readEntity(Deployment.class)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get a specific deployment as an Optional instance. |
| 127 | + * |
| 128 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id</code></pre> |
| 129 | + * |
| 130 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 131 | + * @param deploymentId the ID of a project's deployment |
| 132 | + * @return the specified Deployment as an Optional instance |
| 133 | + */ |
| 134 | + public Optional<Deployment> getOptionalDeployment(Object projectIdOrPath, Integer deploymentId) { |
| 135 | + try { |
| 136 | + return (Optional.ofNullable(getDeployment(projectIdOrPath, deploymentId))); |
| 137 | + } catch (GitLabApiException glae) { |
| 138 | + return (GitLabApi.createOptionalFromException(glae)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates a new deployment for a project. |
| 144 | + * |
| 145 | + * <pre><code>GitLab Endpoint: POST /projects/:id/deployments</code></pre> |
| 146 | + * |
| 147 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 148 | + * @param environment The name of the environment to create the deployment for, required |
| 149 | + * @param sha The SHA of the commit that is deployed, required |
| 150 | + * @param ref The name of the branch or tag that is deployed, required |
| 151 | + * @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false), required |
| 152 | + * @param status The status to filter deployments by, required |
| 153 | + * @return a Deployment instance with info on the added deployment |
| 154 | + * @throws GitLabApiException if any exception occurs |
| 155 | + */ |
| 156 | + public Deployment addDeployment(Object projectIdOrPath, String environment, String sha, String ref, Boolean tag, DeploymentStatus status) throws GitLabApiException { |
| 157 | + |
| 158 | + GitLabApiForm formData = new GitLabApiForm() |
| 159 | + .withParam("environment", environment, true) |
| 160 | + .withParam("sha", sha, true) |
| 161 | + .withParam("ref", ref, true) |
| 162 | + .withParam("tag", tag, true) |
| 163 | + .withParam("status", status, true); |
| 164 | + |
| 165 | + Response response = post(Response.Status.CREATED, formData, |
| 166 | + "projects", getProjectIdOrPath(projectIdOrPath), "deployments"); |
| 167 | + return (response.readEntity(Deployment.class)); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Updates an existing project deploy key. |
| 172 | + * |
| 173 | + * <pre><code>GitLab Endpoint: PUT /projects/:id/deployments/:key_id</code></pre> |
| 174 | + * |
| 175 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 176 | + * @param deploymentId The ID of the deployment to update, required |
| 177 | + * @param status The new status of the deployment, required |
| 178 | + * @return an updated Deployment instance |
| 179 | + * @throws GitLabApiException if any exception occurs |
| 180 | + */ |
| 181 | + public Deployment updateDeployment(Object projectIdOrPath, Integer deploymentId, DeploymentStatus status) throws GitLabApiException { |
| 182 | + |
| 183 | + if (deploymentId == null) { |
| 184 | + throw new RuntimeException("deploymentId cannot be null"); |
| 185 | + } |
| 186 | + |
| 187 | + final Deployment deployment = new Deployment(); |
| 188 | + deployment.setStatus(status); |
| 189 | + final Response response = put(Response.Status.OK, deployment, |
| 190 | + "projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId); |
| 191 | + |
| 192 | + return (response.readEntity(Deployment.class)); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Get a list of Merge Requests shipped with a given deployment. |
| 197 | + * |
| 198 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre> |
| 199 | + * |
| 200 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 201 | + * @param deploymentId The ID of the deployment to update, required |
| 202 | + * @return a list containing the MergeRequest instances shipped with a given deployment |
| 203 | + * @throws GitLabApiException GitLabApiException if any exception occurs during execution |
| 204 | + */ |
| 205 | + public List<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException { |
| 206 | + return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).all()); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Get a Pager of Merge Requests shipped with a given deployment. |
| 211 | + * |
| 212 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre> |
| 213 | + * |
| 214 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 215 | + * @param deploymentId The ID of the deployment to update, required |
| 216 | + * @param itemsPerPage the number of Commit instances that will be fetched per page |
| 217 | + * @return a Pager containing the MergeRequest instances shipped with a given deployment |
| 218 | + * @throws GitLabApiException GitLabApiException if any exception occurs during execution |
| 219 | + */ |
| 220 | + public Pager<MergeRequest> getMergeRequests(Object projectIdOrPath, Integer deploymentId, int itemsPerPage) throws GitLabApiException { |
| 221 | + return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, null, |
| 222 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", deploymentId, "merge_requests")); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Get a Stream of Merge Requests shipped with a given deployment. |
| 227 | + * |
| 228 | + * <pre><code>GitLab Endpoint: GET /projects/:id/deployments/:deployment_id/merge_requests</code></pre> |
| 229 | + * |
| 230 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 231 | + * @param deploymentId The ID of the deployment to update, required |
| 232 | + * @return a Stream containing the MergeRequest instances shipped with a given deployment |
| 233 | + * @throws GitLabApiException GitLabApiException if any exception occurs during execution |
| 234 | + */ |
| 235 | + public Stream<MergeRequest> getMergeRequestsStream(Object projectIdOrPath, Integer deploymentId) throws GitLabApiException { |
| 236 | + return (getMergeRequests(projectIdOrPath, deploymentId, getDefaultPerPage()).stream()); |
| 237 | + } |
| 238 | + |
| 239 | + |
| 240 | +} |
0 commit comments