forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos_stats.go
214 lines (186 loc) · 6.88 KB
/
repos_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2014 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"fmt"
"time"
)
// 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 []WeeklyStats `json:"weeks,omitempty"`
}
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 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.
//
// 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)
if err != nil {
return nil, nil, err
}
var contributorStats []ContributorStats
resp, err := s.client.Do(req, &contributorStats)
if err != nil {
return nil, resp, err
}
return contributorStats, resp, err
}
// 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 *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.
//
// 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)
if err != nil {
return nil, nil, err
}
var weeklyCommitActivity []WeeklyCommitActivity
resp, err := s.client.Do(req, &weeklyCommitActivity)
if err != nil {
return nil, resp, err
}
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
var stats []WeeklyStats
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.
type RepositoryParticipation struct {
All []int `json:"all,omitempty"`
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
// the commit counts for non-owners, you can subtract 'all' from 'owner'.
//
// The array order is oldest week (index 0) to most recent week.
//
// 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/#participation
func (s *RepositoriesService) ListParticipation(owner, repo string) (*RepositoryParticipation, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/stats/participation", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
participation := new(RepositoryParticipation)
resp, err := s.client.Do(req, participation)
if err != nil {
return nil, resp, err
}
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
var cards []PunchCard
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
}