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

fix(codeqlExecuteScan): fixed regexp pattern to correctly parse ssh url #4349

Merged
merged 3 commits into from
May 5, 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
2 changes: 1 addition & 1 deletion cmd/codeqlExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getGitRepoInfo(repoUri string, repoInfo *RepoInfo) error {
return errors.New("repository param is not set or it cannot be auto populated")
}

pat := regexp.MustCompile(`^(https|git):\/\/([\S]+:[\S]+@)?([^\/:]+)[\/:]([^\/:]+\/[\S]+)$`)
pat := regexp.MustCompile(`^(https:\/\/|git@)([\S]+:[\S]+@)?([^\/:]+)[\/:]([^\/:]+\/[\S]+)$`)
matches := pat.FindAllStringSubmatch(repoUri, -1)
if len(matches) > 0 {
match := matches[0]
Expand Down
54 changes: 47 additions & 7 deletions cmd/codeqlExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRunCodeqlExecuteScan(t *testing.T) {
}

func TestGetGitRepoInfo(t *testing.T) {
t.Run("Valid URL1", func(t *testing.T) {
t.Run("Valid https URL1", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://github.hello.test/Testing/fortify.git", &repoInfo)
assert.NoError(t, err)
Expand All @@ -86,15 +86,15 @@ func TestGetGitRepoInfo(t *testing.T) {
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Valid URL2", func(t *testing.T) {
t.Run("Valid https URL2", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://github.hello.test/Testing/fortify", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})
t.Run("Valid URL1 with dots", func(t *testing.T) {
t.Run("Valid https URL1 with dots", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://github.hello.test/Testing/com.sap.fortify.git", &repoInfo)
assert.NoError(t, err)
Expand All @@ -103,15 +103,15 @@ func TestGetGitRepoInfo(t *testing.T) {
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Valid URL2 with dots", func(t *testing.T) {
t.Run("Valid https URL2 with dots", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://github.hello.test/Testing/com.sap.fortify", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "com.sap.fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})
t.Run("Valid URL1 with username and token", func(t *testing.T) {
t.Run("Valid https URL1 with username and token", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://username:[email protected]/Testing/fortify.git", &repoInfo)
assert.NoError(t, err)
Expand All @@ -120,7 +120,7 @@ func TestGetGitRepoInfo(t *testing.T) {
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Valid URL2 with username and token", func(t *testing.T) {
t.Run("Valid https URL2 with username and token", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("https://username:[email protected]/Testing/fortify", &repoInfo)
assert.NoError(t, err)
Expand All @@ -129,7 +129,7 @@ func TestGetGitRepoInfo(t *testing.T) {
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Invalid URL as no org/owner passed", func(t *testing.T) {
t.Run("Invalid https URL as no org/owner passed", func(t *testing.T) {
var repoInfo RepoInfo
assert.Error(t, getGitRepoInfo("https://github.com/fortify", &repoInfo))
})
Expand All @@ -138,6 +138,46 @@ func TestGetGitRepoInfo(t *testing.T) {
var repoInfo RepoInfo
assert.Error(t, getGitRepoInfo("github.hello.test/Testing/fortify", &repoInfo))
})

t.Run("Valid ssh URL1", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("[email protected]/Testing/fortify.git", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Valid ssh URL2", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("[email protected]/Testing/fortify", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})
t.Run("Valid ssh URL1 with dots", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("[email protected]/Testing/com.sap.fortify.git", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "com.sap.fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Valid ssh URL2 with dots", func(t *testing.T) {
var repoInfo RepoInfo
err := getGitRepoInfo("[email protected]/Testing/com.sap.fortify", &repoInfo)
assert.NoError(t, err)
assert.Equal(t, "https://github.hello.test", repoInfo.serverUrl)
assert.Equal(t, "com.sap.fortify", repoInfo.repo)
assert.Equal(t, "Testing", repoInfo.owner)
})

t.Run("Invalid ssh URL as no org/owner passed", func(t *testing.T) {
var repoInfo RepoInfo
assert.Error(t, getGitRepoInfo("[email protected]/fortify", &repoInfo))
})
}

func TestInitGitInfo(t *testing.T) {
Expand Down