|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import org.gitlab4j.api.models.AccessLevel; |
| 4 | +import org.gitlab4j.api.models.ProtectedBranch; |
| 5 | + |
| 6 | +import javax.ws.rs.core.Form; |
| 7 | +import javax.ws.rs.core.GenericType; |
| 8 | +import javax.ws.rs.core.Response; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class ProtectedBranchesApi extends AbstractApi { |
| 12 | + public ProtectedBranchesApi(GitLabApi gitLabApi) { |
| 13 | + super(gitLabApi); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Gets a list of protected branches from a project. |
| 18 | + * |
| 19 | + * GET /projects/:id/protected_branches |
| 20 | + * |
| 21 | + * @param projectId the ID of the project to protect |
| 22 | + * @return the list of protected branches for the project |
| 23 | + * @throws GitLabApiException if any exception occurs |
| 24 | + */ |
| 25 | + public List<ProtectedBranch> getProtectedBranches(Integer projectId) throws GitLabApiException { |
| 26 | + Response response = get(Response.Status.OK, null, "projects", projectId, "protected_branches"); |
| 27 | + return (response.readEntity(new GenericType<List<ProtectedBranch>>() { |
| 28 | + })); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Unprotects the given protected branch or wildcard protected branch. |
| 33 | + * |
| 34 | + * DELETE /projects/:id/protected_branches/:name |
| 35 | + * |
| 36 | + * @param projectId the ID of the project to un-protect |
| 37 | + * @param branchName the name of the branch to un-protect |
| 38 | + * @throws GitLabApiException if any exception occurs |
| 39 | + */ |
| 40 | + public void unprotectBranch(Integer projectId, String branchName) throws GitLabApiException { |
| 41 | + delete(Response.Status.NO_CONTENT, null, "projects", projectId, "protected_branches", urlEncode(branchName)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Protects a single repository branch or several project repository branches using a wildcard protected branch. |
| 46 | + * |
| 47 | + * POST /projects/:id/protected_branches |
| 48 | + * |
| 49 | + * @param projectId the ID of the project to protect |
| 50 | + * @param branchName the name of the branch to protect |
| 51 | + * @return the branch info for the protected branch |
| 52 | + * @throws GitLabApiException if any exception occurs |
| 53 | + */ |
| 54 | + public ProtectedBranch protectBranch(Integer projectId, String branchName) throws GitLabApiException { |
| 55 | + return protectBranch(projectId, branchName, AccessLevel.MASTER, AccessLevel.MASTER); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Protects a single repository branch or several project repository branches using a wildcard protected branch. |
| 60 | + * |
| 61 | + * POST /projects/:id/protected_branches |
| 62 | + * |
| 63 | + * @param projectId the ID of the project to protect |
| 64 | + * @param branchName the name of the branch to protect |
| 65 | + * @param pushAccessLevel Access levels allowed to push (defaults: 40, master access level) |
| 66 | + * @param mergeAccessLevel Access levels allowed to merge (defaults: 40, master access level) |
| 67 | + * @return the branch info for the protected branch |
| 68 | + * @throws GitLabApiException if any exception occurs |
| 69 | + */ |
| 70 | + public ProtectedBranch protectBranch(Integer projectId, String branchName, AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel) throws GitLabApiException { |
| 71 | + Form formData = new GitLabApiForm() |
| 72 | + .withParam("id", projectId, true) |
| 73 | + .withParam("name", branchName, true) |
| 74 | + .withParam("push_access_level", pushAccessLevel.toValue(), false) |
| 75 | + .withParam("merge_access_level", mergeAccessLevel.toValue(), false); |
| 76 | + Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "protected_branches"); |
| 77 | + return (response.readEntity(ProtectedBranch.class)); |
| 78 | + } |
| 79 | +} |
0 commit comments