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