55
66package 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
4548func (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
8487func (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+ }
0 commit comments