-
Notifications
You must be signed in to change notification settings - Fork 1
/
commits.go
103 lines (85 loc) · 2.18 KB
/
commits.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
package comatome
import (
"context"
"errors"
"fmt"
"sort"
"time"
"github.com/google/go-github/v24/github"
)
// CommitsPerRepo is a map to represent commits/repository
type CommitsPerRepo map[string]int
var errIncompleteResult = errors.New("incomplete result error")
// QueryCommitsPerRepo queries commits per repository created on specified term (fromto)
func QueryCommitsPerRepo(c *Client, fromto *FromTo) (CommitsPerRepo, error) {
var (
m CommitsPerRepo
err error
maxRetry = 5
)
emails, _, err := c.Users.ListEmails(context.Background(), nil)
if err != nil {
return nil, err
}
for i := 0; i < maxRetry; i++ {
m, err = queryCommitsPerRepo(c, emails, fromto)
if err == errIncompleteResult {
<-time.After(1 * time.Second)
continue
}
break
}
return m, err
}
func queryCommitsPerRepo(c *Client, emails []*github.UserEmail, fromto *FromTo) (CommitsPerRepo, error) {
m := make(CommitsPerRepo)
from, to := fromto.QueryStr()
for _, email := range emails {
page := 1
for {
// Notice:
// if fromto is 2018-01-01..2018-01-31, the result doesn't include 2018-01-01's commits.
// if 2018-01-01's commits should be included, fromto should be like 2018-12-31..2019-01-31
result, resp, err := c.Search.Commits(
context.Background(),
fmt.Sprintf("author-email:%s author-date:%s..%s", email.GetEmail(), from, to),
&github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
Page: page,
}})
if err != nil {
return nil, fmt.Errorf("failed search commits: %v", err)
}
page = resp.NextPage
if *result.IncompleteResults {
return nil, errIncompleteResult
}
for _, v := range result.Commits {
m[*v.Repository.FullName]++
}
if resp.NextPage == 0 {
break
}
}
}
return m, nil
}
// ShowCommitsPerRepo shows commits/repositories
func ShowCommitsPerRepo(m CommitsPerRepo) {
keys := make([]string, len(m))
index := 0
for k := range m {
keys[index] = k
index++
}
sort.Strings(keys)
total := 0
for _, v := range keys {
total += m[v]
}
fmt.Printf("Created %d commits in %d repositories\n", total, len(m))
for _, v := range keys {
fmt.Printf("%d\t%s\n", m[v], v)
}
}