Skip to content

Commit fbc5be6

Browse files
author
Ruben Vereecken
committed
Support for updated starring API
Supports the updated starring API as mentioned in issue #188. There is now a new struct called StarredRepository which holds a Repository and the new `starred_at` timestamp. For now there is a special media type to request the new API. I've added a TODO for it to be deleted when the new API fully launches.
1 parent 9eb187b commit fbc5be6

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

github/activity_star.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ package github
77

88
import "fmt"
99

10+
// StarredRepository is returned by ListStarred.
11+
type StarredRepository struct {
12+
StarredAt *Timestamp `json:"starred_at,omitempty"`
13+
Repository *Repository `json:"repository,omitempty"`
14+
}
15+
1016
// ListStargazers lists people who have starred the specified repo.
1117
//
1218
// GitHub API Docs: https://developer.github.com/v3/activity/starring/#list-stargazers
@@ -49,7 +55,7 @@ type ActivityListStarredOptions struct {
4955
// will list the starred repositories for the authenticated user.
5056
//
5157
// GitHub API docs: http://developer.github.com/v3/activity/starring/#list-repositories-being-starred
52-
func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]Repository, *Response, error) {
58+
func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]StarredRepository, *Response, error) {
5359
var u string
5460
if user != "" {
5561
u = fmt.Sprintf("users/%v/starred", user)
@@ -66,7 +72,10 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
6672
return nil, nil, err
6773
}
6874

69-
repos := new([]Repository)
75+
// TODO: remove custom Accept header when this API fully launches
76+
req.Header.Set("Accept", mediaTypeStarringPreview)
77+
78+
repos := new([]StarredRepository)
7079
resp, err := s.client.Do(req, repos)
7180
if err != nil {
7281
return nil, resp, err

github/activity_star_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"reflect"
1212
"testing"
13+
"time"
1314
)
1415

1516
func TestActivityService_ListStargazers(t *testing.T) {
@@ -42,15 +43,16 @@ func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
4243

4344
mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) {
4445
testMethod(t, r, "GET")
45-
fmt.Fprint(w, `[{"id":1}]`)
46+
testHeader(t, r, "Accept", mediaTypeStarringPreview)
47+
fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repository":{"id":1}}]`)
4648
})
4749

4850
repos, _, err := client.Activity.ListStarred("", nil)
4951
if err != nil {
5052
t.Errorf("Activity.ListStarred returned error: %v", err)
5153
}
5254

53-
want := []Repository{{ID: Int(1)}}
55+
want := []StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int(1)}}}
5456
if !reflect.DeepEqual(repos, want) {
5557
t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
5658
}
@@ -62,12 +64,13 @@ func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
6264

6365
mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) {
6466
testMethod(t, r, "GET")
67+
testHeader(t, r, "Accept", mediaTypeStarringPreview)
6568
testFormValues(t, r, values{
6669
"sort": "created",
6770
"direction": "asc",
6871
"page": "2",
6972
})
70-
fmt.Fprint(w, `[{"id":2}]`)
73+
fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repository":{"id":2}}]`)
7174
})
7275

7376
opt := &ActivityListStarredOptions{"created", "asc", ListOptions{Page: 2}}
@@ -76,7 +79,7 @@ func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
7679
t.Errorf("Activity.ListStarred returned error: %v", err)
7780
}
7881

79-
want := []Repository{{ID: Int(2)}}
82+
want := []StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int(2)}}}
8083
if !reflect.DeepEqual(repos, want) {
8184
t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
8285
}

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const (
3939

4040
// https://developer.github.com/changes/2015-03-09-licenses-api/
4141
mediaTypeLicensesPreview = "application/vnd.github.drax-preview+json"
42+
43+
// https://developer.github.com/changes/2014-12-09-new-attributes-for-stars-api/
44+
mediaTypeStarringPreview = "application/vnd.github.v3.star+json"
4245
)
4346

4447
// A Client manages communication with the GitHub API.

0 commit comments

Comments
 (0)