Skip to content

Commit

Permalink
Improve logic for versions with pre-release (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Mar 7, 2023
1 parent 8147bfd commit d2e8d25
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
4 changes: 0 additions & 4 deletions cmd/fetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
32 changes: 15 additions & 17 deletions internal/fetchclient/fetchclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand All @@ -315,15 +309,19 @@ 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
}
if !semver.IsValid(version) {
return "", false
}
if semver.Prerelease(version) != "" {
return "", false
}
return version, true
}
6 changes: 1 addition & 5 deletions internal/source/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package source

import (
"io"
"strconv"

"gopkg.in/yaml.v3"
)
Expand All @@ -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)
Expand Down

0 comments on commit d2e8d25

Please sign in to comment.