Skip to content

Commit

Permalink
resolved: SAAS-5256 new sanitise formula for scm id. (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzurielweisberg authored May 9, 2022
1 parent 6950435 commit 6da91d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ build:
-w /build \
-v `pwd`:/build \
golang:1.18 \
go build -o /build/bin/aqua cmd/aqua/main.go|| exit 1
go build -o build/bin/aqua cmd/aqua/main.go|| exit 1
14 changes: 8 additions & 6 deletions pkg/metadata/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ func GetImageDetails(imageName string) (prefix, repo, tag string) {
func GetScmID(scanPath string) (string, error) {
gitConfigFile := filepath.Join(scanPath, ".git", "config")
gitConfig, err := ioutil.ReadFile(gitConfigFile)
system := GetBuildSystem()
if err == nil {
re := regexp.MustCompile(`(?m)^\s*url\s?=\s*(.*)\s*$`)
if re.Match(gitConfig) {
scmID := re.FindStringSubmatch(string(gitConfig))[1]
scmID = sanitiseScmId(system, scmID)
scmID = sanitiseScmId(scmID)

return scmID, nil
}
Expand All @@ -99,10 +98,13 @@ func GetScmID(scanPath string) (string, error) {
return filepath.Base(scanPath), nil
}

func sanitiseScmId(system string, scmID string) string {
if system == "gitlab" {
scmID = strings.Split(scmID, "@")[1]
}
// This function is the formula on the DB, on Atlas side and on Argon side,
// do not change without coordinating the change
func sanitiseScmId(scmID string) string {
scmID = regexp.MustCompile("^.*@").ReplaceAllLiteralString(scmID, "")
scmID = regexp.MustCompile("^https?://").ReplaceAllLiteralString(scmID, "")
scmID = strings.TrimSuffix(scmID, ".git")
scmID = strings.ReplaceAll(scmID, ":", "/")
return scmID
}

Expand Down
25 changes: 10 additions & 15 deletions pkg/metadata/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

func Test_convertScmId(t *testing.T) {
type args struct {
system string
scmID string
scmID string
}
tests := []struct {
name string
Expand All @@ -17,42 +16,38 @@ func Test_convertScmId(t *testing.T) {
{
name: "happy path - bitbucket",
args: args{
scmID: "git@bitbucket.org:repo.git",
system: "bitbucket",
scmID: "git@bitbucket.org:repo.git",
},
want: "git@bitbucket.org:repo.git",
want: "bitbucket.org/repo",
},
{
name: "happy path - github",
args: args{
scmID: "https://github.com/repo.git",
system: "github",
scmID: "https://github.com/repo.git",
},
want: "https://github.com/repo.git",
want: "github.com/repo",
},
{
name: "happy path - other",
args: args{
scmID: "git@othertest.com/repo.git",
system: "other",
scmID: "git@othertest.com/repo.git",
},
want: "git@othertest.com/repo.git",
want: "othertest.com/repo",
},

{
name: "happy path - gitlab",
args: args{
scmID: "https://gitlab-ci-token:123456-abcdef@gitlab.com/aqua/repo.git",
system: "gitlab",
scmID: "https://gitlab-ci-token:123456-abcdef@gitlab.com/aqua/repo.git",
},
want: "gitlab.com/aqua/repo.git",
want: "gitlab.com/aqua/repo",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := sanitiseScmId(tt.args.system, tt.args.scmID); got != tt.want {
if got := sanitiseScmId(tt.args.scmID); got != tt.want {
t.Errorf("sanitiseScmId() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 6da91d4

Please sign in to comment.