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

refactor: remove redundant ssh transformation [IDE-257] #57

Merged
merged 2 commits into from
Jun 13, 2024
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
6 changes: 1 addition & 5 deletions internal/util/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ package util

import (
"fmt"
"net/url"
"strings"

"github.com/go-git/go-git/v5"
"net/url"
)

func GetRepositoryUrl(path string) (string, error) {
Expand All @@ -45,8 +43,6 @@ func GetRepositoryUrl(path string) (string, error) {
repoUrl := remote.Config().URLs[0]
repoUrl, err = SanitiseCredentials(repoUrl)

// we need to return an actual URL, not the SSH
repoUrl = strings.Replace(repoUrl, "git@github.com:", "https://github.com/", 1)
return repoUrl, err
}

Expand Down
28 changes: 0 additions & 28 deletions internal/util/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,6 @@ func Test_GetRepositoryUrl_repo_without_credentials(t *testing.T) {
assert.Equal(t, expectedRepoUrl, actualUrl)
}

func Test_GetRepositoryUrl_repo_with_ssh(t *testing.T) {
// check out a repo and prepare its config to contain credentials in the URL
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git"

repoDir, err := testutil.SetupCustomTestRepo(t, expectedRepoUrl, "master", "", "shallow-goof-locked")
require.NoError(t, err)

repo, err := git.PlainOpenWithOptions(repoDir, &git.PlainOpenOptions{
DetectDotGit: true,
})
require.NoError(t, err)

config, err := repo.Config()
assert.NoError(t, err)

for i := range config.Remotes["origin"].URLs {
config.Remotes["origin"].URLs[i] = "git@github.com:snyk-fixtures/shallow-goof-locked.git"
}

err = repo.SetConfig(config)
assert.NoError(t, err)

// run method under test
actualUrl, err := util.GetRepositoryUrl(repoDir)
assert.NoError(t, err)
assert.Equal(t, expectedRepoUrl, actualUrl)
}

func Test_GetRepositoryUrl_no_repo(t *testing.T) {
repoDir := t.TempDir()
actualUrl, err := util.GetRepositoryUrl(repoDir)
Expand Down
49 changes: 2 additions & 47 deletions scan_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/snyk/code-client-go/scan"
)

func TestSmoke_Scan_HTTPS(t *testing.T) {
func TestSmoke_Scan_IDE(t *testing.T) {
var cloneTargetDir, err = testutil.SetupCustomTestRepo(t, "https://github.com/snyk-labs/nodejs-goof", "0336589", "", "")
assert.NoError(t, err)

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestSmoke_Scan_HTTPS(t *testing.T) {
require.NotNil(t, response.Sarif.Runs[0].Results[0].Locations[0].PhysicalLocation.ArtifactLocation.URI)
}

func Test_SmokeScan_HTTPS_CLI(t *testing.T) {
func Test_SmokeScan_CLI(t *testing.T) {
var cloneTargetDir, err = testutil.SetupCustomTestRepo(t, "https://github.com/snyk-labs/nodejs-goof", "0336589", "", "")
assert.NoError(t, err)

Expand Down Expand Up @@ -142,51 +142,6 @@ func Test_SmokeScan_HTTPS_CLI(t *testing.T) {
require.NotNil(t, response.Sarif.Runs[0].Results[0].Locations[0].PhysicalLocation.ArtifactLocation.URI)
}

func TestSmoke_Scan_SSH(t *testing.T) {
var cloneTargetDir, err = testutil.SetupCustomTestRepo(t, "git@github.com:snyk-labs/nodejs-goof", "0336589", "", "")
assert.NoError(t, err)

target, err := scan.NewRepositoryTarget(cloneTargetDir)
assert.NoError(t, err)

files := sliceToChannel([]string{filepath.Join(cloneTargetDir, "app.js"), filepath.Join(cloneTargetDir, "utils.js")})

logger := zerolog.New(os.Stdout)
instrumentor := testutil.NewTestInstrumentor()
errorReporter := testutil.NewTestErrorReporter()
config := testutil.NewTestConfig()
httpClient := codeClientHTTP.NewHTTPClient(
func() *http.Client {
client := http.Client{
Timeout: time.Duration(180) * time.Second,
Transport: testutil.TestAuthRoundTripper{http.DefaultTransport},
}
return &client
},
codeClientHTTP.WithRetryCount(3),
codeClientHTTP.WithLogger(&logger),
)
trackerFactory := scan.NewNoopTrackerFactory()

codeScanner := codeClient.NewCodeScanner(
config,
httpClient,
codeClient.WithTrackerFactory(trackerFactory),
codeClient.WithInstrumentor(instrumentor),
codeClient.WithErrorReporter(errorReporter),
codeClient.WithLogger(&logger),
)

// let's have a requestID that does not change
span := instrumentor.StartSpan(context.Background(), "UploadAndAnalyze")
defer span.Finish()

response, bundleHash, scanErr := codeScanner.UploadAndAnalyze(span.Context(), uuid.New().String(), target, files, map[string]bool{})
require.NoError(t, scanErr)
require.NotEmpty(t, bundleHash)
require.NotNil(t, response)
}

func TestSmoke_Scan_SubFolder(t *testing.T) {
currDir, err := os.Getwd()
require.NoError(t, err)
Expand Down