From 6d826567bef7ac679083277c387671c2a7f09d3a Mon Sep 17 00:00:00 2001 From: Mike Cook Date: Sat, 8 Oct 2022 00:35:40 -0600 Subject: [PATCH 1/4] Add method to set code_owner_approval_required flag --- .gitignore | 5 ++- .../java/org/gitlab4j/api/AbstractApi.java | 36 +++++++++++++++++++ .../org/gitlab4j/api/GitLabApiClient.java | 29 +++++++++++++++ .../gitlab4j/api/ProtectedBranchesApi.java | 23 ++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 18dd4401d..ca2dad8a1 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,9 @@ local.properties .classpath .settings/ .loadpath + +### Visual Studio Code ### +.vscode # External tool builders .externalToolBuilders/ @@ -45,7 +48,7 @@ target/ # Test properties file for gitlab4j test-gitlab4j.properties -!src/test/resoures/test-gitlab4j.properties +!src/test/resources/test-gitlab4j.properties # git-changelog plugin # .okhttpcache diff --git a/src/main/java/org/gitlab4j/api/AbstractApi.java b/src/main/java/org/gitlab4j/api/AbstractApi.java index 3fd19ddd5..6381549f1 100644 --- a/src/main/java/org/gitlab4j/api/AbstractApi.java +++ b/src/main/java/org/gitlab4j/api/AbstractApi.java @@ -272,6 +272,42 @@ protected Response head(Response.Status expectedStatus, MultivaluedMap queryParams, Object... pathArgs) throws GitLabApiException { + try { + return validate(getApiClient().patch(queryParams, pathArgs), expectedStatus); + } catch (Exception e) { + throw handle(e); + } + } + + /** + * Perform an HTTP PATCH call with the specified query parameters and URL, returning + * a ClientResponse instance with the data returned from the endpoint. + * + * @param expectedStatus the HTTP status that should be returned from the server + * @param queryParams multivalue map of request parameters + * @param url the fully formed path to the GitLab API endpoint + * @return a ClientResponse instance with the data returned from the endpoint + * @throws GitLabApiException if any exception occurs during execution + */ + protected Response patch(Response.Status expectedStatus, MultivaluedMap queryParams, URL url) throws GitLabApiException { + try { + return validate(getApiClient().patch(queryParams, url), expectedStatus); + } catch (Exception e) { + throw handle(e); + } + } + /** * Perform an HTTP POST call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index 3322e8ba1..8fe642294 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -470,6 +470,35 @@ protected Response head(MultivaluedMap queryParams, URL url) { return (invocation(url, queryParams).head()); } + /** + * Perform an HTTP PATCH call with the specified query parameters and path objects, returning + * a ClientResponse instance with the data returned from the endpoint. + * + * @param queryParams multivalue map of request parameters + * @param pathArgs variable list of arguments used to build the URI + * @return a ClientResponse instance with the data returned from the endpoint + * @throws IOException if an error occurs while constructing the URL + */ + protected Response patch(MultivaluedMap queryParams, Object... pathArgs) throws IOException { + URL url = getApiUrl(pathArgs); + return (patch(queryParams, url)); + } + + /** + * Perform an HTTP PATCH call with the specified query parameters and URL, returning + * a ClientResponse instance with the data returned from the endpoint. + * + * @param queryParams multivalue map of request parameters + * @param url the fully formed path to the GitLab API endpoint + * @return a ClientResponse instance with the data returned from the endpoint + */ + protected Response patch(MultivaluedMap queryParams, URL url) { + Entity empty = Entity.text(""); + // use "X-HTTP-Method-Override" header on POST to override to unsupported PATCH + return (invocation(url, queryParams) + .header("X-HTTP-Method-Override", "PATCH").post(empty)); + } + /** * Perform an HTTP POST call with the specified form data and path objects, returning * a ClientResponse instance with the data returned from the endpoint. diff --git a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java index c64f416f3..8c2664ad7 100644 --- a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java +++ b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java @@ -233,4 +233,27 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, "projects", getProjectIdOrPath(projectIdOrPath), "protected_branches"); return (response.readEntity(ProtectedBranch.class)); } + + /** + * Sets the code_owner_approval_required flag on the specified protected branch. + * + *

NOTE: This method is only available in GitLab Premium or higher.

+ * + *
GitLab Endpoint: PATCH /projects/:id/protected_branches/:branch_name?code_owner_approval_required=true
+ * + * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance + * @param branchName the name of the branch to protect, can be a wildcard + * @param codeOwnerApprovalRequired prevent pushes to this branch if it matches an item in the CODEOWNERS file. (defaults: false) + * @return the branch info for the protected branch + * @throws GitLabApiException if any exception occurs + */ + public ProtectedBranch setCodeOwnerApprovalRequired(Object projectIdOrPath, String branchName, + Boolean codeOwnerApprovalRequired) throws GitLabApiException { + Form formData = new GitLabApiForm() + .withParam("code_owner_approval_required", codeOwnerApprovalRequired); + + Response response = patch(Response.Status.OK, formData.asMap(), + "projects", this.getProjectIdOrPath(projectIdOrPath), "protected_branches", urlEncode(branchName)); + return (response.readEntity(ProtectedBranch.class)); + } } From 0d81a3f18035fa44c50a3daa112a3930a809454b Mon Sep 17 00:00:00 2001 From: Mike Cook Date: Sun, 9 Oct 2022 18:34:06 -0600 Subject: [PATCH 2/4] Add test to exercise call without support --- .../gitlab4j/api/ProtectedBranchesApi.java | 2 +- .../api/TestProtectedBranchesApi.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java index 8c2664ad7..41c5867db 100644 --- a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java +++ b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java @@ -243,7 +243,7 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, * * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance * @param branchName the name of the branch to protect, can be a wildcard - * @param codeOwnerApprovalRequired prevent pushes to this branch if it matches an item in the CODEOWNERS file. (defaults: false) + * @param codeOwnerApprovalRequired prevent pushes to this branch if it matches an item in the CODEOWNERS file. * @return the branch info for the protected branch * @throws GitLabApiException if any exception occurs */ diff --git a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java index 842fad9a7..9a647cd5f 100644 --- a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java +++ b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java @@ -1,7 +1,9 @@ package org.gitlab4j.api; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -124,4 +126,21 @@ public void testProtectBranch() throws GitLabApiException { assertTrue(branches.stream() .anyMatch((protectedBranch) -> protectedBranch.getName().equals(TEST_BRANCH_NAME))); } + + @Test + public void testSetCodeOwnerApprovalRequired() throws GitLabApiException { + + assumeTrue(testProject != null); + + ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().getProtectedBranch(testProject, TEST_BRANCH_NAME); + assertNotNull(branch); + // current version returns null, but will return boolean (false) with newer Premium + assertFalse(branch.getCodeOwnerApprovalRequired() != null); + + // current version returns 404, but will return branch with "code_owner_approval_required = true" with newer Premium + GitLabApiException gae = assertThrowsExactly(GitLabApiException.class, + () -> gitLabApi.getProtectedBranchesApi().setCodeOwnerApprovalRequired(testProject, TEST_BRANCH_NAME, true) + ); + assertTrue(gae.getHttpStatus() == 404); + } } From 5b7601358d18b2c74a4a2473aec698273ec9ffd6 Mon Sep 17 00:00:00 2001 From: Mike Cook Date: Sun, 9 Oct 2022 19:24:23 -0600 Subject: [PATCH 3/4] Tweak formatting to match current style --- .../gitlab4j/api/ProtectedBranchesApi.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java index 41c5867db..acdd62d78 100644 --- a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java +++ b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java @@ -187,7 +187,7 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, Integer allowedToPushUserId, Integer allowedToMergeUserId, Integer allowedToUnprotectUserId, Boolean codeOwnerApprovalRequired) throws GitLabApiException { - Form formData = new GitLabApiForm() + Form formData = new GitLabApiForm() .withParam("name", branchName, true) .withParam("allowed_to_push[][user_id]", allowedToPushUserId) .withParam("allowed_to_merge[][user_id]", allowedToMergeUserId) @@ -222,14 +222,14 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, .withParam("name", branchName, true) .withParam("code_owner_approval_required", codeOwnerApprovalRequired); - if (allowedToPush != null) - allowedToPush.getForm(formData, "allowed_to_push"); - if (allowedToMerge != null) - allowedToMerge.getForm(formData, "allowed_to_merge"); - if (allowedToUnprotect != null) - allowedToUnprotect.getForm(formData, "allowed_to_unprotect"); + if (allowedToPush != null) + allowedToPush.getForm(formData, "allowed_to_push"); + if (allowedToMerge != null) + allowedToMerge.getForm(formData, "allowed_to_merge"); + if (allowedToUnprotect != null) + allowedToUnprotect.getForm(formData, "allowed_to_unprotect"); - Response response = post(Response.Status.CREATED, formData.asMap(), + Response response = post(Response.Status.CREATED, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "protected_branches"); return (response.readEntity(ProtectedBranch.class)); } @@ -249,11 +249,12 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, */ public ProtectedBranch setCodeOwnerApprovalRequired(Object projectIdOrPath, String branchName, Boolean codeOwnerApprovalRequired) throws GitLabApiException { - Form formData = new GitLabApiForm() + Form formData = new GitLabApiForm() .withParam("code_owner_approval_required", codeOwnerApprovalRequired); Response response = patch(Response.Status.OK, formData.asMap(), - "projects", this.getProjectIdOrPath(projectIdOrPath), "protected_branches", urlEncode(branchName)); + "projects", this.getProjectIdOrPath(projectIdOrPath), + "protected_branches", urlEncode(branchName)); return (response.readEntity(ProtectedBranch.class)); } } From 24b58c74b08c76f816f7307a2810fc8f27c245a7 Mon Sep 17 00:00:00 2001 From: Mike Cook Date: Sun, 9 Oct 2022 19:31:30 -0600 Subject: [PATCH 4/4] A bit more reformatting --- .../java/org/gitlab4j/api/GitLabApiClient.java | 8 ++++---- .../org/gitlab4j/api/ProtectedBranchesApi.java | 14 +++++++------- .../org/gitlab4j/api/TestProtectedBranchesApi.java | 5 ++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index 8fe642294..169e185ce 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -287,8 +287,8 @@ void enableRequestResponseLogging(Logger logger, Level level, int maxEntityLengt * @param readTimeout the per request read timeout in milliseconds, can be null to use default */ void setRequestTimeout(Integer connectTimeout, Integer readTimeout) { - this.connectTimeout = connectTimeout; - this.readTimeout = readTimeout; + this.connectTimeout = connectTimeout; + this.readTimeout = readTimeout; } /** @@ -496,7 +496,7 @@ protected Response patch(MultivaluedMap queryParams, URL url) { Entity empty = Entity.text(""); // use "X-HTTP-Method-Override" header on POST to override to unsupported PATCH return (invocation(url, queryParams) - .header("X-HTTP-Method-Override", "PATCH").post(empty)); + .header("X-HTTP-Method-Override", "PATCH").post(empty)); } /** @@ -947,7 +947,7 @@ public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngi // Ignore differences between given hostname and certificate hostname HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override - public boolean verify(String hostname, SSLSession session) { + public boolean verify(String hostname, SSLSession session) { return true; } }; diff --git a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java index acdd62d78..c018ff7a6 100644 --- a/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java +++ b/src/main/java/org/gitlab4j/api/ProtectedBranchesApi.java @@ -154,8 +154,8 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, * @throws GitLabApiException if any exception occurs */ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, - AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel, AccessLevel unprotectAccessLevel, - Boolean codeOwnerApprovalRequired) throws GitLabApiException { + AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel, AccessLevel unprotectAccessLevel, + Boolean codeOwnerApprovalRequired) throws GitLabApiException { Form formData = new GitLabApiForm() .withParam("name", branchName, true) .withParam("push_access_level", pushAccessLevel) @@ -184,8 +184,8 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, * @throws GitLabApiException if any exception occurs */ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, - Integer allowedToPushUserId, Integer allowedToMergeUserId, Integer allowedToUnprotectUserId, - Boolean codeOwnerApprovalRequired) throws GitLabApiException { + Integer allowedToPushUserId, Integer allowedToMergeUserId, Integer allowedToUnprotectUserId, + Boolean codeOwnerApprovalRequired) throws GitLabApiException { Form formData = new GitLabApiForm() .withParam("name", branchName, true) @@ -215,8 +215,8 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, * @throws GitLabApiException if any exception occurs */ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, - AllowedTo allowedToPush, AllowedTo allowedToMerge, AllowedTo allowedToUnprotect, - Boolean codeOwnerApprovalRequired) throws GitLabApiException { + AllowedTo allowedToPush, AllowedTo allowedToMerge, AllowedTo allowedToUnprotect, + Boolean codeOwnerApprovalRequired) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm() .withParam("name", branchName, true) @@ -248,7 +248,7 @@ public ProtectedBranch protectBranch(Object projectIdOrPath, String branchName, * @throws GitLabApiException if any exception occurs */ public ProtectedBranch setCodeOwnerApprovalRequired(Object projectIdOrPath, String branchName, - Boolean codeOwnerApprovalRequired) throws GitLabApiException { + Boolean codeOwnerApprovalRequired) throws GitLabApiException { Form formData = new GitLabApiForm() .withParam("code_owner_approval_required", codeOwnerApprovalRequired); diff --git a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java index 9a647cd5f..7b0b480a2 100644 --- a/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java +++ b/src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java @@ -47,7 +47,7 @@ public class TestProtectedBranchesApi extends AbstractIntegrationTest { @BeforeAll public static void setup() { - // Must setup the connection to the GitLab test server and get the test Project instance + // Must setup the connection to the GitLab test server and get the test Project instance gitLabApi = baseTestSetup(); testProject = getTestProject(); } @@ -139,8 +139,7 @@ public void testSetCodeOwnerApprovalRequired() throws GitLabApiException { // current version returns 404, but will return branch with "code_owner_approval_required = true" with newer Premium GitLabApiException gae = assertThrowsExactly(GitLabApiException.class, - () -> gitLabApi.getProtectedBranchesApi().setCodeOwnerApprovalRequired(testProject, TEST_BRANCH_NAME, true) - ); + () -> gitLabApi.getProtectedBranchesApi().setCodeOwnerApprovalRequired(testProject, TEST_BRANCH_NAME, true)); assertTrue(gae.getHttpStatus() == 404); } }