Skip to content

Commit bf23aef

Browse files
committed
implement remaining Activity.Star* methods
1 parent 75b5f1b commit bf23aef

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

github/activity_star.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ package github
77

88
import "fmt"
99

10+
// ListStargazers lists people who have starred the specified repo.
11+
//
12+
// GitHub API Docs: https://developer.github.com/v3/activity/starring/#list-stargazers
13+
func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) ([]User, *Response, error) {
14+
u := fmt.Sprintf("repos/%s/%s/stargazers", owner, repo)
15+
u, err := addOptions(u, opt)
16+
if err != nil {
17+
return nil, nil, err
18+
}
19+
20+
req, err := s.client.NewRequest("GET", u, nil)
21+
if err != nil {
22+
return nil, nil, err
23+
}
24+
25+
stargazers := new([]User)
26+
resp, err := s.client.Do(req, stargazers)
27+
if err != nil {
28+
return nil, resp, err
29+
}
30+
31+
return *stargazers, resp, err
32+
}
33+
1034
// ActivityListStarredOptions specifies the optional parameters to the
1135
// ActivityService.ListStarred method.
1236
type ActivityListStarredOptions struct {
@@ -50,3 +74,41 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
5074

5175
return *repos, resp, err
5276
}
77+
78+
// IsStarred checks if a repository is starred by authenticated user.
79+
//
80+
// GitHub API docs: https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository
81+
func (s *ActivityService) IsStarred(owner, repo string) (bool, *Response, error) {
82+
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
83+
req, err := s.client.NewRequest("GET", u, nil)
84+
if err != nil {
85+
return false, nil, err
86+
}
87+
resp, err := s.client.Do(req, nil)
88+
starred, err := parseBoolResponse(err)
89+
return starred, resp, err
90+
}
91+
92+
// Star a repository as the authenticated user.
93+
//
94+
// GitHub API docs: https://developer.github.com/v3/activity/starring/#star-a-repository
95+
func (s *ActivityService) Star(owner, repo string) (*Response, error) {
96+
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
97+
req, err := s.client.NewRequest("PUT", u, nil)
98+
if err != nil {
99+
return nil, err
100+
}
101+
return s.client.Do(req, nil)
102+
}
103+
104+
// Unstar a repository as the authenticated user.
105+
//
106+
// GitHub API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository
107+
func (s *ActivityService) Unstar(owner, repo string) (*Response, error) {
108+
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
109+
req, err := s.client.NewRequest("DELETE", u, nil)
110+
if err != nil {
111+
return nil, err
112+
}
113+
return s.client.Do(req, nil)
114+
}

github/activity_star_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ import (
1212
"testing"
1313
)
1414

15+
func TestActivityService_ListStargazers(t *testing.T) {
16+
setup()
17+
defer teardown()
18+
19+
mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) {
20+
testMethod(t, r, "GET")
21+
testFormValues(t, r, values{
22+
"page": "2",
23+
})
24+
25+
fmt.Fprint(w, `[{"id":1}]`)
26+
})
27+
28+
stargazers, _, err := client.Activity.ListStargazers("o", "r", &ListOptions{Page: 2})
29+
if err != nil {
30+
t.Errorf("Activity.ListStargazers returned error: %v", err)
31+
}
32+
33+
want := []User{{ID: Int(1)}}
34+
if !reflect.DeepEqual(stargazers, want) {
35+
t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want)
36+
}
37+
}
38+
1539
func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
1640
setup()
1741
defer teardown()
@@ -62,3 +86,82 @@ func TestActivityService_ListStarred_invalidUser(t *testing.T) {
6286
_, _, err := client.Activity.ListStarred("%", nil)
6387
testURLParseError(t, err)
6488
}
89+
90+
func TestActivityService_IsStarred_hasStar(t *testing.T) {
91+
setup()
92+
defer teardown()
93+
94+
mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
95+
testMethod(t, r, "GET")
96+
w.WriteHeader(http.StatusNoContent)
97+
})
98+
99+
star, _, err := client.Activity.IsStarred("o", "r")
100+
if err != nil {
101+
t.Errorf("Activity.IsStarred returned error: %v", err)
102+
}
103+
if want := true; star != want {
104+
t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
105+
}
106+
}
107+
108+
func TestActivityService_IsStarred_noStar(t *testing.T) {
109+
setup()
110+
defer teardown()
111+
112+
mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
113+
testMethod(t, r, "GET")
114+
w.WriteHeader(http.StatusNotFound)
115+
})
116+
117+
star, _, err := client.Activity.IsStarred("o", "r")
118+
if err != nil {
119+
t.Errorf("Activity.IsStarred returned error: %v", err)
120+
}
121+
if want := false; star != want {
122+
t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
123+
}
124+
}
125+
126+
func TestActivityService_IsStarred_invalidID(t *testing.T) {
127+
_, _, err := client.Activity.IsStarred("%", "%")
128+
testURLParseError(t, err)
129+
}
130+
131+
func TestActivityService_Star(t *testing.T) {
132+
setup()
133+
defer teardown()
134+
135+
mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
136+
testMethod(t, r, "PUT")
137+
})
138+
139+
_, err := client.Activity.Star("o", "r")
140+
if err != nil {
141+
t.Errorf("Activity.Star returned error: %v", err)
142+
}
143+
}
144+
145+
func TestActivityService_Star_invalidID(t *testing.T) {
146+
_, err := client.Activity.Star("%", "%")
147+
testURLParseError(t, err)
148+
}
149+
150+
func TestActivityService_Unstar(t *testing.T) {
151+
setup()
152+
defer teardown()
153+
154+
mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
155+
testMethod(t, r, "DELETE")
156+
})
157+
158+
_, err := client.Activity.Unstar("o", "r")
159+
if err != nil {
160+
t.Errorf("Activity.Unstar returned error: %v", err)
161+
}
162+
}
163+
164+
func TestActivityService_Unstar_invalidID(t *testing.T) {
165+
_, err := client.Activity.Unstar("%", "%")
166+
testURLParseError(t, err)
167+
}

tests/integration/activity_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,72 @@ import (
1111
"github.com/google/go-github/github"
1212
)
1313

14+
func TestActivity_Starring(t *testing.T) {
15+
stargazers, _, err := client.Activity.ListStargazers("google", "go-github", nil)
16+
if err != nil {
17+
t.Fatalf("Activity.ListStargazers returned error: %v", err)
18+
}
19+
20+
if len(stargazers) == 0 {
21+
t.Errorf("Activity.ListStargazers('google', 'go-github') returned no stargazers")
22+
}
23+
24+
// the rest of the tests requires auth
25+
if !checkAuth("TestActivity_Starring") {
26+
return
27+
}
28+
29+
// first, check if already starred google/go-github
30+
star, _, err := client.Activity.IsStarred("google", "go-github")
31+
if err != nil {
32+
t.Fatalf("Activity.IsStarred returned error: %v", err)
33+
}
34+
if star {
35+
t.Fatalf("Already starring google/go-github. Please manually unstar it first.")
36+
}
37+
38+
// star google/go-github
39+
_, err = client.Activity.Star("google", "go-github")
40+
if err != nil {
41+
t.Fatalf("Activity.Star returned error: %v", err)
42+
}
43+
44+
// check again and verify starred
45+
star, _, err = client.Activity.IsStarred("google", "go-github")
46+
if err != nil {
47+
t.Fatalf("Activity.IsStarred returned error: %v", err)
48+
}
49+
if !star {
50+
t.Fatalf("Not starred google/go-github after starring it.")
51+
}
52+
53+
// unstar
54+
_, err = client.Activity.Unstar("google", "go-github")
55+
if err != nil {
56+
t.Fatalf("Activity.Unstar returned error: %v", err)
57+
}
58+
59+
// check again and verify not watching
60+
star, _, err = client.Activity.IsStarred("google", "go-github")
61+
if err != nil {
62+
t.Fatalf("Activity.IsStarred returned error: %v", err)
63+
}
64+
if star {
65+
t.Fatalf("Still starred google/go-github after unstarring it.")
66+
}
67+
}
68+
1469
func TestActivity_Watching(t *testing.T) {
70+
watchers, _, err := client.Activity.ListWatchers("google", "go-github", nil)
71+
if err != nil {
72+
t.Fatalf("Activity.ListWatchers returned error: %v", err)
73+
}
74+
75+
if len(watchers) == 0 {
76+
t.Errorf("Activity.ListWatchers('google', 'go-github') returned no watchers")
77+
}
78+
79+
// the rest of the tests requires auth
1580
if !checkAuth("TestActivity_Watching") {
1681
return
1782
}

0 commit comments

Comments
 (0)