1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "time"
12 )
13
14
15
16 type ContributorStats struct {
17 Author *Contributor `json:"author,omitempty"`
18 Total *int `json:"total,omitempty"`
19 Weeks []*WeeklyStats `json:"weeks,omitempty"`
20 }
21
22 func (c ContributorStats) String() string {
23 return Stringify(c)
24 }
25
26
27
28 type WeeklyStats struct {
29 Week *Timestamp `json:"w,omitempty"`
30 Additions *int `json:"a,omitempty"`
31 Deletions *int `json:"d,omitempty"`
32 Commits *int `json:"c,omitempty"`
33 }
34
35 func (w WeeklyStats) String() string {
36 return Stringify(w)
37 }
38
39
40
41
42
43
44
45
46
47
48
49 func (s *RepositoriesService) ListContributorsStats(ctx context.Context, owner, repo string) ([]*ContributorStats, *Response, error) {
50 u := fmt.Sprintf("repos/%v/%v/stats/contributors", owner, repo)
51 req, err := s.client.NewRequest("GET", u, nil)
52 if err != nil {
53 return nil, nil, err
54 }
55
56 var contributorStats []*ContributorStats
57 resp, err := s.client.Do(ctx, req, &contributorStats)
58 if err != nil {
59 return nil, resp, err
60 }
61
62 return contributorStats, resp, nil
63 }
64
65
66
67 type WeeklyCommitActivity struct {
68 Days []int `json:"days,omitempty"`
69 Total *int `json:"total,omitempty"`
70 Week *Timestamp `json:"week,omitempty"`
71 }
72
73 func (w WeeklyCommitActivity) String() string {
74 return Stringify(w)
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88 func (s *RepositoriesService) ListCommitActivity(ctx context.Context, owner, repo string) ([]*WeeklyCommitActivity, *Response, error) {
89 u := fmt.Sprintf("repos/%v/%v/stats/commit_activity", owner, repo)
90 req, err := s.client.NewRequest("GET", u, nil)
91 if err != nil {
92 return nil, nil, err
93 }
94
95 var weeklyCommitActivity []*WeeklyCommitActivity
96 resp, err := s.client.Do(ctx, req, &weeklyCommitActivity)
97 if err != nil {
98 return nil, resp, err
99 }
100
101 return weeklyCommitActivity, resp, nil
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 func (s *RepositoriesService) ListCodeFrequency(ctx context.Context, owner, repo string) ([]*WeeklyStats, *Response, error) {
116 u := fmt.Sprintf("repos/%v/%v/stats/code_frequency", owner, repo)
117 req, err := s.client.NewRequest("GET", u, nil)
118 if err != nil {
119 return nil, nil, err
120 }
121
122 var weeks [][]int
123 resp, err := s.client.Do(ctx, req, &weeks)
124 if err != nil {
125 return nil, resp, err
126 }
127
128
129 var stats []*WeeklyStats
130 for _, week := range weeks {
131 if len(week) != 3 {
132 continue
133 }
134 stat := &WeeklyStats{
135 Week: &Timestamp{time.Unix(int64(week[0]), 0)},
136 Additions: Int(week[1]),
137 Deletions: Int(week[2]),
138 }
139 stats = append(stats, stat)
140 }
141
142 return stats, resp, nil
143 }
144
145
146
147
148 type RepositoryParticipation struct {
149 All []int `json:"all,omitempty"`
150 Owner []int `json:"owner,omitempty"`
151 }
152
153 func (r RepositoryParticipation) String() string {
154 return Stringify(r)
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 func (s *RepositoriesService) ListParticipation(ctx context.Context, owner, repo string) (*RepositoryParticipation, *Response, error) {
172 u := fmt.Sprintf("repos/%v/%v/stats/participation", owner, repo)
173 req, err := s.client.NewRequest("GET", u, nil)
174 if err != nil {
175 return nil, nil, err
176 }
177
178 participation := new(RepositoryParticipation)
179 resp, err := s.client.Do(ctx, req, participation)
180 if err != nil {
181 return nil, resp, err
182 }
183
184 return participation, resp, nil
185 }
186
187
188
189 type PunchCard struct {
190 Day *int
191 Hour *int
192 Commits *int
193 }
194
195
196
197
198
199
200
201
202
203
204 func (s *RepositoriesService) ListPunchCard(ctx context.Context, owner, repo string) ([]*PunchCard, *Response, error) {
205 u := fmt.Sprintf("repos/%v/%v/stats/punch_card", owner, repo)
206 req, err := s.client.NewRequest("GET", u, nil)
207 if err != nil {
208 return nil, nil, err
209 }
210
211 var results [][]int
212 resp, err := s.client.Do(ctx, req, &results)
213 if err != nil {
214 return nil, resp, err
215 }
216
217
218 var cards []*PunchCard
219 for _, result := range results {
220 if len(result) != 3 {
221 continue
222 }
223 card := &PunchCard{
224 Day: Int(result[0]),
225 Hour: Int(result[1]),
226 Commits: Int(result[2]),
227 }
228 cards = append(cards, card)
229 }
230
231 return cards, resp, nil
232 }
233
View as plain text