diff --git a/pkg/update/githubReleases.go b/pkg/update/githubReleases.go index 0f1866f3..1ffc62cd 100644 --- a/pkg/update/githubReleases.go +++ b/pkg/update/githubReleases.go @@ -555,7 +555,7 @@ func (o GitHubReleaseOptions) getLatestVersion(packageNameHash string, versionRe return nil } -func (o GitHubReleaseOptions) shouldSkipVersion(v string) bool { +func shouldSkipVersion(v string) bool { invalid := []string{"alpha", "beta", "rc", "pre"} for _, i := range invalid { if strings.Contains(strings.ToLower(v), i) { @@ -639,7 +639,7 @@ func (o GitHubReleaseOptions) prepareVersion(nameHash, v, id string) (string, er v = strings.ReplaceAll(v, c.Update.VersionSeparator, ".") } - if o.shouldSkipVersion(v) { + if shouldSkipVersion(v) { return "", nil } diff --git a/pkg/update/githubReleases_test.go b/pkg/update/githubReleases_test.go index e3215bcc..52638794 100644 --- a/pkg/update/githubReleases_test.go +++ b/pkg/update/githubReleases_test.go @@ -3,7 +3,6 @@ package update import ( "encoding/json" "fmt" - "log" "os" "path/filepath" "strings" @@ -226,11 +225,7 @@ func TestGitHubReleaseOptions_isVersionPreRelease(t *testing.T) { } for _, tt := range tests { t.Run(tt.version, func(t *testing.T) { - o := GitHubReleaseOptions{ - Logger: log.New(log.Writer(), "test: ", log.LstdFlags|log.Lmsgprefix), - } - - assert.Equalf(t, tt.skip, o.shouldSkipVersion(tt.version), "isVersionPreRelease(%v)", tt.version) + assert.Equalf(t, tt.skip, shouldSkipVersion(tt.version), "isVersionPreRelease(%v)", tt.version) }) } } diff --git a/pkg/update/gitlabRelease.go b/pkg/update/gitlabReleases.go similarity index 76% rename from pkg/update/gitlabRelease.go rename to pkg/update/gitlabReleases.go index 191dbe60..d5ce4c24 100644 --- a/pkg/update/gitlabRelease.go +++ b/pkg/update/gitlabReleases.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "os" - "regexp" "strings" "chainguard.dev/melange/pkg/config" @@ -88,16 +87,14 @@ func (o GitLabReleaseOptions) getLatestGitLabVersions() (map[string]NewVersionRe Commit: release.Commit.ID, }) } - properVersionList, err := prepareVersion(allReleaseList, &o.PackageConfigs[packageName].Config) + v, c, err := prepareLatestVersion(allReleaseList, &o.PackageConfigs[packageName].Config) if err != nil { o.ErrorMessages[packageName] = fmt.Sprintf("Failed to prepare version for %s: %v", packageName, err) continue } - if len(properVersionList) > 0 { - latestVersionResults[packageName] = NewVersionResults{ - Version: properVersionList[0].Version, - Commit: properVersionList[0].Commit, - } + latestVersionResults[packageName] = NewVersionResults{ + Version: v, + Commit: c, } } } @@ -131,16 +128,15 @@ func (o GitLabReleaseOptions) getLatestGitLabVersions() (map[string]NewVersionRe Commit: tag.Commit.ID, }) } - properVersionList, err := prepareVersion(allTagsList, &o.PackageConfigs[packageName].Config) + v, c, err := prepareLatestVersion(allTagsList, &o.PackageConfigs[packageName].Config) if err != nil { o.ErrorMessages[packageName] = fmt.Sprintf("Failed to prepare version for %s: %v", packageName, err) continue } - if len(properVersionList) > 0 { - latestVersionResults[packageName] = NewVersionResults{ - Version: properVersionList[0].Version, - Commit: properVersionList[0].Commit, - } + + latestVersionResults[packageName] = NewVersionResults{ + Version: v, + Commit: c, } } } @@ -148,28 +144,26 @@ func (o GitLabReleaseOptions) getLatestGitLabVersions() (map[string]NewVersionRe return latestVersionResults, o.ErrorMessages, nil } -func prepareVersion(versionList []VersionComit, packageConfig *config.Configuration) ([]VersionComit, error) { - properVersionList := []VersionComit{} +func prepareLatestVersion(versionList []VersionComit, packageConfig *config.Configuration) (latestVersion, commit string, err error) { if len(versionList) == 0 { - return properVersionList, errors.New("No versions found, empty list") + return latestVersion, commit, errors.New("No versions found, empty list of tags/releases") } glm := packageConfig.Update.GitLabMonitor if glm == nil { - return properVersionList, errors.New("No GitLab update configuration found for package") + return latestVersion, commit, errors.New("No GitLab update configuration found for package") } for _, vc := range versionList { - if len(packageConfig.Update.IgnoreRegexPatterns) > 0 { - for _, pattern := range packageConfig.Update.IgnoreRegexPatterns { - regex, err := regexp.Compile(pattern) - if err != nil { - return properVersionList, fmt.Errorf("Failed to compile regex %s", pattern) - } - if regex.MatchString(vc.Version) { - continue - } - } + ignore, err := ignoreVersions(packageConfig.Update.IgnoreRegexPatterns, vc.Version) + if err != nil { + return latestVersion, commit, err + } + if ignore { + continue + } + if shouldSkipVersion(vc.Version) { + continue } if glm.TagFilterPrefix != "" { if !strings.HasPrefix(vc.Version, glm.TagFilterPrefix) { @@ -182,21 +176,27 @@ func prepareVersion(versionList []VersionComit, packageConfig *config.Configurat } } - version := vc.Version + latestVersion = vc.Version + commit = vc.Commit if glm.StripPrefix != "" { - version = strings.TrimPrefix(version, glm.StripPrefix) + latestVersion = strings.TrimPrefix(latestVersion, glm.StripPrefix) } if glm.StripSuffix != "" { - version = strings.TrimSuffix(version, glm.StripSuffix) + latestVersion = strings.TrimSuffix(latestVersion, glm.StripSuffix) } - properVersionList = append(properVersionList, VersionComit{ - Version: version, - Commit: vc.Commit, - }) - } - return properVersionList, nil + latestVersion, er := transformVersion(packageConfig.Update, latestVersion) + if er != nil { + return latestVersion, commit, fmt.Errorf("Failed to apply version transforms to %s. Error: %s", latestVersion, er) + } + break + } + if latestVersion == "" { + return latestVersion, commit, errors.New("No latest version found") + } + return latestVersion, commit, nil } + func (o GitLabReleaseOptions) getSeparateRepoLists() (releaseRepoList, tagRepoList map[string]string) { tagRepoList = make(map[string]string) releaseRepoList = make(map[string]string) diff --git a/pkg/update/gitlabReleases_test.go b/pkg/update/gitlabReleases_test.go new file mode 100644 index 00000000..7a7e4d47 --- /dev/null +++ b/pkg/update/gitlabReleases_test.go @@ -0,0 +1 @@ +package update