diff --git a/commands/audit/audit.go b/commands/audit/audit.go index 67357cb3f..8370a76bf 100644 --- a/commands/audit/audit.go +++ b/commands/audit/audit.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path" "path/filepath" "strconv" "strings" @@ -1100,7 +1101,7 @@ func processScanResults(params *AuditParams, cmdResults *results.SecurityCommand if err != nil { return cmdResults.AddGeneralError(fmt.Errorf("failed to upload scan results to Artifactory: %s", err.Error()), false) } - cmdResults.SetUploadedArtifactPath(filepath.Join(params.GetRtResultRepositoryWithProjectKey(), uploadPath)) + cmdResults.SetUploadedArtifactPath(path.Join(params.GetRtResultRepositoryWithProjectKey(), uploadPath)) if uiRoute, err := getScanResultsUiRoute(params, uploadPath); err != nil { log.Warn(fmt.Sprintf("failed to get scan results UI route: %s", err.Error())) } else if uiRoute != "" { diff --git a/utils/results/output/reportartifact.go b/utils/results/output/reportartifact.go index 0aba909fc..2a5cebc41 100644 --- a/utils/results/output/reportartifact.go +++ b/utils/results/output/reportartifact.go @@ -3,7 +3,8 @@ package output import ( "fmt" "net/url" - "path/filepath" + "path" + "strings" "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" @@ -87,13 +88,13 @@ func uploadViaArtifactoryDirect(serverDetails *config.ServerDetails, rtResultRep SetContentToUpload(cdxResults). SetFilePrefix(string(cmdType)). SetServerDetails(serverDetails). - SetUploadRepository(filepath.ToSlash(filepath.Join(rtResultRepository, artifactFinalRepoPath))). + SetUploadRepository(path.Join(rtResultRepository, artifactFinalRepoPath)). SetProjectKey(projectKey) artifactName, err := uploadCmd.Upload() if err != nil { return "", fmt.Errorf("failed uploading the scan results: %w", err) } - return filepath.ToSlash(filepath.Join(artifactFinalRepoPath, artifactName)), nil + return path.Join(artifactFinalRepoPath, artifactName), nil } func GetCommandResultsPlatformUrlMessage(cmdResults *results.SecurityCommandResults, pretty bool) string { @@ -141,10 +142,10 @@ func getGitContextArtifactPath(gitContext *services.XscGitInfoContext) (string, } if gitContext.PullRequest != nil && gitContext.Target != nil { // pull request, return the path with source and target commit hashes - return filepath.ToSlash(filepath.Join(artifactPath, "PR")), nil + return path.Join(artifactPath, "PR"), nil } // not a pull request, just return the source commit path - return filepath.ToSlash(filepath.Join(artifactPath, "commits")), nil + return path.Join(artifactPath, "commits"), nil } func extractBaseGitPath(gitCloneUrl, sourceBranchName string) (string, error) { @@ -154,5 +155,5 @@ func extractBaseGitPath(gitCloneUrl, sourceBranchName string) (string, error) { return "", err } // Extract the host and path, removing any .git suffix - return gitUrlParsed.Host + "/" + gitUrlParsed.Path[:len(gitUrlParsed.Path)-len(filepath.Ext(gitUrlParsed.Path))] + "/" + sourceBranchName, nil + return path.Join(gitUrlParsed.Host, strings.TrimSuffix(gitUrlParsed.Path, ".git"), sourceBranchName), nil } diff --git a/utils/results/output/reportartifact_test.go b/utils/results/output/reportartifact_test.go index 762df0826..edc9d725b 100644 --- a/utils/results/output/reportartifact_test.go +++ b/utils/results/output/reportartifact_test.go @@ -82,6 +82,55 @@ func TestUploadViaXrayApi_ServerError_ReturnsError(t *testing.T) { assert.Error(t, err) } +func TestGetGitContextArtifactPath(t *testing.T) { + testCases := []struct { + name string + gitContext *clientservices.XscGitInfoContext + expectedResult string + }{ + { + name: "commit scan", + gitContext: &clientservices.XscGitInfoContext{ + Source: clientservices.CommitContext{GitRepoHttpsCloneUrl: "https://github.com/org/repo.git", BranchName: "main"}, + }, + expectedResult: "github.com/org/repo/main/commits", + }, + { + name: "commit scan with a dotted repo name and no .git suffix", + gitContext: &clientservices.XscGitInfoContext{ + Source: clientservices.CommitContext{GitRepoHttpsCloneUrl: "https://github.com/socketio/socket.io", BranchName: "main"}, + }, + expectedResult: "github.com/socketio/socket.io/main/commits", + }, + { + name: "commit scan with a port in the clone url", + gitContext: &clientservices.XscGitInfoContext{ + Source: clientservices.CommitContext{GitRepoHttpsCloneUrl: "https://git.example.com:8443/collection/repo.git", BranchName: "main"}, + }, + expectedResult: "git.example.com:8443/collection/repo/main/commits", + }, + { + name: "pull request scan with a port in the clone url", + gitContext: &clientservices.XscGitInfoContext{ + Source: clientservices.CommitContext{GitRepoHttpsCloneUrl: "https://git.example.com:8443/collection/repo.git", BranchName: "feature"}, + GitDiffContext: clientservices.GitDiffContext{ + Target: &clientservices.CommitContext{GitRepoHttpsCloneUrl: "https://git.example.com:8443/collection/repo.git", BranchName: "main"}, + PullRequest: &clientservices.PullRequestContext{PullRequestId: 1}, + }, + }, + expectedResult: "git.example.com:8443/collection/repo/feature/PR", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + result, err := getGitContextArtifactPath(testCase.gitContext) + require.NoError(t, err) + assert.Equal(t, testCase.expectedResult, result) + }) + } +} + func TestGetLocalArtifactPath(t *testing.T) { testCases := []struct { name string