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	Protected bool   `json:"protected"`
 41}
 42
 43func (v GroupVariable) String() string {
 44	return Stringify(v)
 45}
 46
 47// ListVariables gets a list of all variables for a group.
 48//
 49// GitLab API docs:
 50// https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
 51func (s *GroupVariablesService) ListVariables(gid interface{}, options ...OptionFunc) ([]*GroupVariable, *Response, error) {
 52	group, err := parseID(gid)
 53	if err != nil {
 54		return nil, nil, err
 55	}
 56	u := fmt.Sprintf("groups/%s/variables", pathEscape(group))
 57
 58	req, err := s.client.NewRequest("GET", u, nil, options)
 59	if err != nil {
 60		return nil, nil, err
 61	}
 62
 63	var vs []*GroupVariable
 64	resp, err := s.client.Do(req, &vs)
 65	if err != nil {
 66		return nil, resp, err
 67	}
 68
 69	return vs, resp, err
 70}
 71
 72// GetVariable gets a variable.
 73//
 74// GitLab API docs:
 75// https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
 76func (s *GroupVariablesService) GetVariable(gid interface{}, key string, options ...OptionFunc) (*GroupVariable, *Response, error) {
 77	group, err := parseID(gid)
 78	if err != nil {
 79		return nil, nil, err
 80	}
 81	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
 82
 83	req, err := s.client.NewRequest("GET", u, nil, options)
 84	if err != nil {
 85		return nil, nil, err
 86	}
 87
 88	v := new(GroupVariable)
 89	resp, err := s.client.Do(req, v)
 90	if err != nil {
 91		return nil, resp, err
 92	}
 93
 94	return v, resp, err
 95}
 96
 97// CreateVariable creates a new group variable.
 98//
 99// GitLab API docs:
100// https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
101func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
102	group, err := parseID(gid)
103	if err != nil {
104		return nil, nil, err
105	}
106	u := fmt.Sprintf("groups/%s/variables", pathEscape(group))
107
108	req, err := s.client.NewRequest("POST", u, opt, options)
109	if err != nil {
110		return nil, nil, err
111	}
112
113	v := new(GroupVariable)
114	resp, err := s.client.Do(req, v)
115	if err != nil {
116		return nil, resp, err
117	}
118
119	return v, resp, err
120}
121
122// UpdateVariable updates the position of an existing
123// group issue board list.
124//
125// GitLab API docs:
126// https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
127func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
128	group, err := parseID(gid)
129	if err != nil {
130		return nil, nil, err
131	}
132	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
133
134	req, err := s.client.NewRequest("PUT", u, opt, options)
135	if err != nil {
136		return nil, nil, err
137	}
138
139	v := new(GroupVariable)
140	resp, err := s.client.Do(req, v)
141	if err != nil {
142		return nil, resp, err
143	}
144
145	return v, resp, err
146}
147
148// RemoveVariable removes a group's variable.
149//
150// GitLab API docs:
151// https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
152func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, options ...OptionFunc) (*Response, error) {
153	group, err := parseID(gid)
154	if err != nil {
155		return nil, err
156	}
157	u := fmt.Sprintf("groups/%s/variables/%s", pathEscape(group), url.PathEscape(key))
158
159	req, err := s.client.NewRequest("DELETE", u, nil, options)
160	if err != nil {
161		return nil, err
162	}
163
164	return s.client.Do(req, nil)
165}