Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHTAPBUGS-418]multiple components with context path #346

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions controllers/componentdetectionquery_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,60 @@ var _ = Describe("Component Detection Query controller", func() {
})
})

Context("Create Component Detection Query with multi comp repo with no devfiles and context path set", func() {
It("Should successfully get the correct devfiles and Dockerfiles", func() {
ctx := context.Background()

queryName := HASCompDetQuery + "multicontext" + "29"

hasCompDetectionQuery := &appstudiov1alpha1.ComponentDetectionQuery{
TypeMeta: metav1.TypeMeta{
APIVersion: "appstudio.redhat.com/v1alpha1",
Kind: "ComponentDetectionQuery",
},
ObjectMeta: metav1.ObjectMeta{
Name: queryName,
Namespace: HASNamespace,
},
Spec: appstudiov1alpha1.ComponentDetectionQuerySpec{
GitSource: appstudiov1alpha1.GitSource{
URL: "https://github.com/stuartwdouglas/multi-components-none-path",
Context: "context",
},
},
}

Expect(k8sClient.Create(ctx, hasCompDetectionQuery)).Should(Succeed())

// Look up the has app resource that was created.
// num(conditions) may still be < 1 on the first try, so retry until at least _some_ condition is set
hasCompDetQueryLookupKey := types.NamespacedName{Name: queryName, Namespace: HASNamespace}
createdHasCompDetectionQuery := &appstudiov1alpha1.ComponentDetectionQuery{}
Eventually(func() bool {
k8sClient.Get(context.Background(), hasCompDetQueryLookupKey, createdHasCompDetectionQuery)
return len(createdHasCompDetectionQuery.Status.Conditions) > 1
}, timeout, interval).Should(BeTrue())

// Make sure the a devfile is detected
Expect(len(createdHasCompDetectionQuery.Status.ComponentDetected)).Should(Equal(2))

for devfileName, devfileDesc := range createdHasCompDetectionQuery.Status.ComponentDetected {
Expect(devfileName).Should(Or(ContainSubstring("java-springboot"), ContainSubstring("python")))
Expect(devfileDesc.ComponentStub.Source.GitSource.Context).Should(BeElementOf([]string{"context/devfile-sample-java-springboot-basic", "context/devfile-sample-python-basic"}))
if strings.Contains(devfileName, "java-springboot") {
Expect(devfileDesc.ComponentStub.Source.GitSource.DevfileURL).Should(Equal("https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/devfile.yaml"))
Expect(devfileDesc.ComponentStub.Source.GitSource.DockerfileURL).Should(Equal("https://raw.githubusercontent.com/devfile-samples/devfile-sample-java-springboot-basic/main/docker/Dockerfile"))
} else if strings.Contains(devfileName, "python") {
Expect(devfileDesc.ComponentStub.Source.GitSource.DevfileURL).Should(Equal("https://raw.githubusercontent.com/devfile-samples/devfile-sample-python-basic/main/devfile.yaml"))
Expect(devfileDesc.ComponentStub.Source.GitSource.DockerfileURL).Should(Equal("https://raw.githubusercontent.com/devfile-samples/devfile-sample-python-basic/main/docker/Dockerfile"))
}
}

// Delete the specified Detection Query resource
deleteCompDetQueryCR(hasCompDetQueryLookupKey)
})
})

Context("Create Component Detection Query with repo that cannot find and dockerfile or devfile match", func() {
It("Should successfully return the CDQ", func() {
ctx := context.Background()
Expand Down
8 changes: 4 additions & 4 deletions pkg/devfile/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type AlizerClient struct {
// Map 3 returns a context to the Dockerfile uri or a matched DockerfileURL from the devfile registry if no Dockerfile is present in the context
// Map 4 returns a context to the list of ports that were detected by alizer in the source code, at that given context
func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL string, source appstudiov1alpha1.GitSource) (map[string][]byte, map[string]string, map[string]string, map[string][]int, error) {

devfileMapFromRepo := make(map[string][]byte)
devfilesURLMapFromRepo := make(map[string]string)
dockerfileContextMapFromRepo := make(map[string]string)
Expand All @@ -63,7 +62,8 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri
isDevfilePresent := false
isDockerfilePresent := false
curPath := path.Join(localpath, f.Name())
context := f.Name()
dirName := f.Name()
context := path.Join(source.Context, f.Name())
files, err := ioutil.ReadDir(curPath)
if err != nil {
return nil, nil, nil, nil, err
Expand All @@ -74,7 +74,7 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri
/* #nosec G304 -- false positive, filename is not based on user input*/
devfilePath := path.Join(curPath, f.Name())
// Set the proper devfile URL for the detected devfile
updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(context, f.Name())))
updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(dirName, f.Name())))
if err != nil {
return nil, nil, nil, nil, err
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func search(log logr.Logger, a Alizer, localpath string, devfileRegistryURL stri
devfilePath := path.Join(hiddenDirPath, f.Name())

// Set the proper devfile URL for the detected devfile
updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(context, HiddenDevfileDir, f.Name())))
updatedLink, err := UpdateGitLink(source.URL, source.Revision, path.Join(source.Context, path.Join(dirName, HiddenDevfileDir, f.Name())))
if err != nil {
return nil, nil, nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/devfile/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ func DownloadDevfileAndDockerfile(url string) ([]byte, string, []byte, string) {
// Map 3 returns a context to the Dockerfile uri or a matched DockerfileURL from the devfile registry if no Dockerfile/Containerfile is present in the context
// Map 4 returns a context to the list of ports that were detected by alizer in the source code, at that given context
func ScanRepo(log logr.Logger, a Alizer, localpath string, devfileRegistryURL string, source appstudiov1alpha1.GitSource) (map[string][]byte, map[string]string, map[string]string, map[string][]int, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if the original method could still be used anywhere else, so I added a new one instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original method should be fine to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the PR


return search(log, a, localpath, devfileRegistryURL, source)
}

Expand Down