Skip to content

Commit 3630763

Browse files
committed
add two remaining stats methods
- ListCodeFrequency - ListPunchCard
1 parent d3fb1a2 commit 3630763

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed

github/repos_stats.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
package github
77

8-
import "fmt"
8+
import (
9+
"fmt"
10+
"time"
11+
)
912

1013
// ContributorStats represents a contributor to a repository and their
1114
// weekly contributions to a given repo.
@@ -41,7 +44,7 @@ func (w WeeklyStats) String() string {
4144
// it is now computing the requested statistics. A follow up request, after a
4245
// delay of a second or so, should result in a successful request.
4346
//
44-
// GitHub API docs: https://developer.github.com/v3/repos/statistics/#contributors
47+
// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#contributors
4548
func (s *RepositoriesService) ListContributorsStats(owner, repo string) ([]ContributorStats, *Response, error) {
4649
u := fmt.Sprintf("repos/%v/%v/stats/contributors", owner, repo)
4750
req, err := s.client.NewRequest("GET", u, nil)
@@ -80,7 +83,7 @@ func (w WeeklyCommitActivity) String() string {
8083
// it is now computing the requested statistics. A follow up request, after a
8184
// delay of a second or so, should result in a successful request.
8285
//
83-
// GitHub API docs: https://developer.github.com/v3/repos/statistics/#commit-activity
86+
// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#commit-activity
8487
func (s *RepositoriesService) ListCommitActivity(owner, repo string) ([]WeeklyCommitActivity, *Response, error) {
8588
u := fmt.Sprintf("repos/%v/%v/stats/commit_activity", owner, repo)
8689
req, err := s.client.NewRequest("GET", u, nil)
@@ -97,6 +100,38 @@ func (s *RepositoriesService) ListCommitActivity(owner, repo string) ([]WeeklyCo
97100
return weeklyCommitActivity, resp, err
98101
}
99102

103+
// ListCodeFrequency returns a weekly aggregate of the number of additions and
104+
// deletions pushed to a repository. Returned WeeklyStats will contain
105+
// additiona and deletions, but not total commits.
106+
//
107+
// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#code-frequency
108+
func (s *RepositoriesService) ListCodeFrequency(owner, repo string) ([]WeeklyStats, *Response, error) {
109+
u := fmt.Sprintf("repos/%v/%v/stats/code_frequency", owner, repo)
110+
req, err := s.client.NewRequest("GET", u, nil)
111+
if err != nil {
112+
return nil, nil, err
113+
}
114+
115+
var weeks [][]int
116+
resp, err := s.client.Do(req, &weeks)
117+
118+
// convert int slices into WeeklyStats
119+
stats := make([]WeeklyStats, 0)
120+
for _, week := range weeks {
121+
if len(week) != 3 {
122+
continue
123+
}
124+
stat := WeeklyStats{
125+
Week: &Timestamp{time.Unix(int64(week[0]), 0)},
126+
Additions: Int(week[1]),
127+
Deletions: Int(week[2]),
128+
}
129+
stats = append(stats, stat)
130+
}
131+
132+
return stats, resp, err
133+
}
134+
100135
// RepositoryParticipation is the number of commits by everyone
101136
// who has contributed to the repository (including the owner)
102137
// as well as the number of commits by the owner themself.
@@ -139,3 +174,41 @@ func (s *RepositoriesService) ListParticipation(owner, repo string) (*Repository
139174

140175
return participation, resp, err
141176
}
177+
178+
// PunchCard respresents the number of commits made during a given hour of a
179+
// day of thew eek.
180+
type PunchCard struct {
181+
Day *int // Day of the week (0-6: =Sunday - Saturday).
182+
Hour *int // Hour of day (0-23).
183+
Commits *int // Number of commits.
184+
}
185+
186+
// ListPunchCard returns the number of commits per hour in each day.
187+
//
188+
// GitHub API Docs: https://developer.github.com/v3/repos/statistics/#punch-card
189+
func (s *RepositoriesService) ListPunchCard(owner, repo string) ([]PunchCard, *Response, error) {
190+
u := fmt.Sprintf("repos/%v/%v/stats/punch_card", owner, repo)
191+
req, err := s.client.NewRequest("GET", u, nil)
192+
if err != nil {
193+
return nil, nil, err
194+
}
195+
196+
var results [][]int
197+
resp, err := s.client.Do(req, &results)
198+
199+
// convert int slices into Punchcards
200+
cards := make([]PunchCard, 0)
201+
for _, result := range results {
202+
if len(result) != 3 {
203+
continue
204+
}
205+
card := PunchCard{
206+
Day: Int(result[0]),
207+
Hour: Int(result[1]),
208+
Commits: Int(result[2]),
209+
}
210+
cards = append(cards, card)
211+
}
212+
213+
return cards, resp, err
214+
}

github/repos_stats_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,32 @@ func TestRepositoriesService_ListCommitActivity(t *testing.T) {
103103
}
104104
}
105105

106+
func TestRepositoriesService_ListCodeFrequency(t *testing.T) {
107+
setup()
108+
defer teardown()
109+
110+
mux.HandleFunc("/repos/o/r/stats/code_frequency", func(w http.ResponseWriter, r *http.Request) {
111+
testMethod(t, r, "GET")
112+
113+
fmt.Fprint(w, `[[1302998400, 1124, -435]]`)
114+
})
115+
116+
code, _, err := client.Repositories.ListCodeFrequency("o", "r")
117+
if err != nil {
118+
t.Errorf("RepositoriesService.ListCodeFrequency returned error: %v", err)
119+
}
120+
121+
want := []WeeklyStats{{
122+
Week: &Timestamp{time.Date(2011, 04, 17, 00, 00, 00, 0, time.UTC).Local()},
123+
Additions: Int(1124),
124+
Deletions: Int(-435),
125+
}}
126+
127+
if !reflect.DeepEqual(code, want) {
128+
t.Errorf("RepositoriesService.ListCodeFrequency returned %+v, want %+v", code, want)
129+
}
130+
}
131+
106132
func TestRepositoriesService_Participation(t *testing.T) {
107133
setup()
108134
defer teardown()
@@ -152,3 +178,33 @@ func TestRepositoriesService_Participation(t *testing.T) {
152178
t.Errorf("RepositoriesService.ListParticipation returned %+v, want %+v", participation, want)
153179
}
154180
}
181+
182+
func TestRepositoriesService_ListPunchCard(t *testing.T) {
183+
setup()
184+
defer teardown()
185+
186+
mux.HandleFunc("/repos/o/r/stats/punch_card", func(w http.ResponseWriter, r *http.Request) {
187+
testMethod(t, r, "GET")
188+
189+
fmt.Fprint(w, `[
190+
[0, 0, 5],
191+
[0, 1, 43],
192+
[0, 2, 21]
193+
]`)
194+
})
195+
196+
card, _, err := client.Repositories.ListPunchCard("o", "r")
197+
if err != nil {
198+
t.Errorf("RepositoriesService.ListPunchCard returned error: %v", err)
199+
}
200+
201+
want := []PunchCard{
202+
{Day: Int(0), Hour: Int(0), Commits: Int(5)},
203+
{Day: Int(0), Hour: Int(1), Commits: Int(43)},
204+
{Day: Int(0), Hour: Int(2), Commits: Int(21)},
205+
}
206+
207+
if !reflect.DeepEqual(card, want) {
208+
t.Errorf("RepositoriesService.ListPunchCard returned %+v, want %+v", card, want)
209+
}
210+
}

0 commit comments

Comments
 (0)