This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.go
101 lines (92 loc) · 2.36 KB
/
check.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
package main
import (
"context"
"fmt"
"sort"
"github.com/Masterminds/semver"
"github.com/cli/go-gh"
"golang.org/x/sync/errgroup"
)
func check(filename string, bugfix bool) error {
components, err := readCompoments(filename)
if err != nil {
return err
}
g, ctx := errgroup.WithContext(context.Background())
for name, component := range components {
// Force scope
name := name
component := component
g.Go(func() error {
return checkComponent(ctx, name, component, bugfix)
})
}
return g.Wait()
}
func checkComponent(ctx context.Context, name string, component component, bugfix bool) error {
newerVersion, err := checkComponentNewerVersions(component, bugfix)
if err != nil {
return err
}
if len(newerVersion) > 0 {
fmt.Printf("%s: %v\n", name, newerVersion)
}
return nil
}
func checkComponentNewerVersions(component component, bugfix bool) (semver.Collection, error) {
sVersions, err := fetchVersions(component.Github)
if err != nil {
return nil, err
}
currentVersion, err := semver.NewVersion(component.Version)
if err != nil {
return nil, err
}
newerVersion, err := getNewerVersion(currentVersion, sVersions, bugfix)
if err != nil {
return nil, err
}
return newerVersion, nil
}
func fetchVersions(github string) (semver.Collection, error) {
client, err := gh.RESTClient(nil)
if err != nil {
return nil, err
}
versions := []struct {
Name string
TagName string `json:"tag_name"`
}{}
err = client.Get(fmt.Sprintf("repos/%s/releases", github), &versions)
if err != nil {
return nil, err
}
sVersions := semver.Collection([]*semver.Version{})
for _, v := range versions {
sVersion, err := semver.NewVersion(v.TagName)
if err != nil {
return nil, err
}
sVersions = append(sVersions, sVersion)
}
sort.Sort(sVersions)
return sVersions, nil
}
func getNewerVersion(currentVersion *semver.Version, versions []*semver.Version, bugfix bool) (semver.Collection, error) {
constraint := fmt.Sprintf("> %s", currentVersion)
if bugfix {
nextMinorVersion := currentVersion.IncMinor()
constraint = fmt.Sprintf("> %s, < %s", currentVersion, nextMinorVersion.String())
}
c, err := semver.NewConstraint(constraint)
if err != nil {
return nil, err
}
newerVersion := semver.Collection([]*semver.Version{})
for _, sv := range versions {
if c.Check(sv) {
newerVersion = append(newerVersion, sv)
}
}
return newerVersion, nil
}