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