/
github.go
187 lines (172 loc) 路 4.92 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package warehouse
import (
"context"
"log"
"strings"
"sync"
"github.com/google/go-github/github"
"github.com/jinzhu/gorm"
"go.uber.org/zap"
"golang.org/x/oauth2"
)
func githubPull(target Target, wg *sync.WaitGroup, token string, db *gorm.DB, out chan<- []*Issue) {
defer wg.Done()
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
totalIssues := 0
callOpts := &github.IssueListByRepoOptions{State: "all"}
var lastEntry Issue
if err := db.Where("repository_id = ?", target.ProjectURL()).Order("updated_at desc").First(&lastEntry).Error; err == nil {
callOpts.Since = lastEntry.UpdatedAt
} else {
zap.L().Warn("failed to get last entry", zap.String("repo", target.ProjectURL()), zap.Error(err))
}
for {
issues, resp, err := client.Issues.ListByRepo(ctx, target.Namespace(), target.Project(), callOpts)
if err != nil {
log.Fatal(err)
return
}
totalIssues += len(issues)
zap.L().Debug("paginate",
zap.String("provider", "github"),
zap.String("repo", target.ProjectURL()),
zap.Int("new-issues", len(issues)),
zap.Int("total-issues", totalIssues),
)
normalizedIssues := []*Issue{}
for _, issue := range issues {
normalizedIssues = append(normalizedIssues, fromGithubIssue(issue))
}
out <- normalizedIssues
if resp.NextPage == 0 {
break
}
callOpts.Page = resp.NextPage
}
if rateLimits, _, err := client.RateLimits(ctx); err == nil {
zap.L().Debug("github API rate limiting", zap.Stringer("limit", rateLimits.GetCore()))
}
}
func fromGithubUser(input *github.User) *Account {
name := input.GetName()
if name == "" {
name = input.GetLogin()
}
return &Account{
Base: Base{
ID: input.GetLogin(),
CreatedAt: input.GetCreatedAt().Time,
UpdatedAt: input.GetUpdatedAt().Time,
},
Provider: &Provider{
Base: Base{
ID: "github", // FIXME: support multiple github instances
},
Driver: string(GithubDriver),
},
URL: input.GetURL(),
Location: input.GetLocation(),
Company: input.GetCompany(),
Blog: input.GetBlog(),
Email: input.GetEmail(),
AvatarURL: input.GetAvatarURL(),
Login: input.GetLogin(),
FullName: name,
}
}
func fromGithubRepositoryURL(input string) *Repository {
return &Repository{
Base: Base{
ID: input,
},
URL: input,
Provider: &Provider{
Base: Base{
ID: "github", // FIXME: support multiple github instances
},
Driver: string(GithubDriver),
},
}
}
func fromGithubMilestone(input *github.Milestone) *Milestone {
if input == nil {
return nil
}
parts := strings.Split(input.GetHTMLURL(), "/")
return &Milestone{
Base: Base{
ID: input.GetURL(), // FIXME: make it smaller
CreatedAt: input.GetCreatedAt(),
UpdatedAt: input.GetUpdatedAt(),
},
URL: input.GetURL(),
Title: input.GetTitle(),
Description: input.GetDescription(),
ClosedAt: input.GetClosedAt(),
DueOn: input.GetDueOn(),
Creator: fromGithubUser(input.GetCreator()),
Repository: fromGithubRepositoryURL(strings.Join(parts[0:len(parts)-2], "/")),
}
}
func fromGithubLabel(input *github.Label) *Label {
if input == nil {
return nil
}
return &Label{
Base: Base{
ID: input.GetURL(), // FIXME: make it smaller
},
Name: input.GetName(),
Color: input.GetColor(),
Description: input.GetDescription(),
URL: input.GetURL(),
}
}
func fromGithubIssue(input *github.Issue) *Issue {
parts := strings.Split(input.GetHTMLURL(), "/")
url := strings.Replace(input.GetHTMLURL(), "/pull/", "/issues/", -1)
issue := &Issue{
Base: Base{
ID: url,
CreatedAt: input.GetCreatedAt(),
UpdatedAt: input.GetUpdatedAt(),
},
CompletedAt: input.GetClosedAt(),
Repository: fromGithubRepositoryURL(strings.Join(parts[0:len(parts)-2], "/")),
Title: input.GetTitle(),
State: input.GetState(),
Body: input.GetBody(),
IsPR: input.PullRequestLinks != nil,
URL: url,
IsLocked: input.GetLocked(),
Comments: input.GetComments(),
Upvotes: *input.Reactions.PlusOne,
Downvotes: *input.Reactions.MinusOne,
Labels: make([]*Label, 0),
Assignees: make([]*Account, 0),
Author: fromGithubUser(input.User),
Milestone: fromGithubMilestone(input.Milestone),
/*
IsOrphan bool `json:"is-orphan"`
IsHidden bool `json:"is-hidden"`
BaseWeight int `json:"base-weight"`
Weight int `json:"weight"`
IsEpic bool `json:"is-epic"`
HasEpic bool `json:"has-epic"`
// internal
Parents []*Issue `json:"-" gorm:"-"`
Children []*Issue `json:"-" gorm:"-"`
Duplicates []*Issue `json:"-" gorm:"-"`
*/
}
for _, label := range input.Labels {
issue.Labels = append(issue.Labels, fromGithubLabel(&label))
}
for _, assignee := range input.Assignees {
issue.Assignees = append(issue.Assignees, fromGithubUser(assignee))
}
return issue
}