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

Bug: Fix Crashing when scanning gitlab with sub groups #108

Merged
merged 2 commits into from Oct 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions internal/scm-clients/clients/clients.go
Expand Up @@ -73,10 +73,10 @@ func FetchClientData(accessToken string, repoUrl string, branch string) (*checkm
}, checksIds, nil
}

func getRepoInfo(repoUrl string) (scm string, org string, repo string, err error) {
u, err := url.Parse(repoUrl)
func getRepoInfo(repoFullUrl string) (string, string, string, error) {
u, err := url.Parse(repoFullUrl)
if err != nil || u.Scheme == "" {
logger.Errorf(err, "error in parsing repoUrl %s", repoUrl)
logger.Errorf(err, "error in parsing repoUrl %s", repoFullUrl)
if err == nil {
err = errors.New("error in parsing the host")
}
Expand All @@ -85,9 +85,13 @@ func getRepoInfo(repoUrl string) (scm string, org string, repo string, err error

path := strings.Split(u.EscapedPath(), "/")
if len(path) < 3 {
return "", "", "", fmt.Errorf("missing org/repo in the repository url: %s", repoUrl)
return "", "", "", fmt.Errorf("missing org/repo in the repository url: %s", repoFullUrl)
}
return u.Host, path[1], path[2], nil
repo := path[len(path) - 1]
namespace := strings.Split(u.Path, repo)[0]
trimedNamespace := namespace[1:(len(namespace) - 1)]

return u.Host, trimedNamespace, repo, nil
}

func getClientAdapter(scmName string, accessToken string) (adapter.ClientAdapter, error) {
Expand Down
68 changes: 68 additions & 0 deletions internal/scm-clients/clients/clients_test.go
@@ -0,0 +1,68 @@
package clients

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

type RepoInfo struct {
BaseUrl string
Namespace string
Project string
}

func TestGetRepoInfo(t *testing.T) {

tests := []struct {
Name string
RepoUrl string
ExpectedErr error
Expected RepoInfo
}{{
Name: "missing url schema",
RepoUrl: "gitlab.com/rootgroup/subgroup/secondsubgroup/test",
ExpectedErr: fmt.Errorf("error in parsing the host"),
Expected: RepoInfo{BaseUrl: "", Namespace: "", Project: ""},
}, {
Name: "invalid url",
RepoUrl: "https://gitlab?com/rootgroup/subgroup/secondsubgroup/test",
ExpectedErr: fmt.Errorf("missing org/repo in the repository url: %s", "https://gitlab?com/rootgroup/subgroup/secondsubgroup/test"),
Expected: RepoInfo{BaseUrl: "", Namespace: "", Project: ""},
}, {
Name: "github repo",
RepoUrl: "https://github.com/aquasecurity/chain-bench",
ExpectedErr: nil,
Expected: RepoInfo{BaseUrl: "github.com", Namespace: "aquasecurity", Project: "chain-bench"},
}, {
Name: "gitlab project under root group",
RepoUrl: "https://gitlab.com/rootgroup/test",
ExpectedErr: nil,
Expected: RepoInfo{BaseUrl: "gitlab.com", Namespace: "rootgroup", Project: "test"},
}, {
Name: "gitlab project under sub group",
RepoUrl: "https://gitlab.com/rootgroup/subgroup/secondsubgroup/test",
ExpectedErr: nil,
Expected: RepoInfo{BaseUrl: "gitlab.com", Namespace: "rootgroup/subgroup/secondsubgroup", Project: "test"},
}}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()

baseUrl, namespace, project, err := getRepoInfo(test.RepoUrl)

if test.ExpectedErr == nil {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, test.ExpectedErr.Error())
}
assert.Equal(t, test.Expected.BaseUrl, baseUrl)
assert.Equal(t, test.Expected.Namespace, namespace)
assert.Equal(t, test.Expected.Project, project)
})
}

}