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