...

Source file src/github.com/google/go-github/v33/github/repos_deployments.go

Documentation: github.com/google/go-github/v33/github

     1  // Copyright 2014 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"strings"
    13  )
    14  
    15  // Deployment represents a deployment in a repo
    16  type Deployment struct {
    17  	URL           *string         `json:"url,omitempty"`
    18  	ID            *int64          `json:"id,omitempty"`
    19  	SHA           *string         `json:"sha,omitempty"`
    20  	Ref           *string         `json:"ref,omitempty"`
    21  	Task          *string         `json:"task,omitempty"`
    22  	Payload       json.RawMessage `json:"payload,omitempty"`
    23  	Environment   *string         `json:"environment,omitempty"`
    24  	Description   *string         `json:"description,omitempty"`
    25  	Creator       *User           `json:"creator,omitempty"`
    26  	CreatedAt     *Timestamp      `json:"created_at,omitempty"`
    27  	UpdatedAt     *Timestamp      `json:"updated_at,omitempty"`
    28  	StatusesURL   *string         `json:"statuses_url,omitempty"`
    29  	RepositoryURL *string         `json:"repository_url,omitempty"`
    30  	NodeID        *string         `json:"node_id,omitempty"`
    31  }
    32  
    33  // DeploymentRequest represents a deployment request
    34  type DeploymentRequest struct {
    35  	Ref                   *string     `json:"ref,omitempty"`
    36  	Task                  *string     `json:"task,omitempty"`
    37  	AutoMerge             *bool       `json:"auto_merge,omitempty"`
    38  	RequiredContexts      *[]string   `json:"required_contexts,omitempty"`
    39  	Payload               interface{} `json:"payload,omitempty"`
    40  	Environment           *string     `json:"environment,omitempty"`
    41  	Description           *string     `json:"description,omitempty"`
    42  	TransientEnvironment  *bool       `json:"transient_environment,omitempty"`
    43  	ProductionEnvironment *bool       `json:"production_environment,omitempty"`
    44  }
    45  
    46  // DeploymentsListOptions specifies the optional parameters to the
    47  // RepositoriesService.ListDeployments method.
    48  type DeploymentsListOptions struct {
    49  	// SHA of the Deployment.
    50  	SHA string `url:"sha,omitempty"`
    51  
    52  	// List deployments for a given ref.
    53  	Ref string `url:"ref,omitempty"`
    54  
    55  	// List deployments for a given task.
    56  	Task string `url:"task,omitempty"`
    57  
    58  	// List deployments for a given environment.
    59  	Environment string `url:"environment,omitempty"`
    60  
    61  	ListOptions
    62  }
    63  
    64  // ListDeployments lists the deployments of a repository.
    65  //
    66  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-deployments
    67  func (s *RepositoriesService) ListDeployments(ctx context.Context, owner, repo string, opts *DeploymentsListOptions) ([]*Deployment, *Response, error) {
    68  	u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
    69  	u, err := addOptions(u, opts)
    70  	if err != nil {
    71  		return nil, nil, err
    72  	}
    73  
    74  	req, err := s.client.NewRequest("GET", u, nil)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  
    79  	var deployments []*Deployment
    80  	resp, err := s.client.Do(ctx, req, &deployments)
    81  	if err != nil {
    82  		return nil, resp, err
    83  	}
    84  
    85  	return deployments, resp, nil
    86  }
    87  
    88  // GetDeployment returns a single deployment of a repository.
    89  //
    90  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-deployment
    91  func (s *RepositoriesService) GetDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Deployment, *Response, error) {
    92  	u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
    93  
    94  	req, err := s.client.NewRequest("GET", u, nil)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  
    99  	deployment := new(Deployment)
   100  	resp, err := s.client.Do(ctx, req, deployment)
   101  	if err != nil {
   102  		return nil, resp, err
   103  	}
   104  
   105  	return deployment, resp, nil
   106  }
   107  
   108  // CreateDeployment creates a new deployment for a repository.
   109  //
   110  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-deployment
   111  func (s *RepositoriesService) CreateDeployment(ctx context.Context, owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) {
   112  	u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
   113  
   114  	req, err := s.client.NewRequest("POST", u, request)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  
   119  	// TODO: remove custom Accept headers when APIs fully launch.
   120  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   121  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   122  
   123  	d := new(Deployment)
   124  	resp, err := s.client.Do(ctx, req, d)
   125  	if err != nil {
   126  		return nil, resp, err
   127  	}
   128  
   129  	return d, resp, nil
   130  }
   131  
   132  // DeleteDeployment deletes an existing deployment for a repository.
   133  //
   134  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#delete-a-deployment
   135  func (s *RepositoriesService) DeleteDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Response, error) {
   136  	u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
   137  	req, err := s.client.NewRequest("DELETE", u, nil)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	return s.client.Do(ctx, req, nil)
   142  }
   143  
   144  // DeploymentStatus represents the status of a
   145  // particular deployment.
   146  type DeploymentStatus struct {
   147  	ID *int64 `json:"id,omitempty"`
   148  	// State is the deployment state.
   149  	// Possible values are: "pending", "success", "failure", "error",
   150  	// "inactive", "in_progress", "queued".
   151  	State          *string    `json:"state,omitempty"`
   152  	Creator        *User      `json:"creator,omitempty"`
   153  	Description    *string    `json:"description,omitempty"`
   154  	Environment    *string    `json:"environment,omitempty"`
   155  	NodeID         *string    `json:"node_id,omitempty"`
   156  	CreatedAt      *Timestamp `json:"created_at,omitempty"`
   157  	UpdatedAt      *Timestamp `json:"updated_at,omitempty"`
   158  	TargetURL      *string    `json:"target_url,omitempty"`
   159  	DeploymentURL  *string    `json:"deployment_url,omitempty"`
   160  	RepositoryURL  *string    `json:"repository_url,omitempty"`
   161  	EnvironmentURL *string    `json:"environment_url,omitempty"`
   162  	LogURL         *string    `json:"log_url,omitempty"`
   163  	URL            *string    `json:"url,omitempty"`
   164  }
   165  
   166  // DeploymentStatusRequest represents a deployment request
   167  type DeploymentStatusRequest struct {
   168  	State          *string `json:"state,omitempty"`
   169  	LogURL         *string `json:"log_url,omitempty"`
   170  	Description    *string `json:"description,omitempty"`
   171  	Environment    *string `json:"environment,omitempty"`
   172  	EnvironmentURL *string `json:"environment_url,omitempty"`
   173  	AutoInactive   *bool   `json:"auto_inactive,omitempty"`
   174  }
   175  
   176  // ListDeploymentStatuses lists the statuses of a given deployment of a repository.
   177  //
   178  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-deployment-statuses
   179  func (s *RepositoriesService) ListDeploymentStatuses(ctx context.Context, owner, repo string, deployment int64, opts *ListOptions) ([]*DeploymentStatus, *Response, error) {
   180  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
   181  	u, err := addOptions(u, opts)
   182  	if err != nil {
   183  		return nil, nil, err
   184  	}
   185  
   186  	req, err := s.client.NewRequest("GET", u, nil)
   187  	if err != nil {
   188  		return nil, nil, err
   189  	}
   190  
   191  	// TODO: remove custom Accept headers when APIs fully launch.
   192  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   193  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   194  
   195  	var statuses []*DeploymentStatus
   196  	resp, err := s.client.Do(ctx, req, &statuses)
   197  	if err != nil {
   198  		return nil, resp, err
   199  	}
   200  
   201  	return statuses, resp, nil
   202  }
   203  
   204  // GetDeploymentStatus returns a single deployment status of a repository.
   205  //
   206  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-deployment-status
   207  func (s *RepositoriesService) GetDeploymentStatus(ctx context.Context, owner, repo string, deploymentID, deploymentStatusID int64) (*DeploymentStatus, *Response, error) {
   208  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses/%v", owner, repo, deploymentID, deploymentStatusID)
   209  
   210  	req, err := s.client.NewRequest("GET", u, nil)
   211  	if err != nil {
   212  		return nil, nil, err
   213  	}
   214  
   215  	// TODO: remove custom Accept headers when APIs fully launch.
   216  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   217  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   218  
   219  	d := new(DeploymentStatus)
   220  	resp, err := s.client.Do(ctx, req, d)
   221  	if err != nil {
   222  		return nil, resp, err
   223  	}
   224  
   225  	return d, resp, nil
   226  }
   227  
   228  // CreateDeploymentStatus creates a new status for a deployment.
   229  //
   230  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#create-a-deployment-status
   231  func (s *RepositoriesService) CreateDeploymentStatus(ctx context.Context, owner, repo string, deployment int64, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) {
   232  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
   233  
   234  	req, err := s.client.NewRequest("POST", u, request)
   235  	if err != nil {
   236  		return nil, nil, err
   237  	}
   238  
   239  	// TODO: remove custom Accept headers when APIs fully launch.
   240  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   241  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   242  
   243  	d := new(DeploymentStatus)
   244  	resp, err := s.client.Do(ctx, req, d)
   245  	if err != nil {
   246  		return nil, resp, err
   247  	}
   248  
   249  	return d, resp, nil
   250  }
   251  

View as plain text