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