Browse Source

implement remaining Activity.Star* methods

Will Norris 12 years ago
parent
commit
bf23aef54b
3 changed files with 230 additions and 0 deletions
  1. +62
    -0
      github/activity_star.go
  2. +103
    -0
      github/activity_star_test.go
  3. +65
    -0
      tests/integration/activity_test.go

+ 62
- 0
github/activity_star.go View File

@ -7,6 +7,30 @@ package github
import "fmt" 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 // ActivityListStarredOptions specifies the optional parameters to the
// ActivityService.ListStarred method. // ActivityService.ListStarred method.
type ActivityListStarredOptions struct { type ActivityListStarredOptions struct {
@ -50,3 +74,41 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
return *repos, resp, err 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)
}

+ 103
- 0
github/activity_star_test.go View File

@ -12,6 +12,30 @@ import (
"testing" "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) { func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
@ -62,3 +86,82 @@ func TestActivityService_ListStarred_invalidUser(t *testing.T) {
_, _, err := client.Activity.ListStarred("%", nil) _, _, err := client.Activity.ListStarred("%", nil)
testURLParseError(t, err) 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)
}

+ 65
- 0
tests/integration/activity_test.go View File

@ -11,7 +11,72 @@ import (
"github.com/google/go-github/github" "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) { 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") { if !checkAuth("TestActivity_Watching") {
return return
} }


Loading…
Cancel
Save