Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit 5146c24

Browse files
committed
Fix model for get, create and update group variable
GitLab has inconsistent naming of the filed for hidden variables. It seems for keeping UI consistent, GitLab chose to introduce different naming for the same field. This could have been easy handled in UI by setting two variables on API request instead of introducing combined field for UI. I have added 2 test cases for create and update project API as per the gitlab.com API that I also manually tested.
1 parent 0cb05aa commit 5146c24

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

group_variables.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type GroupVariable struct {
4141
VariableType VariableTypeValue `json:"variable_type"`
4242
Protected bool `json:"protected"`
4343
Masked bool `json:"masked"`
44-
MaskedAndHidden bool `json:"masked_and_hidden"`
44+
Hidden bool `json:"hidden"`
4545
Raw bool `json:"raw"`
4646
EnvironmentScope string `json:"environment_scope"`
4747
Description string `json:"description"`

group_variables_test.go

+60-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestListGroupVariabless(t *testing.T) {
2929
mux.HandleFunc("/api/v4/groups/1/variables",
3030
func(w http.ResponseWriter, r *http.Request) {
3131
testMethod(t, r, http.MethodGet)
32-
fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}]`)
32+
fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`)
3333
})
3434

3535
variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{})
@@ -39,11 +39,11 @@ func TestListGroupVariabless(t *testing.T) {
3939

4040
want := []*GroupVariable{
4141
{
42-
Key: "TEST_VARIABLE_1",
43-
Value: "test1",
44-
Protected: false,
45-
Masked: true,
46-
MaskedAndHidden: true,
42+
Key: "TEST_VARIABLE_1",
43+
Value: "test1",
44+
Protected: false,
45+
Masked: true,
46+
Hidden: true,
4747
},
4848
}
4949

@@ -59,15 +59,15 @@ func TestGetGroupVariable(t *testing.T) {
5959
func(w http.ResponseWriter, r *http.Request) {
6060
testMethod(t, r, http.MethodGet)
6161
testParams(t, r, "filter%5Benvironment_scope%5D=prod")
62-
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`)
62+
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`)
6363
})
6464

6565
variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}})
6666
if err != nil {
6767
t.Errorf("GroupVariables.GetVariable returned error: %v", err)
6868
}
6969

70-
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true}
70+
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false}
7171
if !reflect.DeepEqual(want, variable) {
7272
t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want)
7373
}
@@ -79,7 +79,35 @@ func TestCreateGroupVariable(t *testing.T) {
7979
mux.HandleFunc("/api/v4/groups/1/variables",
8080
func(w http.ResponseWriter, r *http.Request) {
8181
testMethod(t, r, http.MethodPost)
82-
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`)
82+
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value":"test1","protected": false,"masked": true,"hidden": false}`)
83+
})
84+
85+
opt := &CreateGroupVariableOptions{
86+
Key: Ptr("TEST_VARIABLE_1"),
87+
Value: Ptr("test1"),
88+
Protected: Ptr(false),
89+
Masked: Ptr(true),
90+
MaskedAndHidden: Ptr(false),
91+
}
92+
93+
variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil)
94+
if err != nil {
95+
t.Errorf("GroupVariables.CreateVariable returned error: %v", err)
96+
}
97+
98+
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false}
99+
if !reflect.DeepEqual(want, variable) {
100+
t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want)
101+
}
102+
}
103+
104+
func TestCreateGroupVariable_MaskedAndHidden(t *testing.T) {
105+
mux, client := setup(t)
106+
107+
mux.HandleFunc("/api/v4/groups/1/variables",
108+
func(w http.ResponseWriter, r *http.Request) {
109+
testMethod(t, r, http.MethodPost)
110+
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`)
83111
})
84112

85113
opt := &CreateGroupVariableOptions{
@@ -95,7 +123,7 @@ func TestCreateGroupVariable(t *testing.T) {
95123
t.Errorf("GroupVariables.CreateVariable returned error: %v", err)
96124
}
97125

98-
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true}
126+
want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true}
99127
if !reflect.DeepEqual(want, variable) {
100128
t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want)
101129
}
@@ -128,15 +156,35 @@ func TestUpdateGroupVariable(t *testing.T) {
128156
mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1",
129157
func(w http.ResponseWriter, r *http.Request) {
130158
testMethod(t, r, http.MethodPut)
131-
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`)
159+
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`)
160+
})
161+
162+
variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{})
163+
if err != nil {
164+
t.Errorf("GroupVariables.UpdateVariable returned error: %v", err)
165+
}
166+
167+
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false}
168+
if !reflect.DeepEqual(want, variable) {
169+
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want)
170+
}
171+
}
172+
173+
func TestUpdateGroupVariable_MaskedAndHidden(t *testing.T) {
174+
mux, client := setup(t)
175+
176+
mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1",
177+
func(w http.ResponseWriter, r *http.Request) {
178+
testMethod(t, r, http.MethodPut)
179+
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`)
132180
})
133181

134182
variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{})
135183
if err != nil {
136184
t.Errorf("GroupVariables.UpdateVariable returned error: %v", err)
137185
}
138186

139-
want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true}
187+
want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true}
140188
if !reflect.DeepEqual(want, variable) {
141189
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want)
142190
}

0 commit comments

Comments
 (0)