-
Notifications
You must be signed in to change notification settings - Fork 23
/
gitlab.go
172 lines (143 loc) · 4.77 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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package upstream
import (
"errors"
"fmt"
"strings"
"github.com/blang/semver/v4"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/zeitgeist/pkg/gitlab"
)
// GitLab upstream representation.
type GitLab struct {
Base `mapstructure:",squash"`
// GitLab Server if is a self-hosted GitLab instead, default to gitlab.com
Server string
// GitLab URL, e.g. hashicorp/terraform or helm/helm
URL string
// Optional: semver constraints, e.g. < 2.0.0
// Will have no effect if the dependency does not follow Semver
Constraints string
// If branch is specified, the version should be a commit SHA
// Will look for new commits on the branch
Branch string
}
// LatestVersion returns the latest non-draft, non-prerelease GitLab Release
// for the given repository (depending on the Constraints if set).
//
// To authenticate your requests, use the GITLAB_TOKEN environment variable.
func (upstream GitLab) LatestVersion() (string, error) { //nolint:gocritic
log.Debug("Using GitLab flavour")
return latestGitLabVersion(&upstream)
}
func latestGitLabVersion(upstream *GitLab) (string, error) {
if upstream.Branch == "" {
return latestGitLabRelease(upstream)
}
return latestGitlabCommit(upstream)
}
func latestGitLabRelease(upstream *GitLab) (string, error) {
var client *gitlab.GitLab
if upstream.Server == "" {
client = gitlab.New()
} else {
client = gitlab.NewPrivate(upstream.Server)
}
if client == nil {
return "", errors.New(
"cannot configure a GitLab client, make sure you have exported the GITLAB_TOKEN",
)
}
if !strings.Contains(upstream.URL, "/") {
return "", fmt.Errorf(
"invalid gitlab repo: %s\nGitLab repo should be in the form owner/repo e.g., kubernetes/kubernetes",
upstream.URL,
)
}
semverConstraints := upstream.Constraints
if semverConstraints == "" {
// If no range is passed, just use the broadest possible range
semverConstraints = DefaultSemVerConstraints
}
expectedRange, err := semver.ParseRange(semverConstraints)
if err != nil {
return "", fmt.Errorf("invalid semver constraints range: %#v: %w", upstream.Constraints, err)
}
splitURL := strings.Split(upstream.URL, "/")
owner := splitURL[0]
repo := strings.Join(splitURL[1:], "/")
var tags []string
// We'll need to fetch all releases, as GitLab doesn't provide sorting options.
// If we don't do that, we risk running into the case where for example:
// - Version 1.0.0 and 2.0.0 exist
// - A bugfix 1.0.1 gets released
//
// Now the "latest" (date-wise) release is not the highest semver, and not necessarily the one we want
log.Debugf("Retrieving releases for %s/%s...", owner, repo)
releases, err := client.Releases(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitLab releases: %w", err)
}
if len(releases) == 0 {
gitLabTags, err := client.ListTags(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitLab tags: %w", err)
}
for _, tag := range gitLabTags {
tags = append(tags, tag.Name)
}
} else {
for _, release := range releases {
if release.TagName == "" {
log.Debug("Skipping release without TagName")
}
tags = append(tags, release.TagName)
}
}
return selectHighestVersion(upstream.Constraints, expectedRange, tags)
}
func latestGitlabCommit(upstream *GitLab) (string, error) {
var client *gitlab.GitLab
if upstream.Server == "" {
client = gitlab.New()
} else {
client = gitlab.NewPrivate(upstream.Server)
}
if client == nil {
return "", errors.New(
"cannot configure a GitLab client, make sure you have exported the GITLAB_TOKEN",
)
}
splitURL := strings.Split(upstream.URL, "/")
owner := splitURL[0]
repo := strings.Join(splitURL[1:], "/")
log.Debugf("Retrieving repository information for %s/%s...", owner, repo)
repoInfo, err := client.GetRepository(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitLab repository: %w", err)
}
if repoInfo.Archived {
log.Warnf("GitLab repository %s/%s is archived", owner, repo)
}
branches, err := client.Branches(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitLab branches: %w", err)
}
for _, branch := range branches {
if branch.Name == upstream.Branch {
return branch.Commit.ID, nil
}
}
return "", fmt.Errorf("branch '%s' not found", upstream.Branch)
}