-
Notifications
You must be signed in to change notification settings - Fork 1
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
chore: Introduce Option to specify repository URL #42
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f860a39
chore: replace scan path by ScanTarget structure ...
PeterSchafer 1966965
chore: add NewRepositoryTargetFromPath()
PeterSchafer 1604b48
chore: move ScanTarget and add sanitization
PeterSchafer 3f11a6d
chore: use functional Options patterns
PeterSchafer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,14 @@ func GetRepositoryUrl(path string) (string, error) { | |
|
||
// based on the docs, the first URL is being used to fetch, so this is the one we use | ||
repoUrl := remote.Config().URLs[0] | ||
repoUrl, err = sanitiseCredentials(repoUrl) | ||
repoUrl, err = SanitiseCredentials(repoUrl) | ||
|
||
// we need to return an actual URL, not the SSH | ||
repoUrl = strings.Replace(repoUrl, "[email protected]:", "https://github.com/", 1) | ||
return repoUrl, err | ||
} | ||
|
||
func sanitiseCredentials(rawUrl string) (string, error) { | ||
func SanitiseCredentials(rawUrl string) (string, error) { | ||
parsedURL, err := url.Parse(rawUrl) | ||
if err != nil { | ||
return rawUrl, nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package scan | ||
|
||
import "github.com/snyk/code-client-go/internal/util" | ||
|
||
type RepositoryTarget struct { | ||
LocalFilePath string | ||
repositoryUrl string | ||
} | ||
|
||
type Target interface { | ||
GetPath() string | ||
} | ||
|
||
func (r RepositoryTarget) GetPath() string { | ||
return r.LocalFilePath | ||
} | ||
|
||
func (r RepositoryTarget) GetRepositoryUrl() string { | ||
return r.repositoryUrl | ||
} | ||
|
||
type TargetOptions func(*RepositoryTarget) error | ||
|
||
func WithRepositoryUrl(repositoryUrl string) TargetOptions { | ||
return func(target *RepositoryTarget) error { | ||
var err error | ||
target.repositoryUrl, err = util.SanitiseCredentials(repositoryUrl) | ||
return err | ||
} | ||
} | ||
|
||
func NewRepositoryTarget(path string, options ...TargetOptions) (Target, error) { | ||
result := &RepositoryTarget{ | ||
LocalFilePath: path, | ||
} | ||
|
||
for _, option := range options { | ||
option(result) | ||
} | ||
|
||
if len(result.repositoryUrl) == 0 { | ||
var err error | ||
result.repositoryUrl, err = util.GetRepositoryUrl(path) | ||
if err != nil { | ||
return &RepositoryTarget{}, err | ||
} | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package scan | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTarget_pathOnly(t *testing.T) { | ||
expectedPath := "./" | ||
target, err := NewRepositoryTarget(expectedPath) | ||
assert.NoError(t, err) | ||
repoTarget, ok := target.(*RepositoryTarget) | ||
assert.True(t, ok) | ||
assert.NotEmpty(t, repoTarget.GetRepositoryUrl()) | ||
assert.Equal(t, expectedPath, repoTarget.GetPath()) | ||
} | ||
|
||
func TestTarget_pathToNonRepo(t *testing.T) { | ||
expectedPath := t.TempDir() | ||
target, err := NewRepositoryTarget(expectedPath) | ||
assert.Error(t, err) | ||
repoTarget, ok := target.(*RepositoryTarget) | ||
assert.True(t, ok) | ||
assert.Empty(t, repoTarget.GetRepositoryUrl()) | ||
assert.Empty(t, repoTarget.GetPath()) | ||
} | ||
|
||
func TestTarget_withRepoUrl(t *testing.T) { | ||
expectedRepoUrl := "https://myrepo.com/hello_world" | ||
expectedPath := "/hello_world" | ||
target, err := NewRepositoryTarget(expectedPath, WithRepositoryUrl("https://user:[email protected]/hello_world")) | ||
assert.NoError(t, err) | ||
repoTarget, ok := target.(*RepositoryTarget) | ||
assert.True(t, ok) | ||
assert.Equal(t, expectedRepoUrl, repoTarget.GetRepositoryUrl()) | ||
assert.Equal(t, expectedPath, repoTarget.GetPath()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any idea why this changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I know. I'll fix it, I implemented these tests in a non-determinstic way 🙊