-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitlab.go
471 lines (386 loc) · 11.6 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package gits
import (
"context"
"fmt"
"time"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/util"
gitlab "github.com/wbrefvem/go-gitlab"
)
type GitlabProvider struct {
Username string
Client *gitlab.Client
Context context.Context
Server auth.AuthServer
User auth.UserAuth
}
func NewGitlabProvider(server *auth.AuthServer, user *auth.UserAuth) (GitProvider, error) {
c := gitlab.NewClient(nil, user.ApiToken)
return withGitlabClient(server, user, c)
}
// Used by unit tests to inject a mocked client
func withGitlabClient(server *auth.AuthServer, user *auth.UserAuth, client *gitlab.Client) (GitProvider, error) {
provider := &GitlabProvider{
Server: *server,
User: *user,
Username: user.Username,
Client: client,
}
return provider, nil
}
func (g *GitlabProvider) ListRepositories(org string) ([]*GitRepository, error) {
result, _, err := getRepositories(g.Client, g.Username, org)
if err != nil {
return nil, err
}
var repos []*GitRepository
for _, p := range result {
repos = append(repos, fromGitlabProject(p))
}
return repos, nil
}
func (g *GitlabProvider) ListReleases(org string, name string) ([]*GitRelease, error) {
answer := []*GitRelease{}
// TODO
return answer, nil
}
func getRepositories(g *gitlab.Client, username string, org string) ([]*gitlab.Project, *gitlab.Response, error) {
if org == "" {
return g.Projects.ListUserProjects(username, &gitlab.ListProjectsOptions{Owned: gitlab.Bool(true)})
}
return g.Groups.ListGroupProjects(org, nil)
}
func fromGitlabProject(p *gitlab.Project) *GitRepository {
return &GitRepository{
Name: p.Name,
HTMLURL: p.WebURL,
SSHURL: p.SSHURLToRepo,
CloneURL: p.SSHURLToRepo,
Fork: p.ForkedFromProject != nil,
}
}
func (g *GitlabProvider) CreateRepository(org string, name string, private bool) (*GitRepository, error) {
visibility := gitlab.PublicVisibility
if private {
visibility = gitlab.PrivateVisibility
}
p := &gitlab.CreateProjectOptions{
Name: &name,
Visibility: &visibility,
}
project, _, err := g.Client.Projects.CreateProject(p)
if err != nil {
return nil, err
}
return fromGitlabProject(project), nil
}
func projectId(org, username, repoName string) string {
return fmt.Sprintf("%s/%s", owner(org, username), repoName)
}
func owner(org, username string) string {
if org == "" {
return username
}
return org
}
func (g *GitlabProvider) GetRepository(org, name string) (*GitRepository, error) {
pid := projectId(org, g.Username, name)
project, response, err := g.Client.Projects.GetProject(pid)
if err != nil {
return nil, fmt.Errorf("%v", response.Request.URL)
}
return fromGitlabProject(project), nil
}
func (g *GitlabProvider) ListOrganisations() ([]GitOrganisation, error) {
groups, _, err := g.Client.Groups.ListGroups(nil)
if err != nil {
return nil, err
}
var organizations []GitOrganisation
for _, v := range groups {
organizations = append(organizations, GitOrganisation{v.Path})
}
return organizations, nil
}
func (g *GitlabProvider) DeleteRepository(org, name string) error {
pid := projectId(org, g.Username, name)
_, err := g.Client.Projects.DeleteProject(pid)
if err != nil {
return fmt.Errorf("failed to delete repository %s due to: %s", pid, err)
}
return err
}
func (g *GitlabProvider) ForkRepository(originalOrg, name, destinationOrg string) (*GitRepository, error) {
pid := projectId(originalOrg, g.Username, name)
project, _, err := g.Client.Projects.ForkProject(pid)
if err != nil {
return nil, err
}
return fromGitlabProject(project), nil
}
func (g *GitlabProvider) RenameRepository(org, name, newName string) (*GitRepository, error) {
pid := projectId(org, g.Username, name)
options := &gitlab.EditProjectOptions{
Name: &newName,
}
project, _, err := g.Client.Projects.EditProject(pid, options)
if err != nil {
return nil, err
}
return fromGitlabProject(project), nil
}
func (g *GitlabProvider) ValidateRepositoryName(org, name string) error {
pid := projectId(org, g.Username, name)
_, r, err := g.Client.Projects.GetProject(pid)
if err == nil {
return fmt.Errorf("repository %s already exists", pid)
}
if r != nil && r.StatusCode == 404 {
return nil
}
return err
}
func (g *GitlabProvider) CreatePullRequest(data *GitPullRequestArguments) (*GitPullRequest, error) {
owner := data.Owner
repo := data.Repo
title := data.Title
body := data.Body
head := data.Head
base := data.Base
o := &gitlab.CreateMergeRequestOptions{
Title: &title,
Description: &body,
SourceBranch: &head,
TargetBranch: &base,
}
pid := projectId(owner, g.Username, repo)
mr, _, err := g.Client.MergeRequests.CreateMergeRequest(pid, o)
if err != nil {
return nil, err
}
return fromMergeRequest(mr, owner, repo), nil
}
func fromMergeRequest(mr *gitlab.MergeRequest, owner, repo string) *GitPullRequest {
return &GitPullRequest{
Author: mr.Author.Username,
URL: mr.WebURL,
Owner: owner,
Repo: repo,
Number: &mr.IID,
State: &mr.State,
Title: mr.Title,
Body: mr.Description,
}
}
func (g *GitlabProvider) UpdatePullRequestStatus(pr *GitPullRequest) error {
owner := pr.Owner
repo := pr.Repo
pid := projectId(owner, g.Username, repo)
mr, _, err := g.Client.MergeRequests.GetMergeRequest(pid, *pr.Number)
if err != nil {
return err
}
*pr = *fromMergeRequest(mr, owner, repo)
return nil
}
func (p *GitlabProvider) GetPullRequest(owner, repo string, number int) (*GitPullRequest, error) {
pr := &GitPullRequest{
Owner: owner,
Repo: repo,
Number: &number,
}
err := p.UpdatePullRequestStatus(pr)
return pr, err
}
func (g *GitlabProvider) PullRequestLastCommitStatus(pr *GitPullRequest) (string, error) {
owner := pr.Owner
repo := pr.Repo
ref := pr.LastCommitSha
if ref == "" {
return "", fmt.Errorf("missing String for LastCommitSha %#v", pr)
}
pid := projectId(owner, g.Username, repo)
c, _, err := g.Client.Commits.GetCommitStatuses(pid, ref, nil)
if err != nil {
return "", err
}
for _, result := range c {
if result.Status != "" {
return result.Status, nil
}
}
return "", fmt.Errorf("could not find a status for repository %s with ref %s", pid, ref)
}
func (g *GitlabProvider) ListCommitStatus(org string, repo string, sha string) ([]*GitRepoStatus, error) {
pid := projectId(org, g.Username, repo)
c, _, err := g.Client.Commits.GetCommitStatuses(pid, sha, nil)
if err != nil {
return nil, err
}
var statuses []*GitRepoStatus
for _, result := range c {
statuses = append(statuses, fromCommitStatus(result))
}
return statuses, nil
}
func fromCommitStatus(status *gitlab.CommitStatus) *GitRepoStatus {
return &GitRepoStatus{
ID: int64(status.ID),
URL: status.TargetURL,
State: status.Status,
Description: status.Description,
}
}
func (g *GitlabProvider) MergePullRequest(pr *GitPullRequest, message string) error {
pid := projectId(pr.Owner, g.Username, pr.Repo)
opt := &gitlab.AcceptMergeRequestOptions{MergeCommitMessage: &message}
_, _, err := g.Client.MergeRequests.AcceptMergeRequest(pid, *pr.Number, opt)
return err
}
func (g *GitlabProvider) CreateWebHook(data *GitWebHookArguments) error {
pid := projectId(data.Owner, g.Username, data.Repo)
opt := &gitlab.AddProjectHookOptions{
URL: &data.URL,
Token: &data.Secret,
}
_, _, err := g.Client.Projects.AddProjectHook(pid, opt)
return err
}
func (g *GitlabProvider) SearchIssues(org, repo, query string) ([]*GitIssue, error) {
opt := &gitlab.ListProjectIssuesOptions{Search: &query}
return g.searchIssuesWithOptions(org, repo, opt)
}
func (g *GitlabProvider) SearchIssuesClosedSince(org string, repo string, t time.Time) ([]*GitIssue, error) {
closed := "closed"
opt := &gitlab.ListProjectIssuesOptions{State: &closed}
issues, err := g.searchIssuesWithOptions(org, repo, opt)
if err != nil {
return issues, err
}
return FilterIssuesClosedSince(issues, t), nil
}
func (g *GitlabProvider) searchIssuesWithOptions(org string, repo string, opt *gitlab.ListProjectIssuesOptions) ([]*GitIssue, error) {
pid := projectId(org, g.Username, repo)
issues, _, err := g.Client.Issues.ListProjectIssues(pid, opt)
if err != nil {
return nil, err
}
return fromGitlabIssues(issues, owner(org, g.Username), repo), nil
}
func (g *GitlabProvider) GetIssue(org, repo string, number int) (*GitIssue, error) {
owner := owner(org, g.Username)
pid := projectId(org, g.Username, repo)
issue, _, err := g.Client.Issues.GetIssue(pid, number)
if err != nil {
return nil, err
}
return fromGitlabIssue(issue, owner, repo), nil
}
func (g *GitlabProvider) CreateIssue(owner string, repo string, issue *GitIssue) (*GitIssue, error) {
labels := []string{}
for _, label := range issue.Labels {
name := label.Name
if name != "" {
labels = append(labels, name)
}
}
opt := &gitlab.CreateIssueOptions{
Title: &issue.Title,
Description: &issue.Body,
Labels: labels,
}
pid := projectId(owner, g.Username, repo)
gitlabIssue, _, err := g.Client.Issues.CreateIssue(pid, opt)
if err != nil {
return nil, err
}
return fromGitlabIssue(gitlabIssue, owner, repo), nil
}
func fromGitlabIssues(issues []*gitlab.Issue, owner, repo string) []*GitIssue {
var result []*GitIssue
for _, v := range issues {
result = append(result, fromGitlabIssue(v, owner, repo))
}
return result
}
func fromGitlabIssue(issue *gitlab.Issue, owner, repo string) *GitIssue {
var labels []GitLabel
for _, v := range issue.Labels {
labels = append(labels, GitLabel{Name: v})
}
return &GitIssue{
Number: &issue.IID,
URL: issue.WebURL,
Owner: owner,
Repo: repo,
Title: issue.Title,
Body: issue.Description,
Labels: labels,
ClosedAt: issue.ClosedAt,
}
}
func (g *GitlabProvider) AddPRComment(pr *GitPullRequest, comment string) error {
owner := pr.Owner
repo := pr.Repo
opt := &gitlab.CreateMergeRequestNoteOptions{Body: &comment}
pid := projectId(owner, g.Username, repo)
_, _, err := g.Client.Notes.CreateMergeRequestNote(pid, *pr.Number, opt)
return err
}
func (g *GitlabProvider) CreateIssueComment(owner string, repo string, number int, comment string) error {
opt := &gitlab.CreateIssueNoteOptions{Body: &comment}
pid := projectId(owner, g.Username, repo)
_, _, err := g.Client.Notes.CreateIssueNote(pid, number, opt)
return err
}
func (g *GitlabProvider) HasIssues() bool {
return true
}
func (g *GitlabProvider) IsGitHub() bool {
return false
}
func (g *GitlabProvider) IsGitea() bool {
return false
}
func (g *GitlabProvider) IsBitbucket() bool {
return false
}
func (g *GitlabProvider) Kind() string {
return "gitlab"
}
func (g *GitlabProvider) JenkinsWebHookPath(gitURL string, secret string) string {
return "/gitlab/notify_commit"
}
func (g *GitlabProvider) Label() string {
return g.Server.Label()
}
func (p *GitlabProvider) ServerURL() string {
return p.Server.URL
}
func (p *GitlabProvider) CurrentUsername() string {
return p.Username
}
func (p *GitlabProvider) UserInfo(username string) *v1.UserSpec {
users, _, err := p.Client.Users.ListUsers(&gitlab.ListUsersOptions{Username: &username})
if err != nil || len(users) == 0 {
return nil
}
user := users[0]
return &v1.UserSpec{
Username: username,
LinkURL: user.WebsiteURL,
ImageURL: user.AvatarURL,
Name: user.Name,
}
}
func (g *GitlabProvider) UpdateRelease(owner string, repo string, tag string, releaseInfo *GitRelease) error {
return nil
}
func (p *GitlabProvider) IssueURL(org string, name string, number int, isPull bool) string {
return ""
}
// GitlabAccessTokenURL returns the URL to click on to generate a personal access token for the git provider
func GitlabAccessTokenURL(url string) string {
return util.UrlJoin(url, "/profile/personal_access_tokens")
}