Commit db4d5bb3 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for project variable type (#390).

parent 8a793186
...@@ -2645,7 +2645,7 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2645,7 +2645,7 @@ public class ProjectApi extends AbstractApi implements Constants {
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
return createVariable(projectIdOrPath, key, value, isProtected, null, environmentScope); return createVariable(projectIdOrPath, key, value, null, isProtected, null, environmentScope);
} }
/** /**
...@@ -2658,13 +2658,15 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2658,13 +2658,15 @@ public class ProjectApi extends AbstractApi implements Constants {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required * @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 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 value the value for the variable, required
* @param variableType the type of variable. Available types are: env_var (default) and file
* @param isProtected whether the variable is protected, optional * @param isProtected whether the variable is protected, optional
* @param isMasked whether the variable is masked, optional * @param isMasked whether the variable is masked, optional
* @return a Variable instance with the newly created variable * @return a Variable instance with the newly created variable
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException { public Variable createVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
return createVariable(projectIdOrPath, key, value, isProtected, isMasked, null); Boolean isProtected, Boolean isMasked) throws GitLabApiException {
return createVariable(projectIdOrPath, key, value, variableType, isProtected, isMasked, null);
} }
/** /**
...@@ -2677,17 +2679,20 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2677,17 +2679,20 @@ public class ProjectApi extends AbstractApi implements Constants {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required * @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 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 value the value for the variable, required
* @param variableType the type of variable. Available types are: env_var (default) and file
* @param isProtected whether the variable is protected, optional * @param isProtected whether the variable is protected, optional
* @param isMasked whether the variable is masked, optional * @param isMasked whether the variable is masked, optional
* @param environmentScope the environment_scope of the variable, optional * @param environmentScope the environment_scope of the variable, optional
* @return a Variable instance with the newly created variable * @return a Variable instance with the newly created variable
* @throws GitLabApiException if any exception occurs during execution * @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 { public Variable createVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() GitLabApiForm formData = new GitLabApiForm()
.withParam("key", key, true) .withParam("key", key, true)
.withParam("value", value, true) .withParam("value", value, true)
.withParam("variable_type", variableType)
.withParam("protected", isProtected) .withParam("protected", isProtected)
.withParam("masked", isMasked) .withParam("masked", isMasked)
.withParam("environment_scope", environmentScope); .withParam("environment_scope", environmentScope);
...@@ -2708,7 +2713,7 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2708,7 +2713,7 @@ public class ProjectApi extends AbstractApi implements Constants {
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException { public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
return (updateVariable(projectIdOrPath, key, value, isProtected, (String) null)); return (updateVariable(projectIdOrPath, key, value, null, isProtected, null, null));
} }
/** /**
...@@ -2727,7 +2732,7 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2727,7 +2732,7 @@ public class ProjectApi extends AbstractApi implements Constants {
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
return updateVariable(projectIdOrPath, key, value, isProtected, null, environmentScope); return updateVariable(projectIdOrPath, key, value, null, isProtected, null, environmentScope);
} }
/** /**
...@@ -2740,13 +2745,15 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2740,13 +2745,15 @@ public class ProjectApi extends AbstractApi implements Constants {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required * @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 key the key of an existing variable, required
* @param value the value for the variable, required * @param value the value for the variable, required
* @param variableType the type of variable. Available types are: env_var (default) and file
* @param isProtected whether the variable is protected, optional * @param isProtected whether the variable is protected, optional
* @param isMasked whether the variable is masked, optional * @param masked whether the variable is masked, optional
* @return a Variable instance with the updated variable * @return a Variable instance with the updated variable
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked) throws GitLabApiException { public Variable updateVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
return updateVariable(projectIdOrPath, key, value, isProtected, isMasked, null); Boolean isProtected, Boolean masked) throws GitLabApiException {
return updateVariable(projectIdOrPath, key, value, variableType, isProtected, masked, null);
} }
/** /**
...@@ -2759,19 +2766,22 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2759,19 +2766,22 @@ public class ProjectApi extends AbstractApi implements Constants {
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required * @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 key the key of an existing variable, required
* @param value the value for the variable, required * @param value the value for the variable, required
* @param variableType the type of variable. Available types are: env_var (default) and file
* @param isProtected whether the variable is protected, optional * @param isProtected whether the variable is protected, optional
* @param isMasked whether the variable is masked, optional * @param masked whether the variable is masked, optional
* @param environmentScope the environment_scope of the variable, optional. * @param environmentScope the environment_scope of the variable, optional.
* @return a Variable instance with the updated variable * @return a Variable instance with the updated variable
* @throws GitLabApiException if any exception occurs during execution * @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 { public Variable updateVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
Boolean isProtected, Boolean masked, String environmentScope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm() GitLabApiForm formData = new GitLabApiForm()
.withParam("value", value, true) .withParam("value", value, true)
.withParam("variable_type", variableType)
.withParam("protected", isProtected) .withParam("protected", isProtected)
.withParam("masked", isMasked) .withParam("masked", masked)
.withParam("environment_scope", environmentScope); .withParam("environment_scope", environmentScope);
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key); Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
return (response.readEntity(Variable.class)); return (response.readEntity(Variable.class));
} }
......
...@@ -5,13 +5,42 @@ import java.util.List; ...@@ -5,13 +5,42 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
public class Variable { public class Variable {
/**
* Enum for the various Commit build status values.
*/
public enum Type {
ENV_VAR, FILE;
private static JacksonJsonEnumHelper<Type> enumHelper = new JacksonJsonEnumHelper<>(Type.class);
@JsonCreator
public static Type forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
private String key; private String key;
private String value; private String value;
private Type variableType;
@JsonProperty("protected") @JsonProperty("protected")
private Boolean isProtected; private Boolean isProtected;
@JsonProperty("masked") @JsonProperty("masked")
...@@ -42,6 +71,14 @@ public class Variable { ...@@ -42,6 +71,14 @@ public class Variable {
this.value = value; this.value = value;
} }
public Type getVariableType() {
return variableType;
}
public void setVariableType(Type variableType) {
this.variableType = variableType;
}
public Boolean getProtected() { public Boolean getProtected() {
return isProtected; return isProtected;
} }
...@@ -55,7 +92,7 @@ public class Variable { ...@@ -55,7 +92,7 @@ public class Variable {
} }
public void setMasked(Boolean masked) { public void setMasked(Boolean masked) {
isMasked = masked; this.isMasked = masked;
} }
public String getEnvironmentScope() { public String getEnvironmentScope() {
......
...@@ -103,15 +103,15 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -103,15 +103,15 @@ public class TestProjectApi extends AbstractIntegrationTest {
testProject = getTestProject(); testProject = getTestProject();
currentUser = getCurrentUser(); currentUser = getCurrentUser();
deleteAllTestProjects(); deleteAllTransientTestData();
} }
@AfterClass @AfterClass
public static void teardown() throws GitLabApiException { public static void teardown() throws GitLabApiException {
deleteAllTestProjects(); deleteAllTransientTestData();
} }
private static void deleteAllTestProjects() { private static void deleteAllTransientTestData() {
if (gitLabApi == null) { if (gitLabApi == null) {
return; return;
...@@ -147,6 +147,7 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -147,6 +147,7 @@ public class TestProjectApi extends AbstractIntegrationTest {
for (Variable variable : variables) { for (Variable variable : variables) {
if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) { if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) {
gitLabApi.getProjectApi().updateVariable(testProject, variable.getKey(), "EMPTY", false);
gitLabApi.getProjectApi().deleteVariable(testProject, variable.getKey()); gitLabApi.getProjectApi().deleteVariable(testProject, variable.getKey());
} }
} }
...@@ -671,8 +672,8 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -671,8 +672,8 @@ public class TestProjectApi extends AbstractIntegrationTest {
assumeNotNull(testProject); assumeNotNull(testProject);
String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
String value = "TEST_VARIABLE_VALUE_" + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt(); String value = "ABCDEFG12345678" + HelperUtils.getRandomInt();
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, null); Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, true);
assertNotNull(variable); assertNotNull(variable);
assertEquals(key, variable.getKey()); assertEquals(key, variable.getKey());
...@@ -696,14 +697,14 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -696,14 +697,14 @@ public class TestProjectApi extends AbstractIntegrationTest {
assertEquals("NONE", variable.getValue()); assertEquals("NONE", variable.getValue());
assertTrue(variable.getProtected()); assertTrue(variable.getProtected());
gitLabApi.getProjectApi().updateVariable(testProject, key, value, true, true, "DEV"); gitLabApi.getProjectApi().updateVariable(testProject, key, value, Variable.Type.ENV_VAR, false, true, "DEV");
variable = gitLabApi.getProjectApi().getVariable(testProject, key); variable = gitLabApi.getProjectApi().getVariable(testProject, key);
assertNotNull(variable); assertNotNull(variable);
assertEquals(key, variable.getKey()); assertEquals(key, variable.getKey());
assertEquals("NONE", variable.getValue()); assertEquals(value, variable.getValue());
assertTrue(variable.getMasked()); assertEquals(Variable.Type.ENV_VAR, variable.getVariableType());
assertTrue(variable.getProtected()); assertFalse(variable.getProtected());
gitLabApi.getProjectApi().deleteVariable(testProject, key); gitLabApi.getProjectApi().deleteVariable(testProject, key);
variables = gitLabApi.getProjectApi().getVariablesStream(testProject); variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
...@@ -713,6 +714,28 @@ public class TestProjectApi extends AbstractIntegrationTest { ...@@ -713,6 +714,28 @@ public class TestProjectApi extends AbstractIntegrationTest {
assertNull(matchingVariable); assertNull(matchingVariable);
} }
@Test
public void testFileVariable() throws GitLabApiException {
assumeNotNull(testProject);
String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
String value = "/tmp/test.txt";
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, Variable.Type.FILE, null, false);
assertNotNull(variable);
assertEquals(key, variable.getKey());
assertEquals(value, variable.getValue());
assertEquals(Variable.Type.FILE, variable.getVariableType());
gitLabApi.getProjectApi().deleteVariable(testProject, key);
Stream<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
assertNotNull(variables);
Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null);
assertNull(matchingVariable);
}
@Test @Test
public void testGetMembers() throws GitLabApiException { public void testGetMembers() throws GitLabApiException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment