From 145a27a07733471124bf563cfbc1d991ccffba7a Mon Sep 17 00:00:00 2001 From: Philippe Vienne Date: Fri, 14 Jun 2019 17:15:25 +0200 Subject: [PATCH 1/2] Add support for masked variables #387 #388 --- .../java/org/gitlab4j/api/ProjectApi.java | 84 ++++++++++++++++++- .../org/gitlab4j/api/models/Variable.java | 10 +++ .../java/org/gitlab4j/api/TestProjectApi.java | 12 ++- 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index aa5df1256..741adbafb 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -2626,7 +2626,7 @@ public Optional getOptionalVariable(Object projectIdOrPath, String key * @throws GitLabApiException if any exception occurs during execution */ public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException { - return (createVariable(projectIdOrPath, key, value, isProtected, null)); + return (createVariable(projectIdOrPath, key, value, isProtected, (String) null)); } /** @@ -2645,11 +2645,51 @@ public Variable createVariable(Object projectIdOrPath, String key, String value, * @throws GitLabApiException if any exception occurs during execution */ public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { + return createVariable(projectIdOrPath, key, value, isProtected, null, environmentScope); + } + + /** + * Create a new project variable. + * + *

NOTE: Setting the environmentScope is only available on GitLab EE.

+ * + *
GitLab Endpoint: POST /projects/:id/variables
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed, required + * @param value the value for the variable, required + * @param isProtected whether the variable is protected, optional + * @param isMasked whether the variable is masked, optional + * @return a Variable instance with the newly created variable + * @throws GitLabApiException if any exception occurs during execution + */ + public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException { + return createVariable(projectIdOrPath, key, value, isProtected, isMasked, null); + } + + /** + * Create a new project variable. + * + *

NOTE: Setting the environmentScope is only available on GitLab EE.

+ * + *
GitLab Endpoint: POST /projects/:id/variables
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed, required + * @param value the value for the variable, required + * @param isProtected whether the variable is protected, optional + * @param isMasked whether the variable is masked, optional + * @param environmentScope the environment_scope of the variable, optional + * @return a Variable instance with the newly created variable + * @throws GitLabApiException if any exception occurs during execution + */ + public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm() .withParam("key", key, true) .withParam("value", value, true) .withParam("protected", isProtected) + .withParam("masked", isMasked) .withParam("environment_scope", environmentScope); Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables"); return (response.readEntity(Variable.class)); @@ -2668,7 +2708,7 @@ public Variable createVariable(Object projectIdOrPath, String key, String value, * @throws GitLabApiException if any exception occurs during execution */ public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException { - return (updateVariable(projectIdOrPath, key, value, isProtected, null)); + return (updateVariable(projectIdOrPath, key, value, isProtected, (String) null)); } /** @@ -2687,10 +2727,50 @@ public Variable updateVariable(Object projectIdOrPath, String key, String value, * @throws GitLabApiException if any exception occurs during execution */ public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { + return updateVariable(projectIdOrPath, key, value, isProtected, null, environmentScope); + } + + /** + * Update a project variable. + * + *

NOTE: Updating the environmentScope is only available on GitLab EE.

+ * + *
GitLab Endpoint: PUT /projects/:id/variables/:key
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param key the key of an existing variable, required + * @param value the value for the variable, required + * @param isProtected whether the variable is protected, optional + * @param isMasked whether the variable is masked, optional + * @return a Variable instance with the updated variable + * @throws GitLabApiException if any exception occurs during execution + */ + public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException { + return updateVariable(projectIdOrPath, key, value, isProtected, isMasked, null); + } + + /** + * Update a project variable. + * + *

NOTE: Updating the environmentScope is only available on GitLab EE.

+ * + *
GitLab Endpoint: PUT /projects/:id/variables/:key
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param key the key of an existing variable, required + * @param value the value for the variable, required + * @param isProtected whether the variable is protected, optional + * @param isMasked whether the variable is masked, optional + * @param environmentScope the environment_scope of the variable, optional. + * @return a Variable instance with the updated variable + * @throws GitLabApiException if any exception occurs during execution + */ + public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm() .withParam("value", value, true) .withParam("protected", isProtected) + .withParam("masked", isMasked) .withParam("environment_scope", environmentScope); Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key); return (response.readEntity(Variable.class)); diff --git a/src/main/java/org/gitlab4j/api/models/Variable.java b/src/main/java/org/gitlab4j/api/models/Variable.java index c092b833c..89bbbf4b8 100644 --- a/src/main/java/org/gitlab4j/api/models/Variable.java +++ b/src/main/java/org/gitlab4j/api/models/Variable.java @@ -14,6 +14,8 @@ public class Variable { private String value; @JsonProperty("protected") private Boolean isProtected; + @JsonProperty("masked") + private Boolean isMasked; private String environmentScope; public Variable() { @@ -48,6 +50,14 @@ public void setProtected(Boolean isProtected) { this.isProtected = isProtected; } + public Boolean getMasked() { + return isMasked; + } + + public void setMasked(Boolean masked) { + isMasked = masked; + } + public String getEnvironmentScope() { return environmentScope; } diff --git a/src/test/java/org/gitlab4j/api/TestProjectApi.java b/src/test/java/org/gitlab4j/api/TestProjectApi.java index 2d340f414..cb59a4379 100644 --- a/src/test/java/org/gitlab4j/api/TestProjectApi.java +++ b/src/test/java/org/gitlab4j/api/TestProjectApi.java @@ -672,11 +672,12 @@ public void testVariables() throws GitLabApiException { String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String value = "TEST_VARIABLE_VALUE_" + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); - Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null); + Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, null); assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); + assertFalse(variable.getMasked()); Stream variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); @@ -696,6 +697,15 @@ public void testVariables() throws GitLabApiException { assertEquals("NONE", variable.getValue()); assertTrue(variable.getProtected()); + gitLabApi.getProjectApi().updateVariable(testProject, key, value, true, true, "DEV"); + variable = gitLabApi.getProjectApi().getVariable(testProject, key); + + assertNotNull(variable); + assertEquals(key, variable.getKey()); + assertEquals("NONE", variable.getValue()); + assertTrue(variable.getMasked()); + assertTrue(variable.getProtected()); + gitLabApi.getProjectApi().deleteVariable(testProject, key); variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables); From 89023aec43887e9cbe8fddabb9556d04b43e889c Mon Sep 17 00:00:00 2001 From: Philippe Vienne Date: Fri, 14 Jun 2019 17:44:24 +0200 Subject: [PATCH 2/2] [TEST] Removed because it is null value here #388 --- src/test/java/org/gitlab4j/api/TestProjectApi.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/gitlab4j/api/TestProjectApi.java b/src/test/java/org/gitlab4j/api/TestProjectApi.java index cb59a4379..76073e058 100644 --- a/src/test/java/org/gitlab4j/api/TestProjectApi.java +++ b/src/test/java/org/gitlab4j/api/TestProjectApi.java @@ -677,7 +677,6 @@ public void testVariables() throws GitLabApiException { assertNotNull(variable); assertEquals(key, variable.getKey()); assertEquals(value, variable.getValue()); - assertFalse(variable.getMasked()); Stream variables = gitLabApi.getProjectApi().getVariablesStream(testProject); assertNotNull(variables);