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

Commit

Permalink
Merge pull request #56 from eclipse-tractusx/feat_web_format_errors
Browse files Browse the repository at this point in the history
feat: web format errors
  • Loading branch information
tomaszbarwicki authored Oct 25, 2023
2 parents 5315111 + 509f84f commit 70c2dbe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
19 changes: 13 additions & 6 deletions pkg/container/allowed_base_image_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"fmt"
"strings"

txqualitychecks "github.com/eclipse-tractusx/tractusx-quality-checks/pkg/tractusx"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/tractusx"
)

var baseImageAllowList = []string{
Expand All @@ -38,7 +38,7 @@ type AllowedBaseImage struct {
baseDir string
}

func NewAllowedBaseImage(baseDir string) txqualitychecks.QualityGuideline {
func NewAllowedBaseImage(baseDir string) tractusx.QualityGuideline {
return &AllowedBaseImage{baseDir: baseDir}
}

Expand All @@ -54,7 +54,7 @@ func (a *AllowedBaseImage) ExternalDescription() string {
return "https://eclipse-tractusx.github.io/docs/release/trg-4/trg-4-02"
}

func (a *AllowedBaseImage) Test() *txqualitychecks.QualityResult {
func (a *AllowedBaseImage) Test() *tractusx.QualityResult {
foundDockerFiles := findDockerfilesAt(a.baseDir)

checkPassed := true
Expand All @@ -70,18 +70,25 @@ func (a *AllowedBaseImage) Test() *txqualitychecks.QualityResult {
deniedBaseImages = append(deniedBaseImages, file.baseImage())
}
}

return &txqualitychecks.QualityResult{Passed: checkPassed, ErrorDescription: buildErrorDescription(deniedBaseImages)}
if tractusx.ErrorOutputFormat == tractusx.WebErrOutputFormat {
return &tractusx.QualityResult{Passed: checkPassed, ErrorDescription: buildErrorDescription(deniedBaseImages, tractusx.WebErrOutputFormat)}
}
return &tractusx.QualityResult{Passed: checkPassed, ErrorDescription: buildErrorDescription(deniedBaseImages, tractusx.CliErrOutputFormat)}
}

func (a *AllowedBaseImage) IsOptional() bool {
return false
}

func buildErrorDescription(deniedImages []string) string {
// Function to return error message of failed test.
// There are two types of formatting: cli (default) and web
func buildErrorDescription(deniedImages []string, errorFormat int) string {
if len(deniedImages) == 0 {
return ""
}
if errorFormat == tractusx.WebErrOutputFormat {
return "Dockerfile(s) use not approved images:<br>" + strings.Join(deniedImages, "<br>")
}
return "We want to align on docker base images. We detected a Dockerfile specifying " +
strings.Join(deniedImages, ", ") + "\n\tAllowed images are: \n\t - " +
strings.Join(baseImageAllowList, "\n\t - ")
Expand Down
11 changes: 7 additions & 4 deletions pkg/helm/helmchart_structure_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ func (r *HelmStructureExists) Test() *tractusx.QualityResult {
}

if len(missingFiles) > 0 || !chartsValid {
return &tractusx.QualityResult{ErrorDescription: "+ Following Helm Chart structure files are missing: " + strings.Join(missingFiles, ", ") +
errorDescriptionCharts}
errMsg := "+ Following Helm Chart structure files are missing: " + strings.Join(missingFiles, ", ") + errorDescriptionCharts
if tractusx.ErrorOutputFormat == tractusx.WebErrOutputFormat {
return &tractusx.QualityResult{ErrorDescription: strings.ReplaceAll(errMsg, "\n", "<br>")}
}
return &tractusx.QualityResult{ErrorDescription: errMsg}
}
return &tractusx.QualityResult{Passed: true}
}
Expand All @@ -105,14 +108,14 @@ 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...)
*missingFiles = append(*missingFiles, []string{strings.Split(missingFile[0], "charts")[1][1:]}...)
}
}
}

func validateChart(chartyamlfile string) (bool, string) {
isValid := true
returnMessage := "\n\t+ Analysis for " + chartyamlfile + ": "
returnMessage := "\n\t+ Analysis for " + strings.Split(chartyamlfile, "charts")[1][1:] + ": "
cyf := helm.ChartYamlFromFile(chartyamlfile)
missingFields := cyf.GetMissingMandatoryFields()

Expand Down
7 changes: 7 additions & 0 deletions pkg/tractusx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ type Printer interface {
LogWarning(warning string)
LogError(err string)
}

var ErrorOutputFormat = CliErrOutputFormat

const (
CliErrOutputFormat = iota
WebErrOutputFormat = iota
)

0 comments on commit 70c2dbe

Please sign in to comment.