diff --git a/github/repos_stats.go b/github/repos_stats.go index beb1266..452ff18 100644 --- a/github/repos_stats.go +++ b/github/repos_stats.go @@ -5,7 +5,10 @@ package github -import "fmt" +import ( + "fmt" + "time" +) // ContributorStats represents a contributor to a repository and their // weekly contributions to a given repo. @@ -41,7 +44,7 @@ func (w WeeklyStats) String() string { // 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 +// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#contributors 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) @@ -80,7 +83,7 @@ func (w WeeklyCommitActivity) String() string { // 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 +// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#commit-activity 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) @@ -97,6 +100,38 @@ func (s *RepositoriesService) ListCommitActivity(owner, repo string) ([]WeeklyCo return weeklyCommitActivity, resp, err } +// ListCodeFrequency returns a weekly aggregate of the number of additions and +// deletions pushed to a repository. Returned WeeklyStats will contain +// additiona and deletions, but not total commits. +// +// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#code-frequency +func (s *RepositoriesService) ListCodeFrequency(owner, repo string) ([]WeeklyStats, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/stats/code_frequency", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var weeks [][]int + resp, err := s.client.Do(req, &weeks) + + // convert int slices into WeeklyStats + stats := make([]WeeklyStats, 0) + for _, week := range weeks { + if len(week) != 3 { + continue + } + stat := WeeklyStats{ + Week: &Timestamp{time.Unix(int64(week[0]), 0)}, + Additions: Int(week[1]), + Deletions: Int(week[2]), + } + stats = append(stats, stat) + } + + return stats, resp, err +} + // RepositoryParticipation is the number of commits by everyone // who has contributed to the repository (including the owner) // as well as the number of commits by the owner themself. @@ -139,3 +174,41 @@ func (s *RepositoriesService) ListParticipation(owner, repo string) (*Repository return participation, resp, err } + +// PunchCard respresents the number of commits made during a given hour of a +// day of thew eek. +type PunchCard struct { + Day *int // Day of the week (0-6: =Sunday - Saturday). + Hour *int // Hour of day (0-23). + Commits *int // Number of commits. +} + +// ListPunchCard returns the number of commits per hour in each day. +// +// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#punch-card +func (s *RepositoriesService) ListPunchCard(owner, repo string) ([]PunchCard, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/stats/punch_card", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var results [][]int + resp, err := s.client.Do(req, &results) + + // convert int slices into Punchcards + cards := make([]PunchCard, 0) + for _, result := range results { + if len(result) != 3 { + continue + } + card := PunchCard{ + Day: Int(result[0]), + Hour: Int(result[1]), + Commits: Int(result[2]), + } + cards = append(cards, card) + } + + return cards, resp, err +} diff --git a/github/repos_stats_test.go b/github/repos_stats_test.go index 2cd79d2..d5fb5f4 100644 --- a/github/repos_stats_test.go +++ b/github/repos_stats_test.go @@ -103,6 +103,32 @@ func TestRepositoriesService_ListCommitActivity(t *testing.T) { } } +func TestRepositoriesService_ListCodeFrequency(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/stats/code_frequency", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + fmt.Fprint(w, `[[1302998400, 1124, -435]]`) + }) + + code, _, err := client.Repositories.ListCodeFrequency("o", "r") + if err != nil { + t.Errorf("RepositoriesService.ListCodeFrequency returned error: %v", err) + } + + want := []WeeklyStats{{ + Week: &Timestamp{time.Date(2011, 04, 17, 00, 00, 00, 0, time.UTC).Local()}, + Additions: Int(1124), + Deletions: Int(-435), + }} + + if !reflect.DeepEqual(code, want) { + t.Errorf("RepositoriesService.ListCodeFrequency returned %+v, want %+v", code, want) + } +} + func TestRepositoriesService_Participation(t *testing.T) { setup() defer teardown() @@ -152,3 +178,33 @@ func TestRepositoriesService_Participation(t *testing.T) { t.Errorf("RepositoriesService.ListParticipation returned %+v, want %+v", participation, want) } } + +func TestRepositoriesService_ListPunchCard(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/stats/punch_card", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + fmt.Fprint(w, `[ + [0, 0, 5], + [0, 1, 43], + [0, 2, 21] + ]`) + }) + + card, _, err := client.Repositories.ListPunchCard("o", "r") + if err != nil { + t.Errorf("RepositoriesService.ListPunchCard returned error: %v", err) + } + + want := []PunchCard{ + {Day: Int(0), Hour: Int(0), Commits: Int(5)}, + {Day: Int(0), Hour: Int(1), Commits: Int(43)}, + {Day: Int(0), Hour: Int(2), Commits: Int(21)}, + } + + if !reflect.DeepEqual(card, want) { + t.Errorf("RepositoriesService.ListPunchCard returned %+v, want %+v", card, want) + } +}