123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package github
- import (
- "context"
- "fmt"
- "time"
- )
- 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)
- }
- 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)
- }
- func (s *RepositoriesService) ListContributorsStats(ctx context.Context, 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(ctx, req, &contributorStats)
- if err != nil {
- return nil, resp, err
- }
- return contributorStats, resp, nil
- }
- 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)
- }
- func (s *RepositoriesService) ListCommitActivity(ctx context.Context, 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(ctx, req, &weeklyCommitActivity)
- if err != nil {
- return nil, resp, err
- }
- return weeklyCommitActivity, resp, nil
- }
- func (s *RepositoriesService) ListCodeFrequency(ctx context.Context, 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(ctx, req, &weeks)
-
- 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
- }
- type RepositoryParticipation struct {
- All []int `json:"all,omitempty"`
- Owner []int `json:"owner,omitempty"`
- }
- func (r RepositoryParticipation) String() string {
- return Stringify(r)
- }
- func (s *RepositoriesService) ListParticipation(ctx context.Context, 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(ctx, req, participation)
- if err != nil {
- return nil, resp, err
- }
- return participation, resp, nil
- }
- type PunchCard struct {
- Day *int
- Hour *int
- Commits *int
- }
- func (s *RepositoriesService) ListPunchCard(ctx context.Context, 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(ctx, req, &results)
-
- 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
- }
|