repos_stats.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2014 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. )
  11. // ContributorStats represents a contributor to a repository and their
  12. // weekly contributions to a given repo.
  13. type ContributorStats struct {
  14. Author *Contributor `json:"author,omitempty"`
  15. Total *int `json:"total,omitempty"`
  16. Weeks []WeeklyStats `json:"weeks,omitempty"`
  17. }
  18. func (c ContributorStats) String() string {
  19. return Stringify(c)
  20. }
  21. // WeeklyStats represents the number of additions, deletions and commits
  22. // a Contributor made in a given week.
  23. type WeeklyStats struct {
  24. Week *Timestamp `json:"w,omitempty"`
  25. Additions *int `json:"a,omitempty"`
  26. Deletions *int `json:"d,omitempty"`
  27. Commits *int `json:"c,omitempty"`
  28. }
  29. func (w WeeklyStats) String() string {
  30. return Stringify(w)
  31. }
  32. // ListContributorsStats gets a repo's contributor list with additions,
  33. // deletions and commit counts.
  34. //
  35. // If this is the first time these statistics are requested for the given
  36. // repository, this method will return an *AcceptedError and a status code of
  37. // 202. This is because this is the status that GitHub returns to signify that
  38. // it is now computing the requested statistics. A follow up request, after a
  39. // delay of a second or so, should result in a successful request.
  40. //
  41. // GitHub API docs: https://developer.github.com/v3/repos/statistics/#contributors
  42. func (s *RepositoriesService) ListContributorsStats(ctx context.Context, owner, repo string) ([]*ContributorStats, *Response, error) {
  43. u := fmt.Sprintf("repos/%v/%v/stats/contributors", owner, repo)
  44. req, err := s.client.NewRequest("GET", u, nil)
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. var contributorStats []*ContributorStats
  49. resp, err := s.client.Do(ctx, req, &contributorStats)
  50. if err != nil {
  51. return nil, resp, err
  52. }
  53. return contributorStats, resp, nil
  54. }
  55. // WeeklyCommitActivity represents the weekly commit activity for a repository.
  56. // The days array is a group of commits per day, starting on Sunday.
  57. type WeeklyCommitActivity struct {
  58. Days []int `json:"days,omitempty"`
  59. Total *int `json:"total,omitempty"`
  60. Week *Timestamp `json:"week,omitempty"`
  61. }
  62. func (w WeeklyCommitActivity) String() string {
  63. return Stringify(w)
  64. }
  65. // ListCommitActivity returns the last year of commit activity
  66. // grouped by week. The days array is a group of commits per day,
  67. // starting on Sunday.
  68. //
  69. // If this is the first time these statistics are requested for the given
  70. // repository, this method will return an *AcceptedError and a status code of
  71. // 202. This is because this is the status that GitHub returns to signify that
  72. // it is now computing the requested statistics. A follow up request, after a
  73. // delay of a second or so, should result in a successful request.
  74. //
  75. // GitHub API docs: https://developer.github.com/v3/repos/statistics/#commit-activity
  76. func (s *RepositoriesService) ListCommitActivity(ctx context.Context, owner, repo string) ([]*WeeklyCommitActivity, *Response, error) {
  77. u := fmt.Sprintf("repos/%v/%v/stats/commit_activity", owner, repo)
  78. req, err := s.client.NewRequest("GET", u, nil)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. var weeklyCommitActivity []*WeeklyCommitActivity
  83. resp, err := s.client.Do(ctx, req, &weeklyCommitActivity)
  84. if err != nil {
  85. return nil, resp, err
  86. }
  87. return weeklyCommitActivity, resp, nil
  88. }
  89. // ListCodeFrequency returns a weekly aggregate of the number of additions and
  90. // deletions pushed to a repository. Returned WeeklyStats will contain
  91. // additions and deletions, but not total commits.
  92. //
  93. // If this is the first time these statistics are requested for the given
  94. // repository, this method will return an *AcceptedError and a status code of
  95. // 202. This is because this is the status that GitHub returns to signify that
  96. // it is now computing the requested statistics. A follow up request, after a
  97. // delay of a second or so, should result in a successful request.
  98. //
  99. // GitHub API docs: https://developer.github.com/v3/repos/statistics/#code-frequency
  100. func (s *RepositoriesService) ListCodeFrequency(ctx context.Context, owner, repo string) ([]*WeeklyStats, *Response, error) {
  101. u := fmt.Sprintf("repos/%v/%v/stats/code_frequency", owner, repo)
  102. req, err := s.client.NewRequest("GET", u, nil)
  103. if err != nil {
  104. return nil, nil, err
  105. }
  106. var weeks [][]int
  107. resp, err := s.client.Do(ctx, req, &weeks)
  108. // convert int slices into WeeklyStats
  109. var stats []*WeeklyStats
  110. for _, week := range weeks {
  111. if len(week) != 3 {
  112. continue
  113. }
  114. stat := &WeeklyStats{
  115. Week: &Timestamp{time.Unix(int64(week[0]), 0)},
  116. Additions: Int(week[1]),
  117. Deletions: Int(week[2]),
  118. }
  119. stats = append(stats, stat)
  120. }
  121. return stats, resp, err
  122. }
  123. // RepositoryParticipation is the number of commits by everyone
  124. // who has contributed to the repository (including the owner)
  125. // as well as the number of commits by the owner themself.
  126. type RepositoryParticipation struct {
  127. All []int `json:"all,omitempty"`
  128. Owner []int `json:"owner,omitempty"`
  129. }
  130. func (r RepositoryParticipation) String() string {
  131. return Stringify(r)
  132. }
  133. // ListParticipation returns the total commit counts for the 'owner'
  134. // and total commit counts in 'all'. 'all' is everyone combined,
  135. // including the 'owner' in the last 52 weeks. If you’d like to get
  136. // the commit counts for non-owners, you can subtract 'all' from 'owner'.
  137. //
  138. // The array order is oldest week (index 0) to most recent week.
  139. //
  140. // If this is the first time these statistics are requested for the given
  141. // repository, this method will return an *AcceptedError and a status code of
  142. // 202. This is because this is the status that GitHub returns to signify that
  143. // it is now computing the requested statistics. A follow up request, after a
  144. // delay of a second or so, should result in a successful request.
  145. //
  146. // GitHub API docs: https://developer.github.com/v3/repos/statistics/#participation
  147. func (s *RepositoriesService) ListParticipation(ctx context.Context, owner, repo string) (*RepositoryParticipation, *Response, error) {
  148. u := fmt.Sprintf("repos/%v/%v/stats/participation", owner, repo)
  149. req, err := s.client.NewRequest("GET", u, nil)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. participation := new(RepositoryParticipation)
  154. resp, err := s.client.Do(ctx, req, participation)
  155. if err != nil {
  156. return nil, resp, err
  157. }
  158. return participation, resp, nil
  159. }
  160. // PunchCard represents the number of commits made during a given hour of a
  161. // day of the week.
  162. type PunchCard struct {
  163. Day *int // Day of the week (0-6: =Sunday - Saturday).
  164. Hour *int // Hour of day (0-23).
  165. Commits *int // Number of commits.
  166. }
  167. // ListPunchCard returns the number of commits per hour in each day.
  168. //
  169. // If this is the first time these statistics are requested for the given
  170. // repository, this method will return an *AcceptedError and a status code of
  171. // 202. This is because this is the status that GitHub returns to signify that
  172. // it is now computing the requested statistics. A follow up request, after a
  173. // delay of a second or so, should result in a successful request.
  174. //
  175. // GitHub API docs: https://developer.github.com/v3/repos/statistics/#punch-card
  176. func (s *RepositoriesService) ListPunchCard(ctx context.Context, owner, repo string) ([]*PunchCard, *Response, error) {
  177. u := fmt.Sprintf("repos/%v/%v/stats/punch_card", owner, repo)
  178. req, err := s.client.NewRequest("GET", u, nil)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. var results [][]int
  183. resp, err := s.client.Do(ctx, req, &results)
  184. // convert int slices into Punchcards
  185. var cards []*PunchCard
  186. for _, result := range results {
  187. if len(result) != 3 {
  188. continue
  189. }
  190. card := &PunchCard{
  191. Day: Int(result[0]),
  192. Hour: Int(result[1]),
  193. Commits: Int(result[2]),
  194. }
  195. cards = append(cards, card)
  196. }
  197. return cards, resp, err
  198. }