|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import javax.ws.rs.core.Form; |
| 8 | +import javax.ws.rs.core.Response; |
| 9 | + |
| 10 | +import org.gitlab4j.api.models.ExternalStatusCheck; |
| 11 | +import org.gitlab4j.api.models.ExternalStatusCheckProtectedBranch; |
| 12 | +import org.gitlab4j.api.models.ExternalStatusCheckResult; |
| 13 | +import org.gitlab4j.api.models.ExternalStatusCheckStatus; |
| 14 | +import org.gitlab4j.api.models.ExternalStatusCheckStatus.Status; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class implements the client side API for the GitLab external status checks. |
| 18 | + * See <a href="https://docs.gitlab.com/ee/api/status_checks.html">External Status Checks API</a> for more information. |
| 19 | + */ |
| 20 | +public class ExternalStatusCheckApi extends AbstractApi { |
| 21 | + |
| 22 | + public ExternalStatusCheckApi(GitLabApi gitLabApi) { |
| 23 | + super(gitLabApi); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Gets a list of all external status checks for a given project. |
| 28 | + * |
| 29 | + * <pre><code>GitLab Endpoint: GET /projects/:id/external_status_checks</code></pre> |
| 30 | + * |
| 31 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 32 | + * @return a List of ExternalStatusCheck |
| 33 | + * @throws GitLabApiException if any exception occurs |
| 34 | + */ |
| 35 | + public List<ExternalStatusCheck> getExternalStatusChecks(Object projectIdOrPath) throws GitLabApiException { |
| 36 | + return (getExternalStatusChecks(projectIdOrPath, getDefaultPerPage()).all()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets a Pager of all external status checks for a given project. |
| 41 | + * |
| 42 | + * <pre><code>GitLab Endpoint: GET /projects/:id/external_status_checks</code></pre> |
| 43 | + * |
| 44 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 45 | + * @param itemsPerPage the number of ExternalStatusCheck instances that will be fetched per page |
| 46 | + * @return the Pager of ExternalStatusCheck instances |
| 47 | + * @throws GitLabApiException if any exception occurs |
| 48 | + */ |
| 49 | + public Pager<ExternalStatusCheck> getExternalStatusChecks(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 50 | + return (new Pager<ExternalStatusCheck>(this, ExternalStatusCheck.class, itemsPerPage, null, |
| 51 | + "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks")); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets a Stream of all external status checks for a given project. |
| 56 | + * |
| 57 | + * <pre><code>GitLab Endpoint: GET /projects/:id/external_status_checks</code></pre> |
| 58 | + * |
| 59 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 60 | + * @return a Stream of ExternalStatusCheck |
| 61 | + * @throws GitLabApiException if any exception occurs |
| 62 | + */ |
| 63 | + public Stream<ExternalStatusCheck> getExternalStatusChecksStream(Object projectIdOrPath) throws GitLabApiException { |
| 64 | + return (getExternalStatusChecks(projectIdOrPath, getDefaultPerPage()).stream()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a new external status check. |
| 69 | + * |
| 70 | + * <pre><code>GitLab Endpoint: POST /projects/:id/external_status_checks</code></pre> |
| 71 | + * |
| 72 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 73 | + * @param name Display name of external status check (required) |
| 74 | + * @param externalUrl URL of external status check resource (optional) |
| 75 | + * @param protectedBranchIds IDs of protected branches to scope the rule by (optional) |
| 76 | + * @return an ExternalStatusCheck instance containing info on the newly created externalStatusCheck |
| 77 | + * @throws GitLabApiException if any exception occurs |
| 78 | + */ |
| 79 | + public ExternalStatusCheck createExternalStatusCheck(Object projectIdOrPath, String name, String externalUrl, List<Long> protectedBranchIds) throws GitLabApiException { |
| 80 | + Form formData = new GitLabApiForm() |
| 81 | + .withParam("name", name, true) |
| 82 | + .withParam("external_url", externalUrl, true) |
| 83 | + .withParam("protected_branch_ids", protectedBranchIds); |
| 84 | + Response response = post(Response.Status.CREATED, formData.asMap(), |
| 85 | + "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks"); |
| 86 | + return (response.readEntity(ExternalStatusCheck.class)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates a new external status check using the information contained in the provided ExternalStatusCheck instance. Only the following |
| 91 | + * fields from the ExternalStatusCheck instance are used: |
| 92 | + * <pre><code> |
| 93 | + * name - Display name of external status check (required) |
| 94 | + * external url - URL of external status check resource (required) |
| 95 | + * protected branches - the id of the protected branches (optional) |
| 96 | + * </code></pre> |
| 97 | + * |
| 98 | + * <pre><code>GitLab Endpoint: POST /projects/:id/external_status_checks</code></pre> |
| 99 | + * |
| 100 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 101 | + * @param externalStatusCheck the ExternalStatusCheck instance with information for the new external status check |
| 102 | + * @return an ExternalStatusCheck instance containing info on the newly created externalStatusCheck |
| 103 | + * @throws GitLabApiException if any exception occurs |
| 104 | + */ |
| 105 | + public ExternalStatusCheck createExternalStatusCheck(Object projectIdOrPath, ExternalStatusCheck externalStatusCheck) throws GitLabApiException { |
| 106 | + List<Long> protectedBranchIds; |
| 107 | + if(externalStatusCheck.getProtectedBranches() == null) { |
| 108 | + protectedBranchIds = null; |
| 109 | + } else { |
| 110 | + protectedBranchIds = externalStatusCheck.getProtectedBranches().stream().map(ExternalStatusCheckProtectedBranch::getId).collect(Collectors.toList()); |
| 111 | + } |
| 112 | + Form formData = new GitLabApiForm() |
| 113 | + .withParam("name", externalStatusCheck.getName(), true) |
| 114 | + .withParam("external_url", externalStatusCheck.getExternalUrl(), true) |
| 115 | + .withParam("protected_branch_ids", protectedBranchIds); |
| 116 | + Response response = post(Response.Status.CREATED, formData.asMap(), |
| 117 | + "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks"); |
| 118 | + return (response.readEntity(ExternalStatusCheck.class)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Updates an existing external status check. |
| 123 | + * |
| 124 | + * <pre><code>GitLab Endpoint: PUT /projects/:id/external_status_checks/:check_id</code></pre> |
| 125 | + * |
| 126 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 127 | + * @param checkId ID of an external status check to update (required) |
| 128 | + * @param name Display name of external status check (optional) |
| 129 | + * @param externalUrl URL of external status check resource (optional) |
| 130 | + * @param protectedBranchIds IDs of protected branches to scope the rule by (optional) |
| 131 | + * @return an ExternalStatusCheck instance containing info on the newly created ExternalStatusCheck |
| 132 | + * @throws GitLabApiException if any exception occurs |
| 133 | + */ |
| 134 | + public ExternalStatusCheck updateExternalStatusCheck(Object projectIdOrPath, Long checkId, String name, String externalUrl, List<Long> protectedBranchIds) throws GitLabApiException { |
| 135 | + Form formData = new GitLabApiForm() |
| 136 | + .withParam("name", name) |
| 137 | + .withParam("external_url", externalUrl) |
| 138 | + .withParam("protected_branch_ids", protectedBranchIds); |
| 139 | + Response response = put(Response.Status.OK, formData.asMap(), |
| 140 | + "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks", checkId); |
| 141 | + return (response.readEntity(ExternalStatusCheck.class)); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Updates an external status check using the information contained in the provided ExternalStatusCheck instance. Only the following |
| 146 | + * fields from the ExternalStatusCheck instance are used: |
| 147 | + * <pre><code> |
| 148 | + * id - the id of the external status check (required) |
| 149 | + * name - Display name of external status check (optional) |
| 150 | + * external url - URL of external status check resource (optional) |
| 151 | + * protected branches - the id of the protected branches (optional) |
| 152 | + * </code></pre> |
| 153 | + * <pre><code>GitLab Endpoint: PUT /projects/:id/external_status_checks/:check_id</code></pre> |
| 154 | + * |
| 155 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 156 | + * @param externalStatusCheck the ExternalStatusCheck instance with update information |
| 157 | + * @return an ExternalStatusCheck instance containing info on the updated ExternalStatusCheck |
| 158 | + * @throws GitLabApiException if any exception occurs |
| 159 | + */ |
| 160 | + public ExternalStatusCheck updateExternalStatusCheck(Object projectIdOrPath, ExternalStatusCheck externalStatusCheck) throws GitLabApiException { |
| 161 | + if (externalStatusCheck == null || externalStatusCheck.getId() == null) { |
| 162 | + throw new GitLabApiException("the specified external status check is null or has no id"); |
| 163 | + } |
| 164 | + List<Long> protectedBranchIds = getProtectedBranchIds(externalStatusCheck); |
| 165 | + Form formData = new GitLabApiForm() |
| 166 | + .withParam("name", externalStatusCheck.getName()) |
| 167 | + .withParam("external_url", externalStatusCheck.getExternalUrl()) |
| 168 | + .withParam("protected_branch_ids", protectedBranchIds); |
| 169 | + Response response = put(Response.Status.OK, formData.asMap(), |
| 170 | + "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks", externalStatusCheck.getId()); |
| 171 | + return (response.readEntity(ExternalStatusCheck.class)); |
| 172 | + } |
| 173 | + |
| 174 | + private List<Long> getProtectedBranchIds(ExternalStatusCheck externalStatusCheck) { |
| 175 | + if(externalStatusCheck.getProtectedBranches() == null) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + return externalStatusCheck.getProtectedBranches().stream().map(ExternalStatusCheckProtectedBranch::getId).collect(Collectors.toList()); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Deletes an external status check. |
| 183 | + * |
| 184 | + * <pre><code>GitLab Endpoint: DELETE /projects/:id/external_status_checks/:check_id</code></pre> |
| 185 | + * |
| 186 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 187 | + * @param checkId ID of an external status check |
| 188 | + * @throws GitLabApiException if any exception occurs |
| 189 | + */ |
| 190 | + public void deleteExternalStatusCheck(Object projectIdOrPath, Long checkId) throws GitLabApiException { |
| 191 | + delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "external_status_checks", checkId); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Gets a list of all statuses of the external status checks for a given merge request. |
| 196 | + * |
| 197 | + * <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/status_checks</code></pre> |
| 198 | + * |
| 199 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 200 | + * @param mergeRequestIid the merge request IID to get the statuses |
| 201 | + * @return a List of ExternalStatusCheckStatus |
| 202 | + * @throws GitLabApiException if any exception occurs |
| 203 | + */ |
| 204 | + public List<ExternalStatusCheckStatus> getExternalStatusCheckStatuses(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException { |
| 205 | + return (getExternalStatusCheckStatuses(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).all()); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Gets a Pager of all statuses of the external status checks for a given merge request. |
| 210 | + * |
| 211 | + * <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/status_checks</code></pre> |
| 212 | + * |
| 213 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 214 | + * @param mergeRequestIid the merge request IID to get the statuses |
| 215 | + * @param itemsPerPage the number of ExternalStatusCheckStatus instances that will be fetched per page |
| 216 | + * @return the Pager of ExternalStatusCheckStatus instances |
| 217 | + * @throws GitLabApiException if any exception occurs |
| 218 | + */ |
| 219 | + public Pager<ExternalStatusCheckStatus> getExternalStatusCheckStatuses(Object projectIdOrPath, Long mergeRequestIid, int itemsPerPage) throws GitLabApiException { |
| 220 | + return (new Pager<ExternalStatusCheckStatus>(this, ExternalStatusCheckStatus.class, itemsPerPage, null, |
| 221 | + "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "status_checks")); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Gets a Stream of all statuses of the external status checks for a given merge request. |
| 226 | + * |
| 227 | + * <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid/status_checks</code></pre> |
| 228 | + * |
| 229 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 230 | + * @param mergeRequestIid the merge request IID to get the statuses |
| 231 | + * @return a Stream of ExternalStatusCheckStatus |
| 232 | + * @throws GitLabApiException if any exception occurs |
| 233 | + */ |
| 234 | + public Stream<ExternalStatusCheckStatus> getExternalStatusCheckStatusesStream(Object projectIdOrPath, Long mergeRequestIid) throws GitLabApiException { |
| 235 | + return (getExternalStatusCheckStatuses(projectIdOrPath, mergeRequestIid, getDefaultPerPage()).stream()); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Set the status of an external status check for a given merge request. |
| 240 | + * |
| 241 | + * <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests/:merge_request_iid/status_check_responses</code></pre> |
| 242 | + * |
| 243 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 244 | + * @param mergeRequestIid the merge request IID to get the statuses |
| 245 | + * @param sha the commit SHA to set the status for (required) |
| 246 | + * @param externalStatusCheckId ID of an external status check (required) |
| 247 | + * @param status the status to set (optional) |
| 248 | + * @return an ExternalStatusCheckResult instance containing info on the newly created status |
| 249 | + * @throws GitLabApiException if any exception occurs |
| 250 | + */ |
| 251 | + public ExternalStatusCheckResult setStatusOfExternalStatusCheck(Object projectIdOrPath, Long mergeRequestIid, String sha, Long externalStatusCheckId, Status status) throws GitLabApiException { |
| 252 | + Form formData = new GitLabApiForm() |
| 253 | + .withParam("sha", sha) |
| 254 | + .withParam("external_status_check_id", externalStatusCheckId) |
| 255 | + .withParam("status", status); |
| 256 | + Response response = post(Response.Status.CREATED, formData.asMap(), |
| 257 | + "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "status_check_responses"); |
| 258 | + return (response.readEntity(ExternalStatusCheckResult.class)); |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Retry the specified failed external status check for a single merge request. Even though the merge request hasn’t changed, this endpoint resends the current state of merge request to the defined external service. |
| 263 | + * |
| 264 | + * <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests/:merge_request_iid/status_checks/:external_status_check_id/retry</code></pre> |
| 265 | + * |
| 266 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 267 | + * @param mergeRequestIid the merge request IID to get the statuses |
| 268 | + * @param externalStatusCheckId ID of an external status check |
| 269 | + * @throws GitLabApiException if any exception occurs |
| 270 | + */ |
| 271 | + public void retryExternalStatusCheck(Object projectIdOrPath, Long mergeRequestIid, Long externalStatusCheckId) throws GitLabApiException { |
| 272 | + post(Response.Status.ACCEPTED, (Form)null, "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "status_checks", externalStatusCheckId, "retry"); |
| 273 | + } |
| 274 | + |
| 275 | +} |
0 commit comments