Repository: xanzy/go-gitlab
Pull request #1336 (2022-01-05 22:25:27)

Add project variable filter environment scope option for Get,Update and Remove functions.
The github API documentation says it is not required, so i added it as option. 


Patch:
diff --git a/project_variables.go b/project_variables.go
index 2c4d4e9..5f121ff 100644
--- a/project_variables.go
+++ b/project_variables.go
@@ -48,6 +48,11 @@ func (v ProjectVariable) String() string {
 	return Stringify(v)
 }
 
+//VariableFilter filters available for project variable related functions
+type VariableFilter struct {
+	EnvironmentScope string `url:"environment_scope, omitempty" json:"environment_scope,omitempty"`
+}
+
 // ListProjectVariablesOptions represents the available options for listing variables
 // in a project.
 //
@@ -80,18 +85,27 @@ func (s *ProjectVariablesService) ListVariables(pid interface{}, opt *ListProjec
 	return vs, resp, err
 }
 
+// GetProjectVariableOptions represents the available GetVariable()
+// options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/project_level_variables.html#show-variable-details
+type GetProjectVariableOptions struct {
+	Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
+}
+
 // GetVariable gets a variable.
 //
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/project_level_variables.html#show-variable-details
-func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, options ...RequestOptionFunc) (*ProjectVariable, *Response, error) {
+func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, opt *GetProjectVariableOptions, options ...RequestOptionFunc) (*ProjectVariable, *Response, error) {
 	project, err := parseID(pid)
 	if err != nil {
 		return nil, nil, err
 	}
 	u := fmt.Sprintf("projects/%s/variables/%s", PathEscape(project), url.PathEscape(key))
 
-	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
+	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
 	if err != nil {
 		return nil, nil, err
 	}
@@ -155,6 +169,7 @@ type UpdateProjectVariableOptions struct {
 	Protected        *bool              `url:"protected,omitempty" json:"protected,omitempty"`
 	Masked           *bool              `url:"masked,omitempty" json:"masked,omitempty"`
 	EnvironmentScope *string            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
+	Filter           *VariableFilter    `url:"filter,omitempty" json:"filter,omitempty"`
 }
 
 // UpdateVariable updates a project's variable.
@@ -182,18 +197,27 @@ func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, op
 	return v, resp, err
 }
 
+// RemoveProjectVariableOptions represents the available RemoveVariable()
+// options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/project_level_variables.html#remove-variable
+type RemoveProjectVariableOptions struct {
+	Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
+}
+
 // RemoveVariable removes a project's variable.
 //
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/project_level_variables.html#remove-variable
-func (s *ProjectVariablesService) RemoveVariable(pid interface{}, key string, options ...RequestOptionFunc) (*Response, error) {
+func (s *ProjectVariablesService) RemoveVariable(pid interface{}, key string, opt *RemoveProjectVariableOptions, options ...RequestOptionFunc) (*Response, error) {
 	project, err := parseID(pid)
 	if err != nil {
 		return nil, err
 	}
 	u := fmt.Sprintf("projects/%s/variables/%s", PathEscape(project), url.PathEscape(key))
 
-	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
+	req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
 	if err != nil {
 		return nil, err
 	}


Test patch:
diff --git a/project_variables_test.go b/project_variables_test.go
index de1de74..235f3a6 100644
--- a/project_variables_test.go
+++ b/project_variables_test.go
@@ -61,6 +61,7 @@ func TestProjectVariablesService_GetVariable(t *testing.T) {
 
 	mux.HandleFunc("/api/v4/projects/1/variables/TEST_VARIABLE_1", func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, http.MethodGet)
+		testParams(t, r, "filter%5Benvironment_scope%5D=prod")
 		fmt.Fprintf(w, `
 			{
 				"key": "TEST_VARIABLE_1",
@@ -81,7 +82,7 @@ func TestProjectVariablesService_GetVariable(t *testing.T) {
 		EnvironmentScope: "",
 	}
 
-	pv, resp, err := client.ProjectVariables.GetVariable(1, "TEST_VARIABLE_1", nil, nil)
+	pv, resp, err := client.ProjectVariables.GetVariable(1, "TEST_VARIABLE_1", &GetProjectVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}, nil)
 	require.NoError(t, err)
 	require.NotNil(t, resp)
 	require.Equal(t, want, pv)
@@ -156,6 +157,7 @@ func TestProjectVariablesService_UpdateVariable(t *testing.T) {
 
 	mux.HandleFunc("/api/v4/projects/1/variables/NEW_VARIABLE", func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, http.MethodPut)
+		testBody(t, r, `{"filter":{"environment_scope":"prod"}}`)
 		fmt.Fprintf(w, `
 			{
 				"key": "NEW_VARIABLE",
@@ -177,7 +179,7 @@ func TestProjectVariablesService_UpdateVariable(t *testing.T) {
 		EnvironmentScope: "*",
 	}
 
-	pv, resp, err := client.ProjectVariables.UpdateVariable(1, "NEW_VARIABLE", nil, nil)
+	pv, resp, err := client.ProjectVariables.UpdateVariable(1, "NEW_VARIABLE", &UpdateProjectVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}, nil)
 	require.NoError(t, err)
 	require.NotNil(t, resp)
 	require.Equal(t, want, pv)
@@ -204,9 +206,10 @@ func TestProjectVariablesService_RemoveVariable(t *testing.T) {
 
 	mux.HandleFunc("/api/v4/projects/1/variables/VARIABLE_1", func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, http.MethodDelete)
+		testParams(t, r, "filter%5Benvironment_scope%5D=prod")
 	})
 
-	resp, err := client.ProjectVariables.RemoveVariable(1, "VARIABLE_1", nil, nil)
+	resp, err := client.ProjectVariables.RemoveVariable(1, "VARIABLE_1", &RemoveProjectVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}}, nil)
 	require.NoError(t, err)
 	require.NotNil(t, resp)