github.com/google/go-github/v66@v66.0.0/github/repos_deployments.go (about)

     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/rest/deployments/deployments#list-deployments
    67  //
    68  //meta:operation GET /repos/{owner}/{repo}/deployments
    69  func (s *RepositoriesService) ListDeployments(ctx context.Context, owner, repo string, opts *DeploymentsListOptions) ([]*Deployment, *Response, error) {
    70  	u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
    71  	u, err := addOptions(u, opts)
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  
    76  	req, err := s.client.NewRequest("GET", u, nil)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	var deployments []*Deployment
    82  	resp, err := s.client.Do(ctx, req, &deployments)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return deployments, resp, nil
    88  }
    89  
    90  // GetDeployment returns a single deployment of a repository.
    91  //
    92  // GitHub API docs: https://docs.github.com/rest/deployments/deployments#get-a-deployment
    93  //
    94  //meta:operation GET /repos/{owner}/{repo}/deployments/{deployment_id}
    95  func (s *RepositoriesService) GetDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Deployment, *Response, error) {
    96  	u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
    97  
    98  	req, err := s.client.NewRequest("GET", u, nil)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	deployment := new(Deployment)
   104  	resp, err := s.client.Do(ctx, req, deployment)
   105  	if err != nil {
   106  		return nil, resp, err
   107  	}
   108  
   109  	return deployment, resp, nil
   110  }
   111  
   112  // CreateDeployment creates a new deployment for a repository.
   113  //
   114  // GitHub API docs: https://docs.github.com/rest/deployments/deployments#create-a-deployment
   115  //
   116  //meta:operation POST /repos/{owner}/{repo}/deployments
   117  func (s *RepositoriesService) CreateDeployment(ctx context.Context, owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) {
   118  	u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
   119  
   120  	req, err := s.client.NewRequest("POST", u, request)
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	// TODO: remove custom Accept headers when APIs fully launch.
   126  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   127  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   128  
   129  	d := new(Deployment)
   130  	resp, err := s.client.Do(ctx, req, d)
   131  	if err != nil {
   132  		return nil, resp, err
   133  	}
   134  
   135  	return d, resp, nil
   136  }
   137  
   138  // DeleteDeployment deletes an existing deployment for a repository.
   139  //
   140  // GitHub API docs: https://docs.github.com/rest/deployments/deployments#delete-a-deployment
   141  //
   142  //meta:operation DELETE /repos/{owner}/{repo}/deployments/{deployment_id}
   143  func (s *RepositoriesService) DeleteDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Response, error) {
   144  	u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
   145  	req, err := s.client.NewRequest("DELETE", u, nil)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	return s.client.Do(ctx, req, nil)
   150  }
   151  
   152  // DeploymentStatus represents the status of a
   153  // particular deployment.
   154  type DeploymentStatus struct {
   155  	ID *int64 `json:"id,omitempty"`
   156  	// State is the deployment state.
   157  	// Possible values are: "pending", "success", "failure", "error",
   158  	// "inactive", "in_progress", "queued".
   159  	State          *string    `json:"state,omitempty"`
   160  	Creator        *User      `json:"creator,omitempty"`
   161  	Description    *string    `json:"description,omitempty"`
   162  	Environment    *string    `json:"environment,omitempty"`
   163  	NodeID         *string    `json:"node_id,omitempty"`
   164  	CreatedAt      *Timestamp `json:"created_at,omitempty"`
   165  	UpdatedAt      *Timestamp `json:"updated_at,omitempty"`
   166  	TargetURL      *string    `json:"target_url,omitempty"`
   167  	DeploymentURL  *string    `json:"deployment_url,omitempty"`
   168  	RepositoryURL  *string    `json:"repository_url,omitempty"`
   169  	EnvironmentURL *string    `json:"environment_url,omitempty"`
   170  	LogURL         *string    `json:"log_url,omitempty"`
   171  	URL            *string    `json:"url,omitempty"`
   172  }
   173  
   174  // DeploymentStatusRequest represents a deployment request
   175  type DeploymentStatusRequest struct {
   176  	State          *string `json:"state,omitempty"`
   177  	LogURL         *string `json:"log_url,omitempty"`
   178  	Description    *string `json:"description,omitempty"`
   179  	Environment    *string `json:"environment,omitempty"`
   180  	EnvironmentURL *string `json:"environment_url,omitempty"`
   181  	AutoInactive   *bool   `json:"auto_inactive,omitempty"`
   182  }
   183  
   184  // ListDeploymentStatuses lists the statuses of a given deployment of a repository.
   185  //
   186  // GitHub API docs: https://docs.github.com/rest/deployments/statuses#list-deployment-statuses
   187  //
   188  //meta:operation GET /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
   189  func (s *RepositoriesService) ListDeploymentStatuses(ctx context.Context, owner, repo string, deployment int64, opts *ListOptions) ([]*DeploymentStatus, *Response, error) {
   190  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
   191  	u, err := addOptions(u, opts)
   192  	if err != nil {
   193  		return nil, nil, err
   194  	}
   195  
   196  	req, err := s.client.NewRequest("GET", u, nil)
   197  	if err != nil {
   198  		return nil, nil, err
   199  	}
   200  
   201  	// TODO: remove custom Accept headers when APIs fully launch.
   202  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   203  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   204  
   205  	var statuses []*DeploymentStatus
   206  	resp, err := s.client.Do(ctx, req, &statuses)
   207  	if err != nil {
   208  		return nil, resp, err
   209  	}
   210  
   211  	return statuses, resp, nil
   212  }
   213  
   214  // GetDeploymentStatus returns a single deployment status of a repository.
   215  //
   216  // GitHub API docs: https://docs.github.com/rest/deployments/statuses#get-a-deployment-status
   217  //
   218  //meta:operation GET /repos/{owner}/{repo}/deployments/{deployment_id}/statuses/{status_id}
   219  func (s *RepositoriesService) GetDeploymentStatus(ctx context.Context, owner, repo string, deploymentID, deploymentStatusID int64) (*DeploymentStatus, *Response, error) {
   220  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses/%v", owner, repo, deploymentID, deploymentStatusID)
   221  
   222  	req, err := s.client.NewRequest("GET", u, nil)
   223  	if err != nil {
   224  		return nil, nil, err
   225  	}
   226  
   227  	// TODO: remove custom Accept headers when APIs fully launch.
   228  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   229  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   230  
   231  	d := new(DeploymentStatus)
   232  	resp, err := s.client.Do(ctx, req, d)
   233  	if err != nil {
   234  		return nil, resp, err
   235  	}
   236  
   237  	return d, resp, nil
   238  }
   239  
   240  // CreateDeploymentStatus creates a new status for a deployment.
   241  //
   242  // GitHub API docs: https://docs.github.com/rest/deployments/statuses#create-a-deployment-status
   243  //
   244  //meta:operation POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
   245  func (s *RepositoriesService) CreateDeploymentStatus(ctx context.Context, owner, repo string, deployment int64, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) {
   246  	u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
   247  
   248  	req, err := s.client.NewRequest("POST", u, request)
   249  	if err != nil {
   250  		return nil, nil, err
   251  	}
   252  
   253  	// TODO: remove custom Accept headers when APIs fully launch.
   254  	acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   255  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   256  
   257  	d := new(DeploymentStatus)
   258  	resp, err := s.client.Do(ctx, req, d)
   259  	if err != nil {
   260  		return nil, resp, err
   261  	}
   262  
   263  	return d, resp, nil
   264  }