From 6a500f0a3f918f5c1df2be8baa075d60dfe5046d Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 31 May 2013 13:15:57 -0700 Subject: [PATCH] add repo status methods (list and create) --- github/repos.go | 52 ++++++++++++++++++++++++++++++++ github/repos_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/github/repos.go b/github/repos.go index f41d5d6..06b89b0 100644 --- a/github/repos.go +++ b/github/repos.go @@ -240,3 +240,55 @@ func (s *RepositoriesService) CreateFork(owner, repo string, opt *RepositoryCrea _, err = s.client.Do(req, fork) return fork, err } + +// RepoStatus represents the status of a repository at a particular reference. +type RepoStatus struct { + ID int `json:"id,omitempty"` + + // State is the current state of the repository. Possible values are: + // pending, success, error, or failure. + State string `json:"state,omitempty"` + + // TargetURL is the URL of the page representing this status. It will be + // linked from the GitHub UI to allow users to see the source of the status. + TargetURL string `json:"target_url,omitempty"` + + // Description is a short high level summary of the status. + Description string `json:"description,omitempty"` + + Creator *User `json:"creator,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` +} + +// ListStatuses lists the statuses of a repository at the specified +// reference. ref can be a SHA, a branch name, or a tag name. +// +// GitHub API docs: http://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref +func (s *RepositoriesService) ListStatuses(owner, repo, ref string) ([]RepoStatus, error) { + url_ := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, ref) + req, err := s.client.NewRequest("GET", url_, nil) + if err != nil { + return nil, err + } + + statuses := new([]RepoStatus) + _, err = s.client.Do(req, statuses) + return *statuses, err +} + +// CreateStatus creates a new status for a repository at the specified +// reference. Ref can be a SHA, a branch name, or a tag name. +// +// GitHub API docs: http://developer.github.com/v3/repos/statuses/#create-a-status +func (s *RepositoriesService) CreateStatus(owner, repo, ref string, status *RepoStatus) (*RepoStatus, error) { + url_ := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, ref) + req, err := s.client.NewRequest("POST", url_, status) + if err != nil { + return nil, err + } + + statuses := new(RepoStatus) + _, err = s.client.Do(req, statuses) + return statuses, err +} diff --git a/github/repos_test.go b/github/repos_test.go index 0a853b3..b6f5a36 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -328,3 +328,75 @@ func TestRepositoriesService_CreateFork_invalidOwner(t *testing.T) { t.Errorf("Expected URL parse error, got %+v", err) } } + +func TestRepositoriesService_ListStatuses(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { + if m := "GET"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + fmt.Fprint(w, `[{"id":1}]`) + }) + + statuses, err := client.Repositories.ListStatuses("o", "r", "r") + if err != nil { + t.Errorf("Repositories.ListStatuses returned error: %v", err) + } + + want := []RepoStatus{RepoStatus{ID: 1}} + if !reflect.DeepEqual(statuses, want) { + t.Errorf("Repositories.ListStatuses returned %+v, want %+v", statuses, want) + } +} + +func TestRepositoriesService_ListStatuses_invalidOwner(t *testing.T) { + _, err := client.Repositories.ListStatuses("%", "r", "r") + if err == nil { + t.Errorf("Expected error to be returned") + } + if err, ok := err.(*url.Error); !ok { + t.Errorf("Expected URL parse error, got %+v", err) + } +} + +func TestRepositoriesService_CreateStatus(t *testing.T) { + setup() + defer teardown() + + input := &RepoStatus{State: "s", TargetURL: "t", Description: "d"} + + mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { + v := new(RepoStatus) + json.NewDecoder(r.Body).Decode(v) + + if m := "POST"; m != r.Method { + t.Errorf("Request method = %v, want %v", r.Method, m) + } + if !reflect.DeepEqual(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + fmt.Fprint(w, `{"id":1}`) + }) + + status, err := client.Repositories.CreateStatus("o", "r", "r", input) + if err != nil { + t.Errorf("Repositories.CreateStatus returned error: %v", err) + } + + want := &RepoStatus{ID: 1} + if !reflect.DeepEqual(status, want) { + t.Errorf("Repositories.CreateStatus returned %+v, want %+v", status, want) + } +} + +func TestRepositoriesService_CreateStatus_invalidOwner(t *testing.T) { + _, err := client.Repositories.CreateStatus("%", "r", "r", nil) + if err == nil { + t.Errorf("Expected error to be returned") + } + if err, ok := err.(*url.Error); !ok { + t.Errorf("Expected URL parse error, got %+v", err) + } +}