From 309efb1cc43318b3a5878aaad7f5a11d3486ca51 Mon Sep 17 00:00:00 2001 From: Stephanie Cao Date: Fri, 20 Oct 2023 10:48:14 -0400 Subject: [PATCH] [DEVHAS-493]support devfile.yml (#400) * support devfile.yml Signed-off-by: Stephanie * fix test failure Signed-off-by: Stephanie --------- Signed-off-by: Stephanie --- cdq-analysis/pkg/detect.go | 12 +++++++----- cdq-analysis/pkg/devfile.go | 21 ++++++++++++++------- pkg/devfile/devfile_test.go | 28 ++++++++++++++++++++++++---- pkg/spi/spi.go | 2 +- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/cdq-analysis/pkg/detect.go b/cdq-analysis/pkg/detect.go index 3736074fa..ae67fc4cb 100644 --- a/cdq-analysis/pkg/detect.go +++ b/cdq-analysis/pkg/detect.go @@ -75,7 +75,8 @@ func search(log logr.Logger, a Alizer, localpath string, srcContext string, cdqI } for _, f := range files { lowerCaseFileName := strings.ToLower(f.Name()) - if lowerCaseFileName == DevfileName || lowerCaseFileName == HiddenDevfileName { + if lowerCaseFileName == Devfile || lowerCaseFileName == HiddenDevfile || + lowerCaseFileName == DevfileYml || lowerCaseFileName == HiddenDirDevfileYml { // Check for devfile.yaml or .devfile.yaml /* #nosec G304 -- false positive, filename is not based on user input*/ devfilePath := path.Join(curPath, f.Name()) @@ -97,7 +98,7 @@ func search(log logr.Logger, a Alizer, localpath string, srcContext string, cdqI isDevfilePresent = true } } else if f.IsDir() && f.Name() == HiddenDevfileDir { - // Check for .devfile/devfile.yaml or .devfile/.devfile.yaml + // Check for .devfile/devfile.yaml, .devfile/.devfile.yaml, .devfile/devfile.yml or .devfile/.devfile.yml // if the dir is .devfile, we dont increment currentLevel // consider devfile.yaml and .devfile/devfile.yaml as the same level, for example hiddenDirPath := path.Join(curPath, HiddenDevfileDir) @@ -107,8 +108,9 @@ func search(log logr.Logger, a Alizer, localpath string, srcContext string, cdqI } for _, f := range hiddenfiles { lowerCaseFileName := strings.ToLower(f.Name()) - if lowerCaseFileName == DevfileName || lowerCaseFileName == HiddenDevfileName { - // Check for devfile.yaml or .devfile.yaml + if lowerCaseFileName == Devfile || lowerCaseFileName == HiddenDevfile || + lowerCaseFileName == DevfileYml || lowerCaseFileName == HiddenDirDevfileYml { + // Check for devfile.yaml , .devfile.yaml, devfile.yml or .devfile.yml /* #nosec G304 -- false positive, filename is not based on user input*/ devfilePath := path.Join(hiddenDirPath, f.Name()) // Set the proper devfile URL for the detected devfile @@ -368,7 +370,7 @@ func AnalyzeAndDetectDevfile(a Alizer, path, devfileRegistryURL string) ([]byte, if err != nil { return nil, "", "", nil, err } - detectedDevfileEndpoint, err := UpdateGitLink(sampleRepoURL, "", DevfileName) + detectedDevfileEndpoint, err := UpdateGitLink(sampleRepoURL, "", Devfile) if err != nil { return nil, "", "", nil, err } diff --git a/cdq-analysis/pkg/devfile.go b/cdq-analysis/pkg/devfile.go index 9cd6ac748..c636240b0 100644 --- a/cdq-analysis/pkg/devfile.go +++ b/cdq-analysis/pkg/devfile.go @@ -36,8 +36,10 @@ import ( ) const ( - DevfileName = "devfile.yaml" - HiddenDevfileName = ".devfile.yaml" + DevfileName = "devfile" + HiddenDevfileName = ".devfile" + YamlExtension = ".yaml" + YmlExtension = ".yml" HiddenDevfileDir = ".devfile" DockerfileName = "Dockerfile" AlternateDockerfileName = "dockerfile" @@ -46,10 +48,14 @@ const ( DockerDir = "docker" BuildDir = "build" - Devfile = DevfileName // devfile.yaml - HiddenDevfile = HiddenDevfileName // .devfile.yaml - HiddenDirDevfile = HiddenDevfileDir + "/" + DevfileName // .devfile/devfile.yaml - HiddenDirHiddenDevfile = HiddenDevfileDir + "/" + HiddenDevfileName // .devfile/.devfile.yaml + Devfile = DevfileName + YamlExtension // devfile.yaml + HiddenDevfile = HiddenDevfileName + YamlExtension // .devfile.yaml + DevfileYml = DevfileName + YmlExtension // devfile.yml + HiddenDevfilYml = HiddenDevfileName + YmlExtension // .devfile.yml + HiddenDirDevfile = HiddenDevfileDir + "/" + DevfileName + YamlExtension // .devfile/devfile.yaml + HiddenDirHiddenDevfile = HiddenDevfileDir + "/" + HiddenDevfileName + YamlExtension // .devfile/.devfile.yaml + HiddenDirDevfileYml = HiddenDevfileDir + "/" + DevfileName + YmlExtension // .devfile/devfile.yml + HiddenDirHiddenDevfileYml = HiddenDevfileDir + "/" + HiddenDevfileName + YmlExtension // .devfile/.devfile.yml Dockerfile = DockerfileName // Dockerfile HiddenDirDockerfile = HiddenDockerDir + "/" + DockerfileName // .docker/Dockerfile @@ -75,7 +81,8 @@ const ( CDQAnalysisImage = "quay.io/redhat-appstudio/cdq-analysis:latest" ) -var ValidDevfileLocations = []string{Devfile, HiddenDevfile, HiddenDirDevfile, HiddenDirHiddenDevfile} +var ValidDevfileLocations = []string{Devfile, HiddenDevfile, DevfileYml, HiddenDevfilYml, HiddenDirDevfile, + HiddenDirHiddenDevfile, HiddenDirDevfileYml, HiddenDirHiddenDevfileYml} var ValidDockerfileLocations = []string{Dockerfile, DockerDirDockerfile, HiddenDirDockerfile, BuildDirDockerfile, AlternateDockerfile, DockerDirAlternateDockerfile, HiddenDirAlternateDockerfile, BuildDirAlternateDockerfile, diff --git a/pkg/devfile/devfile_test.go b/pkg/devfile/devfile_test.go index 1a696cfcb..7aa109d34 100644 --- a/pkg/devfile/devfile_test.go +++ b/pkg/devfile/devfile_test.go @@ -200,24 +200,44 @@ func TestFindAndDownloadDevfile(t *testing.T) { }{ { name: "Curl devfile.yaml", - url: "https://raw.githubusercontent.com/maysunfaisal/devfilepriority/main/case1", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case1", wantDevfileContext: "devfile.yaml", }, { name: "Curl .devfile.yaml", - url: "https://raw.githubusercontent.com/maysunfaisal/devfilepriority/main/case2", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case2", wantDevfileContext: ".devfile.yaml", }, + { + name: "Curl devfile.yml", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case3", + wantDevfileContext: "devfile.yml", + }, + { + name: "Curl .devfile.yml", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case4", + wantDevfileContext: ".devfile.yml", + }, { name: "Curl .devfile/devfile.yaml", - url: "https://raw.githubusercontent.com/maysunfaisal/devfilepriority/main/case3", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case5", wantDevfileContext: ".devfile/devfile.yaml", }, { name: "Curl .devfile/.devfile.yaml", - url: "https://raw.githubusercontent.com/maysunfaisal/devfilepriority/main/case4", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case6", wantDevfileContext: ".devfile/.devfile.yaml", }, + { + name: "Curl .devfile/devfile.yml", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case7", + wantDevfileContext: ".devfile/devfile.yml", + }, + { + name: "Curl .devfile/.devfile.yml", + url: "https://raw.githubusercontent.com/has-resources/devfile-priority/main/case8", + wantDevfileContext: ".devfile/.devfile.yml", + }, { name: "Cannot curl for a devfile", url: "https://github.com/octocat/Hello-World", diff --git a/pkg/spi/spi.go b/pkg/spi/spi.go index ba756c0a3..1127e7ddc 100644 --- a/pkg/spi/spi.go +++ b/pkg/spi/spi.go @@ -62,7 +62,7 @@ func (e *SPIFileContentRequestError) Error() string { return "SPIFileContentRequest failed " + e.Message } -var ValidDevfileLocations = []string{cdqanalysis.Devfile, cdqanalysis.HiddenDevfile, cdqanalysis.HiddenDirDevfile, cdqanalysis.HiddenDirHiddenDevfile} +var ValidDevfileLocations = cdqanalysis.ValidDevfileLocations // GetFileContents is a wrapper call to scm file retriever's GetFileContents() func (s SPIClient) GetFileContents(ctx context.Context, name string, component v1alpha1.Component, repoUrl string, filepath string, ref string) (io.ReadCloser, error) {