...

Source file src/github.com/xanzy/go-gitlab/deployments.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  package gitlab
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"time"
    22  )
    23  
    24  // DeploymentsService handles communication with the deployment related methods
    25  // of the GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html
    28  type DeploymentsService struct {
    29  	client *Client
    30  }
    31  
    32  // Deployment represents the Gitlab deployment
    33  type Deployment struct {
    34  	ID          int          `json:"id"`
    35  	IID         int          `json:"iid"`
    36  	Ref         string       `json:"ref"`
    37  	SHA         string       `json:"sha"`
    38  	Status      string       `json:"status"`
    39  	CreatedAt   *time.Time   `json:"created_at"`
    40  	UpdatedAt   *time.Time   `json:"updated_at"`
    41  	User        *ProjectUser `json:"user"`
    42  	Environment *Environment `json:"environment"`
    43  	Deployable  struct {
    44  		ID         int        `json:"id"`
    45  		Status     string     `json:"status"`
    46  		Stage      string     `json:"stage"`
    47  		Name       string     `json:"name"`
    48  		Ref        string     `json:"ref"`
    49  		Tag        bool       `json:"tag"`
    50  		Coverage   float64    `json:"coverage"`
    51  		CreatedAt  *time.Time `json:"created_at"`
    52  		StartedAt  *time.Time `json:"started_at"`
    53  		FinishedAt *time.Time `json:"finished_at"`
    54  		Duration   float64    `json:"duration"`
    55  		User       *User      `json:"user"`
    56  		Commit     *Commit    `json:"commit"`
    57  		Pipeline   struct {
    58  			ID        int        `json:"id"`
    59  			SHA       string     `json:"sha"`
    60  			Ref       string     `json:"ref"`
    61  			Status    string     `json:"status"`
    62  			CreatedAt *time.Time `json:"created_at"`
    63  			UpdatedAt *time.Time `json:"updated_at"`
    64  		} `json:"pipeline"`
    65  		Runner *Runner `json:"runner"`
    66  	} `json:"deployable"`
    67  }
    68  
    69  // ListProjectDeploymentsOptions represents the available ListProjectDeployments() options.
    70  //
    71  // GitLab API docs:
    72  // https://docs.gitlab.com/ee/api/deployments.html#list-project-deployments
    73  type ListProjectDeploymentsOptions struct {
    74  	ListOptions
    75  	OrderBy     *string `url:"order_by,omitempty" json:"order_by,omitempty"`
    76  	Sort        *string `url:"sort,omitempty" json:"sort,omitempty"`
    77  	Environment *string `url:"environment,omitempty" json:"environment,omitempty"`
    78  	Status      *string `url:"status,omitempty" json:"status,omitempty"`
    79  
    80  	// Only for Gitlab versions less than 14
    81  	UpdatedAfter  *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
    82  	UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
    83  
    84  	// Only for Gitlab 14 or higher
    85  	FinishedAfter  *time.Time `url:"finished_after,omitempty" json:"finished_after,omitempty"`
    86  	FinishedBefore *time.Time `url:"finished_before,omitempty" json:"finished_before,omitempty"`
    87  }
    88  
    89  // ListProjectDeployments gets a list of deployments in a project.
    90  //
    91  // GitLab API docs:
    92  // https://docs.gitlab.com/ee/api/deployments.html#list-project-deployments
    93  func (s *DeploymentsService) ListProjectDeployments(pid interface{}, opts *ListProjectDeploymentsOptions, options ...RequestOptionFunc) ([]*Deployment, *Response, error) {
    94  	project, err := parseID(pid)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  	u := fmt.Sprintf("projects/%s/deployments", PathEscape(project))
    99  
   100  	req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
   101  	if err != nil {
   102  		return nil, nil, err
   103  	}
   104  
   105  	var ds []*Deployment
   106  	resp, err := s.client.Do(req, &ds)
   107  	if err != nil {
   108  		return nil, resp, err
   109  	}
   110  
   111  	return ds, resp, nil
   112  }
   113  
   114  // GetProjectDeployment get a deployment for a project.
   115  //
   116  // GitLab API docs:
   117  // https://docs.gitlab.com/ee/api/deployments.html#get-a-specific-deployment
   118  func (s *DeploymentsService) GetProjectDeployment(pid interface{}, deployment int, options ...RequestOptionFunc) (*Deployment, *Response, error) {
   119  	project, err := parseID(pid)
   120  	if err != nil {
   121  		return nil, nil, err
   122  	}
   123  	u := fmt.Sprintf("projects/%s/deployments/%d", PathEscape(project), deployment)
   124  
   125  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   126  	if err != nil {
   127  		return nil, nil, err
   128  	}
   129  
   130  	d := new(Deployment)
   131  	resp, err := s.client.Do(req, d)
   132  	if err != nil {
   133  		return nil, resp, err
   134  	}
   135  
   136  	return d, resp, nil
   137  }
   138  
   139  // CreateProjectDeploymentOptions represents the available
   140  // CreateProjectDeployment() options.
   141  //
   142  // GitLab API docs:
   143  // https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment
   144  type CreateProjectDeploymentOptions struct {
   145  	Environment *string                `url:"environment,omitempty" json:"environment,omitempty"`
   146  	Ref         *string                `url:"ref,omitempty" json:"ref,omitempty"`
   147  	SHA         *string                `url:"sha,omitempty" json:"sha,omitempty"`
   148  	Tag         *bool                  `url:"tag,omitempty" json:"tag,omitempty"`
   149  	Status      *DeploymentStatusValue `url:"status,omitempty" json:"status,omitempty"`
   150  }
   151  
   152  // CreateProjectDeployment creates a project deployment.
   153  //
   154  // GitLab API docs:
   155  // https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment
   156  func (s *DeploymentsService) CreateProjectDeployment(pid interface{}, opt *CreateProjectDeploymentOptions, options ...RequestOptionFunc) (*Deployment, *Response, error) {
   157  	project, err := parseID(pid)
   158  	if err != nil {
   159  		return nil, nil, err
   160  	}
   161  	u := fmt.Sprintf("projects/%s/deployments", PathEscape(project))
   162  
   163  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   164  	if err != nil {
   165  		return nil, nil, err
   166  	}
   167  
   168  	d := new(Deployment)
   169  	resp, err := s.client.Do(req, &d)
   170  	if err != nil {
   171  		return nil, resp, err
   172  	}
   173  
   174  	return d, resp, nil
   175  }
   176  
   177  // UpdateProjectDeploymentOptions represents the available
   178  // UpdateProjectDeployment() options.
   179  //
   180  // GitLab API docs:
   181  // https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment
   182  type UpdateProjectDeploymentOptions struct {
   183  	Status *DeploymentStatusValue `url:"status,omitempty" json:"status,omitempty"`
   184  }
   185  
   186  // UpdateProjectDeployment updates a project deployment.
   187  //
   188  // GitLab API docs:
   189  // https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment
   190  func (s *DeploymentsService) UpdateProjectDeployment(pid interface{}, deployment int, opt *UpdateProjectDeploymentOptions, options ...RequestOptionFunc) (*Deployment, *Response, error) {
   191  	project, err := parseID(pid)
   192  	if err != nil {
   193  		return nil, nil, err
   194  	}
   195  	u := fmt.Sprintf("projects/%s/deployments/%d", PathEscape(project), deployment)
   196  
   197  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   198  	if err != nil {
   199  		return nil, nil, err
   200  	}
   201  
   202  	d := new(Deployment)
   203  	resp, err := s.client.Do(req, &d)
   204  	if err != nil {
   205  		return nil, resp, err
   206  	}
   207  
   208  	return d, resp, nil
   209  }
   210  
   211  // ApproveOrRejectProjectDeploymentOptions represents the available
   212  // ApproveOrRejectProjectDeployment() options.
   213  //
   214  // GitLab API docs:
   215  // https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment
   216  type ApproveOrRejectProjectDeploymentOptions struct {
   217  	Status        *DeploymentApprovalStatus `url:"status,omitempty" json:"status,omitempty"`
   218  	Comment       *string                   `url:"comment,omitempty" json:"comment,omitempty"`
   219  	RepresentedAs *string                   `url:"represented_as,omitempty" json:"represented_as,omitempty"`
   220  }
   221  
   222  // ApproveOrRejectProjectDeployment approve or reject a blocked deployment.
   223  //
   224  // GitLab API docs:
   225  // https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment
   226  func (s *DeploymentsService) ApproveOrRejectProjectDeployment(pid interface{}, deployment int,
   227  	opt *ApproveOrRejectProjectDeploymentOptions, options ...RequestOptionFunc,
   228  ) (*Response, error) {
   229  	project, err := parseID(pid)
   230  	if err != nil {
   231  		return nil, err
   232  	}
   233  	u := fmt.Sprintf("projects/%s/deployments/%d/approval", PathEscape(project), deployment)
   234  
   235  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	return s.client.Do(req, nil)
   241  }
   242  
   243  // DeleteProjectDeployment delete a project deployment.
   244  //
   245  // GitLab API docs:
   246  // https://docs.gitlab.com/ee/api/deployments.html#delete-a-specific-deployment
   247  func (s *DeploymentsService) DeleteProjectDeployment(pid interface{}, deployment int, options ...RequestOptionFunc) (*Response, error) {
   248  	project, err := parseID(pid)
   249  	if err != nil {
   250  		return nil, err
   251  	}
   252  	u := fmt.Sprintf("projects/%s/deployments/%d", PathEscape(project), deployment)
   253  
   254  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   255  	if err != nil {
   256  		return nil, err
   257  	}
   258  
   259  	return s.client.Do(req, nil)
   260  }
   261  

View as plain text