Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

fix: exclude non-chart directories at helm chart structure check #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions pkg/helm/helmchart_structure_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
package helm

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/eclipse-tractusx/tractusx-quality-checks/internal/filesystem"
"github.com/eclipse-tractusx/tractusx-quality-checks/internal/helm"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/tractusx"
)

var helmStructureFiles []string = []string{
".helmignore",
"LICENSE",
"README.md",
"values.yaml",
}

type HelmStructureExists struct {
baseDir string
}
Expand All @@ -51,45 +56,31 @@ func (r *HelmStructureExists) ExternalDescription() string {
return "https://eclipse-tractusx.github.io/docs/release/trg-5/trg-5-02"
}

func (r *HelmStructureExists) IsOptional() bool {
return false
}

func (r *HelmStructureExists) Test() *tractusx.QualityResult {
helmStructureFiles := []string{
".helmignore",
"Chart.yaml",
"LICENSE",
"README.md",
"values.yaml",
}
var missingFiles []string
var chartYamlFiles []string
errorDescriptionCharts := ""
chartsValid := true

chartDir := path.Join(r.baseDir, "charts")
if fi, err := os.Stat(chartDir); err != nil || !fi.IsDir() {
return &tractusx.QualityResult{Passed: true}
}

helmCharts, err := os.ReadDir(chartDir)
if err != nil || len(helmCharts) == 0 {
return &tractusx.QualityResult{ErrorDescription: fmt.Sprintf("Can't read Helm Charts at %s.", chartDir)}
return &tractusx.QualityResult{Passed: true}
}

var missingFiles []string
var chartYamlFiles []string
for _, hc := range helmCharts {
if hc.IsDir() {
for _, fname := range helmStructureFiles {
fpath := filepath.Join(chartDir, hc.Name(), fname)
isMissing := filesystem.CheckMissingFiles([]string{fpath})
if fname == "Chart.yaml" && isMissing == nil {
chartYamlFiles = append(chartYamlFiles, fpath)
} else if isMissing != nil {
missingFiles = append(missingFiles, isMissing...)
}
}
if !isChartDirectory(path.Join(chartDir, hc.Name())) {
continue
}
chartYamlFiles = append(chartYamlFiles, path.Join(chartDir, hc.Name(), "Chart.yaml"))
getMissingChartFiles(path.Join(chartDir, hc.Name()), &missingFiles)
}

errorDescriptionCharts := ""
chartsValid := true
if len(chartYamlFiles) > 0 {

for _, fpath := range chartYamlFiles {
isValid, msg := validateChart(fpath)
chartsValid = chartsValid && isValid
Expand All @@ -104,8 +95,19 @@ func (r *HelmStructureExists) Test() *tractusx.QualityResult {
return &tractusx.QualityResult{Passed: true}
}

func (r *HelmStructureExists) IsOptional() bool {
return false
func isChartDirectory(dir string) bool {
chartYamlPath := path.Join(dir, "Chart.yaml")
_, err := os.Stat(chartYamlPath)
return err == nil
}

func getMissingChartFiles(chartPath string, missingFiles *[]string) {
for _, fileToCheck := range helmStructureFiles {
missingFile := filesystem.CheckMissingFiles([]string{path.Join(chartPath, fileToCheck)})
if missingFile != nil {
*missingFiles = append(*missingFiles, missingFile...)
}
}
}

func validateChart(chartyamlfile string) (bool, string) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/helm/helmchart_structure_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ func TestShouldPassIfHelmDirIsMissing(t *testing.T) {
}
}

func TestShouldFailForEmptyChartsDir(t *testing.T) {
func TestShouldPassForEmptyChartsDir(t *testing.T) {
os.Mkdir("charts", 0750)
defer os.Remove("charts")

result := NewHelmStructureExists("./").Test()

if result.Passed {
t.Errorf("Helm directory doesn't contain any charts hence test should fail.")
if !result.Passed {
t.Errorf("Helm directory doesn't contain any charts hence test should pass.")
}
}

func TestShouldFailIfHelmStructureIsMissing(t *testing.T) {
os.MkdirAll("charts/exampleChart", 0750)
filesystem.CreateFiles([]string{"charts/exampleChart/Chart.yaml"})
defer os.RemoveAll("charts")

helmStructureTest := NewHelmStructureExists("./")
Expand Down