-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.go
92 lines (73 loc) · 1.87 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
package updatechecker
import (
"context"
"errors"
"io/ioutil"
"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"
)
type Dependency struct {
Name string `json:"name"`
Version string `json:"version"`
}
type Args struct {
Dependency *Dependency `json:"dependency"`
}
// GetVersions returns a list of versions for the given dependency that
// are within the same major version.
func GetVersions(args *Args) (interface{}, error) {
if args.Dependency == nil {
return nil, errors.New("Expected args.dependency to not be nil")
}
currentVersion := args.Dependency.Version
modload.LoadModFile(context.Background())
repo := modfetch.Lookup("direct", args.Dependency.Name)
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)
var candidateVersions []string
Outer:
for _, v := range versions {
if semver.Major(v) != currentMajor {
continue
}
for _, exclude := range excludes {
if v == exclude {
continue Outer
}
}
candidateVersions = append(candidateVersions, v)
}
return candidateVersions, 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
}