github.com/google/go-github/v50@v50.2.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/en/rest/commits/statuses#list-commit-statuses-for-a-reference 49 func (s *RepositoriesService) ListStatuses(ctx context.Context, owner, repo, ref string, opts *ListOptions) ([]*RepoStatus, *Response, error) { 50 u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, refURLEscape(ref)) 51 u, err := addOptions(u, opts) 52 if err != nil { 53 return nil, nil, err 54 } 55 56 req, err := s.client.NewRequest("GET", u, nil) 57 if err != nil { 58 return nil, nil, err 59 } 60 61 var statuses []*RepoStatus 62 resp, err := s.client.Do(ctx, req, &statuses) 63 if err != nil { 64 return nil, resp, err 65 } 66 67 return statuses, resp, nil 68 } 69 70 // CreateStatus creates a new status for a repository at the specified 71 // reference. Ref can be a SHA, a branch name, or a tag name. 72 // 73 // GitHub API docs: https://docs.github.com/en/rest/commits/statuses#create-a-commit-status 74 func (s *RepositoriesService) CreateStatus(ctx context.Context, owner, repo, ref string, status *RepoStatus) (*RepoStatus, *Response, error) { 75 u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, refURLEscape(ref)) 76 req, err := s.client.NewRequest("POST", u, status) 77 if err != nil { 78 return nil, nil, err 79 } 80 81 repoStatus := new(RepoStatus) 82 resp, err := s.client.Do(ctx, req, repoStatus) 83 if err != nil { 84 return nil, resp, err 85 } 86 87 return repoStatus, resp, nil 88 } 89 90 // CombinedStatus represents the combined status of a repository at a particular reference. 91 type CombinedStatus struct { 92 // State is the combined state of the repository. Possible values are: 93 // failure, pending, or success. 94 State *string `json:"state,omitempty"` 95 96 Name *string `json:"name,omitempty"` 97 SHA *string `json:"sha,omitempty"` 98 TotalCount *int `json:"total_count,omitempty"` 99 Statuses []*RepoStatus `json:"statuses,omitempty"` 100 101 CommitURL *string `json:"commit_url,omitempty"` 102 RepositoryURL *string `json:"repository_url,omitempty"` 103 } 104 105 func (s CombinedStatus) String() string { 106 return Stringify(s) 107 } 108 109 // GetCombinedStatus returns the combined status of a repository at the specified 110 // reference. ref can be a SHA, a branch name, or a tag name. 111 // 112 // GitHub API docs: https://docs.github.com/en/rest/commits/statuses#get-the-combined-status-for-a-specific-reference 113 func (s *RepositoriesService) GetCombinedStatus(ctx context.Context, owner, repo, ref string, opts *ListOptions) (*CombinedStatus, *Response, error) { 114 u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, refURLEscape(ref)) 115 u, err := addOptions(u, opts) 116 if err != nil { 117 return nil, nil, err 118 } 119 120 req, err := s.client.NewRequest("GET", u, nil) 121 if err != nil { 122 return nil, nil, err 123 } 124 125 status := new(CombinedStatus) 126 resp, err := s.client.Do(ctx, req, status) 127 if err != nil { 128 return nil, resp, err 129 } 130 131 return status, resp, nil 132 }