-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
97 lines (84 loc) · 1.91 KB
/
fetcher.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/google/go-github/github"
)
type Filter func(*github.User) bool
func and(a Filter, b Filter) Filter {
return func(u *github.User) bool {
return a(u) && b(u)
}
}
func or(a Filter, b Filter) Filter {
return func(u *github.User) bool {
return a(u) || b(u)
}
}
type RepoContributersFetcher struct {
client *github.Client
filter Filter
}
func (fetcher *RepoContributersFetcher) processRepo(r string) {
fmt.Printf("Start processing '%v'\n", r)
a := strings.Split(r, "/")
if len(a) != 2 {
fmt.Printf("'%v' is incorrect\n", r)
return
}
owner, repo := a[0], a[1]
ch := fetcher.fetch(owner, repo)
f, err := os.OpenFile(repo+".csv", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("%v\n", err)
return
}
defer f.Close()
for ret := range ch {
f.Write([]byte("\""))
_, err := f.Write([]byte(strings.Join(ret, "\",\"")))
if err != nil {
fmt.Printf("%v\n", err)
return
}
f.Write([]byte("\"\n"))
}
}
func v(p *string) string {
if p != nil {
return *p
}
return ""
}
func (fetcher *RepoContributersFetcher) fetch(owner, repo string) <-chan []string {
ch := make(chan []string)
stats, _, err := fetcher.client.Repositories.ListContributorsStats(owner, repo)
if err != nil {
fmt.Printf("%v\n", err)
}
go func() {
ch <- fetcher.getHeader()
for _, s := range stats {
c := s.Author
u, _, err := fetcher.client.Users.Get(*c.Login)
if err != nil {
fmt.Printf("%v\n", err)
continue
}
if fetcher.filter(u) {
ch <- fetcher.getUser(u, *s.Total)
}
}
close(ch)
}()
return ch
}
func (fetcher *RepoContributersFetcher) getHeader() []string {
return []string{"Name", "Email", "Location", "URL", "Points"}
}
func (fetcher *RepoContributersFetcher) getUser(u *github.User, points int) []string {
return []string{v(u.Name), v(u.Email),
v(u.Location), v(u.HTMLURL), strconv.Itoa(points)}
}