diff --git a/commands.go b/commands.go index 08e479619..d67f0892f 100644 --- a/commands.go +++ b/commands.go @@ -13,15 +13,13 @@ import ( "github.com/jfrog/frogbot/v2/scanpullrequest" "github.com/jfrog/frogbot/v2/scanrepository" "github.com/jfrog/frogbot/v2/utils" - "github.com/jfrog/frogbot/v2/utils/outputwriter" - "github.com/jfrog/jfrog-cli-security/utils/xsc" clitool "github.com/urfave/cli/v2" ) type FrogbotCommand interface { // Run the command - Run(config utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) error + Run(config utils.Repository, client vcsclient.VcsClient) error } func GetCommands() []*clitool.Command { @@ -54,8 +52,6 @@ func Exec(command FrogbotCommand, commandName string) (err error) { if err != nil { return err } - // Check if the user has access to the frogbot repository (to access the resources needed) - frogbotRepoConnection := utils.CheckConnection(outputwriter.FrogbotRepoUrl) // Build the server configuration file originalJfrogHomeDir, tempJFrogHomeDir, err := utils.BuildServerConfigFile(frogbotDetails.ServerDetails) @@ -79,7 +75,7 @@ func Exec(command FrogbotCommand, commandName string) (err error) { // Invoke the command interface log.Info(fmt.Sprintf("Running Frogbot %q command", commandName)) - err = command.Run(frogbotDetails.Repository, frogbotDetails.GitClient, frogbotRepoConnection) + err = command.Run(frogbotDetails.Repository, frogbotDetails.GitClient) if err != nil { if reportError := xsc.ReportError(frogbotDetails.XrayVersion, frogbotDetails.XscVersion, frogbotDetails.ServerDetails, err, "frogbot", frogbotDetails.Repository.JFrogProjectKey); reportError != nil { diff --git a/scanpullrequest/scanpullrequest.go b/scanpullrequest/scanpullrequest.go index 1282f37ce..df59f32a1 100644 --- a/scanpullrequest/scanpullrequest.go +++ b/scanpullrequest/scanpullrequest.go @@ -37,11 +37,9 @@ type targetPair struct { type ScanPullRequestCmd struct{} -func (pr *ScanPullRequestCmd) Run(repository utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) (err error) { - repoConfig := &repository - repoConfig.OutputWriter.SetHasInternetConnection(frogbotRepoConnection.IsConnected()) - if repoConfig.Params.Git.PullRequestDetails, err = client.GetPullRequestByID(context.Background(), - repoConfig.Params.Git.RepoOwner, repoConfig.Params.Git.RepoName, int(repoConfig.Params.Git.PullRequestDetails.ID)); err != nil { +func (pr *ScanPullRequestCmd) Run(repository utils.Repository, client vcsclient.VcsClient) (err error) { + if repository.Params.Git.PullRequestDetails, err = client.GetPullRequestByID(context.Background(), + repository.Params.Git.RepoOwner, repository.Params.Git.RepoName, int(repository.Params.Git.PullRequestDetails.ID)); err != nil { return } pullRequestDetails := &repository.Params.Git.PullRequestDetails diff --git a/scanpullrequest/scanpullrequest_test.go b/scanpullrequest/scanpullrequest_test.go index 5dc8e7ee5..6bb63d769 100644 --- a/scanpullrequest/scanpullrequest_test.go +++ b/scanpullrequest/scanpullrequest_test.go @@ -262,7 +262,7 @@ func testScanPullRequest(t *testing.T, projectName string) { // Run "frogbot scan pull request" var scanPullRequest ScanPullRequestCmd - err := scanPullRequest.Run(config, client, utils.MockHasConnection()) + err := scanPullRequest.Run(config, client) assert.NoError(t, err) // Check env sanitize diff --git a/scanrepository/scanrepository.go b/scanrepository/scanrepository.go index bd4301f05..f04d14925 100644 --- a/scanrepository/scanrepository.go +++ b/scanrepository/scanrepository.go @@ -50,8 +50,7 @@ type ScanRepositoryCmd struct { XscVersion string } -func (sr *ScanRepositoryCmd) Run(repository utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) (err error) { - repository.OutputWriter.SetHasInternetConnection(frogbotRepoConnection.IsConnected()) +func (sr *ScanRepositoryCmd) Run(repository utils.Repository, client vcsclient.VcsClient) (err error) { sr.XrayVersion = repository.Params.XrayVersion sr.XscVersion = repository.Params.XscVersion if err = sr.setCommandPrerequisites(&repository, client); err != nil { @@ -106,8 +105,7 @@ func (sr *ScanRepositoryCmd) setCommandPrerequisites(repository *utils.Repositor SetResultsContext(repositoryCloneUrl, repository.Params.JFrogPlatform.JFrogProjectKey, false). SetConfigProfile(repository.Params.ConfigProfile) - // Set the outputwriter interface for the relevant vcs git provider - sr.OutputWriter = outputwriter.GetCompatibleOutputWriter(repository.Params.Git.GitProvider) + sr.OutputWriter = repository.OutputWriter sr.OutputWriter.SetSizeLimit(client) // Set the git client to perform git operations sr.gitManager, err = utils.NewGitManager(). diff --git a/scanrepository/scanrepository_test.go b/scanrepository/scanrepository_test.go index 1c8eb8076..f6a4b011d 100644 --- a/scanrepository/scanrepository_test.go +++ b/scanrepository/scanrepository_test.go @@ -205,7 +205,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) { // Run var cmd = ScanRepositoryCmd{XrayVersion: xrayVersion, XscVersion: xscVersion, dryRun: true, dryRunRepoPath: testDir} - err = cmd.Run(repository, client, utils.MockHasConnection()) + err = cmd.Run(repository, client) defer func() { assert.NoError(t, os.Chdir(baseDir)) }() @@ -334,7 +334,7 @@ pr body // Run var cmd = ScanRepositoryCmd{dryRun: true, dryRunRepoPath: testDir} - err = cmd.Run(repository, client, utils.MockHasConnection()) + err = cmd.Run(repository, client) defer func() { assert.NoError(t, os.Chdir(baseDir)) }() diff --git a/utils/getconfiguration.go b/utils/getconfiguration.go index 944b9b49e..c6b5d4496 100644 --- a/utils/getconfiguration.go +++ b/utils/getconfiguration.go @@ -40,7 +40,9 @@ type Repository struct { } func (r *Repository) setOutputWriterDetails() { - r.OutputWriter = outputwriter.GetCompatibleOutputWriter(r.Params.Git.GitProvider) + // Check if the user has access to the frogbot repository (to access the resources needed) + frogbotRepoConnection := CheckConnection(outputwriter.FrogbotRepoUrl) + r.OutputWriter = outputwriter.GetCompatibleOutputWriter(r.Params.Git.GitProvider, frogbotRepoConnection.IsConnected()) } type Params struct { diff --git a/utils/outputwriter/outputwriter.go b/utils/outputwriter/outputwriter.go index 17a43d8b0..e8c83ae90 100644 --- a/utils/outputwriter/outputwriter.go +++ b/utils/outputwriter/outputwriter.go @@ -100,13 +100,11 @@ func (mo *MarkdownOutput) SetSizeLimit(client vcsclient.VcsClient) { mo.descriptionSizeLimit = client.GetPullRequestDetailsSizeLimit() } -func GetCompatibleOutputWriter(provider vcsutils.VcsProvider) OutputWriter { - switch provider { - case vcsutils.BitbucketServer: - return &SimplifiedOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: true}} - default: - return &StandardOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: true}} +func GetCompatibleOutputWriter(provider vcsutils.VcsProvider, hasInternetConnection bool) OutputWriter { + if provider == vcsutils.BitbucketServer { + return &SimplifiedOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: hasInternetConnection}} } + return &StandardOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: hasInternetConnection}} } func MarkdownComment(text string) string { diff --git a/utils/testsutils.go b/utils/testsutils.go index a9dea02f1..86aebfa75 100644 --- a/utils/testsutils.go +++ b/utils/testsutils.go @@ -66,10 +66,6 @@ func SetEnvsAndAssertWithCallback(t *testing.T, envs map[string]string) func() { } } -func MockHasConnection() *UrlAccessChecker { - return &UrlAccessChecker{url: "url", connected: true} -} - // Create a temporary directory and copy the content of "testdata/testDir" into it func CopyTestdataProjectsToTemp(t *testing.T, testDir string) (tmpDir string, restoreFunc func()) { // Copy project to a temporary directory