Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions src/main/java/org/gitlab4j/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ public Optional<Variable> 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));
}

/**
Expand All @@ -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.
*
* <p>NOTE: Setting the environmentScope is only available on GitLab EE.</p>
*
* <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre>
*
* @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.
*
* <p>NOTE: Setting the environmentScope is only available on GitLab EE.</p>
*
* <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre>
*
* @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));
Expand All @@ -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));
}

/**
Expand All @@ -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.
*
* <p>NOTE: Updating the environmentScope is only available on GitLab EE.</p>
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre>
*
* @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.
*
* <p>NOTE: Updating the environmentScope is only available on GitLab EE.</p>
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre>
*
* @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));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Variable {
private String value;
@JsonProperty("protected")
private Boolean isProtected;
@JsonProperty("masked")
private Boolean isMasked;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PhilippeVienne
No need to name this isMasked, just name it masked and remove the @JsonProperty("masked") line.

The reason isProtected was used is because protected is a reserved keyword in Java.

private String environmentScope;

public Variable() {
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/org/gitlab4j/api/TestProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ 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());
Expand All @@ -696,6 +696,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);
Expand Down