-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (93 loc) · 2.48 KB
/
main.go
File metadata and controls
113 lines (93 loc) · 2.48 KB
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
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"cloud.google.com/go/datastore"
"github.com/api7/contributor-graph/api/internal/gcpdb"
"github.com/api7/contributor-graph/api/internal/ghapi"
"github.com/api7/contributor-graph/api/internal/utils"
"github.com/google/go-github/v33/github"
)
var repos = []string{"repo"}
func main() {
ctx := context.Background()
dbCli, err := datastore.NewClient(ctx, utils.ProjectID)
if err != nil {
panic(err)
}
defer dbCli.Close()
tokens, err := gcpdb.GetTokens(dbCli)
if err != nil {
panic(err)
}
ghCli := ghapi.GetGithubClient(ctx, tokens[0].Token)
for _, repo := range repos {
var res [][]string
res = append(res, []string{"id", "name", "email", "contributrions", "compamy", "location", "bio", "github url"})
fmt.Println(repo)
logins, contributions := getTop500Contributors(ghCli, repo)
fmt.Printf("get %d contributors\n", len(logins))
users := getUserMeta(ghCli, logins)
for i, u := range users {
res = append(res, []string{logins[i], u.GetName(), u.GetEmail(), contributions[i], u.GetCompany(), u.GetLocation(), fmt.Sprintf("%q", u.GetBio()), u.GetHTMLURL()})
}
_, repoName, err := ghapi.SplitRepo(repo)
if err != nil {
panic(err)
}
file, err := os.Create(repoName + "_contributors.csv")
if err != nil {
panic(err)
}
w := csv.NewWriter(file)
for _, r := range res {
if err := w.Write(r); err != nil {
panic(err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
}
func getTop500Contributors(client *github.Client, repo string) ([]string, []string) {
ctx := context.Background()
owner, repoName, err := ghapi.SplitRepo(repo)
if err != nil {
panic(err)
}
listConOpts := &github.ListContributorsOptions{ListOptions: ghapi.ListOpts}
var logins, contributions []string
for {
cons, resp, err := client.Repositories.ListContributors(ctx, owner, repoName, listConOpts)
if err != nil {
panic(err)
}
for _, c := range cons {
logins = append(logins, c.GetLogin())
contributions = append(contributions, strconv.Itoa(c.GetContributions()))
}
if resp.NextPage == 0 {
break
}
listConOpts.Page = resp.NextPage
}
return logins, contributions
}
func getUserMeta(client *github.Client, logins []string) []*github.User {
ctx := context.Background()
var users []*github.User
for _, l := range logins {
user, _, err := client.Users.Get(ctx, l)
if err != nil {
panic(err)
}
users = append(users, user)
}
return users
}