group_variables.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. )
  21. // GroupVariablesService handles communication with the
  22. // group variables related methods of the GitLab API.
  23. //
  24. // GitLab API docs:
  25. // https://docs.gitlab.com/ee/api/group_level_variables.html
  26. type GroupVariablesService struct {
  27. client *Client
  28. }
  29. // GroupVariable represents a GitLab group Variable.
  30. //
  31. // GitLab API docs:
  32. // https://docs.gitlab.com/ee/api/group_level_variables.html
  33. type GroupVariable struct {
  34. Key string `json:"key"`
  35. Value string `json:"value"`
  36. Protected bool `json:"protected"`
  37. }
  38. func (v GroupVariable) String() string {
  39. return Stringify(v)
  40. }
  41. // ListVariables gets a list of all variables for a group.
  42. //
  43. // GitLab API docs:
  44. // https://docs.gitlab.com/ee/api/group_level_variables.html#list-group-variables
  45. func (s *GroupVariablesService) ListVariables(gid interface{}, options ...OptionFunc) ([]*GroupVariable, *Response, error) {
  46. group, err := parseID(gid)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. u := fmt.Sprintf("groups/%s/variables", url.QueryEscape(group))
  51. req, err := s.client.NewRequest("GET", u, nil, options)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. var vs []*GroupVariable
  56. resp, err := s.client.Do(req, &vs)
  57. if err != nil {
  58. return nil, resp, err
  59. }
  60. return vs, resp, err
  61. }
  62. // GetVariable gets a variable.
  63. //
  64. // GitLab API docs:
  65. // https://docs.gitlab.com/ee/api/group_level_variables.html#show-variable-details
  66. func (s *GroupVariablesService) GetVariable(gid interface{}, key string, options ...OptionFunc) (*GroupVariable, *Response, error) {
  67. group, err := parseID(gid)
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. u := fmt.Sprintf("groups/%s/variables/%s", url.QueryEscape(group), url.QueryEscape(key))
  72. req, err := s.client.NewRequest("GET", u, nil, options)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. v := new(GroupVariable)
  77. resp, err := s.client.Do(req, v)
  78. if err != nil {
  79. return nil, resp, err
  80. }
  81. return v, resp, err
  82. }
  83. // CreateVariable creates a new group variable.
  84. //
  85. // GitLab API docs:
  86. // https://docs.gitlab.com/ee/api/group_level_variables.html#create-variable
  87. func (s *GroupVariablesService) CreateVariable(gid interface{}, opt *CreateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
  88. group, err := parseID(gid)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. u := fmt.Sprintf("groups/%s/variables", url.QueryEscape(group))
  93. req, err := s.client.NewRequest("POST", u, opt, options)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. v := new(GroupVariable)
  98. resp, err := s.client.Do(req, v)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return v, resp, err
  103. }
  104. // UpdateVariable updates the position of an existing
  105. // group issue board list.
  106. //
  107. // GitLab API docs:
  108. // https://docs.gitlab.com/ee/api/group_level_variables.html#update-variable
  109. func (s *GroupVariablesService) UpdateVariable(gid interface{}, key string, opt *UpdateVariableOptions, options ...OptionFunc) (*GroupVariable, *Response, error) {
  110. group, err := parseID(gid)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. u := fmt.Sprintf("groups/%s/variables/%s",
  115. url.QueryEscape(group),
  116. url.QueryEscape(key),
  117. )
  118. req, err := s.client.NewRequest("PUT", u, opt, options)
  119. if err != nil {
  120. return nil, nil, err
  121. }
  122. v := new(GroupVariable)
  123. resp, err := s.client.Do(req, v)
  124. if err != nil {
  125. return nil, resp, err
  126. }
  127. return v, resp, err
  128. }
  129. // RemoveVariable removes a group's variable.
  130. //
  131. // GitLab API docs:
  132. // https://docs.gitlab.com/ee/api/group_level_variables.html#remove-variable
  133. func (s *GroupVariablesService) RemoveVariable(gid interface{}, key string, options ...OptionFunc) (*Response, error) {
  134. group, err := parseID(gid)
  135. if err != nil {
  136. return nil, err
  137. }
  138. u := fmt.Sprintf("groups/%s/variables/%s",
  139. url.QueryEscape(group),
  140. url.QueryEscape(key),
  141. )
  142. req, err := s.client.NewRequest("DELETE", u, nil, options)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return s.client.Do(req, nil)
  147. }