Skip to content

Commit 1084773

Browse files
PhilippeViennegmessner
authored andcommitted
Add support for CI masked variables (#388)
* Add support for masked variables #387 #388
1 parent 124d923 commit 1084773

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ public Optional<Variable> getOptionalVariable(Object projectIdOrPath, String key
26262626
* @throws GitLabApiException if any exception occurs during execution
26272627
*/
26282628
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
2629-
return (createVariable(projectIdOrPath, key, value, isProtected, null));
2629+
return (createVariable(projectIdOrPath, key, value, isProtected, (String) null));
26302630
}
26312631

26322632
/**
@@ -2645,11 +2645,51 @@ public Variable createVariable(Object projectIdOrPath, String key, String value,
26452645
* @throws GitLabApiException if any exception occurs during execution
26462646
*/
26472647
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
2648+
return createVariable(projectIdOrPath, key, value, isProtected, null, environmentScope);
2649+
}
2650+
2651+
/**
2652+
* Create a new project variable.
2653+
*
2654+
* <p>NOTE: Setting the environmentScope is only available on GitLab EE.</p>
2655+
*
2656+
* <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre>
2657+
*
2658+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
2659+
* @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
2660+
* @param value the value for the variable, required
2661+
* @param isProtected whether the variable is protected, optional
2662+
* @param isMasked whether the variable is masked, optional
2663+
* @return a Variable instance with the newly created variable
2664+
* @throws GitLabApiException if any exception occurs during execution
2665+
*/
2666+
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException {
2667+
return createVariable(projectIdOrPath, key, value, isProtected, isMasked, null);
2668+
}
2669+
2670+
/**
2671+
* Create a new project variable.
2672+
*
2673+
* <p>NOTE: Setting the environmentScope is only available on GitLab EE.</p>
2674+
*
2675+
* <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre>
2676+
*
2677+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
2678+
* @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
2679+
* @param value the value for the variable, required
2680+
* @param isProtected whether the variable is protected, optional
2681+
* @param isMasked whether the variable is masked, optional
2682+
* @param environmentScope the environment_scope of the variable, optional
2683+
* @return a Variable instance with the newly created variable
2684+
* @throws GitLabApiException if any exception occurs during execution
2685+
*/
2686+
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
26482687

26492688
GitLabApiForm formData = new GitLabApiForm()
26502689
.withParam("key", key, true)
26512690
.withParam("value", value, true)
26522691
.withParam("protected", isProtected)
2692+
.withParam("masked", isMasked)
26532693
.withParam("environment_scope", environmentScope);
26542694
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables");
26552695
return (response.readEntity(Variable.class));
@@ -2668,7 +2708,7 @@ public Variable createVariable(Object projectIdOrPath, String key, String value,
26682708
* @throws GitLabApiException if any exception occurs during execution
26692709
*/
26702710
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
2671-
return (updateVariable(projectIdOrPath, key, value, isProtected, null));
2711+
return (updateVariable(projectIdOrPath, key, value, isProtected, (String) null));
26722712
}
26732713

26742714
/**
@@ -2687,10 +2727,50 @@ public Variable updateVariable(Object projectIdOrPath, String key, String value,
26872727
* @throws GitLabApiException if any exception occurs during execution
26882728
*/
26892729
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
2730+
return updateVariable(projectIdOrPath, key, value, isProtected, null, environmentScope);
2731+
}
2732+
2733+
/**
2734+
* Update a project variable.
2735+
*
2736+
* <p>NOTE: Updating the environmentScope is only available on GitLab EE.</p>
2737+
*
2738+
* <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre>
2739+
*
2740+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
2741+
* @param key the key of an existing variable, required
2742+
* @param value the value for the variable, required
2743+
* @param isProtected whether the variable is protected, optional
2744+
* @param isMasked whether the variable is masked, optional
2745+
* @return a Variable instance with the updated variable
2746+
* @throws GitLabApiException if any exception occurs during execution
2747+
*/
2748+
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException {
2749+
return updateVariable(projectIdOrPath, key, value, isProtected, isMasked, null);
2750+
}
2751+
2752+
/**
2753+
* Update a project variable.
2754+
*
2755+
* <p>NOTE: Updating the environmentScope is only available on GitLab EE.</p>
2756+
*
2757+
* <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre>
2758+
*
2759+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
2760+
* @param key the key of an existing variable, required
2761+
* @param value the value for the variable, required
2762+
* @param isProtected whether the variable is protected, optional
2763+
* @param isMasked whether the variable is masked, optional
2764+
* @param environmentScope the environment_scope of the variable, optional.
2765+
* @return a Variable instance with the updated variable
2766+
* @throws GitLabApiException if any exception occurs during execution
2767+
*/
2768+
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
26902769

26912770
GitLabApiForm formData = new GitLabApiForm()
26922771
.withParam("value", value, true)
26932772
.withParam("protected", isProtected)
2773+
.withParam("masked", isMasked)
26942774
.withParam("environment_scope", environmentScope);
26952775
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
26962776
return (response.readEntity(Variable.class));

src/main/java/org/gitlab4j/api/models/Variable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class Variable {
1414
private String value;
1515
@JsonProperty("protected")
1616
private Boolean isProtected;
17+
@JsonProperty("masked")
18+
private Boolean isMasked;
1719
private String environmentScope;
1820

1921
public Variable() {
@@ -48,6 +50,14 @@ public void setProtected(Boolean isProtected) {
4850
this.isProtected = isProtected;
4951
}
5052

53+
public Boolean getMasked() {
54+
return isMasked;
55+
}
56+
57+
public void setMasked(Boolean masked) {
58+
isMasked = masked;
59+
}
60+
5161
public String getEnvironmentScope() {
5262
return environmentScope;
5363
}

src/test/java/org/gitlab4j/api/TestProjectApi.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ public void testVariables() throws GitLabApiException {
672672

673673
String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
674674
String value = "TEST_VARIABLE_VALUE_" + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
675-
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null);
675+
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, null);
676676

677677
assertNotNull(variable);
678678
assertEquals(key, variable.getKey());
@@ -696,6 +696,15 @@ public void testVariables() throws GitLabApiException {
696696
assertEquals("NONE", variable.getValue());
697697
assertTrue(variable.getProtected());
698698

699+
gitLabApi.getProjectApi().updateVariable(testProject, key, value, true, true, "DEV");
700+
variable = gitLabApi.getProjectApi().getVariable(testProject, key);
701+
702+
assertNotNull(variable);
703+
assertEquals(key, variable.getKey());
704+
assertEquals("NONE", variable.getValue());
705+
assertTrue(variable.getMasked());
706+
assertTrue(variable.getProtected());
707+
699708
gitLabApi.getProjectApi().deleteVariable(testProject, key);
700709
variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
701710
assertNotNull(variables);

0 commit comments

Comments
 (0)