environments.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright 2017, Sander van Harmelen
  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. // EnvironmentsService handles communication with the environment related methods
  22. // of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/environments.html
  25. type EnvironmentsService struct {
  26. client *Client
  27. }
  28. // Environment represents a GitLab environment.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ce/api/environments.html
  31. type Environment struct {
  32. ID int `json:"id"`
  33. Name string `json:"name"`
  34. Slug string `json:"slug"`
  35. ExternalURL string `json:"external_url"`
  36. }
  37. func (env Environment) String() string {
  38. return Stringify(env)
  39. }
  40. // ListEnvironmentsOptions represents the available ListEnvironments() options.
  41. //
  42. // GitLab API docs:
  43. // https://docs.gitlab.com/ee/api/environments.html#list-environments
  44. type ListEnvironmentsOptions ListOptions
  45. // ListEnvironments gets a list of environments from a project, sorted by name
  46. // alphabetically.
  47. //
  48. // GitLab API docs:
  49. // https://docs.gitlab.com/ee/api/environments.html#list-environments
  50. func (s *EnvironmentsService) ListEnvironments(pid interface{}, opts *ListEnvironmentsOptions, options ...OptionFunc) ([]*Environment, *Response, error) {
  51. project, err := parseID(pid)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. u := fmt.Sprintf("projects/%s/environments", url.QueryEscape(project))
  56. req, err := s.client.NewRequest("GET", u, opts, options)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. var envs []*Environment
  61. resp, err := s.client.Do(req, &envs)
  62. if err != nil {
  63. return nil, resp, err
  64. }
  65. return envs, resp, err
  66. }
  67. // CreateEnvironmentOptions represents the available CreateEnvironment() options.
  68. //
  69. // GitLab API docs:
  70. // https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment
  71. type CreateEnvironmentOptions struct {
  72. Name *string `url:"name,omitempty" json:"name,omitempty"`
  73. ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
  74. }
  75. // CreateEnvironment adds a environment to a project. This is an idempotent
  76. // method and can be called multiple times with the same parameters. Createing
  77. // an environment that is already a environment does not affect the
  78. // existing environmentship.
  79. //
  80. // GitLab API docs:
  81. // https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment
  82. func (s *EnvironmentsService) CreateEnvironment(pid interface{}, opt *CreateEnvironmentOptions, options ...OptionFunc) (*Environment, *Response, error) {
  83. project, err := parseID(pid)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. u := fmt.Sprintf("projects/%s/environments", url.QueryEscape(project))
  88. req, err := s.client.NewRequest("POST", u, opt, options)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. env := new(Environment)
  93. resp, err := s.client.Do(req, env)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return env, resp, err
  98. }
  99. // EditEnvironmentOptions represents the available EditEnvironment() options.
  100. //
  101. // GitLab API docs:
  102. // https://docs.gitlab.com/ee/api/environments.html#edit-an-existing-environment
  103. type EditEnvironmentOptions struct {
  104. Name *string `url:"name,omitempty" json:"name,omitempty"`
  105. ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
  106. }
  107. // EditEnvironment updates a project team environment to a specified access level..
  108. //
  109. // GitLab API docs:
  110. // https://docs.gitlab.com/ee/api/environments.html#edit-an-existing-environment
  111. func (s *EnvironmentsService) EditEnvironment(pid interface{}, environment int, opt *EditEnvironmentOptions, options ...OptionFunc) (*Environment, *Response, error) {
  112. project, err := parseID(pid)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. u := fmt.Sprintf("projects/%s/environments/%d", url.QueryEscape(project), environment)
  117. req, err := s.client.NewRequest("PUT", u, opt, options)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. env := new(Environment)
  122. resp, err := s.client.Do(req, env)
  123. if err != nil {
  124. return nil, resp, err
  125. }
  126. return env, resp, err
  127. }
  128. // DeleteEnvironment removes a environment from a project team.
  129. //
  130. // GitLab API docs:
  131. // https://docs.gitlab.com/ce/api/environments.html#remove-a-environment-from-a-group-or-project
  132. func (s *EnvironmentsService) DeleteEnvironment(pid interface{}, environment int, options ...OptionFunc) (*Response, error) {
  133. project, err := parseID(pid)
  134. if err != nil {
  135. return nil, err
  136. }
  137. u := fmt.Sprintf("projects/%s/environments/%d", url.QueryEscape(project), environment)
  138. req, err := s.client.NewRequest("DELETE", u, nil, options)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return s.client.Do(req, nil)
  143. }