group_variables.go

  1//
  2// Copyright 2018, Patrick Webster
  3//
  4// Licensed under the Apache License, Version 2.0 (the "License");
  5// you may not use this file except in compliance with the License.
  6// You may obtain a copy of the License at
  7//
  8//     http://www.apache.org/licenses/LICENSE-2.0
  9//
 10// Unless required by applicable law or agreed to in writing, software
 11// distributed under the License is distributed on an "AS IS" BASIS,
 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13// See the License for the specific language governing permissions and
 14// limitations under the License.
 15//
 16
 17package gitlab
 18
 19import (
 20	"fmt"
 21	"net/url"
 22)
 23
 24// GroupVariablesService handles communication with the
 25// group variables related methods of the GitLab API.
 26//
 27// GitLab API docs:
 28// https://docs.gitlab.com/ee/api/group_level_variables.html
 29type GroupVariablesService struct {
 30	client *Client
 31}
 32
 33// GroupVariable represents a GitLab group Variable.
 34//
 35// GitLab API docs:
 36// https://docs.gitlab.com/ee/api/group_level_variables.html
 37type GroupVariable struct {
 38	Key          string            `json:"key"`
 39	Value        string            `json:"value"`
 40	VariableType VariableTypeValue `json:"variable_type"`
 41	Protected    bool              `json:"protected"`
 42}
 43
 44func (v GroupVariable) String() string {
 45	return Stringify(v)
 46}
 47
 48// ListGroupVariablesOptions represents the available options for listing variables
 49// for a group.
 50//
 51// GitLab API docs:
 52// https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
 53type ListGroupVariablesOptions ListOptions
 54
 55// ListVariables gets a list of all variables for a group.
 56//
 57// GitLab API docs:
 58// https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
 59func (s *GroupVariablesService) ListVariables(gid interface{}, opt *ListGroupVariablesOptions, options ...OptionFunc) ([]*GroupVariable, *Response, error) {
 60	group, err := parseID(gid)
 61	if err != nil {
 62		return nil, nil, err
 63	}
 64	u := fmt.Sprintf("groups/%s/variables", pathEscape(group))
 65
 66	req, err := s.client.NewRequest("GET", u, opt, options)
 67	if err != nil {
 68		return nil, nil, err
 69	}
 70
 71	var vs []*GroupVariable
 72	resp, err := s.client.Do(req, &vs)
 73	if err != nil {
 74		return nil, resp, err
 75	}
 76
 77	return vs, resp, err
 78}
 79
 80// GetVariable gets a variable.
 81//
 82// GitLab API docs:
 83// https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
 84func (s *GroupVariablesService) GetVariable(gid interface{}, key string, options ...OptionFunc) (*GroupVariable, *Response, error) {
 85	group, err := parseID(gid)
 86	if err != nil {
 87		return nil, nil, err
 88	}
 89	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
 90
 91	req, err := s.client.NewRequest("GET", u, nil, options)
 92	if err != nil {
 93		return nil, nil, err
 94	}
 95
 96	v := new(GroupVariable)
 97	resp, err := s.client.Do(req, v)
 98	if err != nil {
 99		return nil, resp, err
100	}
101
102	return v, resp, err
103}
104
105// CreateGroupVariableOptions represents the available CreateVariable()
106// options.
107//
108// GitLab API docs:
109// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
110type CreateGroupVariableOptions struct {
111	Key          *string            `url:"key,omitempty" json:"key,omitempty"`
112	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
113	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
114	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
115}
116
117// CreateVariable creates a new group variable.
118//
119// GitLab API docs:
120// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
121func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateGroupVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
122	group, err := parseID(gid)
123	if err != nil {
124		return nil, nil, err
125	}
126	u := fmt.Sprintf("groups/%s/variables", pathEscape(group))
127
128	req, err := s.client.NewRequest("POST", u, opt, options)
129	if err != nil {
130		return nil, nil, err
131	}
132
133	v := new(GroupVariable)
134	resp, err := s.client.Do(req, v)
135	if err != nil {
136		return nil, resp, err
137	}
138
139	return v, resp, err
140}
141
142// UpdateGroupVariableOptions represents the available UpdateVariable()
143// options.
144//
145// GitLab API docs:
146// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
147type UpdateGroupVariableOptions struct {
148	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
149	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
150	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
151}
152
153// UpdateVariable updates the position of an existing
154// group issue board list.
155//
156// GitLab API docs:
157// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
158func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateGroupVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
159	group, err := parseID(gid)
160	if err != nil {
161		return nil, nil, err
162	}
163	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
164
165	req, err := s.client.NewRequest("PUT", u, opt, options)
166	if err != nil {
167		return nil, nil, err
168	}
169
170	v := new(GroupVariable)
171	resp, err := s.client.Do(req, v)
172	if err != nil {
173		return nil, resp, err
174	}
175
176	return v, resp, err
177}
178
179// RemoveVariable removes a group's variable.
180//
181// GitLab API docs:
182// https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
183func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, options ...OptionFunc) (*Response, error) {
184	group, err := parseID(gid)
185	if err != nil {
186		return nil, err
187	}
188	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
189
190	req, err := s.client.NewRequest("DELETE", u, nil, options)
191	if err != nil {
192		return nil, err
193	}
194
195	return s.client.Do(req, nil)
196}