Skip to content

Commit

Permalink
Merge branch 'add-filter-to-group-variables' into 'main'
Browse files Browse the repository at this point in the history
Add filter to group variables update and delete

Closes xanzy#746

See merge request gitlab-org/api/client-go!2169
  • Loading branch information
timofurrer committed Feb 24, 2025
2 parents 3bcfffd + 72e52c9 commit 6430f4d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 63 deletions.
36 changes: 23 additions & 13 deletions group_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (
// group variables related methods of the GitLab API.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html
// https://docs.gitlab.com/api/group_level_variables/
type GroupVariablesService struct {
client *Client
}

// GroupVariable represents a GitLab group Variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html
// https://docs.gitlab.com/api/group_level_variables/
type GroupVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Expand All @@ -55,13 +55,13 @@ func (v GroupVariable) String() string {
// for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
// https://docs.gitlab.com/api/group_level_variables/#list-group-variables
type ListGroupVariablesOptions ListOptions

// ListVariables gets a list of all variables for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
// https://docs.gitlab.com/api/group_level_variables/#list-group-variables
func (s *GroupVariablesService) ListVariables(gid interface{}, opt *ListGroupVariablesOptions, options ...RequestOptionFunc) ([]*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
Expand All @@ -87,15 +87,15 @@ func (s *GroupVariablesService) ListVariables(gid interface{}, opt *ListGroupVar
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
// https://docs.gitlab.com/api/group_level_variables/#show-variable-details
type GetGroupVariableOptions struct {
Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
}

// GetVariable gets a variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
// https://docs.gitlab.com/api/group_level_variables/#show-variable-details
func (s *GroupVariablesService) GetVariable(gid interface{}, key string, opt *GetGroupVariableOptions, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
Expand All @@ -121,7 +121,7 @@ func (s *GroupVariablesService) GetVariable(gid interface{}, key string, opt *Ge
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
// https://docs.gitlab.com/api/group_level_variables/#create-variable
type CreateGroupVariableOptions struct {
Key *string `url:"key,omitempty" json:"key,omitempty"`
Value *string `url:"value,omitempty" json:"value,omitempty"`
Expand All @@ -137,7 +137,7 @@ type CreateGroupVariableOptions struct {
// CreateVariable creates a new group variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
// https://docs.gitlab.com/api/group_level_variables/#create-variable
func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateGroupVariableOptions, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
Expand All @@ -163,11 +163,12 @@ func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateGroup
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
// https://docs.gitlab.com/api/group_level_variables/#update-variable
type UpdateGroupVariableOptions struct {
Value *string `url:"value,omitempty" json:"value,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
Raw *bool `url:"raw,omitempty" json:"raw,omitempty"`
Expand All @@ -178,7 +179,7 @@ type UpdateGroupVariableOptions struct {
// group issue board list.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
// https://docs.gitlab.com/api/group_level_variables/#update-variable
func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateGroupVariableOptions, options ...RequestOptionFunc) (*GroupVariable, *Response, error) {
group, err := parseID(gid)
if err != nil {
Expand All @@ -200,18 +201,27 @@ func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt
return v, resp, nil
}

// RemoveGroupVariableOptions represents the available RemoveVariable()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_level_variables/#remove-variable
type RemoveGroupVariableOptions struct {
Filter *VariableFilter `url:"filter,omitempty" json:"filter,omitempty"`
}

// RemoveVariable removes a group's variable.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, options ...RequestOptionFunc) (*Response, error) {
// https://docs.gitlab.com/api/group_level_variables/#remove-variable
func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, opt *RemoveGroupVariableOptions, options ...RequestOptionFunc) (*Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("groups/%s/variables/%s", PathEscape(group), 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
97 changes: 47 additions & 50 deletions group_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListGroupVariabless(t *testing.T) {
Expand All @@ -32,10 +33,9 @@ func TestListGroupVariabless(t *testing.T) {
fmt.Fprint(w, `[{"key": "TEST_VARIABLE_1","value": "test1","protected": false,"masked": true,"hidden": true}]`)
})

variables, _, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{})
if err != nil {
t.Errorf("GroupVariables.ListVariables returned error: %v", err)
}
variables, resp, err := client.GroupVariables.ListVariables(1, &ListGroupVariablesOptions{})
assert.NoError(t, err)
assert.NotNil(t, resp)

want := []*GroupVariable{
{
Expand All @@ -47,9 +47,7 @@ func TestListGroupVariabless(t *testing.T) {
},
}

if !reflect.DeepEqual(want, variables) {
t.Errorf("GroupVariables.ListVariablesreturned %+v, want %+v", variables, want)
}
assert.ElementsMatch(t, want, variables)
}

func TestGetGroupVariable(t *testing.T) {
Expand All @@ -62,15 +60,12 @@ func TestGetGroupVariable(t *testing.T) {
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"}})
if err != nil {
t.Errorf("GroupVariables.GetVariable returned error: %v", err)
}
variable, resp, err := client.GroupVariables.GetVariable(1, "TEST_VARIABLE_1", &GetGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}})
assert.NoError(t, err)
assert.NotNil(t, resp)

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)
}
assert.Equal(t, want, variable)
}

func TestCreateGroupVariable(t *testing.T) {
Expand All @@ -90,15 +85,12 @@ func TestCreateGroupVariable(t *testing.T) {
MaskedAndHidden: Ptr(false),
}

variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil)
if err != nil {
t.Errorf("GroupVariables.CreateVariable returned error: %v", err)
}
variable, resp, err := client.GroupVariables.CreateVariable(1, opt, nil)
assert.NoError(t, err)
assert.NotNil(t, resp)

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)
}
assert.Equal(t, want, variable)
}

func TestCreateGroupVariable_MaskedAndHidden(t *testing.T) {
Expand All @@ -118,15 +110,12 @@ func TestCreateGroupVariable_MaskedAndHidden(t *testing.T) {
MaskedAndHidden: Ptr(true),
}

variable, _, err := client.GroupVariables.CreateVariable(1, opt, nil)
if err != nil {
t.Errorf("GroupVariables.CreateVariable returned error: %v", err)
}
variable, resp, err := client.GroupVariables.CreateVariable(1, opt, nil)
assert.NoError(t, err)
assert.NotNil(t, resp)

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)
}
assert.Equal(t, want, variable)
}

func TestDeleteGroupVariable(t *testing.T) {
Expand All @@ -138,16 +127,13 @@ func TestDeleteGroupVariable(t *testing.T) {
w.WriteHeader(http.StatusAccepted)
})

resp, err := client.GroupVariables.RemoveVariable(1, "TEST_VARIABLE_1")
if err != nil {
t.Errorf("GroupVariables.RemoveVariable returned error: %v", err)
}
resp, err := client.GroupVariables.RemoveVariable(1, "TEST_VARIABLE_1", &RemoveGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}})
assert.NoError(t, err)
assert.NotNil(t, resp)

want := http.StatusAccepted
got := resp.StatusCode
if got != want {
t.Errorf("GroupVariables.RemoveVariable returned %d, want %d", got, want)
}
assert.Equal(t, want, got)
}

func TestUpdateGroupVariable(t *testing.T) {
Expand All @@ -159,15 +145,29 @@ func TestUpdateGroupVariable(t *testing.T) {
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)
}
variable, resp, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{})
assert.NoError(t, err)
assert.NotNil(t, resp)

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)
}
assert.Equal(t, want, variable)
}

func TestUpdateGroupVariable_Filter(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","value": "test1","protected": false,"masked": true,"hidden": false}`)
})

variable, resp, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{Filter: &VariableFilter{EnvironmentScope: "prod"}})
assert.NoError(t, err)
assert.NotNil(t, resp)

want := &GroupVariable{Key: "TEST_VARIABLE_1", Value: "test1", Protected: false, Masked: true, Hidden: false}
assert.Equal(t, want, variable)
}

func TestUpdateGroupVariable_MaskedAndHidden(t *testing.T) {
Expand All @@ -179,13 +179,10 @@ func TestUpdateGroupVariable_MaskedAndHidden(t *testing.T) {
fmt.Fprint(w, `{"key": "TEST_VARIABLE_1","protected": false,"masked": true,"hidden": true}`)
})

variable, _, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{})
if err != nil {
t.Errorf("GroupVariables.UpdateVariable returned error: %v", err)
}
variable, resp, err := client.GroupVariables.UpdateVariable(1, "TEST_VARIABLE_1", &UpdateGroupVariableOptions{})
assert.NoError(t, err)
assert.NotNil(t, resp)

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)
}
assert.Equal(t, want, variable)
}

0 comments on commit 6430f4d

Please sign in to comment.