From d2e8d257e2083a2fa0c420bd8eced591a63dfaa2 Mon Sep 17 00:00:00 2001 From: Michael Fridman Date: Tue, 7 Mar 2023 12:42:15 -0500 Subject: [PATCH] Improve logic for versions with pre-release (#406) --- cmd/fetcher/main.go | 4 ---- internal/fetchclient/fetchclient.go | 32 ++++++++++++++--------------- internal/source/config.go | 6 +----- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/cmd/fetcher/main.go b/cmd/fetcher/main.go index b1eb70768..5117000b3 100644 --- a/cmd/fetcher/main.go +++ b/cmd/fetcher/main.go @@ -206,10 +206,6 @@ func run(ctx context.Context, root string) ([]createdPlugin, error) { } latestVersions[config.CacheKey()] = newVersion } - // For now we ignore prerelease versions. But this may change in the future. - if semver.Prerelease(newVersion) != "" && !config.IncludePrerelease { - continue - } // example: library/grpc pluginDir := filepath.Dir(config.Filename) ok, err := checkDirExists(filepath.Join(pluginDir, newVersion)) diff --git a/internal/fetchclient/fetchclient.go b/internal/fetchclient/fetchclient.go index 4be319943..2f4bd751e 100644 --- a/internal/fetchclient/fetchclient.go +++ b/internal/fetchclient/fetchclient.go @@ -153,8 +153,8 @@ func (c *Client) fetchCrate(ctx context.Context, name string) (string, error) { // from the server's index. continue } - if versionWithPrefix, ok := ensureSemverPrefix(version.Num); ok { - versions = append(versions, versionWithPrefix) + if v, ok := ensureSemverPrefix(version.Num); ok { + versions = append(versions, v) } } if len(versions) == 0 { @@ -255,15 +255,13 @@ func (c *Client) fetchMaven(ctx context.Context, group string, name string) (str } latestVersion := "" for _, version := range metadata.Versioning.Versions { - if !strings.HasPrefix(version, "v") { - version = "v" + version - } - if !semver.IsValid(version) || semver.Prerelease(version) != "" { + v, ok := ensureSemverPrefix(version) + if !ok { continue } - version = semver.Canonical(version) - if latestVersion == "" || semver.Compare(latestVersion, version) < 0 { - latestVersion = version + v = semver.Canonical(v) + if latestVersion == "" || semver.Compare(latestVersion, v) < 0 { + latestVersion = v } } if latestVersion == "" { @@ -295,12 +293,8 @@ func (c *Client) fetchGithub(ctx context.Context, owner string, repository strin if tag.Name == nil { continue } - // Skip versions with pre-releases (e.g. v1.2.3-alpha.1). - if semver.Prerelease(*tag.Name) != "" { - continue - } - if version, ok := ensureSemverPrefix(*tag.Name); ok { - versions = append(versions, version) + if v, ok := ensureSemverPrefix(*tag.Name); ok { + versions = append(versions, v) } } page = response.NextPage @@ -315,9 +309,10 @@ func (c *Client) fetchGithub(ctx context.Context, owner string, repository strin return versions[len(versions)-1], nil } -// ensureSemverPrefix ensures the given version is valid semver, optionally +// ensureSemverPrefix checks if the given version is valid semver, optionally // prefixing with "v". The output version is not guaranteed to be the same -// as input. +// as input. This function returns false if the version is not valid semver or +// is a prerelease. func ensureSemverPrefix(version string) (string, bool) { if !strings.HasPrefix(version, "v") { version = "v" + version @@ -325,5 +320,8 @@ func ensureSemverPrefix(version string) (string, bool) { if !semver.IsValid(version) { return "", false } + if semver.Prerelease(version) != "" { + return "", false + } return version, true } diff --git a/internal/source/config.go b/internal/source/config.go index a88ec7f51..d0996fe5a 100644 --- a/internal/source/config.go +++ b/internal/source/config.go @@ -2,7 +2,6 @@ package source import ( "io" - "strconv" "gopkg.in/yaml.v3" ) @@ -28,13 +27,10 @@ type Cacheable interface { type Config struct { Filename string `yaml:"-"` Source Source `yaml:"source"` - // IncludePrerelease includes semver prereleases when fetching versions - // from upstream. - IncludePrerelease bool `yaml:"include_prerelease"` } func (c Config) CacheKey() string { - return c.Source.CacheKey() + "-" + strconv.FormatBool(c.IncludePrerelease) + return c.Source.CacheKey() } var _ Cacheable = (*Config)(nil)