From 3ac66ef9d6974722adb5f7a1f5d368b7992a92bc Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 29 Oct 2014 11:44:18 -0700 Subject: [PATCH] add support for combined status --- github/repos_statuses.go | 55 +++++++++++++++++++++++++++++++---- github/repos_statuses_test.go | 24 ++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/github/repos_statuses.go b/github/repos_statuses.go index 1fd55bc..0379a2b 100644 --- a/github/repos_statuses.go +++ b/github/repos_statuses.go @@ -12,7 +12,8 @@ import ( // RepoStatus represents the status of a repository at a particular reference. type RepoStatus struct { - ID *int `json:"id,omitempty"` + ID *int `json:"id,omitempty"` + URL *string `json:"url,omitempty"` // State is the current state of the repository. Possible values are: // pending, success, error, or failure. @@ -42,7 +43,7 @@ func (r RepoStatus) String() string { // // GitHub API docs: http://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref func (s *RepositoriesService) ListStatuses(owner, repo, ref string, opt *ListOptions) ([]RepoStatus, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, ref) + u := fmt.Sprintf("repos/%v/%v/commits/%v/statuses", owner, repo, ref) u, err := addOptions(u, opt) if err != nil { return nil, nil, err @@ -73,11 +74,55 @@ func (s *RepositoriesService) CreateStatus(owner, repo, ref string, status *Repo return nil, nil, err } - statuses := new(RepoStatus) - resp, err := s.client.Do(req, statuses) + repoStatus := new(RepoStatus) + resp, err := s.client.Do(req, repoStatus) + if err != nil { + return nil, resp, err + } + + return repoStatus, resp, err +} + +// CombinedStatus represents the combined status of a repository at a particular reference. +type CombinedStatus struct { + // State is the combined state of the repository. Possible values are: + // failture, pending, or success. + State *string `json:"state,omitempty"` + + Name *string `json:"name,omitempty"` + SHA *string `json:"sha,omitempty"` + TotalCount *int `json:"total_count,omitempty"` + Statuses []RepoStatus `json:"statuses,omitempty"` + + CommitURL *string `json:"commit_url,omitempty"` + RepositoryURL *string `json:"repository_url,omitempty"` +} + +func (s CombinedStatus) String() string { + return Stringify(s) +} + +// GetCombinedStatus returns the combined status of a repository at the specified +// reference. ref can be a SHA, a branch name, or a tag name. +// +// GitHub API docs: https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref +func (s *RepositoriesService) GetCombinedStatus(owner, repo, ref string, opt *ListOptions) (*CombinedStatus, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/commits/%v/status", owner, repo, ref) + u, err := addOptions(u, opt) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + status := new(CombinedStatus) + resp, err := s.client.Do(req, status) if err != nil { return nil, resp, err } - return statuses, resp, err + return status, resp, err } diff --git a/github/repos_statuses_test.go b/github/repos_statuses_test.go index ebce542..8b23052 100644 --- a/github/repos_statuses_test.go +++ b/github/repos_statuses_test.go @@ -17,7 +17,7 @@ func TestRepositoriesService_ListStatuses(t *testing.T) { setup() defer teardown() - mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/repos/o/r/commits/r/statuses", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") testFormValues(t, r, values{"page": "2"}) fmt.Fprint(w, `[{"id":1}]`) @@ -72,3 +72,25 @@ func TestRepositoriesService_CreateStatus_invalidOwner(t *testing.T) { _, _, err := client.Repositories.CreateStatus("%", "r", "r", nil) testURLParseError(t, err) } + +func TestRepositoriesService_GetCombinedStatus(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/commits/r/status", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"page": "2"}) + fmt.Fprint(w, `{"state":"success", "statuses":[{"id":1}]}`) + }) + + opt := &ListOptions{Page: 2} + status, _, err := client.Repositories.GetCombinedStatus("o", "r", "r", opt) + if err != nil { + t.Errorf("Repositories.GetCombinedStatus returned error: %v", err) + } + + want := &CombinedStatus{State: String("success"), Statuses: []RepoStatus{{ID: Int(1)}}} + if !reflect.DeepEqual(status, want) { + t.Errorf("Repositories.GetCombinedStatus returned %+v, want %+v", status, want) + } +}