environments.go

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