From 2a048140eabe8e390265123edf7c1b600edf16da Mon Sep 17 00:00:00 2001 From: Yogesh Date: Thu, 21 Nov 2024 18:18:43 +0100 Subject: [PATCH 1/3] Add Hidden field to GroupVariable struct --- group_variables.go | 2 ++ group_variables_test.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/group_variables.go b/group_variables.go index 69fe44592..066f3e4d1 100644 --- a/group_variables.go +++ b/group_variables.go @@ -41,6 +41,7 @@ type GroupVariable struct { VariableType VariableTypeValue `json:"variable_type"` Protected bool `json:"protected"` Masked bool `json:"masked"` + Hidden bool `json:"hidden"` Raw bool `json:"raw"` EnvironmentScope string `json:"environment_scope"` Description string `json:"description"` @@ -127,6 +128,7 @@ type CreateGroupVariableOptions struct { Description *string `url:"description,omitempty" json:"description,omitempty"` EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"` Masked *bool `url:"masked,omitempty" json:"masked,omitempty"` + Hidden *bool `url:"hidden,omitempty" json:"hidden,omitempty"` Protected *bool `url:"protected,omitempty" json:"protected,omitempty"` Raw *bool `url:"raw,omitempty" json:"raw,omitempty"` VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"` diff --git a/group_variables_test.go b/group_variables_test.go index 4d79d3927..47d793b04 100644 --- a/group_variables_test.go +++ b/group_variables_test.go @@ -29,7 +29,7 @@ func TestListGroupVariabless(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}]`) + fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`) }) variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{}) @@ -43,6 +43,7 @@ func TestListGroupVariabless(t *testing.T) { Value: "test1", Protected: false, Masked: true, + Hidden: true, }, } @@ -58,7 +59,7 @@ func TestGetGroupVariable(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) testParams(t, r, "filter%5Benvironment_scope%5D=prod") - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}`) }) variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}) @@ -66,7 +67,7 @@ func TestGetGroupVariable(t *testing.T) { t.Errorf("GroupVariables.GetVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want) } @@ -78,7 +79,7 @@ func TestCreateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}`) }) opt := &CreateGroupVariableOptions{ @@ -86,6 +87,7 @@ func TestCreateGroupVariable(t *testing.T) { Value: Ptr("test1"), Protected: Ptr(false), Masked: Ptr(true), + Hidden: Ptr(true), } variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil) @@ -93,7 +95,7 @@ func TestCreateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.CreateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) } @@ -126,7 +128,7 @@ func TestUpdateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPut) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) }) variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) @@ -134,7 +136,7 @@ func TestUpdateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} if !reflect.DeepEqual(want, variable) { t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) } From 0cb05aa1f98ca1fbbf3eeaa46cc15e82e249a705 Mon Sep 17 00:00:00 2001 From: Yogesh Date: Mon, 2 Dec 2024 16:31:49 +0100 Subject: [PATCH 2/3] Revert name as per GitLab API --- group_variables.go | 4 ++-- group_variables_test.go | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/group_variables.go b/group_variables.go index 066f3e4d1..b3e7bdfe6 100644 --- a/group_variables.go +++ b/group_variables.go @@ -41,7 +41,7 @@ type GroupVariable struct { VariableType VariableTypeValue `json:"variable_type"` Protected bool `json:"protected"` Masked bool `json:"masked"` - Hidden bool `json:"hidden"` + MaskedAndHidden bool `json:"masked_and_hidden"` Raw bool `json:"raw"` EnvironmentScope string `json:"environment_scope"` Description string `json:"description"` @@ -128,7 +128,7 @@ type CreateGroupVariableOptions struct { Description *string `url:"description,omitempty" json:"description,omitempty"` EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"` Masked *bool `url:"masked,omitempty" json:"masked,omitempty"` - Hidden *bool `url:"hidden,omitempty" json:"hidden,omitempty"` + MaskedAndHidden *bool `url:"masked_and_hidden,omitempty" json:"hidden,omitempty"` Protected *bool `url:"protected,omitempty" json:"protected,omitempty"` Raw *bool `url:"raw,omitempty" json:"raw,omitempty"` VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"` diff --git a/group_variables_test.go b/group_variables_test.go index 47d793b04..153317b45 100644 --- a/group_variables_test.go +++ b/group_variables_test.go @@ -29,7 +29,7 @@ func TestListGroupVariabless(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`) + fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}]`) }) variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{}) @@ -39,11 +39,11 @@ func TestListGroupVariabless(t *testing.T) { want := []*GroupVariable{ { - Key: "TEST_VARIABLE_1", - Value: "test1", - Protected: false, - Masked: true, - Hidden: true, + Key: "TEST_VARIABLE_1", + Value: "test1", + Protected: false, + Masked: true, + MaskedAndHidden: true, }, } @@ -59,7 +59,7 @@ func TestGetGroupVariable(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) testParams(t, r, "filter%5Benvironment_scope%5D=prod") - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) }) variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}) @@ -67,7 +67,7 @@ func TestGetGroupVariable(t *testing.T) { t.Errorf("GroupVariables.GetVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want) } @@ -79,15 +79,15 @@ func TestCreateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) }) opt := &CreateGroupVariableOptions{ - Key: Ptr("TEST_VARIABLE_1"), - Value: Ptr("test1"), - Protected: Ptr(false), - Masked: Ptr(true), - Hidden: Ptr(true), + Key: Ptr("TEST_VARIABLE_1"), + Value: Ptr("test1"), + Protected: Ptr(false), + Masked: Ptr(true), + MaskedAndHidden: Ptr(true), } variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil) @@ -95,7 +95,7 @@ func TestCreateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.CreateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) } @@ -128,7 +128,7 @@ func TestUpdateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPut) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) }) variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) @@ -136,7 +136,7 @@ func TestUpdateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} if !reflect.DeepEqual(want, variable) { t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) } From 5146c247bffcd03c040e2928d960488c451a7f02 Mon Sep 17 00:00:00 2001 From: Yogesh Date: Sun, 8 Dec 2024 18:04:46 +0100 Subject: [PATCH 3/3] 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. --- group_variables.go | 2 +- group_variables_test.go | 72 ++++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/group_variables.go b/group_variables.go index b3e7bdfe6..7978e594e 100644 --- a/group_variables.go +++ b/group_variables.go @@ -41,7 +41,7 @@ type GroupVariable struct { VariableType VariableTypeValue `json:"variable_type"` Protected bool `json:"protected"` Masked bool `json:"masked"` - MaskedAndHidden bool `json:"masked_and_hidden"` + Hidden bool `json:"hidden"` Raw bool `json:"raw"` EnvironmentScope string `json:"environment_scope"` Description string `json:"description"` diff --git a/group_variables_test.go b/group_variables_test.go index 153317b45..0c0de84bb 100644 --- a/group_variables_test.go +++ b/group_variables_test.go @@ -29,7 +29,7 @@ func TestListGroupVariabless(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}]`) + fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`) }) variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{}) @@ -39,11 +39,11 @@ func TestListGroupVariabless(t *testing.T) { want := []*GroupVariable{ { - Key: "TEST_VARIABLE_1", - Value: "test1", - Protected: false, - Masked: true, - MaskedAndHidden: true, + Key: "TEST_VARIABLE_1", + Value: "test1", + Protected: false, + Masked: true, + Hidden: true, }, } @@ -59,7 +59,7 @@ func TestGetGroupVariable(t *testing.T) { func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) testParams(t, r, "filter%5Benvironment_scope%5D=prod") - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) }) variable, _, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}) @@ -67,7 +67,7 @@ func TestGetGroupVariable(t *testing.T) { t.Errorf("GroupVariables.GetVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.GetVariable returned %+v, want %+v", variable, want) } @@ -79,7 +79,35 @@ func TestCreateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"masked_and_hidden": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value":"test1","protected": false,"masked": true,"hidden": false}`) + }) + + opt := &CreateGroupVariableOptions{ + Key: Ptr("TEST_VARIABLE_1"), + Value: Ptr("test1"), + Protected: Ptr(false), + Masked: Ptr(true), + MaskedAndHidden: Ptr(false), + } + + variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil) + if err != nil { + t.Errorf("GroupVariables.CreateVariable returned error: %v", err) + } + + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} + if !reflect.DeepEqual(want, variable) { + t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) + } +} + +func TestCreateGroupVariable_MaskedAndHidden(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/groups/1/variables", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`) }) opt := &CreateGroupVariableOptions{ @@ -95,7 +123,7 @@ func TestCreateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.CreateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, MaskedAndHidden: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("GroupVariables.CreateVariable returned %+v, want %+v", variable, want) } @@ -128,7 +156,27 @@ func TestUpdateGroupVariable(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPut) - fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true}`) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": false}`) + }) + + variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) + if err != nil { + t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) + } + + want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false} + if !reflect.DeepEqual(want, variable) { + t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) + } +} + +func TestUpdateGroupVariable_MaskedAndHidden(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/groups/1/variables/TEST_VARIABLE_1", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`) }) variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{}) @@ -136,7 +184,7 @@ func TestUpdateGroupVariable(t *testing.T) { t.Errorf("GroupVariables.UpdateVariable returned error: %v", err) } - want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true} + want := &GroupVariable{Key: "TEST_VARIABLE_1", Protected: false, Masked: true, Hidden: true} if !reflect.DeepEqual(want, variable) { t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", variable, want) }