Skip to content

Commit

Permalink
Merge pull request #474 from eclipse-tractusx/fix/repo_structure_check
Browse files Browse the repository at this point in the history
fix: repo structure check
  • Loading branch information
tomaszbarwicki authored Jan 30, 2024
2 parents a348ad0 + 1a3b084 commit 2cf3757
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 44 deletions.
2 changes: 1 addition & 1 deletion release-automation/cmd/run_trg_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var checkLocalCmd = &cobra.Command{
docs.NewReadmeExists(basedir),
helm.NewHelmStructureExists(basedir),
helm.NewResourceMgmt(basedir),
repo.NewDefaultBranch(),
repo.NewDefaultBranch(basedir),
repo.NewLeadingRepositoryDefined(basedir),
repo.NewRepoStructureExists(basedir),
}
Expand Down
2 changes: 1 addition & 1 deletion release-automation/internal/dashboard/tractusx.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func initializeChecksForDirectory(dir string) []tractusx.QualityGuideline {
checks = append(checks, docs.NewReadmeExists(dir))
checks = append(checks, docs.NewInstallExists(dir))
checks = append(checks, docs.NewChangelogExists(dir))
//checks = append(checks, repo.NewRepoStructureExists(dir))
checks = append(checks, repo.NewRepoStructureExists(dir))
checks = append(checks, repo.NewLeadingRepositoryDefined(dir))
checks = append(checks, container.NewAllowedBaseImage(dir))
checks = append(checks, helm.NewHelmStructureExists(dir))
Expand Down
4 changes: 2 additions & 2 deletions release-automation/internal/helm/chartyaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ChartYamlFromFile(ymlfile string) *Chartyaml {

func (c *Chartyaml) IsVersionValid() bool {
/*
Below regular expresion is used to verify version string according to Semantic Versioning schema (https://semver.org).
Below regular expression is used to verify version string according to Semantic Versioning schema (https://semver.org).
Following examples match:
- "1.2.3"
- "1.0.0-alpha"
Expand All @@ -78,7 +78,7 @@ func (c *Chartyaml) IsVersionValid() bool {

match, err := regexp.MatchString(regexPattern, c.Version)
if err != nil {
fmt.Println("Error occured when validating semantic version.")
fmt.Println("Error occurred when validating semantic version.")
return false
}
return match
Expand Down
7 changes: 4 additions & 3 deletions release-automation/internal/repo/default_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
)

type defaultBranch struct {
baseDir string
}

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

func (d defaultBranch) Name() string {
Expand All @@ -47,7 +48,7 @@ func (d defaultBranch) ExternalDescription() string {
}

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

if *repoInfo.Fork {
// There is no need to enforce default branches on forks
Expand Down
16 changes: 12 additions & 4 deletions release-automation/internal/repo/repo_structure_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,21 @@ func (c RepoStructureExists) Test() *tractusx.QualityResult {
path.Join(c.baseDir, "SECURITY.md"),
}

mandatoryForLeadingRepo := []string{"docs", "charts"}
mandatoryForLeadingRepo := []string{
path.Join(c.baseDir, "docs"),
path.Join(c.baseDir, "charts"),
}

printer := &tractusx.StdoutPrinter{}

if isLeadingRepo() {
if isLeadingRepo(c.baseDir) {
listOfMandatoryFilesToBeChecked = append(listOfMandatoryFilesToBeChecked, mandatoryForLeadingRepo...)
}

missingMandatoryFiles := filesystem.CheckMissingFiles(listOfMandatoryFilesToBeChecked)
missingOptionalFiles := filesystem.CheckMissingFiles(listOfOptionalFilesToBeChecked)
if dependencyFiles := filesystem.FindPrefixedFiles(c.baseDir, "DEPENDENCIES"); dependencyFiles == nil {
missingMandatoryFiles = append(missingMandatoryFiles, "DEPENDENCIES")
missingMandatoryFiles = append(missingMandatoryFiles, path.Join(c.baseDir, "DEPENDENCIES"))
}

if len(missingOptionalFiles) > 0 {
Expand All @@ -90,7 +94,11 @@ func (c RepoStructureExists) Test() *tractusx.QualityResult {
}

if len(missingMandatoryFiles) > 0 {
return &tractusx.QualityResult{ErrorDescription: "The check detected following mandatory files missing: " + strings.Join(missingMandatoryFiles, ", ")}
cleanMissingFiles := []string{}
for _, missingFile := range missingMandatoryFiles {
cleanMissingFiles = append(cleanMissingFiles, strings.Split(missingFile, c.baseDir)[1][1:])
}
return &tractusx.QualityResult{ErrorDescription: "The check detected following mandatory files missing: " + strings.Join(cleanMissingFiles, ", ")}
}

return &tractusx.QualityResult{Passed: true}
Expand Down
62 changes: 34 additions & 28 deletions release-automation/internal/repo/repo_structure_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package repo
import (
"os"
"testing"

"path"
"tractusx-release-automation/internal/filesystem"
)

Expand All @@ -44,33 +44,32 @@ var listOfDirsToBeCreated = []string{
const metadataTestFile = "./test/metadata_test_template.yaml"

func TestShouldPassIfRepoStructureExistsWithoutOptional(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

filesystem.CreateFiles(listOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, listOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))

repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(listOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("Structure exists with optional files, but test still fails.")
}
}

func TestShouldPassIfRepoStructureExistsWithOptional(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

listOfFilesToBeCreated = append(listOfFilesToBeCreated, []string{"INSTALL.md", "AUTHORS.md"}...)

filesystem.CreateFiles(listOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, listOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))


repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(listOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("Structure exists without optional files, but test still fails.")
Expand All @@ -79,37 +78,36 @@ func TestShouldPassIfRepoStructureExistsWithOptional(t *testing.T) {
}

func TestShouldFailIfRepoStructureIsMissing(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

repostructureTest := NewRepoStructureExists("./")
repoStructureTest := NewRepoStructureExists(dir)

result := repostructureTest.Test()
result := repoStructureTest.Test()

if result.Passed {
t.Errorf("RepoStructureExist should fail if repo structure exists.")
}
}

func TestShouldPassWithMultipleDependenciesFiles(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

newListOfFilesToBeCreated := append(listOfFilesToBeCreated[:len(listOfFilesToBeCreated)-1], []string{"DEPENDENCIES_FRONTEND", "DEPENDENCIES_BACKEND"}...)
filesystem.CreateFiles(newListOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, newListOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))

repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(newListOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("There is multiple DEPENDENCIES files, test should pass.")
}
}

func setEnv(t *testing.T) {
copyTemplateFileTo(".tractusx", t)
func setEnv(dir string, t *testing.T) {
copyTemplateFileTo(path.Join(dir,".tractusx"), t)
_ = os.Setenv("GITHUB_REPOSITORY", "eclipse-tractusx/sig-infra")
_ = os.Setenv("GITHUB_REPOSITORY_OWNER", "tester")
}
Expand All @@ -124,3 +122,11 @@ func copyTemplateFileTo(path string, t *testing.T) {
t.Errorf("Could not copy template file to designated path")
}
}

func createFilesPaths(dir string, files []string) []string {
fullPaths := []string{}
for _, file := range files {
fullPaths = append(fullPaths, path.Join(dir, file))
}
return fullPaths
}
11 changes: 6 additions & 5 deletions release-automation/internal/repo/repoinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"strings"
"path"

"github.com/go-ini/ini"
"tractusx-release-automation/internal/tractusx"
Expand All @@ -38,7 +39,7 @@ type RepoInfo struct {
// It leverages environment variables typically available in GitHub workflows.
// As fallback option, the local git config (.git/config) file is used.
// Results are returned as *RepoInfo
func GetRepoBaseInfo() *RepoInfo {
func GetRepoBaseInfo(repoDir string) *RepoInfo {
const (
BASEURL = "https://github.com/"
SSHBASE = "[email protected]:"
Expand All @@ -57,7 +58,7 @@ func GetRepoBaseInfo() *RepoInfo {
}

// Parse local git configuration when executing locally
cfg, err := ini.Load(".git/config")
cfg, err := ini.Load(path.Join(repoDir, ".git/config"))
if err != nil {
fmt.Printf("Failed to read file: %v", err)
}
Expand All @@ -79,9 +80,9 @@ func GetRepoBaseInfo() *RepoInfo {
return &result
}

func isLeadingRepo() bool {
metadata, err := tractusx.MetadataFromLocalFile("./")
repoInfo := GetRepoBaseInfo()
func isLeadingRepo(repoDir string) bool {
metadata, err := tractusx.MetadataFromLocalFile(repoDir)
repoInfo := GetRepoBaseInfo(repoDir)
fullRepoName := "https://github.com/eclipse-tractusx/" + (*repoInfo).Reponame

if err != nil || metadata.LeadingRepository != fullRepoName {
Expand Down

0 comments on commit 2cf3757

Please sign in to comment.