build_variables.go

  1package gitlab
  2
  3import (
  4	"fmt"
  5)
  6
  7// BuildVariablesService handles communication with the project variables related methods
  8// of the Gitlab API
  9//
 10// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
 11type BuildVariablesService struct {
 12	client *Client
 13}
 14
 15// BuildVariable represents a variable available for each build of the given project
 16//
 17// Gitlab API Docs : https://docs.gitlab.com/ce/api/build_variables.html
 18type BuildVariable struct {
 19	Key       string `json:"key"`
 20	Value     string `json:"value"`
 21	Protected bool   `json:"protected"`
 22}
 23
 24func (v BuildVariable) String() string {
 25	return Stringify(v)
 26}
 27
 28// ListBuildVariablesOptions are the parameters to ListBuildVariables()
 29//
 30// Gitlab API Docs:
 31// https://docs.gitlab.com/ce/api/build_variables.html#list-project-variables
 32type ListBuildVariablesOptions ListOptions
 33
 34// ListBuildVariables gets the a list of project variables in a project
 35//
 36// Gitlab API Docs:
 37// https://docs.gitlab.com/ce/api/build_variables.html#list-project-variables
 38func (s *BuildVariablesService) ListBuildVariables(pid interface{}, opts *ListBuildVariablesOptions, options ...OptionFunc) ([]*BuildVariable, *Response, error) {
 39	project, err := parseID(pid)
 40	if err != nil {
 41		return nil, nil, err
 42	}
 43	u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
 44
 45	req, err := s.client.NewRequest("GET", u, opts, options)
 46	if err != nil {
 47		return nil, nil, err
 48	}
 49
 50	var v []*BuildVariable
 51	resp, err := s.client.Do(req, &v)
 52	if err != nil {
 53		return nil, resp, err
 54	}
 55
 56	return v, resp, err
 57}
 58
 59// GetBuildVariable gets a single project variable of a project
 60//
 61// Gitlab API Docs:
 62// https://docs.gitlab.com/ce/api/build_variables.html#show-variable-details
 63func (s *BuildVariablesService) GetBuildVariable(pid interface{}, key string, options ...OptionFunc) (*BuildVariable, *Response, error) {
 64	project, err := parseID(pid)
 65	if err != nil {
 66		return nil, nil, err
 67	}
 68	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
 69
 70	req, err := s.client.NewRequest("GET", u, nil, options)
 71	if err != nil {
 72		return nil, nil, err
 73	}
 74
 75	v := new(BuildVariable)
 76	resp, err := s.client.Do(req, v)
 77	if err != nil {
 78		return nil, resp, err
 79	}
 80
 81	return v, resp, err
 82}
 83
 84// CreateBuildVariableOptions are the parameters to CreateBuildVariable()
 85//
 86// Gitlab API Docs:
 87// https://docs.gitlab.com/ce/api/build_variables.html#create-variable
 88type CreateBuildVariableOptions struct {
 89	Key       *string `url:"key" json:"key"`
 90	Value     *string `url:"value" json:"value"`
 91	Protected *bool   `url:"protected,omitempty" json:"protected,omitempty"`
 92}
 93
 94// CreateBuildVariable creates a variable for a given project
 95//
 96// Gitlab API Docs:
 97// https://docs.gitlab.com/ce/api/build_variables.html#create-variable
 98func (s *BuildVariablesService) CreateBuildVariable(pid interface{}, opt *CreateBuildVariableOptions, options ...OptionFunc) (*BuildVariable, *Response, error) {
 99	project, err := parseID(pid)
100	if err != nil {
101		return nil, nil, err
102	}
103	u := fmt.Sprintf("projects/%s/variables", pathEscape(project))
104
105	req, err := s.client.NewRequest("POST", u, opt, options)
106	if err != nil {
107		return nil, nil, err
108	}
109
110	v := new(BuildVariable)
111	resp, err := s.client.Do(req, v)
112	if err != nil {
113		return nil, resp, err
114	}
115
116	return v, resp, err
117}
118
119// UpdateBuildVariableOptions are the parameters to UpdateBuildVariable()
120//
121// Gitlab API Docs:
122// https://docs.gitlab.com/ce/api/build_variables.html#update-variable
123type UpdateBuildVariableOptions struct {
124	Key       *string `url:"key" json:"key"`
125	Value     *string `url:"value" json:"value"`
126	Protected *bool   `url:"protected,omitempty" json:"protected,omitempty"`
127}
128
129// UpdateBuildVariable updates an existing project variable
130// The variable key must exist
131//
132// Gitlab API Docs:
133// https://docs.gitlab.com/ce/api/build_variables.html#update-variable
134func (s *BuildVariablesService) UpdateBuildVariable(pid interface{}, key string, opt *UpdateBuildVariableOptions, options ...OptionFunc) (*BuildVariable, *Response, error) {
135	project, err := parseID(pid)
136	if err != nil {
137		return nil, nil, err
138	}
139	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
140
141	req, err := s.client.NewRequest("PUT", u, opt, options)
142	if err != nil {
143		return nil, nil, err
144	}
145
146	v := new(BuildVariable)
147	resp, err := s.client.Do(req, v)
148	if err != nil {
149		return nil, resp, err
150	}
151
152	return v, resp, err
153}
154
155// RemoveBuildVariable removes a project variable of a given project identified by its key
156//
157// Gitlab API Docs:
158// https://docs.gitlab.com/ce/api/build_variables.html#remove-variable
159func (s *BuildVariablesService) RemoveBuildVariable(pid interface{}, key string, options ...OptionFunc) (*Response, error) {
160	project, err := parseID(pid)
161	if err != nil {
162		return nil, err
163	}
164	u := fmt.Sprintf("projects/%s/variables/%s", pathEscape(project), key)
165
166	req, err := s.client.NewRequest("DELETE", u, nil, options)
167	if err != nil {
168		return nil, err
169	}
170
171	return s.client.Do(req, nil)
172}