From d3fb1a22428ef6a32726e4b85fbc507d5222301f Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 11 Apr 2014 16:19:12 -0700 Subject: [PATCH] minor updates to repo stats - use Timestamp for Week fields instead of int - rename WeekHash to WeekStats --- github/repos_stats.go | 78 ++++++++++++++++++++++++-------------- github/repos_stats_test.go | 15 ++++---- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/github/repos_stats.go b/github/repos_stats.go index 1d64546..beb1266 100644 --- a/github/repos_stats.go +++ b/github/repos_stats.go @@ -10,36 +10,47 @@ import "fmt" // ContributorStats represents a contributor to a repository and their // weekly contributions to a given repo. type ContributorStats struct { - Author *Contributor `json:"author,omitempty"` - Total *int `json:"total,omitempty"` - Weeks []WeeklyHash `json:"weeks,omitempty"` + Author *Contributor `json:"author,omitempty"` + Total *int `json:"total,omitempty"` + Weeks []WeeklyStats `json:"weeks,omitempty"` } -// WeeklyHash represents the number of additions, deletions and commits +func (c ContributorStats) String() string { + return Stringify(c) +} + +// WeeklyStats represents the number of additions, deletions and commits // a Contributor made in a given week. -type WeeklyHash struct { - Week *int `json:"w,omitempty"` - Additions *int `json:"a,omitempty"` - Deletions *int `json:"d,omitempty"` - Commits *int `json:"c,omitempty"` +type WeeklyStats struct { + Week *Timestamp `json:"w,omitempty"` + Additions *int `json:"a,omitempty"` + Deletions *int `json:"d,omitempty"` + Commits *int `json:"c,omitempty"` +} + +func (w WeeklyStats) String() string { + return Stringify(w) } -// ListContributorsStats gets a repo's contributor list with additions, deletions and commit counts. -// If this is the first time these statistics are requested for the given repository, this method -// will return a non-nil error and a status code of 202. This is because this is the status that github -// returns to signify that it is now computing the requested statistics. A follow up request, after -// a delay of a second or so, should result in a successful request. +// ListContributorsStats gets a repo's contributor list with additions, +// deletions and commit counts. +// +// If this is the first time these statistics are requested for the given +// repository, this method will return a non-nil error and a status code of +// 202. This is because this is the status that github returns to signify that +// it is now computing the requested statistics. A follow up request, after a +// delay of a second or so, should result in a successful request. // // GitHub API docs: https://developer.github.com/v3/repos/statistics/#contributors -func (s *RepositoriesService) ListContributorsStats(owner, repo string) (*[]ContributorStats, *Response, error) { +func (s *RepositoriesService) ListContributorsStats(owner, repo string) ([]ContributorStats, *Response, error) { u := fmt.Sprintf("repos/%v/%v/stats/contributors", owner, repo) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err } - contributorStats := new([]ContributorStats) - resp, err := s.client.Do(req, contributorStats) + var contributorStats []ContributorStats + resp, err := s.client.Do(req, &contributorStats) if err != nil { return nil, resp, err } @@ -50,30 +61,35 @@ func (s *RepositoriesService) ListContributorsStats(owner, repo string) (*[]Cont // WeeklyCommitActivity represents the weekly commit activity for a repository. // The days array is a group of commits per day, starting on Sunday. type WeeklyCommitActivity struct { - Days []int `json:"days,omitempty"` - Total *int `json:"total,omitempty"` - Week *int `json:"week,omitempty"` + Days []int `json:"days,omitempty"` + Total *int `json:"total,omitempty"` + Week *Timestamp `json:"week,omitempty"` +} + +func (w WeeklyCommitActivity) String() string { + return Stringify(w) } // ListCommitActivity returns the last year of commit activity // grouped by week. The days array is a group of commits per day, -// starting on Sunday. If this is the first time these statistics are -// requested for the given repository, this method will return a -// non-nil error and a status code of 202. This is because this is the -// status that github returns to signify that it is now computing the -// requested statistics. A follow up request, after a delay of a second -// or so, should result in a successful request. +// starting on Sunday. +// +// If this is the first time these statistics are requested for the given +// repository, this method will return a non-nil error and a status code of +// 202. This is because this is the status that github returns to signify that +// it is now computing the requested statistics. A follow up request, after a +// delay of a second or so, should result in a successful request. // // GitHub API docs: https://developer.github.com/v3/repos/statistics/#commit-activity -func (s *RepositoriesService) ListCommitActivity(owner, repo string) (*[]WeeklyCommitActivity, *Response, error) { +func (s *RepositoriesService) ListCommitActivity(owner, repo string) ([]WeeklyCommitActivity, *Response, error) { u := fmt.Sprintf("repos/%v/%v/stats/commit_activity", owner, repo) req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err } - weeklyCommitActivity := new([]WeeklyCommitActivity) - resp, err := s.client.Do(req, weeklyCommitActivity) + var weeklyCommitActivity []WeeklyCommitActivity + resp, err := s.client.Do(req, &weeklyCommitActivity) if err != nil { return nil, resp, err } @@ -89,6 +105,10 @@ type RepositoryParticipation struct { Owner []int `json:"owner,omitempty"` } +func (r RepositoryParticipation) String() string { + return Stringify(r) +} + // ListParticipation returns the total commit counts for the 'owner' // and total commit counts in 'all'. 'all' is everyone combined, // including the 'owner' in the last 52 weeks. If you’d like to get diff --git a/github/repos_stats_test.go b/github/repos_stats_test.go index 5237f2a..2cd79d2 100644 --- a/github/repos_stats_test.go +++ b/github/repos_stats_test.go @@ -10,6 +10,7 @@ import ( "net/http" "reflect" "testing" + "time" ) func TestRepositoriesService_ListContributorsStats(t *testing.T) { @@ -44,15 +45,15 @@ func TestRepositoriesService_ListContributorsStats(t *testing.T) { t.Errorf("RepositoriesService.ListContributorsStats returned error: %v", err) } - want := &[]ContributorStats{ + want := []ContributorStats{ ContributorStats{ Author: &Contributor{ ID: Int(1), }, Total: Int(135), - Weeks: []WeeklyHash{ - WeeklyHash{ - Week: Int(1367712000), + Weeks: []WeeklyStats{ + { + Week: &Timestamp{time.Date(2013, 05, 05, 00, 00, 00, 0, time.UTC).Local()}, Additions: Int(6898), Deletions: Int(77), Commits: Int(10), @@ -89,11 +90,11 @@ func TestRepositoriesService_ListCommitActivity(t *testing.T) { t.Errorf("RepositoriesService.ListCommitActivity returned error: %v", err) } - want := &[]WeeklyCommitActivity{ - WeeklyCommitActivity{ + want := []WeeklyCommitActivity{ + { Days: []int{0, 3, 26, 20, 39, 1, 0}, Total: Int(89), - Week: Int(1336280400), + Week: &Timestamp{time.Date(2012, 05, 06, 05, 00, 00, 0, time.UTC).Local()}, }, }