From 7de6f38d9887b931fd0b6392c06c47ef8572444d Mon Sep 17 00:00:00 2001 From: Daria Kuznetsova Date: Fri, 5 May 2023 19:57:47 +0300 Subject: [PATCH] fix(codeqlExecuteScan): fixed regexp pattern to correctly parse ssh url (#4349) --- cmd/codeqlExecuteScan.go | 2 +- cmd/codeqlExecuteScan_test.go | 54 ++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/codeqlExecuteScan.go b/cmd/codeqlExecuteScan.go index b57ca6afc..59b390ec0 100644 --- a/cmd/codeqlExecuteScan.go +++ b/cmd/codeqlExecuteScan.go @@ -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] diff --git a/cmd/codeqlExecuteScan_test.go b/cmd/codeqlExecuteScan_test.go index e4f99e4bc..a19e53243 100644 --- a/cmd/codeqlExecuteScan_test.go +++ b/cmd/codeqlExecuteScan_test.go @@ -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) @@ -86,7 +86,7 @@ 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) @@ -94,7 +94,7 @@ func TestGetGitRepoInfo(t *testing.T) { 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) @@ -103,7 +103,7 @@ 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) @@ -111,7 +111,7 @@ func TestGetGitRepoInfo(t *testing.T) { 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:token@github.hello.test/Testing/fortify.git", &repoInfo) assert.NoError(t, err) @@ -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:token@github.hello.test/Testing/fortify", &repoInfo) assert.NoError(t, err) @@ -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)) }) @@ -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("git@github.hello.test/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("git@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 ssh URL1 with dots", func(t *testing.T) { + var repoInfo RepoInfo + err := getGitRepoInfo("git@github.hello.test/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("git@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("Invalid ssh URL as no org/owner passed", func(t *testing.T) { + var repoInfo RepoInfo + assert.Error(t, getGitRepoInfo("git@github.com/fortify", &repoInfo)) + }) } func TestInitGitInfo(t *testing.T) {