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	Protected        bool   `json:"protected"`
 41	EnvironmentScope string `json:"environment_scope"`
 42}
 43
 44func (v ProjectVariable) String() string {
 45	return Stringify(v)
 46}
 47
 48// ListVariables gets a list of all variables in a project.
 49//
 50// GitLab API docs:
 51// https://docs.gitlab.com/ee/api/project_level_variables.html#list-project-variables
 52func (s *ProjectVariablesService) ListVariables(pid interface{}, options ...OptionFunc) ([]*ProjectVariable, *Response, error) {
 53	project, err := parseID(pid)
 54	if err != nil {
 55		return nil, nil, err
 56	}
 57	u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
 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 []*ProjectVariable
 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/project_level_variables.html#show-variable-details
 77func (s *ProjectVariablesService) GetVariable(pid interface{}, key string, options ...OptionFunc) (*ProjectVariable, *Response, error) {
 78	project, err := parseID(pid)
 79	if err != nil {
 80		return nil, nil, err
 81	}
 82	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), 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(ProjectVariable)
 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// CreateVariableOptions represents the available
 99// CreateVariable() options.
100//
101// GitLab API docs:
102// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
103type CreateVariableOptions struct {
104	Key              *string `url:"key,omitempty" json:"key,omitempty"`
105	Value            *string `url:"value,omitempty" json:"value,omitempty"`
106	Protected        *bool   `url:"protected,omitempty" json:"protected,omitempty"`
107	EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
108}
109
110// CreateVariable creates a new project variable.
111//
112// GitLab API docs:
113// https://docs.gitlab.com/ee/api/project_level_variables.html#create-variable
114func (s *ProjectVariablesService) CreateVariable(pid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
115	project, err := parseID(pid)
116	if err != nil {
117		return nil, nil, err
118	}
119	u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
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(ProjectVariable)
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// UpdateVariableOptions represents the available
136// UpdateVariable() options.
137//
138// GitLab API docs:
139// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
140type UpdateVariableOptions struct {
141	Value            *string `url:"value,omitempty" json:"value,omitempty"`
142	Protected        *bool   `url:"protected,omitempty" json:"protected,omitempty"`
143	EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
144}
145
146// UpdateVariable updates a project's variable
147//
148// GitLab API docs:
149// https://docs.gitlab.com/ee/api/project_level_variables.html#update-variable
150func (s *ProjectVariablesService) UpdateVariable(pid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*ProjectVariable, *Response, error) {
151	project, err := parseID(pid)
152	if err != nil {
153		return nil, nil, err
154	}
155	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), url.PathEscape(key))
156
157	req, err := s.client.NewRequest("PUT", u, opt, options)
158	if err != nil {
159		return nil, nil, err
160	}
161
162	v := new(ProjectVariable)
163	resp, err := s.client.Do(req, v)
164	if err != nil {
165		return nil, resp, err
166	}
167
168	return v, resp, err
169}
170
171// RemoveVariable removes a project's variable.
172//
173// GitLab API docs:
174// https://docs.gitlab.com/ee/api/project_level_variables.html#remove-variable
175func (s *ProjectVariablesService) RemoveVariable(pid interface{}, key string, options ...OptionFunc) (*Response, error) {
176	project, err := parseID(pid)
177	if err != nil {
178		return nil, err
179	}
180	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), url.PathEscape(key))
181
182	req, err := s.client.NewRequest("DELETE", u, nil, options)
183	if err != nil {
184		return nil, err
185	}
186
187	return s.client.Do(req, nil)
188}