-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab.go
More file actions
124 lines (98 loc) · 3.1 KB
/
gitlab.go
File metadata and controls
124 lines (98 loc) · 3.1 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2023, the WebKit for Windows project authors. Please see the
// AUTHORS file for details. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package reqcheck
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/api/client-go"
)
type gitlabClient struct {
client *gitlab.Client
}
func NewGitLab(uri, token string) (Client, error) {
return NewGitLabClient(uri, token, http.DefaultClient)
}
func NewGitLabClient(uri, token string, cl *http.Client) (Client, error) {
// Parse the url
gitlabURL, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("could not parse gitlab link: %w", err)
}
// Get base url for API
relBaseURL, _ := url.Parse("./api/v4")
baseURL := gitlabURL.ResolveReference(relBaseURL)
logrus.WithFields(logrus.Fields{
"gitlab-url": gitlabURL.String(),
"base-url": baseURL.String(),
}).Debug("connecting to gitlab instance")
// Create the client
client, err := gitlab.NewClient(
token,
gitlab.WithBaseURL(baseURL.String()),
gitlab.WithHTTPClient(cl),
)
if err != nil {
return nil, fmt.Errorf("could not connect to gitlab instance %s: %w", uri, err)
}
return &gitlabClient{client: client}, nil
}
func (c *gitlabClient) ListReleases(ctx context.Context, owner, name string, opt ListOptions) ([]Release, error) {
glOpts := &gitlab.ListReleasesOptions{
ListOptions: gitlab.ListOptions{
Page: opt.Page,
PerPage: opt.PerPage,
},
}
logrus.WithFields(logrus.Fields{
"owner": owner,
"name": name,
"page": glOpts.Page,
"per-page": glOpts.PerPage,
}).Debug("listing gitlab releases")
releases, _, err := c.client.Releases.ListReleases(fmt.Sprintf("%s/%s", owner, name), glOpts)
if err != nil {
return nil, fmt.Errorf("error getting releases from repository %s/%s: %w", owner, name, err)
}
r := make([]Release, 0, glOpts.PerPage)
for _, release := range releases {
tagName := release.TagName
logrus.WithFields(logrus.Fields{
"tag": tagName,
"commit": release.Commit.ID,
}).Debug("found release")
r = append(r, Release{Tag: tagName, SemVer: generateVersion(tagName, versionMatcher)})
}
return r, nil
}
func (c *gitlabClient) ListTags(ctx context.Context, owner, name string, opt ListOptions) ([]Release, error) {
glOpts := &gitlab.ListTagsOptions{
ListOptions: gitlab.ListOptions{
Page: opt.Page,
PerPage: opt.PerPage,
},
}
logrus.WithFields(logrus.Fields{
"owner": owner,
"name": name,
"page": glOpts.Page,
"per-page": glOpts.PerPage,
}).Debug("listing gitlab tags")
tags, _, err := c.client.Tags.ListTags(fmt.Sprintf("%s/%s", owner, name), glOpts)
if err != nil {
return nil, fmt.Errorf("error getting tags from repository %s/%s: %w", owner, name, err)
}
r := make([]Release, 0, glOpts.PerPage)
for _, tag := range tags {
tagName := tag.Name
logrus.WithFields(logrus.Fields{
"tag": tagName,
"commit": tag.Commit.ID,
}).Debug("found tag")
r = append(r, Release{Tag: tagName, SemVer: generateVersion(tagName, versionMatcher)})
}
return r, nil
}