/
gitlab.go
212 lines (195 loc) 路 5.24 KB
/
gitlab.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package warehouse
import (
"fmt"
"net/url"
"strings"
"sync"
"time"
"github.com/jinzhu/gorm"
gitlab "github.com/xanzy/go-gitlab"
"go.uber.org/zap"
)
func gitlabPull(target Target, wg *sync.WaitGroup, token string, db *gorm.DB, out chan []*Issue) {
defer wg.Done()
client := gitlab.NewClient(nil, token)
if err := client.SetBaseURL(fmt.Sprintf("%s/api/v4", target.ProviderURL())); err != nil {
zap.L().Error("failed to configure GitLab client", zap.Error(err))
return
}
total := 0
gitlabOpts := &gitlab.ListProjectIssuesOptions{
ListOptions: gitlab.ListOptions{
PerPage: 30,
Page: 1,
},
}
var lastEntry Issue
if err := db.Where("repository_id = ?", target.ProjectURL()).Order("updated_at desc").First(&lastEntry).Error; err == nil {
gitlabOpts.UpdatedAfter = &lastEntry.UpdatedAt
}
// FIXME: fetch PRs
for {
issues, resp, err := client.Issues.ListProjectIssues(target.Path(), gitlabOpts)
if err != nil {
zap.L().Error("failed to pull issues", zap.Error(err))
return
}
total += len(issues)
zap.L().Debug("paginate",
zap.String("provider", "gitlab"),
zap.String("repo", target.ProjectURL()),
zap.Int("new-issues", len(issues)),
zap.Int("total-issues", total),
)
normalizedIssues := []*Issue{}
for _, issue := range issues {
normalizedIssues = append(normalizedIssues, fromGitlabIssue(issue))
}
out <- normalizedIssues
if resp.NextPage == 0 {
break
}
gitlabOpts.ListOptions.Page = resp.NextPage
}
}
func fromGitlabIssue(input *gitlab.Issue) *Issue {
repoURL := input.Links.Project
if repoURL == "" {
repoURL = strings.Replace(input.WebURL, fmt.Sprintf("/issues/%d", input.IID), "", -1)
}
//out, _ := json.MarshalIndent(input, "", " ")
//fmt.Println(string(out))
repo := fromGitlabRepositoryURL(repoURL)
issue := &Issue{
Base: Base{
ID: input.WebURL,
CreatedAt: *input.CreatedAt,
UpdatedAt: *input.UpdatedAt,
},
Repository: repo,
Title: input.Title,
State: input.State,
Body: input.Description,
IsPR: false,
URL: input.WebURL,
IsLocked: false, // not supported on GitLab
Comments: 0, // not supported directly
Upvotes: input.Upvotes,
Downvotes: input.Downvotes,
Labels: make([]*Label, 0),
Assignees: make([]*Account, 0),
Author: fromGitlabIssueAuthor(repo.Provider, input.Author),
Milestone: fromGitlabMilestone(repo, 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:"-"`
*/
}
if input.ClosedAt != nil {
issue.CompletedAt = *input.ClosedAt
}
for _, label := range input.Labels {
issue.Labels = append(issue.Labels, fromGitlabLabelname(repo, label))
}
//issue.Assignees = append(issue.Assignees, fromGitlabIssueAssignee(input.Assignee))
for _, assignee := range input.Assignees {
issue.Assignees = append(issue.Assignees, fromGitlabIssueAssignee(repo.Provider, assignee))
}
return issue
}
func fromGitlabLabelname(repository *Repository, name string) *Label {
url := fmt.Sprintf("%s/labels/%s", repository.URL, name)
return &Label{
Base: Base{
ID: url,
},
Name: name,
Color: "aaaacc",
URL: url,
// Description:
}
}
func fromGitlabIssueAssignee(provider *Provider, input *gitlab.IssueAssignee) *Account {
author := gitlab.IssueAuthor(*input)
return fromGitlabIssueAuthor(provider, &author)
}
func fromGitlabIssueAuthor(provider *Provider, input *gitlab.IssueAuthor) *Account {
name := input.Name
if name == "" {
name = input.Username
}
account := Account{
Base: Base{
ID: input.WebURL,
// UpdatedAt:
// CreatedAt:
},
Provider: &Provider{
Base: Base{
ID: "gitlab", // FIXME: support multiple gitlab instances
},
Driver: string(GitlabDriver),
},
// Email:
FullName: name,
Login: input.Username,
URL: input.WebURL,
// State: // FIXME: investigate what to do with this
// Location:
// Company:
// Blog:
AvatarURL: input.AvatarURL,
}
return &account
}
func fromGitlabRepositoryURL(input string) *Repository {
u, err := url.Parse(input)
if err != nil {
zap.L().Warn("invalid repository URL", zap.String("URL", input))
return nil
}
providerURL := fmt.Sprintf("%s://%s", u.Scheme, u.Host)
return &Repository{
Base: Base{
ID: input,
},
URL: input,
Provider: &Provider{
Base: Base{
ID: "gitlab", // FIXME: support multiple gitlab instances
},
URL: providerURL,
Driver: string(GitlabDriver),
},
}
}
func fromGitlabMilestone(repository *Repository, input *gitlab.Milestone) *Milestone {
if input == nil {
return nil
}
url := fmt.Sprintf("%s/milestones/%d", repository.URL, input.ID)
milestone := Milestone{
Base: Base{
ID: url,
CreatedAt: *input.CreatedAt,
UpdatedAt: *input.UpdatedAt,
},
URL: url,
Title: input.Title,
Description: input.Description,
}
if input.DueDate != nil {
milestone.DueOn = time.Time(*input.DueDate)
}
// startdate // FIXME: todo
// state // FIXME: todo
return &milestone
}