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
17package gitlab
18
19import (
20 "fmt"
21)
22
23// EnvironmentsService handles communication with the environment related methods
24// of the GitLab API.
25//
26// GitLab API docs: https://docs.gitlab.com/ce/api/environments.html
27type EnvironmentsService struct {
28 client *Client
29}
30
31// Environment represents a GitLab environment.
32//
33// GitLab API docs: https://docs.gitlab.com/ce/api/environments.html
34type Environment struct {
35 ID int `json:"id"`
36 Name string `json:"name"`
37 Slug string `json:"slug"`
38 ExternalURL string `json:"external_url"`
39 LastDeployment *Deployment `json:"last_deployment"`
40}
41
42func (env Environment) String() string {
43 return Stringify(env)
44}
45
46// ListEnvironmentsOptions represents the available ListEnvironments() options.
47//
48// GitLab API docs:
49// https://docs.gitlab.com/ee/api/environments.html#list-environments
50type ListEnvironmentsOptions ListOptions
51
52// ListEnvironments gets a list of environments from a project, sorted by name
53// alphabetically.
54//
55// GitLab API docs:
56// https://docs.gitlab.com/ee/api/environments.html#list-environments
57func (s *EnvironmentsService) ListEnvironments(pid interface{}, opts *ListEnvironmentsOptions, options ...OptionFunc) ([]*Environment, *Response, error) {
58 project, err := parseID(pid)
59 if err != nil {
60 return nil, nil, err
61 }
62 u := fmt.Sprintf("projects/%s/environments", pathEscape(project))
63
64 req, err := s.client.NewRequest("GET", u, opts, options)
65 if err != nil {
66 return nil, nil, err
67 }
68
69 var envs []*Environment
70 resp, err := s.client.Do(req, &envs)
71 if err != nil {
72 return nil, resp, err
73 }
74
75 return envs, resp, err
76}
77
78// GetEnvironment gets a specific environment from a project.
79//
80// GitLab API docs:
81// https://docs.gitlab.com/ee/api/environments.html#get-a-specific-environment
82func (s *EnvironmentsService) GetEnvironment(pid interface{}, environment int, 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/%d", pathEscape(project), environment)
88
89 req, err := s.client.NewRequest("GET", u, nil, options)
90 if err != nil {
91 return nil, nil, err
92 }
93
94 env := new(Environment)
95 resp, err := s.client.Do(req, env)
96 if err != nil {
97 return nil, resp, err
98 }
99
100 return env, resp, err
101}
102
103// CreateEnvironmentOptions represents the available CreateEnvironment() options.
104//
105// GitLab API docs:
106// https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment
107type CreateEnvironmentOptions struct {
108 Name *string `url:"name,omitempty" json:"name,omitempty"`
109 ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
110}
111
112// CreateEnvironment adds an environment to a project. This is an idempotent
113// method and can be called multiple times with the same parameters. Createing
114// an environment that is already a environment does not affect the
115// existing environmentship.
116//
117// GitLab API docs:
118// https://docs.gitlab.com/ee/api/environments.html#create-a-new-environment
119func (s *EnvironmentsService) CreateEnvironment(pid interface{}, opt *CreateEnvironmentOptions, options ...OptionFunc) (*Environment, *Response, error) {
120 project, err := parseID(pid)
121 if err != nil {
122 return nil, nil, err
123 }
124 u := fmt.Sprintf("projects/%s/environments", pathEscape(project))
125
126 req, err := s.client.NewRequest("POST", u, opt, options)
127 if err != nil {
128 return nil, nil, err
129 }
130
131 env := new(Environment)
132 resp, err := s.client.Do(req, env)
133 if err != nil {
134 return nil, resp, err
135 }
136
137 return env, resp, err
138}
139
140// EditEnvironmentOptions represents the available EditEnvironment() options.
141//
142// GitLab API docs:
143// https://docs.gitlab.com/ee/api/environments.html#edit-an-existing-environment
144type EditEnvironmentOptions struct {
145 Name *string `url:"name,omitempty" json:"name,omitempty"`
146 ExternalURL *string `url:"external_url,omitempty" json:"external_url,omitempty"`
147}
148
149// EditEnvironment updates a project team environment to a specified access level..
150//
151// GitLab API docs:
152// https://docs.gitlab.com/ee/api/environments.html#edit-an-existing-environment
153func (s *EnvironmentsService) EditEnvironment(pid interface{}, environment int, opt *EditEnvironmentOptions, options ...OptionFunc) (*Environment, *Response, error) {
154 project, err := parseID(pid)
155 if err != nil {
156 return nil, nil, err
157 }
158 u := fmt.Sprintf("projects/%s/environments/%d", pathEscape(project), environment)
159
160 req, err := s.client.NewRequest("PUT", u, opt, options)
161 if err != nil {
162 return nil, nil, err
163 }
164
165 env := new(Environment)
166 resp, err := s.client.Do(req, env)
167 if err != nil {
168 return nil, resp, err
169 }
170
171 return env, resp, err
172}
173
174// DeleteEnvironment removes an environment from a project team.
175//
176// GitLab API docs:
177// https://docs.gitlab.com/ce/api/environments.html#remove-a-environment-from-a-group-or-project
178func (s *EnvironmentsService) DeleteEnvironment(pid interface{}, environment int, options ...OptionFunc) (*Response, error) {
179 project, err := parseID(pid)
180 if err != nil {
181 return nil, err
182 }
183 u := fmt.Sprintf("projects/%s/environments/%d", pathEscape(project), environment)
184
185 req, err := s.client.NewRequest("DELETE", u, nil, options)
186 if err != nil {
187 return nil, err
188 }
189
190 return s.client.Do(req, nil)
191}
192
193// StopEnvironment stop an environment from a project team.
194//
195// GitLab API docs:
196// https://docs.gitlab.com/ce/api/environments.html#stop-an-environment
197func (s *EnvironmentsService) StopEnvironment(pid interface{}, environmentID int, options ...OptionFunc) (*Response, error) {
198 project, err := parseID(pid)
199 if err != nil {
200 return nil, err
201 }
202 u := fmt.Sprintf("projects/%s/environments/%d/stop", pathEscape(project), environmentID)
203
204 req, err := s.client.NewRequest("POST", u, nil, options)
205 if err != nil {
206 return nil, err
207 }
208
209 return s.client.Do(req, nil)
210}