Skip to content

Commit

Permalink
Use tarnsformVersion to get proper one
Browse files Browse the repository at this point in the history
Signed-off-by: debasishbsws <[email protected]>
  • Loading branch information
debasishbsws committed Jul 2, 2024
1 parent 694c357 commit 4f81086
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
4 changes: 2 additions & 2 deletions pkg/update/githubReleases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/update/githubReleases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package update
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
})
}
}
Expand Down
72 changes: 36 additions & 36 deletions pkg/update/gitlabRelease.go → pkg/update/gitlabReleases.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"

"chainguard.dev/melange/pkg/config"
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -131,45 +128,42 @@ 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,
}
}
}

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

Check failure on line 152 in pkg/update/gitlabReleases.go

View workflow job for this annotation

GitHub Actions / lint

packageConfig.Update.GitLabMonitor undefined (type "chainguard.dev/melange/pkg/config".Update has no field or method GitLabMonitor) (typecheck)
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) {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/update/gitlabReleases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package update

0 comments on commit 4f81086

Please sign in to comment.