-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
79 lines (69 loc) · 1.83 KB
/
github.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
package main
import (
"context"
"log"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func setUpGHClient(githubToken string) (context.Context, *github.Client) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
return ctx, github.NewClient(tc)
}
type GithubResult struct {
name string
repo *github.Repository
}
// Get additional per-repo information (current # of stars, primary programming language, description)
// directly from GitHub
func RepoWorker(ctx context.Context, client *github.Client, jobs <-chan string, results chan<- GithubResult) {
for repoName := range jobs {
log.Println("Getting", repoName)
nameParts := strings.Split(repoName, "/")
owner, name := nameParts[0], nameParts[1]
var repo *github.Repository
var err error
// Loop until we're not timed out
for {
repo, _, err = client.Repositories.Get(ctx, owner, name)
if err != nil {
log.Println(err)
if _, ok := err.(*github.RateLimitError); ok {
time.Sleep(time.Minute)
continue
}
}
break
}
if repo == nil {
log.Println("Repo is nil:", repoName)
results <- GithubResult{repoName, &github.Repository{}}
} else {
results <- GithubResult{repoName, repo}
}
}
}
func GetGHRepoInfo(data DataTable, githubToken string, nProc int) map[string]github.Repository {
ctx, client := setUpGHClient(githubToken)
GHInfoMap := make(map[string]github.Repository)
numJobs := len(data)
jobs := make(chan string, numJobs)
results := make(chan GithubResult, numJobs)
for i := 0; i < nProc; i++ {
go RepoWorker(ctx, client, jobs, results)
}
for repo := range data {
jobs <- repo
}
close(jobs)
for i := 0; i < numJobs; i++ {
result := <-results
GHInfoMap[result.name] = *result.repo
}
return GHInfoMap
}