Skip to content

Commit a8d27f7

Browse files
AlekSitalos-bot
authored andcommitted
fix: detect updates for runc and similar pkgs
Refs #48. Signed-off-by: Alexey Palazhchenko <[email protected]>
1 parent aa62d4a commit a8d27f7

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

internal/pkg/update/github_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ func TestLatestGithub(t *testing.T) {
5858
BaseURL: "https://github.com/protocolbuffers/protobuf/releases/",
5959
LatestURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.tar.gz",
6060
},
61+
62+
// https://github.com/opencontainers/runc/releases has releases with extra assets (and no version in the file name).
63+
"https://github.com/opencontainers/runc/releases/download/v1.0.0/runc.tar.xz": {
64+
HasUpdate: true,
65+
BaseURL: "https://github.com/opencontainers/runc/releases/",
66+
LatestURL: "",
67+
},
68+
"https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz": {
69+
HasUpdate: false,
70+
BaseURL: "https://github.com/opencontainers/runc/releases/",
71+
LatestURL: "https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz",
72+
},
6173
} {
6274
source, expected := source, expected
6375

internal/pkg/update/version.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ package update
66

77
import (
88
"fmt"
9-
"net/url"
109
"path/filepath"
10+
"regexp"
1111
"strings"
1212

1313
"github.com/Masterminds/semver"
1414
)
1515

1616
var (
17+
versionRE = regexp.MustCompile(semver.SemVerRegex) // the same as semver.versionRegex except "^" and "$"
18+
1719
commonExtensions = map[string]struct{}{
1820
".bz2": {},
1921
".diff": {},
@@ -28,15 +30,7 @@ var (
2830

2931
// extractVersion extracts SemVer version from file name or URL.
3032
func extractVersion(s string) (*semver.Version, error) {
31-
// extract file name
32-
u, err := url.Parse(s)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
s = filepath.Base(u.Path)
38-
39-
// remove common extensions
33+
// remove common extensions like .bz2 that would confuse SemVer parser
4034
found := true
4135
for found {
4236
ext := filepath.Ext(s)
@@ -45,15 +39,13 @@ func extractVersion(s string) (*semver.Version, error) {
4539
}
4640
}
4741

48-
// remove package name, keep only version
49-
i := strings.IndexAny(s, "0123456789")
50-
if i < 0 {
51-
return nil, fmt.Errorf("failed to remove package name from %q", s)
42+
matches := versionRE.FindAllString(s, -1)
43+
if len(matches) == 0 {
44+
return nil, fmt.Errorf("failed to find version in %q", s)
5245
}
5346

54-
s = s[i:]
55-
56-
res, err := semver.NewVersion(s)
47+
// use the last match to skip hostnames, folders, etc
48+
res, err := semver.NewVersion(matches[len(matches)-1])
5749
if err != nil {
5850
return nil, fmt.Errorf("%q: %w", s, err)
5951
}

internal/pkg/update/version_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestExtractVersion(t *testing.T) {
2323

2424
"https://github.com/pullmoll/void-linux/archive/refs/tags/v1.2.7.tar.gz": "1.2.7",
2525
"https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.tar.gz": "3.17.3",
26+
"https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz": "1.0.1",
2627
} {
2728
s, expected := s, expected
2829
t.Run(s, func(t *testing.T) {

0 commit comments

Comments
 (0)