deployments.go

  1//
  2// Copyright 2018, 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
 16package gitlab
 17
 18import (
 19	"fmt"
 20	"time"
 21)
 22
 23// DeploymentsService handles communication with the deployment related methods
 24// of the GitLab API.
 25//
 26// GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html
 27type DeploymentsService struct {
 28	client *Client
 29}
 30
 31// Deployment represents the Gitlab deployment
 32type Deployment struct {
 33	ID          int          `json:"id"`
 34	IID         int          `json:"iid"`
 35	Ref         string       `json:"ref"`
 36	SHA         string       `json:"sha"`
 37	CreatedAt   *time.Time   `json:"created_at"`
 38	User        *ProjectUser `json:"user"`
 39	Environment *Environment `json:"environment"`
 40	Deployable  struct {
 41		ID         int        `json:"id"`
 42		Status     string     `json:"status"`
 43		Stage      string     `json:"stage"`
 44		Name       string     `json:"name"`
 45		Ref        string     `json:"ref"`
 46		Tag        bool       `json:"tag"`
 47		Coverage   float64    `json:"coverage"`
 48		CreatedAt  *time.Time `json:"created_at"`
 49		StartedAt  *time.Time `json:"started_at"`
 50		FinishedAt *time.Time `json:"finished_at"`
 51		Duration   float64    `json:"duration"`
 52		User       *User      `json:"user"`
 53		Commit     *Commit    `json:"commit"`
 54		Pipeline   struct {
 55			ID     int    `json:"id"`
 56			SHA    string `json:"sha"`
 57			Ref    string `json:"ref"`
 58			Status string `json:"status"`
 59		} `json:"pipeline"`
 60		Runner *Runner `json:"runner"`
 61	} `json:"deployable"`
 62}
 63
 64// ListProjectDeploymentsOptions represents the available ListProjectDeployments() options.
 65//
 66// GitLab API docs:
 67// https://docs.gitlab.com/ce/api/deployments.html#list-project-deployments
 68type ListProjectDeploymentsOptions struct {
 69	ListOptions
 70	OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
 71	Sort    *string `url:"sort,omitempty" json:"sort,omitempty"`
 72}
 73
 74// ListProjectDeployments gets a list of deployments in a project.
 75//
 76// GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html#list-project-deployments
 77func (s *DeploymentsService) ListProjectDeployments(pid interface{}, opts *ListProjectDeploymentsOptions, options ...OptionFunc) ([]*Deployment, *Response, error) {
 78	project, err := parseID(pid)
 79	if err != nil {
 80		return nil, nil, err
 81	}
 82	u := fmt.Sprintf("projects/%s/deployments", pathEscape(project))
 83
 84	req, err := s.client.NewRequest("GET", u, opts, options)
 85	if err != nil {
 86		return nil, nil, err
 87	}
 88
 89	var ds []*Deployment
 90	resp, err := s.client.Do(req, &ds)
 91	if err != nil {
 92		return nil, resp, err
 93	}
 94
 95	return ds, resp, err
 96}
 97
 98// GetProjectDeployment get a deployment for a project.
 99//
100// GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html#get-a-specific-deployment
101func (s *DeploymentsService) GetProjectDeployment(pid interface{}, deployment int, options ...OptionFunc) (*Deployment, *Response, error) {
102	project, err := parseID(pid)
103	if err != nil {
104		return nil, nil, err
105	}
106	u := fmt.Sprintf("projects/%s/deployments/%d", pathEscape(project), deployment)
107
108	req, err := s.client.NewRequest("GET", u, nil, options)
109	if err != nil {
110		return nil, nil, err
111	}
112
113	d := new(Deployment)
114	resp, err := s.client.Do(req, d)
115	if err != nil {
116		return nil, resp, err
117	}
118
119	return d, resp, err
120}