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

feat: move checks to public package #41

Merged
merged 10 commits into from
Aug 3, 2023
5 changes: 5 additions & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go/golang/github.com%2Fcpuguy83%2Fgo-md2man/v2/v2.0.2, MIT, approved, clearlydef
go/golang/github.com%2Fcreack/pty/v1.1.9, MIT, approved, clearlydefined
go/golang/github.com%2Fdavecgh/go-spew/v1.1.0, ISC, approved, clearlydefined
go/golang/github.com%2Fdavecgh/go-spew/v1.1.1, ISC, approved, clearlydefined
go/golang/github.com%2Ffatih/color/v1.15.0, MIT, approved, #9689
go/golang/github.com%2Fgo-ini/ini/v1.67.0, Apache-2.0, approved, clearlydefined
go/golang/github.com%2Fgolang/protobuf/v1.3.1, BSD-3-Clause, approved, clearlydefined
go/golang/github.com%2Fgolang/protobuf/v1.5.0, BSD-3-Clause, approved, #5706
Expand All @@ -19,6 +20,9 @@ go/golang/github.com%2Fkr/pretty/v0.2.1, MIT, approved, clearlydefined
go/golang/github.com%2Fkr/pty/v1.1.1, MIT, approved, clearlydefined
go/golang/github.com%2Fkr/text/v0.1.0, MIT, approved, clearlydefined
go/golang/github.com%2Fkr/text/v0.2.0, MIT, approved, clearlydefined
go/golang/github.com%2Fmattn/go-colorable/v0.1.13, MIT, approved, clearlydefined
go/golang/github.com%2Fmattn/go-isatty/v0.0.16, MIT, approved, clearlydefined
go/golang/github.com%2Fmattn/go-isatty/v0.0.17, MIT, approved, clearlydefined
go/golang/github.com%2Fpmezard/go-difflib/v1.0.0, BSD-3-Clause, approved, clearlydefined
go/golang/github.com%2Frussross%2Fblackfriday/v2/v2.1.0, BSD-2-Clause, approved, clearlydefined
go/golang/github.com%2Fspf13/cobra/v1.7.0, Apache-2.0, approved, clearlydefined
Expand Down Expand Up @@ -50,6 +54,7 @@ go/golang/golang.org%2Fx/sys/v0.0.0-20201119102817-f84b799fce68, Apache-2.0 AND
go/golang/golang.org%2Fx/sys/v0.0.0-20210615035016-665e8c7367d1, Apache-2.0 AND BSD-3-Clause AND BSD-3-Clause, approved, #3437
go/golang/golang.org%2Fx/sys/v0.0.0-20220520151302-bc2c85ada10a, Apache-2.0 AND BSD-3-Clause AND BSD-3-Clause, approved, #3437
go/golang/golang.org%2Fx/sys/v0.0.0-20220722155257-8c9f86f7a55f, Apache-2.0 AND BSD-3-Clause AND BSD-3-Clause, approved, #3437
go/golang/golang.org%2Fx/sys/v0.0.0-20220811171246-fbc7d0a398ab, Apache-2.0 AND BSD-3-Clause AND BSD-3-Clause, approved, #3437
go/golang/golang.org%2Fx/sys/v0.10.0, BSD-3-Clause, approved, #9282
go/golang/golang.org%2Fx/sys/v0.2.0, BSD-3-Clause, approved, #6379
go/golang/golang.org%2Fx/sys/v0.3.0, BSD-3-Clause, approved, #9289
Expand Down
24 changes: 13 additions & 11 deletions cmd/checkLocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@ import (
"os"

txqualitychecks "github.com/eclipse-tractusx/tractusx-quality-checks/internal"
"github.com/eclipse-tractusx/tractusx-quality-checks/internal/docs"
"github.com/eclipse-tractusx/tractusx-quality-checks/internal/helm"
"github.com/eclipse-tractusx/tractusx-quality-checks/internal/repo"
irepo "github.com/eclipse-tractusx/tractusx-quality-checks/internal/repo"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/container"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/docs"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/helm"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/repo"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/tractusx"
"github.com/spf13/cobra"
)

var releaseGuidelines = []txqualitychecks.QualityGuideline{
docs.NewReadmeExists(),
docs.NewInstallExists(),
docs.NewChangelogExists(),
repo.NewLeadingRepositoryDefined(),
repo.NewDefaultBranch(),
var releaseGuidelines = []tractusx.QualityGuideline{
docs.NewReadmeExists("./"),
docs.NewInstallExists("./"),
docs.NewChangelogExists("./"),
repo.NewLeadingRepositoryDefined("./"),
irepo.NewDefaultBranch(),
container.NewAllowedBaseImage("./"),
repo.NewRepoStructureExists(),
helm.NewHelmStructureExists(),
repo.NewRepoStructureExists("./"),
helm.NewHelmStructureExists("./"),
}

// checkLocalCmd represents the checkLocal command
Expand Down
75 changes: 0 additions & 75 deletions internal/docs/install_exists_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

package filesystem

import "os"
import (
"log"
"os"
)

func CreateFiles(files []string) {
for _, file := range files {
os.Create(file)
if _, err := os.Create(file); err != nil {
log.Fatalf("could not create file %s. Error %s", file, err)
}
}
}

Expand Down
19 changes: 10 additions & 9 deletions internal/helm/chartyaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ package helm

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"reflect"
"regexp"

"gopkg.in/yaml.v3"
)

type chartyaml struct {
type Chartyaml struct {
ApiVersion string `yaml:"apiVersion"`
Name string `yaml:"name"`
Description string `yaml:"description"`
AppVersion string `yaml:"appVersion"`
Version string `yaml:"version"`
}

func newChartYaml() *chartyaml {
return &chartyaml{
func newChartYaml() *Chartyaml {
return &Chartyaml{
ApiVersion: "",
Name: "",
Description: "",
Expand All @@ -45,14 +46,14 @@ func newChartYaml() *chartyaml {
}
}

func chartYamlFromFile(ymlfile string) *chartyaml {
func ChartYamlFromFile(ymlfile string) *Chartyaml {
data, err := ioutil.ReadFile(ymlfile)
if err != nil {
fmt.Printf("Unable to read %v.\n", ymlfile)
return nil
}

var c chartyaml
var c Chartyaml
err = yaml.Unmarshal(data, &c)
if err != nil {
fmt.Printf("Unable to parse YAML file: %v.\n", ymlfile)
Expand All @@ -62,7 +63,7 @@ func chartYamlFromFile(ymlfile string) *chartyaml {
return &c
}

func (c *chartyaml) isVersionValid() bool {
func (c *Chartyaml) IsVersionValid() bool {
/*
Below regular expresion is used to verify version string according to Semantic Versioning schema (https://semver.org).
Following examples match:
Expand All @@ -83,12 +84,12 @@ func (c *chartyaml) isVersionValid() bool {
return match
}

func (c *chartyaml) getMissingMandatoryFields() []string {
func (c *Chartyaml) GetMissingMandatoryFields() []string {
chartValues := reflect.ValueOf(*c)
chartType := chartValues.Type()
numChartFields := chartValues.NumField()

missingFields := []string{}
var missingFields []string
for i := 0; i < numChartFields; i++ {
chartField := chartType.Field(i)
chartFieldValue := chartValues.Field(i)
Expand Down
14 changes: 7 additions & 7 deletions internal/helm/chartyaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package helm
import "testing"

func TestShouldPassIfCanReadChartYaml(t *testing.T) {
c := chartYamlFromFile("test/TestChartValid.yaml")
c := ChartYamlFromFile("test/TestChartValid.yaml")
if c.Name != "test-application" {
t.Errorf("Test should pass but name of the helm chart is not 'test-application'.")
}
Expand All @@ -31,31 +31,31 @@ func TestShouldPassIfCanReadChartYaml(t *testing.T) {
func TestShouldBeValidForSemanticVersion(t *testing.T) {
c := newChartYaml()
c.Version = "1.2.3"
if !c.isVersionValid() {
if !c.IsVersionValid() {
t.Errorf("Test should pass since version is valid according Semantic Version schema.")
}
}

func TestShouldBeInValidForSemanticVersion(t *testing.T) {
c := newChartYaml()
c.Version = "1.2.ABC"
if c.isVersionValid() {
if c.IsVersionValid() {
t.Errorf("Test should fail since version is invalid according to Semantic Version schema.")
}
}

func TestShouldPassIfConfigurationSettingsArePresent(t *testing.T) {
c := chartYamlFromFile("test/TestChartValid.yaml")
missingFields := c.getMissingMandatoryFields()
c := ChartYamlFromFile("test/TestChartValid.yaml")
missingFields := c.GetMissingMandatoryFields()

if len(missingFields) > 0 {
t.Errorf("Test should pass since all configuration settings are set.")
}
}

func TestShouldFailIfConfigurationSettingsAreMissing(t *testing.T) {
c := chartYamlFromFile("test/TestChartInValid.yaml")
missingFields := c.getMissingMandatoryFields()
c := ChartYamlFromFile("test/TestChartInValid.yaml")
missingFields := c.GetMissingMandatoryFields()

if len(missingFields) == 0 {
t.Errorf("Test should fail since one configuration setting is missing.")
Expand Down
30 changes: 23 additions & 7 deletions internal/repo/default_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
package repo

import (
"github.com/eclipse-tractusx/tractusx-quality-checks/internal"
"context"
"fmt"

"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/repo"
"github.com/eclipse-tractusx/tractusx-quality-checks/pkg/tractusx"
"github.com/google/go-github/v50/github"
)

type defaultBranch struct {
}

func NewDefaultBranch() txqualitychecks.QualityGuideline {
func NewDefaultBranch() tractusx.QualityGuideline {
return &defaultBranch{}
}

Expand All @@ -43,25 +47,37 @@ func (d defaultBranch) ExternalDescription() string {
return "https://eclipse-tractusx.github.io/docs/release/trg-2/trg-2-1"
}

func (d defaultBranch) Test() *txqualitychecks.QualityResult {
repoInfo := repo.GetRepoMetadata(repo.GetRepoBaseInfo())
func (d defaultBranch) Test() *tractusx.QualityResult {
repoInfo := getRepoInfo(repo.GetRepoBaseInfo())

if *repoInfo.Fork {
// There is no need to enforce default branches on forks
// Since all the other checks should be executable on forks, we cannot let this single check break a workflow
return &txqualitychecks.QualityResult{Passed: true}
return &tractusx.QualityResult{Passed: true}
}

if *repoInfo.DefaultBranch != "main" {
return &txqualitychecks.QualityResult{
return &tractusx.QualityResult{
Passed: false,
ErrorDescription: "Default branch not set to 'main'.",
}
}

return &txqualitychecks.QualityResult{Passed: true}
return &tractusx.QualityResult{Passed: true}
}

func (d defaultBranch) IsOptional() bool {
return false
}

func getRepoInfo(repo *repo.RepoInfo) *github.Repository {
ctx := context.Background()
client := *github.NewClient(nil)

repoInfo, _, err := client.Repositories.Get(ctx, repo.Owner, repo.Reponame)
if err != nil {
fmt.Printf("Error querying GitHub API:\n%v\n", err)
}

return repoInfo
}
8 changes: 5 additions & 3 deletions internal/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ package txqualitychecks
import (
"errors"
"fmt"

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

type GuidelineTestRunner struct {
guidelines []QualityGuideline
printer Printer
guidelines []tractusx.QualityGuideline
printer tractusx.Printer
}

func NewTestRunner(tests []QualityGuideline) *GuidelineTestRunner {
func NewTestRunner(tests []tractusx.QualityGuideline) *GuidelineTestRunner {
return &GuidelineTestRunner{guidelines: tests, printer: &StdoutPrinter{}}
}

Expand Down
14 changes: 9 additions & 5 deletions internal/test_runner_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package txqualitychecks

import "fmt"
import (
"fmt"

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

type Guideline struct {
name string
Expand Down Expand Up @@ -48,8 +52,8 @@ func (f FailingQualityGuideline) ExternalDescription() string {
return f.externalDescription
}

func (f FailingQualityGuideline) Test() *QualityResult {
return &QualityResult{Passed: false}
func (f FailingQualityGuideline) Test() *tractusx.QualityResult {
return &tractusx.QualityResult{Passed: false}
}

type PassingQualityGuideline struct {
Expand All @@ -72,8 +76,8 @@ func (p PassingQualityGuideline) ExternalDescription() string {
return p.externalDescription
}

func (p PassingQualityGuideline) Test() *QualityResult {
return &QualityResult{Passed: true}
func (p PassingQualityGuideline) Test() *tractusx.QualityResult {
return &tractusx.QualityResult{Passed: true}
}

type PrinterMock struct {
Expand Down
Loading