Skip to content

Commit

Permalink
Merge pull request #1336 from Bouwdie/project-variable-filter-option
Browse files Browse the repository at this point in the history
Add project variable filter environment scope option for Get,Update and Remove functions.
  • Loading branch information
svanharmelen authored Feb 10, 2022
2 parents 0988ef6 + 7c78bc4 commit 8aa55a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
32 changes: 28 additions & 4 deletions project_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions project_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 8aa55a7

Please sign in to comment.