|
29 | 29 | import java.util.Date; |
30 | 30 | import java.util.List; |
31 | 31 | import java.util.Map; |
| 32 | +import java.util.Objects; |
32 | 33 | import java.util.Optional; |
33 | 34 | import java.util.stream.Stream; |
34 | 35 |
|
|
44 | 45 | import org.gitlab4j.api.models.ApprovalRuleParams; |
45 | 46 | import org.gitlab4j.api.models.AuditEvent; |
46 | 47 | import org.gitlab4j.api.models.Badge; |
| 48 | +import org.gitlab4j.api.models.CustomAttribute; |
47 | 49 | import org.gitlab4j.api.models.Event; |
48 | 50 | import org.gitlab4j.api.models.FileUpload; |
49 | 51 | import org.gitlab4j.api.models.Issue; |
|
69 | 71 | * @see <a href="https://docs.gitlab.com/ce/api/members.html">Group and project members API at GitLab</a> |
70 | 72 | * @see <a href="https://docs.gitlab.com/ce/api/access_requests.html#group-and-project-access-requests-api">Group and project access requests API</a> |
71 | 73 | * @see <a href="https://docs.gitlab.com/ee/api/project_badges.html">Project badges API</a> |
72 | | - * @see <a href="https://docs.gitlab.com/ce/api/merge_request_approvals.html"> |
73 | | - * @see <a href="https://docs.gitlab.com/ee/api/audit_events.html#retrieve-all-project-audit-events">Project audit events API</a> |
74 | | - * Merge request approvals API (Project-level) at GitLab</a> |
| 74 | + * @see <a href="https://docs.gitlab.com/ce/api/merge_request_approvals.html"> * Merge request approvals API (Project-level) at GitLab</a> |
| 75 | + * @see <a href="https://docs.gitlab.com/ce/api/audit_events.html#retrieve-all-project-audit-events">Project audit events API</a> |
| 76 | + * @see <a href="https://docs.gitlab.com/ce/api/custom_attributes.html">Custom Attributes API</a> |
75 | 77 | */ |
76 | 78 | public class ProjectApi extends AbstractApi implements Constants { |
77 | 79 |
|
@@ -103,9 +105,8 @@ public ProjectFetches getProjectStatistics(Object projectIdOrPath) throws GitLab |
103 | 105 | * |
104 | 106 | * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
105 | 107 | * @return an Optional instance with the value for the project fetch statistics for the last 30 day |
106 | | - * @throws GitLabApiException if any exception occurs during execution |
107 | 108 | */ |
108 | | - public Optional<ProjectFetches> getOptionalProjectStatistics(Object projectIdOrPath) throws GitLabApiException { |
| 109 | + public Optional<ProjectFetches> getOptionalProjectStatistics(Object projectIdOrPath) { |
109 | 110 | try { |
110 | 111 | return (Optional.ofNullable(getProjectStatistics(projectIdOrPath))); |
111 | 112 | } catch (GitLabApiException glae) { |
@@ -3446,4 +3447,123 @@ public void deleteApprovalRule(Object projectIdOrPath, Integer approvalRuleId) t |
3446 | 3447 |
|
3447 | 3448 | delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "approval_rules", approvalRuleId); |
3448 | 3449 | } |
| 3450 | + |
| 3451 | + /** |
| 3452 | + * Get all custom attributes for the specified project. |
| 3453 | + * |
| 3454 | + * <pre><code>GitLab Endpoint: GET /projects/:id/custom_attributes</code></pre> |
| 3455 | + * |
| 3456 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3457 | + * @return a list of project's CustomAttributes |
| 3458 | + * @throws GitLabApiException if any exception occurs |
| 3459 | + */ |
| 3460 | + public List<CustomAttribute> getCustomAttributes(final Object projectIdOrPath) throws GitLabApiException { |
| 3461 | + return (getCustomAttributes(projectIdOrPath, getDefaultPerPage()).all()); |
| 3462 | + } |
| 3463 | + |
| 3464 | + /** |
| 3465 | + * Get a Pager of custom attributes for the specified project. |
| 3466 | + * |
| 3467 | + * <pre><code>GitLab Endpoint: GET /projects/:id/custom_attributes</code></pre> |
| 3468 | + * |
| 3469 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3470 | + * @param itemsPerPage the number of items per page |
| 3471 | + * @return a Pager of project's custom attributes |
| 3472 | + * @throws GitLabApiException if any exception occurs |
| 3473 | + */ |
| 3474 | + public Pager<CustomAttribute> getCustomAttributes(final Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 3475 | + return (new Pager<CustomAttribute>(this, CustomAttribute.class, itemsPerPage, null, |
| 3476 | + "projects", getProjectIdOrPath(projectIdOrPath), "custom_attributes")); |
| 3477 | + } |
| 3478 | + |
| 3479 | + /** |
| 3480 | + * Get a Stream of all custom attributes for the specified project. |
| 3481 | + * |
| 3482 | + * <pre><code>GitLab Endpoint: GET /projects/:id/custom_attributes</code></pre> |
| 3483 | + * |
| 3484 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3485 | + * @return a Stream of project's custom attributes |
| 3486 | + * @throws GitLabApiException if any exception occurs |
| 3487 | + */ |
| 3488 | + public Stream<CustomAttribute> getCustomAttributesStream(final Object projectIdOrPath) throws GitLabApiException { |
| 3489 | + return (getCustomAttributes(projectIdOrPath, getDefaultPerPage()).stream()); |
| 3490 | + } |
| 3491 | + |
| 3492 | + /** |
| 3493 | + * Get a single custom attribute for the specified project. |
| 3494 | + * |
| 3495 | + * <pre><code>GitLab Endpoint: GET /projects/:id/custom_attributes/:key</code></pre> |
| 3496 | + * |
| 3497 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3498 | + * @param key the key for the custom attribute |
| 3499 | + * @return a CustomAttribute instance for the specified key |
| 3500 | + * @throws GitLabApiException if any exception occurs |
| 3501 | + */ |
| 3502 | + public CustomAttribute getCustomAttribute(final Object projectIdOrPath, final String key) throws GitLabApiException { |
| 3503 | + Response response = get(Response.Status.OK, null, |
| 3504 | + "projects", getProjectIdOrPath(projectIdOrPath), "custom_attributes", key); |
| 3505 | + return (response.readEntity(CustomAttribute.class)); |
| 3506 | + } |
| 3507 | + |
| 3508 | + /** |
| 3509 | + * Get an Optional instance with the value for a single custom attribute for the specified project. |
| 3510 | + * |
| 3511 | + * <pre><code>GitLab Endpoint: GET /projects/:id/custom_attributes/:key</code></pre> |
| 3512 | + * |
| 3513 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 3514 | + * @param key the key for the custom attribute, required |
| 3515 | + * @return an Optional instance with the value for a single custom attribute for the specified project |
| 3516 | + */ |
| 3517 | + public Optional<CustomAttribute> geOptionalCustomAttribute(final Object projectIdOrPath, final String key) { |
| 3518 | + try { |
| 3519 | + return (Optional.ofNullable(getCustomAttribute(projectIdOrPath, key))); |
| 3520 | + } catch (GitLabApiException glae) { |
| 3521 | + return (GitLabApi.createOptionalFromException(glae)); |
| 3522 | + } |
| 3523 | + } |
| 3524 | + |
| 3525 | + /** |
| 3526 | + * Set a custom attribute for the specified project. The attribute will be updated if it already exists, |
| 3527 | + * or newly created otherwise. |
| 3528 | + * |
| 3529 | + * <pre><code>GitLab Endpoint: PUT /projects/:id/custom_attributes/:key</code></pre> |
| 3530 | + * |
| 3531 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3532 | + * @param key the key for the custom attribute |
| 3533 | + * @param value the value for the customAttribute |
| 3534 | + * @return a CustomAttribute instance for the updated or created custom attribute |
| 3535 | + * @throws GitLabApiException if any exception occurs |
| 3536 | + */ |
| 3537 | + public CustomAttribute setCustomAttribute(final Object projectIdOrPath, final String key, final String value) throws GitLabApiException { |
| 3538 | + |
| 3539 | + if (Objects.isNull(key) || key.trim().isEmpty()) { |
| 3540 | + throw new IllegalArgumentException("Key cannot be null or empty"); |
| 3541 | + } |
| 3542 | + if (Objects.isNull(value) || value.trim().isEmpty()) { |
| 3543 | + throw new IllegalArgumentException("Value cannot be null or empty"); |
| 3544 | + } |
| 3545 | + |
| 3546 | + GitLabApiForm formData = new GitLabApiForm().withParam("value", value); |
| 3547 | + Response response = putWithFormData(Response.Status.OK, formData, |
| 3548 | + "projects", getProjectIdOrPath(projectIdOrPath), "custom_attributes", key); |
| 3549 | + return (response.readEntity(CustomAttribute.class)); |
| 3550 | + } |
| 3551 | + |
| 3552 | + /** |
| 3553 | + * Delete a custom attribute for the specified project. |
| 3554 | + * |
| 3555 | + * <pre><code>GitLab Endpoint: DELETE /projects/:id/custom_attributes/:key</code></pre> |
| 3556 | + * |
| 3557 | + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance |
| 3558 | + * @param key the key of the custom attribute to delete |
| 3559 | + * @throws GitLabApiException if any exception occurs |
| 3560 | + */ |
| 3561 | + public void deleteCustomAttribute(final Object projectIdOrPath, final String key) throws GitLabApiException { |
| 3562 | + |
| 3563 | + if (Objects.isNull(key) || key.trim().isEmpty()) { |
| 3564 | + throw new IllegalArgumentException("Key can't be null or empty"); |
| 3565 | + } |
| 3566 | + |
| 3567 | + delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "custom_attributes", key); |
| 3568 | + } |
3449 | 3569 | } |
0 commit comments