Skip to content

Commit

Permalink
Add support for Activities.ListStargazers response with timestamp.
Browse files Browse the repository at this point in the history
Add custom header to request to get an alternate response with star creation timestamp.

Solves google#319
Closes google#320
  • Loading branch information
Andrew Ryabchun authored and willnorris committed Apr 9, 2016
1 parent 1acbd99 commit eea6140
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions github/activity_star.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ type StarredRepository struct {
Repository *Repository `json:"repo,omitempty"`
}

// Stargazer represents a user that has starred a repository.
type Stargazer struct {
StarredAt *Timestamp `json:"starred_at,omitempty"`
User *User `json:"user,omitempty"`
}

// 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) {
func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) ([]Stargazer, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/stargazers", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
Expand All @@ -28,7 +34,10 @@ func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) (
return nil, nil, err
}

stargazers := new([]User)
// TODO: remove custom Accept header when this API fully launches
req.Header.Set("Accept", mediaTypeStarringPreview)

stargazers := new([]Stargazer)
resp, err := s.client.Do(req, stargazers)
if err != nil {
return nil, resp, err
Expand Down
5 changes: 3 additions & 2 deletions github/activity_star_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ func TestActivityService_ListStargazers(t *testing.T) {

mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeStarringPreview)
testFormValues(t, r, values{
"page": "2",
})

fmt.Fprint(w, `[{"id":1}]`)
fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","user":{"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)}}
want := []Stargazer{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, User: &User{ID: Int(1)}}}
if !reflect.DeepEqual(stargazers, want) {
t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want)
}
Expand Down

0 comments on commit eea6140

Please sign in to comment.