-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathghapi.go
More file actions
107 lines (93 loc) · 2.96 KB
/
ghapi.go
File metadata and controls
107 lines (93 loc) · 2.96 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
package ghapi
import (
"context"
"fmt"
"math/rand"
"net/http"
"strings"
"time"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
"github.com/api7/contributor-graph/api/internal/utils"
)
var (
ListOpts = github.ListOptions{PerPage: 100}
)
func SplitRepo(repo string) (string, string, error) {
strs := strings.Split(repo, "/")
if len(strs) != 2 {
return "", "", fmt.Errorf("Repo format error")
}
return strs[0], strs[1], nil
}
func GetGithubClient(ctx context.Context, token string) *github.Client {
var tc *http.Client
if token != "" {
tc = getToken(ctx, token)
}
return github.NewClient(tc)
}
func FormatCommits(ctx context.Context, comLists []*utils.ConList) ([]utils.ReturnCon, int, error) {
var returnCons []utils.ReturnCon
var authors []string
var timeLast time.Time
var numNotCount int
for i, c := range comLists {
if c.Date.IsZero() {
numNotCount++
continue
}
if compareSameDay(c.Date, timeLast) {
authors = append(authors, c.Author)
} else {
if len(authors) > 0 {
returnCons = append(returnCons, utils.ReturnCon{timeLast, i - numNotCount, authors})
}
timeLast = c.Date
authors = []string{c.Author}
}
}
returnCons = append(returnCons, utils.ReturnCon{timeLast, len(comLists) - numNotCount, authors})
return returnCons, http.StatusOK, nil
}
// TODO: support goroutine
func GetCommits(ctx context.Context, ghCli *github.Client, repoName string, listCommitOpts *github.CommitsListOptions, isSearch bool) ([]*github.RepositoryCommit, *github.Response, int, error) {
owner, repo, err := SplitRepo(repoName)
if err != nil {
return nil, nil, http.StatusBadRequest, err
}
commits, resp, err := ghCli.Repositories.ListCommits(ctx, owner, repo, listCommitOpts)
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
return nil, nil, http.StatusNotFound, fmt.Errorf("Repo not found")
}
if _, ok := err.(*github.RateLimitError); ok || strings.Contains(err.Error(), "403 API rate limit exceeded") {
if isSearch {
return nil, nil, http.StatusForbidden, fmt.Errorf("Hit rate limit! Need Github Token to increase API quota!")
}
// give it another random chance to see if magic happens
*ghCli = *GetGithubClient(ctx, utils.UpdateToken[rand.Intn(len(utils.UpdateToken))])
commits, resp, err = ghCli.Repositories.ListCommits(ctx, owner, repo, listCommitOpts)
if err != nil {
return nil, nil, http.StatusForbidden, fmt.Errorf("Hit rate limit")
}
fmt.Println("MAGIC happens and let's rolling again!")
} else {
return nil, nil, http.StatusInternalServerError, err
}
}
return commits, resp, http.StatusOK, nil
}
func compareSameDay(time1 time.Time, time2 time.Time) bool {
if time1.Day() == time2.Day() && time1.Month() == time2.Month() && time1.Year() == time2.Year() {
return true
}
return false
}
func getToken(ctx context.Context, token string) *http.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
return tc
}