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// ProjectVariablesService handles communication with the
25// project variables related methods of the GitLab API.
26//
27// GitLab API docs:
28// https://docs.gitlab.com/ee/api/project_level_variables.html
29type ProjectVariablesService struct {
30 client *Client
31}
32
33// ProjectVariable represents a GitLab Project Variable.
34//
35// GitLab API docs:
36// https://docs.gitlab.com/ee/api/project_level_variables.html
37type ProjectVariable struct {
38 Key string `json:"key"`
39 Value string `json:"value"`
40 Protected bool `json:"protected"`
41 Masked bool `json:"masked"`
42 EnvironmentScope string `json:"environment_scope"`
43}
44
45func (v ProjectVariable) String() string {
46 return Stringify(v)
47}
48
49// ListVariables gets a list of all variables in a project.
50//
51// GitLab API docs:
52// https://docs.gitlab.com/ee/api/project_level_variables.html#list-project-variables
53func (s *ProjectVariablesService) ListVariables(pid interface{}, options ...OptionFunc) ([]*ProjectVariable, *Response, error) {
54 project, err := parseID(pid)
55 if err != nil {
56 return nil, nil, err
57 }
58 u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
59
60 req, err := s.client.NewRequest("GET", u, nil, options)
61 if err != nil {
62 return nil, nil, err
63 }
64
65 var vs []*ProjectVariable
66 resp, err := s.client.Do(req, &vs)
67 if err != nil {
68 return nil, resp, err
69 }
70
71 return vs, resp, err
72}
73
74// GetVariable gets a variable.
75//
76// GitLab API docs:
77// https://docs.gitlab.com/ee/api/project_level_variables.html#show-variable-details
78func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, options ...OptionFunc) (*ProjectVariable, *Response, error) {
79 project, err := parseID(pid)
80 if err != nil {
81 return nil, nil, err
82 }
83 u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), url.PathEscape(key))
84
85 req, err := s.client.NewRequest("GET", u, nil, options)
86 if err != nil {
87 return nil, nil, err
88 }
89
90 v := new(ProjectVariable)
91 resp, err := s.client.Do(req, v)
92 if err != nil {
93 return nil, resp, err
94 }
95
96 return v, resp, err
97}
98
99// CreateVariableOptions represents the available
100// CreateVariable() options.
101//
102// GitLab API docs:
103// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
104type CreateVariableOptions struct {
105 Key *string `url:"key,omitempty" json:"key,omitempty"`
106 Value *string `url:"value,omitempty" json:"value,omitempty"`
107 Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
108 Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
109 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
110}
111
112// CreateVariable creates a new project variable.
113//
114// GitLab API docs:
115// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
116func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
117 project, err := parseID(pid)
118 if err != nil {
119 return nil, nil, err
120 }
121 u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
122
123 req, err := s.client.NewRequest("POST", u, opt, options)
124 if err != nil {
125 return nil, nil, err
126 }
127
128 v := new(ProjectVariable)
129 resp, err := s.client.Do(req, v)
130 if err != nil {
131 return nil, resp, err
132 }
133
134 return v, resp, err
135}
136
137// UpdateVariableOptions represents the available
138// UpdateVariable() options.
139//
140// GitLab API docs:
141// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
142type UpdateVariableOptions struct {
143 Value *string `url:"value,omitempty" json:"value,omitempty"`
144 Protected *bool `url:"protected,omitempty" json:"protected,omitempty"`
145 Masked *bool `url:"masked,omitempty" json:"masked,omitempty"`
146 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
147}
148
149// UpdateVariable updates a project's variable
150//
151// GitLab API docs:
152// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
153func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
154 project, err := parseID(pid)
155 if err != nil {
156 return nil, nil, err
157 }
158 u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), url.PathEscape(key))
159
160 req, err := s.client.NewRequest("PUT", u, opt, options)
161 if err != nil {
162 return nil, nil, err
163 }
164
165 v := new(ProjectVariable)
166 resp, err := s.client.Do(req, v)
167 if err != nil {
168 return nil, resp, err
169 }
170
171 return v, resp, err
172}
173
174// RemoveVariable removes a project's variable.
175//
176// GitLab API docs:
177// https://docs.gitlab.com/ee/api/project_level_variables.html#remove-variable
178func (s *ProjectVariablesService) RemoveVariable(pid interface{}, key string, options ...OptionFunc) (*Response, error) {
179 project, err := parseID(pid)
180 if err != nil {
181 return nil, err
182 }
183 u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), url.PathEscape(key))
184
185 req, err := s.client.NewRequest("DELETE", u, nil, options)
186 if err != nil {
187 return nil, err
188 }
189
190 return s.client.Do(req, nil)
191}