From dde21645c333e18b734efb9e61b17b356d56d3a4 Mon Sep 17 00:00:00 2001 From: Amirhesam Adibinia Date: Tue, 7 Feb 2023 18:32:54 +0330 Subject: [PATCH] fix: fix non helmified projects (#27) * fix: fix non helmified projects * refactor: apply PR's comments --- internal/models/base/base_analyzer.go | 14 +++++++------- internal/models/version_analyzer.go | 2 +- .../versionanalyzers/helm_version_analyzer.go | 13 +++++++++++++ .../versionanalyzers/helm_version_analyzer_test.go | 14 ++++++++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/internal/models/base/base_analyzer.go b/internal/models/base/base_analyzer.go index f473dde..bbbb723 100644 --- a/internal/models/base/base_analyzer.go +++ b/internal/models/base/base_analyzer.go @@ -29,13 +29,13 @@ func (a *BaseAnalyzer) GetExtraArgumentDefinitions() []models.ArgumentDefinition return []models.ArgumentDefinition{} } -// CompareVersions Returns 0 if the two version are equal, negative if left < right, and positive if left > right. -func (a *BaseAnalyzer) CompareVersions(left string, right string) int { - if len(left) > 0 && left[0] != 'v' { - left = "v" + left +// CompareVersions Returns 0 if the two version are equal, negative if oldVersion < newVersion, and positive if oldVersion > newVersion. +func (a *BaseAnalyzer) CompareVersions(oldVersion string, newVersion string) int { + if len(oldVersion) > 0 && oldVersion[0] != 'v' { + oldVersion = "v" + oldVersion } - if len(right) > 0 && right[0] != 'v' { - right = "v" + right + if len(newVersion) > 0 && newVersion[0] != 'v' { + newVersion = "v" + newVersion } - return semver.Compare(left, right) + return semver.Compare(oldVersion, newVersion) } diff --git a/internal/models/version_analyzer.go b/internal/models/version_analyzer.go index 7586ecf..8796034 100644 --- a/internal/models/version_analyzer.go +++ b/internal/models/version_analyzer.go @@ -5,5 +5,5 @@ type VersionAnalyzer interface { ChangeNeedsVersionUpdate(changedFilePaths []string, extraArgs *ArgumentValues) bool ReadVersion(projectRootPath string, extraArgs *ArgumentValues) (string, error) GetExtraArgumentDefinitions() []ArgumentDefinition - CompareVersions(left string, right string) int + CompareVersions(oldVersion string, newVersion string) int } diff --git a/internal/models/versionanalyzers/helm_version_analyzer.go b/internal/models/versionanalyzers/helm_version_analyzer.go index ce7ee15..3dc9ee8 100644 --- a/internal/models/versionanalyzers/helm_version_analyzer.go +++ b/internal/models/versionanalyzers/helm_version_analyzer.go @@ -1,6 +1,8 @@ package versionanalyzers import ( + "github.com/pkg/errors" + "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "os" "path/filepath" @@ -57,6 +59,9 @@ type helmChart struct { func (a *HelmVersionAnalyzer) ReadVersion(projectRootDir string, extraArgs *models.ArgumentValues) (string, error) { rootDir := filepath.Join(projectRootDir, *(*extraArgs)[HelmArgumentKeyRootDir]) + if _, err := os.Stat(filepath.Join(rootDir, "Chart.yaml")); errors.Is(err, os.ErrNotExist) { + return "", nil + } chartFileContent, err := os.ReadFile(filepath.Join(rootDir, "Chart.yaml")) if err != nil { return "", err @@ -72,3 +77,11 @@ func (a *HelmVersionAnalyzer) ReadVersion(projectRootDir string, extraArgs *mode func (a *HelmVersionAnalyzer) GetName() string { return versionAnalyzerNameHelm } + +func (a *HelmVersionAnalyzer) CompareVersions(oldVersion, newVersion string) int { + if oldVersion == "" { + logrus.Info("old revision has no 'Chart.yaml' file") + return -1 + } + return a.BaseAnalyzer.CompareVersions(oldVersion, newVersion) +} diff --git a/internal/models/versionanalyzers/helm_version_analyzer_test.go b/internal/models/versionanalyzers/helm_version_analyzer_test.go index cff98c3..6f2d461 100644 --- a/internal/models/versionanalyzers/helm_version_analyzer_test.go +++ b/internal/models/versionanalyzers/helm_version_analyzer_test.go @@ -45,12 +45,14 @@ version: 1.2.3 # It is recommended to use it with quotes. appVersion: "0.1.0" ` - utils.PanicError(os.WriteFile("./Chart.yaml", []byte(chartContent), 0644)) + s.NoError(os.WriteFile("./Chart.yaml", []byte(chartContent), 0644)) + s.NoError(os.MkdirAll("./test-dir", os.ModePerm)) s.helmVersionAnalyzer = HelmVersionAnalyzer{} } func (s *HelmVersionAnalyzerTestSuite) TearDownTest() { - utils.PanicError(os.Remove("./Chart.yaml")) + s.NoError(os.Remove("./Chart.yaml")) + s.NoError(os.RemoveAll("./test-dir")) } func (s *HelmVersionAnalyzerTestSuite) TestRepositoryContainsHelmVersionAnalyzer() { @@ -116,3 +118,11 @@ func (s *HelmVersionAnalyzerTestSuite) TestVersionUpdateIsNeededWhenSomeChangesA }) s.True(needsVersionUpdate) } + +func (s *HelmVersionAnalyzerTestSuite) TestNonHelmifiedRevision() { + helmRootDir := "./test-dir" + version := utils.GetResultOrPanic(s.helmVersionAnalyzer.ReadVersion(".", &models.ArgumentValues{ + HelmArgumentKeyRootDir: &helmRootDir, + })) + s.Equal("", version) +}