github.com/google/go-github/v70@v70.0.0/github/repos_statuses.go (about)

     1  // Copyright 2013 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  	"fmt"
    11  )
    12  
    13  // RepoStatus represents the status of a repository at a particular reference.
    14  type RepoStatus struct {
    15  	ID     *int64  `json:"id,omitempty"`
    16  	NodeID *string `json:"node_id,omitempty"`
    17  	URL    *string `json:"url,omitempty"`
    18  
    19  	// State is the current state of the repository. Possible values are:
    20  	// pending, success, error, or failure.
    21  	State *string `json:"state,omitempty"`
    22  
    23  	// TargetURL is the URL of the page representing this status. It will be
    24  	// linked from the GitHub UI to allow users to see the source of the status.
    25  	TargetURL *string `json:"target_url,omitempty"`
    26  
    27  	// Description is a short high level summary of the status.
    28  	Description *string `json:"description,omitempty"`
    29  
    30  	// A string label to differentiate this status from the statuses of other systems.
    31  	Context *string `json:"context,omitempty"`
    32  
    33  	// AvatarURL is the URL of the avatar of this status.
    34  	AvatarURL *string `json:"avatar_url,omitempty"`
    35  
    36  	Creator   *User      `json:"creator,omitempty"`
    37  	CreatedAt *Timestamp `json:"created_at,omitempty"`
    38  	UpdatedAt *Timestamp `json:"updated_at,omitempty"`
    39  }
    40  
    41  func (r RepoStatus) String() string {
    42  	return Stringify(r)
    43  }
    44  
    45  // ListStatuses lists the statuses of a repository at the specified
    46  // reference. ref can be a SHA, a branch name, or a tag name.
    47  //
    48  // GitHub API docs: https://docs.github.com/rest/commits/statuses#list-commit-statuses-for-a-reference
    49  //
    50  //meta:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses
    51  func (s *RepositoriesService) ListStatuses(ctx context.Context, owner, repo, ref string, opts *ListOptions) ([]*RepoStatus, *Response, error) {
    52  	u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, refURLEscape(ref))
    53  	u, err := addOptions(u, opts)
    54  	if err != nil {
    55  		return nil, nil, err
    56  	}
    57  
    58  	req, err := s.client.NewRequest("GET", u, nil)
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	var statuses []*RepoStatus
    64  	resp, err := s.client.Do(ctx, req, &statuses)
    65  	if err != nil {
    66  		return nil, resp, err
    67  	}
    68  
    69  	return statuses, resp, nil
    70  }
    71  
    72  // CreateStatus creates a new status for a repository at the specified
    73  // reference. Ref can be a SHA, a branch name, or a tag name.
    74  //
    75  // GitHub API docs: https://docs.github.com/rest/commits/statuses#create-a-commit-status
    76  //
    77  //meta:operation POST /repos/{owner}/{repo}/statuses/{sha}
    78  func (s *RepositoriesService) CreateStatus(ctx context.Context, owner, repo, ref string, status *RepoStatus) (*RepoStatus, *Response, error) {
    79  	u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, refURLEscape(ref))
    80  	req, err := s.client.NewRequest("POST", u, status)
    81  	if err != nil {
    82  		return nil, nil, err
    83  	}
    84  
    85  	repoStatus := new(RepoStatus)
    86  	resp, err := s.client.Do(ctx, req, repoStatus)
    87  	if err != nil {
    88  		return nil, resp, err
    89  	}
    90  
    91  	return repoStatus, resp, nil
    92  }
    93  
    94  // CombinedStatus represents the combined status of a repository at a particular reference.
    95  type CombinedStatus struct {
    96  	// State is the combined state of the repository. Possible values are:
    97  	// failure, pending, or success.
    98  	State *string `json:"state,omitempty"`
    99  
   100  	Name       *string       `json:"name,omitempty"`
   101  	SHA        *string       `json:"sha,omitempty"`
   102  	TotalCount *int          `json:"total_count,omitempty"`
   103  	Statuses   []*RepoStatus `json:"statuses,omitempty"`
   104  
   105  	CommitURL     *string `json:"commit_url,omitempty"`
   106  	RepositoryURL *string `json:"repository_url,omitempty"`
   107  }
   108  
   109  func (s CombinedStatus) String() string {
   110  	return Stringify(s)
   111  }
   112  
   113  // GetCombinedStatus returns the combined status of a repository at the specified
   114  // reference. ref can be a SHA, a branch name, or a tag name.
   115  //
   116  // GitHub API docs: https://docs.github.com/rest/commits/statuses#get-the-combined-status-for-a-specific-reference
   117  //
   118  //meta:operation GET /repos/{owner}/{repo}/commits/{ref}/status
   119  func (s *RepositoriesService) GetCombinedStatus(ctx context.Context, owner, repo, ref string, opts *ListOptions) (*CombinedStatus, *Response, error) {
   120  	u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, refURLEscape(ref))
   121  	u, err := addOptions(u, opts)
   122  	if err != nil {
   123  		return nil, nil, err
   124  	}
   125  
   126  	req, err := s.client.NewRequest("GET", u, nil)
   127  	if err != nil {
   128  		return nil, nil, err
   129  	}
   130  
   131  	status := new(CombinedStatus)
   132  	resp, err := s.client.Do(ctx, req, status)
   133  	if err != nil {
   134  		return nil, resp, err
   135  	}
   136  
   137  	return status, resp, nil
   138  }