-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.go
116 lines (93 loc) · 2.35 KB
/
main.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
package updatechecker
import (
"errors"
"io/ioutil"
"regexp"
"github.com/dependabot/gomodules-extracted/cmd/go/_internal_/modfetch"
"github.com/dependabot/gomodules-extracted/cmd/go/_internal_/modload"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
var (
pseudoVersionRegexp = regexp.MustCompile(`\b\d{14}-[0-9a-f]{12}$`)
)
type Dependency struct {
Name string `json:"name"`
Version string `json:"version"`
Indirect bool `json:"indirect"`
}
type IgnoreRange struct {
MinVersionInclusive string `json:"min_version_inclusive"`
MaxVersionExclusive string `json:"max_version_exclusive"`
}
type Args struct {
Dependency *Dependency `json:"dependency"`
IgnoreRanges []*IgnoreRange `json:"ignore_ranges"`
}
func GetUpdatedVersion(args *Args) (interface{}, error) {
if args.Dependency == nil {
return nil, errors.New("Expected args.dependency to not be nil")
}
currentVersion := args.Dependency.Version
currentPrerelease := semver.Prerelease(currentVersion)
if pseudoVersionRegexp.MatchString(currentPrerelease) {
return currentVersion, nil
}
modload.InitMod()
repo, err := modfetch.Lookup("direct", args.Dependency.Name)
if err != nil {
return nil, err
}
versions, err := repo.Versions("")
if err != nil {
return nil, err
}
excludes, err := goModExcludes(args.Dependency.Name)
if err != nil {
return nil, err
}
currentMajor := semver.Major(currentVersion)
latestVersion := args.Dependency.Version
Outer:
for _, v := range versions {
if semver.Major(v) != currentMajor {
continue
}
if semver.Compare(v, latestVersion) < 1 {
continue
}
if currentPrerelease == "" && semver.Prerelease(v) != "" {
continue
}
for _, exclude := range excludes {
if v == exclude {
continue Outer
}
}
latestVersion = v
}
return latestVersion, nil
}
func goModExcludes(dependency string) ([]string, error) {
data, err := ioutil.ReadFile("go.mod")
if err != nil {
return nil, err
}
var f *modfile.File
// TODO library detection - don't consider exclude etc for libraries
if "library" == "true" {
f, err = modfile.ParseLax("go.mod", data, nil)
} else {
f, err = modfile.Parse("go.mod", data, nil)
}
if err != nil {
return nil, err
}
var excludes []string
for _, e := range f.Exclude {
if e.Mod.Path == dependency {
excludes = append(excludes, e.Mod.Version)
}
}
return excludes, nil
}