Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect updates for runc and similar pkgs #67

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/pkg/update/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func TestLatestGithub(t *testing.T) {
BaseURL: "https://github.com/protocolbuffers/protobuf/releases/",
LatestURL: "https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.tar.gz",
},

// https://github.com/opencontainers/runc/releases has releases with extra assets (and no version in the file name).
"https://github.com/opencontainers/runc/releases/download/v1.0.0/runc.tar.xz": {
HasUpdate: true,
BaseURL: "https://github.com/opencontainers/runc/releases/",
LatestURL: "",
},
"https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz": {
HasUpdate: false,
BaseURL: "https://github.com/opencontainers/runc/releases/",
LatestURL: "https://github.com/opencontainers/runc/releases/download/v1.0.1/runc.tar.xz",
},
} {
source, expected := source, expected

Expand Down
26 changes: 9 additions & 17 deletions internal/pkg/update/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ package update

import (
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"

"github.com/Masterminds/semver"
)

var (
versionRE = regexp.MustCompile(semver.SemVerRegex) // the same as semver.versionRegex except "^" and "$"

commonExtensions = map[string]struct{}{
".bz2": {},
".diff": {},
Expand All @@ -28,15 +30,7 @@ var (

// extractVersion extracts SemVer version from file name or URL.
func extractVersion(s string) (*semver.Version, error) {
// extract file name
u, err := url.Parse(s)
if err != nil {
return nil, err
}

s = filepath.Base(u.Path)

// remove common extensions
// remove common extensions like .bz2 that would confuse SemVer parser
found := true
for found {
ext := filepath.Ext(s)
Expand All @@ -45,15 +39,13 @@ func extractVersion(s string) (*semver.Version, error) {
}
}

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

s = s[i:]

res, err := semver.NewVersion(s)
// use the last match to skip hostnames, folders, etc
res, err := semver.NewVersion(matches[len(matches)-1])
if err != nil {
return nil, fmt.Errorf("%q: %w", s, err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/update/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestExtractVersion(t *testing.T) {

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