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