From bf23aef54b1b90ff39a223ede1beed5750974c00 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 10 Apr 2014 15:55:37 -0700 Subject: [PATCH] implement remaining Activity.Star* methods --- github/activity_star.go | 62 +++++++++++++++++ github/activity_star_test.go | 103 +++++++++++++++++++++++++++++ tests/integration/activity_test.go | 65 ++++++++++++++++++ 3 files changed, 230 insertions(+) diff --git a/github/activity_star.go b/github/activity_star.go index e2473f7..982f24d 100644 --- a/github/activity_star.go +++ b/github/activity_star.go @@ -7,6 +7,30 @@ package github import "fmt" +// ListStargazers lists people who have starred the specified repo. +// +// GitHub API Docs: https://developer.github.com/v3/activity/starring/#list-stargazers +func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) ([]User, *Response, error) { + u := fmt.Sprintf("repos/%s/%s/stargazers", owner, repo) + 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 + } + + stargazers := new([]User) + resp, err := s.client.Do(req, stargazers) + if err != nil { + return nil, resp, err + } + + return *stargazers, resp, err +} + // ActivityListStarredOptions specifies the optional parameters to the // ActivityService.ListStarred method. type ActivityListStarredOptions struct { @@ -50,3 +74,41 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio return *repos, resp, err } + +// IsStarred checks if a repository is starred by authenticated user. +// +// GitHub API docs: https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository +func (s *ActivityService) IsStarred(owner, repo string) (bool, *Response, error) { + u := fmt.Sprintf("user/starred/%v/%v", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return false, nil, err + } + resp, err := s.client.Do(req, nil) + starred, err := parseBoolResponse(err) + return starred, resp, err +} + +// Star a repository as the authenticated user. +// +// GitHub API docs: https://developer.github.com/v3/activity/starring/#star-a-repository +func (s *ActivityService) Star(owner, repo string) (*Response, error) { + u := fmt.Sprintf("user/starred/%v/%v", owner, repo) + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + return s.client.Do(req, nil) +} + +// Unstar a repository as the authenticated user. +// +// GitHub API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository +func (s *ActivityService) Unstar(owner, repo string) (*Response, error) { + u := fmt.Sprintf("user/starred/%v/%v", owner, repo) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + return s.client.Do(req, nil) +} diff --git a/github/activity_star_test.go b/github/activity_star_test.go index 3ff34d0..ae33b93 100644 --- a/github/activity_star_test.go +++ b/github/activity_star_test.go @@ -12,6 +12,30 @@ import ( "testing" ) +func TestActivityService_ListStargazers(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "page": "2", + }) + + fmt.Fprint(w, `[{"id":1}]`) + }) + + stargazers, _, err := client.Activity.ListStargazers("o", "r", &ListOptions{Page: 2}) + if err != nil { + t.Errorf("Activity.ListStargazers returned error: %v", err) + } + + want := []User{{ID: Int(1)}} + if !reflect.DeepEqual(stargazers, want) { + t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want) + } +} + func TestActivityService_ListStarred_authenticatedUser(t *testing.T) { setup() defer teardown() @@ -62,3 +86,82 @@ func TestActivityService_ListStarred_invalidUser(t *testing.T) { _, _, err := client.Activity.ListStarred("%", nil) testURLParseError(t, err) } + +func TestActivityService_IsStarred_hasStar(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNoContent) + }) + + star, _, err := client.Activity.IsStarred("o", "r") + if err != nil { + t.Errorf("Activity.IsStarred returned error: %v", err) + } + if want := true; star != want { + t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want) + } +} + +func TestActivityService_IsStarred_noStar(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + star, _, err := client.Activity.IsStarred("o", "r") + if err != nil { + t.Errorf("Activity.IsStarred returned error: %v", err) + } + if want := false; star != want { + t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want) + } +} + +func TestActivityService_IsStarred_invalidID(t *testing.T) { + _, _, err := client.Activity.IsStarred("%", "%") + testURLParseError(t, err) +} + +func TestActivityService_Star(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + }) + + _, err := client.Activity.Star("o", "r") + if err != nil { + t.Errorf("Activity.Star returned error: %v", err) + } +} + +func TestActivityService_Star_invalidID(t *testing.T) { + _, err := client.Activity.Star("%", "%") + testURLParseError(t, err) +} + +func TestActivityService_Unstar(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Activity.Unstar("o", "r") + if err != nil { + t.Errorf("Activity.Unstar returned error: %v", err) + } +} + +func TestActivityService_Unstar_invalidID(t *testing.T) { + _, err := client.Activity.Unstar("%", "%") + testURLParseError(t, err) +} diff --git a/tests/integration/activity_test.go b/tests/integration/activity_test.go index 30b4d2b..5ca64da 100644 --- a/tests/integration/activity_test.go +++ b/tests/integration/activity_test.go @@ -11,7 +11,72 @@ import ( "github.com/google/go-github/github" ) +func TestActivity_Starring(t *testing.T) { + stargazers, _, err := client.Activity.ListStargazers("google", "go-github", nil) + if err != nil { + t.Fatalf("Activity.ListStargazers returned error: %v", err) + } + + if len(stargazers) == 0 { + t.Errorf("Activity.ListStargazers('google', 'go-github') returned no stargazers") + } + + // the rest of the tests requires auth + if !checkAuth("TestActivity_Starring") { + return + } + + // first, check if already starred google/go-github + star, _, err := client.Activity.IsStarred("google", "go-github") + if err != nil { + t.Fatalf("Activity.IsStarred returned error: %v", err) + } + if star { + t.Fatalf("Already starring google/go-github. Please manually unstar it first.") + } + + // star google/go-github + _, err = client.Activity.Star("google", "go-github") + if err != nil { + t.Fatalf("Activity.Star returned error: %v", err) + } + + // check again and verify starred + star, _, err = client.Activity.IsStarred("google", "go-github") + if err != nil { + t.Fatalf("Activity.IsStarred returned error: %v", err) + } + if !star { + t.Fatalf("Not starred google/go-github after starring it.") + } + + // unstar + _, err = client.Activity.Unstar("google", "go-github") + if err != nil { + t.Fatalf("Activity.Unstar returned error: %v", err) + } + + // check again and verify not watching + star, _, err = client.Activity.IsStarred("google", "go-github") + if err != nil { + t.Fatalf("Activity.IsStarred returned error: %v", err) + } + if star { + t.Fatalf("Still starred google/go-github after unstarring it.") + } +} + func TestActivity_Watching(t *testing.T) { + watchers, _, err := client.Activity.ListWatchers("google", "go-github", nil) + if err != nil { + t.Fatalf("Activity.ListWatchers returned error: %v", err) + } + + if len(watchers) == 0 { + t.Errorf("Activity.ListWatchers('google', 'go-github') returned no watchers") + } + + // the rest of the tests requires auth if !checkAuth("TestActivity_Watching") { return }