-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_command.go
76 lines (64 loc) · 2.03 KB
/
check_command.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
package resource
import (
"sort"
"github.com/cppforlife/go-semi-semantic/version"
gitlab "gitlab.com/gitlab-org/api/client-go"
)
type CheckCommand struct {
gitlab GitLab
}
func NewCheckCommand(gitlab GitLab) *CheckCommand {
return &CheckCommand{
gitlab: gitlab,
}
}
func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
versionParser, err := newVersionParser(request.Source.TagFilter)
if err != nil {
return []Version{}, err
}
// fetch available releases
releases, err := c.gitlab.ListReleases()
if err != nil {
return []Version{}, err
}
// filter releases
filteredReleases := []*gitlab.Release{}
targetVersion, err := version.NewVersionFromString(versionParser.parse(request.Version.Tag))
if (request.Version != Version{}) && err != nil {
return []Version{}, err
}
for _, r := range releases {
current, err := version.NewVersionFromString(versionParser.parse(r.TagName))
// must match tag regex
if err != nil {
continue
}
// when given, keep only releases greater-or-equal than the target version
if (request.Version == Version{}) || !current.IsLt(targetVersion) {
filteredReleases = append(filteredReleases, r)
}
}
// sort releases from older to newer
sort.Slice(filteredReleases, func(i, j int) bool {
// errors ignored since has already been filtered out by regexp
first, _ := version.NewVersionFromString(versionParser.parse(filteredReleases[i].Name))
second, _ := version.NewVersionFromString(versionParser.parse(filteredReleases[j].Name))
return first.IsLt(second)
})
// no version available
if len(filteredReleases) == 0 {
return []Version{}, nil
}
// first check, no target version given, reply last available release
latestRelease := filteredReleases[len(filteredReleases)-1]
if (request.Version == Version{}) {
return []Version{versionFromRelease(latestRelease)}, nil
}
// built list of next available versions
nextVersions := []Version{}
for _, r := range filteredReleases {
nextVersions = append(nextVersions, versionFromRelease(r))
}
return nextVersions, nil
}