Skip to content

Commit

Permalink
refactor: remove redundant ssh transformation [IDE-257] (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu authored Jun 13, 2024
1 parent f8e7eba commit 48f53df
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 80 deletions.
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

0 comments on commit 48f53df

Please sign in to comment.