build_variables.go 4.9 KB

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