Skip to content

Commit d3fb1a2

Browse files
committed
minor updates to repo stats
- use Timestamp for Week fields instead of int - rename WeekHash to WeekStats
1 parent 57201fd commit d3fb1a2

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

github/repos_stats.go

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,47 @@ import "fmt"
1010
// ContributorStats represents a contributor to a repository and their
1111
// weekly contributions to a given repo.
1212
type ContributorStats struct {
13-
Author *Contributor `json:"author,omitempty"`
14-
Total *int `json:"total,omitempty"`
15-
Weeks []WeeklyHash `json:"weeks,omitempty"`
13+
Author *Contributor `json:"author,omitempty"`
14+
Total *int `json:"total,omitempty"`
15+
Weeks []WeeklyStats `json:"weeks,omitempty"`
1616
}
1717

18-
// WeeklyHash represents the number of additions, deletions and commits
18+
func (c ContributorStats) String() string {
19+
return Stringify(c)
20+
}
21+
22+
// WeeklyStats represents the number of additions, deletions and commits
1923
// a Contributor made in a given week.
20-
type WeeklyHash struct {
21-
Week *int `json:"w,omitempty"`
22-
Additions *int `json:"a,omitempty"`
23-
Deletions *int `json:"d,omitempty"`
24-
Commits *int `json:"c,omitempty"`
24+
type WeeklyStats struct {
25+
Week *Timestamp `json:"w,omitempty"`
26+
Additions *int `json:"a,omitempty"`
27+
Deletions *int `json:"d,omitempty"`
28+
Commits *int `json:"c,omitempty"`
29+
}
30+
31+
func (w WeeklyStats) String() string {
32+
return Stringify(w)
2533
}
2634

27-
// ListContributorsStats gets a repo's contributor list with additions, deletions and commit counts.
28-
// If this is the first time these statistics are requested for the given repository, this method
29-
// will return a non-nil error and a status code of 202. This is because this is the status that github
30-
// returns to signify that it is now computing the requested statistics. A follow up request, after
31-
// a delay of a second or so, should result in a successful request.
35+
// ListContributorsStats gets a repo's contributor list with additions,
36+
// deletions and commit counts.
37+
//
38+
// If this is the first time these statistics are requested for the given
39+
// repository, this method will return a non-nil error and a status code of
40+
// 202. This is because this is the status that github returns to signify that
41+
// it is now computing the requested statistics. A follow up request, after a
42+
// delay of a second or so, should result in a successful request.
3243
//
3344
// GitHub API docs: https://developer.github.com/v3/repos/statistics/#contributors
34-
func (s *RepositoriesService) ListContributorsStats(owner, repo string) (*[]ContributorStats, *Response, error) {
45+
func (s *RepositoriesService) ListContributorsStats(owner, repo string) ([]ContributorStats, *Response, error) {
3546
u := fmt.Sprintf("repos/%v/%v/stats/contributors", owner, repo)
3647
req, err := s.client.NewRequest("GET", u, nil)
3748
if err != nil {
3849
return nil, nil, err
3950
}
4051

41-
contributorStats := new([]ContributorStats)
42-
resp, err := s.client.Do(req, contributorStats)
52+
var contributorStats []ContributorStats
53+
resp, err := s.client.Do(req, &contributorStats)
4354
if err != nil {
4455
return nil, resp, err
4556
}
@@ -50,30 +61,35 @@ func (s *RepositoriesService) ListContributorsStats(owner, repo string) (*[]Cont
5061
// WeeklyCommitActivity represents the weekly commit activity for a repository.
5162
// The days array is a group of commits per day, starting on Sunday.
5263
type WeeklyCommitActivity struct {
53-
Days []int `json:"days,omitempty"`
54-
Total *int `json:"total,omitempty"`
55-
Week *int `json:"week,omitempty"`
64+
Days []int `json:"days,omitempty"`
65+
Total *int `json:"total,omitempty"`
66+
Week *Timestamp `json:"week,omitempty"`
67+
}
68+
69+
func (w WeeklyCommitActivity) String() string {
70+
return Stringify(w)
5671
}
5772

5873
// ListCommitActivity returns the last year of commit activity
5974
// grouped by week. The days array is a group of commits per day,
60-
// starting on Sunday. If this is the first time these statistics are
61-
// requested for the given repository, this method will return a
62-
// non-nil error and a status code of 202. This is because this is the
63-
// status that github returns to signify that it is now computing the
64-
// requested statistics. A follow up request, after a delay of a second
65-
// or so, should result in a successful request.
75+
// starting on Sunday.
76+
//
77+
// If this is the first time these statistics are requested for the given
78+
// repository, this method will return a non-nil error and a status code of
79+
// 202. This is because this is the status that github returns to signify that
80+
// it is now computing the requested statistics. A follow up request, after a
81+
// delay of a second or so, should result in a successful request.
6682
//
6783
// GitHub API docs: https://developer.github.com/v3/repos/statistics/#commit-activity
68-
func (s *RepositoriesService) ListCommitActivity(owner, repo string) (*[]WeeklyCommitActivity, *Response, error) {
84+
func (s *RepositoriesService) ListCommitActivity(owner, repo string) ([]WeeklyCommitActivity, *Response, error) {
6985
u := fmt.Sprintf("repos/%v/%v/stats/commit_activity", owner, repo)
7086
req, err := s.client.NewRequest("GET", u, nil)
7187
if err != nil {
7288
return nil, nil, err
7389
}
7490

75-
weeklyCommitActivity := new([]WeeklyCommitActivity)
76-
resp, err := s.client.Do(req, weeklyCommitActivity)
91+
var weeklyCommitActivity []WeeklyCommitActivity
92+
resp, err := s.client.Do(req, &weeklyCommitActivity)
7793
if err != nil {
7894
return nil, resp, err
7995
}
@@ -89,6 +105,10 @@ type RepositoryParticipation struct {
89105
Owner []int `json:"owner,omitempty"`
90106
}
91107

108+
func (r RepositoryParticipation) String() string {
109+
return Stringify(r)
110+
}
111+
92112
// ListParticipation returns the total commit counts for the 'owner'
93113
// and total commit counts in 'all'. 'all' is everyone combined,
94114
// including the 'owner' in the last 52 weeks. If you’d like to get

github/repos_stats_test.go

Lines changed: 8 additions & 7 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 TestRepositoriesService_ListContributorsStats(t *testing.T) {
@@ -44,15 +45,15 @@ func TestRepositoriesService_ListContributorsStats(t *testing.T) {
4445
t.Errorf("RepositoriesService.ListContributorsStats returned error: %v", err)
4546
}
4647

47-
want := &[]ContributorStats{
48+
want := []ContributorStats{
4849
ContributorStats{
4950
Author: &Contributor{
5051
ID: Int(1),
5152
},
5253
Total: Int(135),
53-
Weeks: []WeeklyHash{
54-
WeeklyHash{
55-
Week: Int(1367712000),
54+
Weeks: []WeeklyStats{
55+
{
56+
Week: &Timestamp{time.Date(2013, 05, 05, 00, 00, 00, 0, time.UTC).Local()},
5657
Additions: Int(6898),
5758
Deletions: Int(77),
5859
Commits: Int(10),
@@ -89,11 +90,11 @@ func TestRepositoriesService_ListCommitActivity(t *testing.T) {
8990
t.Errorf("RepositoriesService.ListCommitActivity returned error: %v", err)
9091
}
9192

92-
want := &[]WeeklyCommitActivity{
93-
WeeklyCommitActivity{
93+
want := []WeeklyCommitActivity{
94+
{
9495
Days: []int{0, 3, 26, 20, 39, 1, 0},
9596
Total: Int(89),
96-
Week: Int(1336280400),
97+
Week: &Timestamp{time.Date(2012, 05, 06, 05, 00, 00, 0, time.UTC).Local()},
9798
},
9899
}
99100

0 commit comments

Comments
 (0)