Skip to content

Commit

Permalink
adding a second term for getFilesFromTree for makeing sure we scaning…
Browse files Browse the repository at this point in the history
… the right path

Signed-off-by: jnathangreeg <[email protected]>
  • Loading branch information
jnathangreeg committed Nov 11, 2024
1 parent d82e8da commit 6f47f8d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core/pkg/resourcehandler/repositoryscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,14 @@ func (g *GitHubRepository) getFilesFromTree(filesExtensions []string) []string {
return []string{}
}
}

basePath := g.path
if basePath != "" && !strings.HasSuffix(basePath, "/") {
basePath += "/"
}

for _, path := range g.tree.InnerTrees {
if g.path != "" && !strings.HasPrefix(path.Path, g.path) {
if basePath != "" && !strings.HasPrefix(path.Path, basePath) {
continue
}
if slices.Contains(filesExtensions, getFileExtension(path.Path)) {
Expand All @@ -240,6 +246,7 @@ func (g *GitHubRepository) getFilesFromTree(filesExtensions []string) []string {
return urls
}


func (g *GitHubRepository) rowYamlUrl() string {
return fmt.Sprintf("https://raw.githubusercontent.com/%s/%s", joinOwnerNRepo(g.owner, g.repo), g.branch)
}
Expand Down
79 changes: 79 additions & 0 deletions core/pkg/resourcehandler/repositoryscanner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resourcehandler

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,6 +14,27 @@ var (
// urlD = "https://raw.githubusercontent.com/kubescape/kubescape/master/examples/online-boutique/adservice.yaml"
)

var mockTree = tree{
InnerTrees: []innerTree{
{Path: "charts/fluent-bit/values.yaml"},
{Path: "charts/fluent-bit/templates/configmap.yaml"},
{Path: "charts/other-chart/templates/deployment.yaml"},
{Path: "README.md"},
},
}

func newMockGitHubRepository(path string, isFile bool) *GitHubRepository {
return &GitHubRepository{
host: "github.com",
owner: "grafana",
repo: "helm-charts",
branch: "main",
path: path,
isFile: isFile,
tree: mockTree,
}
}

/*
TODO: tests were commented out due to actual http calls ; http calls should be mocked.
Expand Down Expand Up @@ -143,3 +165,60 @@ func TestGithubParse(t *testing.T) {
assert.False(t, gh.isFile)
}
}

func TestGetFilesFromTree(t *testing.T) {
tests := []struct {
name string
repo *GitHubRepository
extensions []string
expectedResults []string
}{
{
name: "Scan entire repo for YAML files",
repo: newMockGitHubRepository("", false),
extensions: []string{"yaml", "yml"},
expectedResults: []string{
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/fluent-bit/values.yaml",
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/fluent-bit/templates/configmap.yaml",
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/other-chart/templates/deployment.yaml",
},
},
{
name: "Scan specific folder (fluent-bit) for YAML files",
repo: newMockGitHubRepository("charts/fluent-bit", false),
extensions: []string{"yaml", "yml"},
expectedResults: []string{
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/fluent-bit/values.yaml",
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/fluent-bit/templates/configmap.yaml",
},
},
{
name: "Scan root with non-matching extension (JSON)",
repo: newMockGitHubRepository("", false),
extensions: []string{"json"},
expectedResults: []string{},
},
{
name: "Scan specific file",
repo: newMockGitHubRepository("charts/fluent-bit/values.yaml", true),
extensions: []string{"yaml"},
expectedResults: []string{
"https://raw.githubusercontent.com/grafana/helm-charts/main/charts/fluent-bit/values.yaml",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.repo.getFilesFromTree(tt.extensions)

if len(got) == 0 && len(tt.expectedResults) == 0 {
return // both are empty, so this test case passes
}

if !reflect.DeepEqual(got, tt.expectedResults) {
t.Errorf("getFilesFromTree() = %v, want %v", got, tt.expectedResults)
}
})
}
}

0 comments on commit 6f47f8d

Please sign in to comment.