...

Source file src/github.com/xanzy/go-gitlab/environments.go

Documentation: github.com/xanzy/go-gitlab

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

View as plain text