Skip to content

Commit db4d5bb

Browse files
committed
Added support for project variable type (#390).
1 parent 8a79318 commit db4d5bb

File tree

3 files changed

+93
-23
lines changed

3 files changed

+93
-23
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,7 +2645,7 @@ 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);
2648+
return createVariable(projectIdOrPath, key, value, null, isProtected, null, environmentScope);
26492649
}
26502650

26512651
/**
@@ -2658,13 +2658,15 @@ public Variable createVariable(Object projectIdOrPath, String key, String value,
26582658
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
26592659
* @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
26602660
* @param value the value for the variable, required
2661+
* @param variableType the type of variable. Available types are: env_var (default) and file
26612662
* @param isProtected whether the variable is protected, optional
26622663
* @param isMasked whether the variable is masked, optional
26632664
* @return a Variable instance with the newly created variable
26642665
* @throws GitLabApiException if any exception occurs during execution
26652666
*/
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);
2667+
public Variable createVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
2668+
Boolean isProtected, Boolean isMasked) throws GitLabApiException {
2669+
return createVariable(projectIdOrPath, key, value, variableType, isProtected, isMasked, null);
26682670
}
26692671

26702672
/**
@@ -2677,17 +2679,20 @@ public Variable createVariable(Object projectIdOrPath, String key, String value,
26772679
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
26782680
* @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
26792681
* @param value the value for the variable, required
2682+
* @param variableType the type of variable. Available types are: env_var (default) and file
26802683
* @param isProtected whether the variable is protected, optional
26812684
* @param isMasked whether the variable is masked, optional
26822685
* @param environmentScope the environment_scope of the variable, optional
26832686
* @return a Variable instance with the newly created variable
26842687
* @throws GitLabApiException if any exception occurs during execution
26852688
*/
2686-
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
2689+
public Variable createVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
2690+
Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
26872691

26882692
GitLabApiForm formData = new GitLabApiForm()
26892693
.withParam("key", key, true)
26902694
.withParam("value", value, true)
2695+
.withParam("variable_type", variableType)
26912696
.withParam("protected", isProtected)
26922697
.withParam("masked", isMasked)
26932698
.withParam("environment_scope", environmentScope);
@@ -2708,7 +2713,7 @@ public Variable createVariable(Object projectIdOrPath, String key, String value,
27082713
* @throws GitLabApiException if any exception occurs during execution
27092714
*/
27102715
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
2711-
return (updateVariable(projectIdOrPath, key, value, isProtected, (String) null));
2716+
return (updateVariable(projectIdOrPath, key, value, null, isProtected, null, null));
27122717
}
27132718

27142719
/**
@@ -2727,7 +2732,7 @@ public Variable updateVariable(Object projectIdOrPath, String key, String value,
27272732
* @throws GitLabApiException if any exception occurs during execution
27282733
*/
27292734
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
2730-
return updateVariable(projectIdOrPath, key, value, isProtected, null, environmentScope);
2735+
return updateVariable(projectIdOrPath, key, value, null, isProtected, null, environmentScope);
27312736
}
27322737

27332738
/**
@@ -2740,13 +2745,15 @@ public Variable updateVariable(Object projectIdOrPath, String key, String value,
27402745
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
27412746
* @param key the key of an existing variable, required
27422747
* @param value the value for the variable, required
2748+
* @param variableType the type of variable. Available types are: env_var (default) and file
27432749
* @param isProtected whether the variable is protected, optional
2744-
* @param isMasked whether the variable is masked, optional
2750+
* @param masked whether the variable is masked, optional
27452751
* @return a Variable instance with the updated variable
27462752
* @throws GitLabApiException if any exception occurs during execution
27472753
*/
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);
2754+
public Variable updateVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
2755+
Boolean isProtected, Boolean masked) throws GitLabApiException {
2756+
return updateVariable(projectIdOrPath, key, value, variableType, isProtected, masked, null);
27502757
}
27512758

27522759
/**
@@ -2759,19 +2766,22 @@ public Variable updateVariable(Object projectIdOrPath, String key, String value,
27592766
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
27602767
* @param key the key of an existing variable, required
27612768
* @param value the value for the variable, required
2769+
* @param variableType the type of variable. Available types are: env_var (default) and file
27622770
* @param isProtected whether the variable is protected, optional
2763-
* @param isMasked whether the variable is masked, optional
2771+
* @param masked whether the variable is masked, optional
27642772
* @param environmentScope the environment_scope of the variable, optional.
27652773
* @return a Variable instance with the updated variable
27662774
* @throws GitLabApiException if any exception occurs during execution
27672775
*/
2768-
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, Boolean isMasked, String environmentScope) throws GitLabApiException {
2776+
public Variable updateVariable(Object projectIdOrPath, String key, String value, Variable.Type variableType,
2777+
Boolean isProtected, Boolean masked, String environmentScope) throws GitLabApiException {
27692778

27702779
GitLabApiForm formData = new GitLabApiForm()
27712780
.withParam("value", value, true)
2781+
.withParam("variable_type", variableType)
27722782
.withParam("protected", isProtected)
2773-
.withParam("masked", isMasked)
2774-
.withParam("environment_scope", environmentScope);
2783+
.withParam("masked", masked)
2784+
.withParam("environment_scope", environmentScope);
27752785
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
27762786
return (response.readEntity(Variable.class));
27772787
}

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,42 @@
55
import java.util.Map;
66

77
import org.gitlab4j.api.utils.JacksonJson;
8+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
89

10+
import com.fasterxml.jackson.annotation.JsonCreator;
911
import com.fasterxml.jackson.annotation.JsonProperty;
12+
import com.fasterxml.jackson.annotation.JsonValue;
1013

1114
public class Variable {
1215

16+
/**
17+
* Enum for the various Commit build status values.
18+
*/
19+
public enum Type {
20+
21+
ENV_VAR, FILE;
22+
23+
private static JacksonJsonEnumHelper<Type> enumHelper = new JacksonJsonEnumHelper<>(Type.class);
24+
25+
@JsonCreator
26+
public static Type forValue(String value) {
27+
return enumHelper.forValue(value);
28+
}
29+
30+
@JsonValue
31+
public String toValue() {
32+
return (enumHelper.toString(this));
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return (enumHelper.toString(this));
38+
}
39+
}
40+
1341
private String key;
1442
private String value;
43+
private Type variableType;
1544
@JsonProperty("protected")
1645
private Boolean isProtected;
1746
@JsonProperty("masked")
@@ -42,6 +71,14 @@ public void setValue(String value) {
4271
this.value = value;
4372
}
4473

74+
public Type getVariableType() {
75+
return variableType;
76+
}
77+
78+
public void setVariableType(Type variableType) {
79+
this.variableType = variableType;
80+
}
81+
4582
public Boolean getProtected() {
4683
return isProtected;
4784
}
@@ -55,7 +92,7 @@ public Boolean getMasked() {
5592
}
5693

5794
public void setMasked(Boolean masked) {
58-
isMasked = masked;
95+
this.isMasked = masked;
5996
}
6097

6198
public String getEnvironmentScope() {

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ public static void setup() {
103103
testProject = getTestProject();
104104
currentUser = getCurrentUser();
105105

106-
deleteAllTestProjects();
106+
deleteAllTransientTestData();
107107
}
108108

109109
@AfterClass
110110
public static void teardown() throws GitLabApiException {
111-
deleteAllTestProjects();
111+
deleteAllTransientTestData();
112112
}
113113

114-
private static void deleteAllTestProjects() {
114+
private static void deleteAllTransientTestData() {
115115

116116
if (gitLabApi == null) {
117117
return;
@@ -147,6 +147,7 @@ private static void deleteAllTestProjects() {
147147

148148
for (Variable variable : variables) {
149149
if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) {
150+
gitLabApi.getProjectApi().updateVariable(testProject, variable.getKey(), "EMPTY", false);
150151
gitLabApi.getProjectApi().deleteVariable(testProject, variable.getKey());
151152
}
152153
}
@@ -671,8 +672,8 @@ public void testVariables() throws GitLabApiException {
671672
assumeNotNull(testProject);
672673

673674
String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
674-
String value = "TEST_VARIABLE_VALUE_" + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
675-
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, null);
675+
String value = "ABCDEFG12345678" + HelperUtils.getRandomInt();
676+
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, null, null, true);
676677

677678
assertNotNull(variable);
678679
assertEquals(key, variable.getKey());
@@ -696,14 +697,14 @@ public void testVariables() throws GitLabApiException {
696697
assertEquals("NONE", variable.getValue());
697698
assertTrue(variable.getProtected());
698699

699-
gitLabApi.getProjectApi().updateVariable(testProject, key, value, true, true, "DEV");
700+
gitLabApi.getProjectApi().updateVariable(testProject, key, value, Variable.Type.ENV_VAR, false, true, "DEV");
700701
variable = gitLabApi.getProjectApi().getVariable(testProject, key);
701702

702703
assertNotNull(variable);
703704
assertEquals(key, variable.getKey());
704-
assertEquals("NONE", variable.getValue());
705-
assertTrue(variable.getMasked());
706-
assertTrue(variable.getProtected());
705+
assertEquals(value, variable.getValue());
706+
assertEquals(Variable.Type.ENV_VAR, variable.getVariableType());
707+
assertFalse(variable.getProtected());
707708

708709
gitLabApi.getProjectApi().deleteVariable(testProject, key);
709710
variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
@@ -713,6 +714,28 @@ public void testVariables() throws GitLabApiException {
713714
assertNull(matchingVariable);
714715
}
715716

717+
@Test
718+
public void testFileVariable() throws GitLabApiException {
719+
720+
assumeNotNull(testProject);
721+
722+
String key = TEST_VARIABLE_KEY_PREFIX + HelperUtils.getRandomInt() + "_" + HelperUtils.getRandomInt();
723+
String value = "/tmp/test.txt";
724+
Variable variable = gitLabApi.getProjectApi().createVariable(testProject, key, value, Variable.Type.FILE, null, false);
725+
726+
assertNotNull(variable);
727+
assertEquals(key, variable.getKey());
728+
assertEquals(value, variable.getValue());
729+
assertEquals(Variable.Type.FILE, variable.getVariableType());
730+
731+
gitLabApi.getProjectApi().deleteVariable(testProject, key);
732+
Stream<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(testProject);
733+
assertNotNull(variables);
734+
735+
Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null);
736+
assertNull(matchingVariable);
737+
}
738+
716739
@Test
717740
public void testGetMembers() throws GitLabApiException {
718741

0 commit comments

Comments
 (0)