Skip to content

Commit

Permalink
[DEVHAS-493]support devfile.yml (#400)
Browse files Browse the repository at this point in the history
* support devfile.yml

Signed-off-by: Stephanie <yangcao@redhat.com>

* fix test failure

Signed-off-by: Stephanie <yangcao@redhat.com>

---------

Signed-off-by: Stephanie <yangcao@redhat.com>
  • Loading branch information
yangcao77 authored Oct 20, 2023
1 parent ba47cab commit 309efb1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
12 changes: 7 additions & 5 deletions cdq-analysis/pkg/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 14 additions & 7 deletions cdq-analysis/pkg/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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,
Expand Down
28 changes: 24 additions & 4 deletions pkg/devfile/devfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 309efb1

Please sign in to comment.