Skip to content

Commit e41e5e5

Browse files
authored
Fix internet connection setting in output writer and simplify flow (#1013)
1 parent bb7bc1b commit e41e5e5

File tree

8 files changed

+17
-29
lines changed

8 files changed

+17
-29
lines changed

commands.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ import (
1313
"github.com/jfrog/frogbot/v2/scanpullrequest"
1414
"github.com/jfrog/frogbot/v2/scanrepository"
1515
"github.com/jfrog/frogbot/v2/utils"
16-
"github.com/jfrog/frogbot/v2/utils/outputwriter"
17-
1816
"github.com/jfrog/jfrog-cli-security/utils/xsc"
1917
clitool "github.com/urfave/cli/v2"
2018
)
2119

2220
type FrogbotCommand interface {
2321
// Run the command
24-
Run(config utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) error
22+
Run(config utils.Repository, client vcsclient.VcsClient) error
2523
}
2624

2725
func GetCommands() []*clitool.Command {
@@ -54,8 +52,6 @@ func Exec(command FrogbotCommand, commandName string) (err error) {
5452
if err != nil {
5553
return err
5654
}
57-
// Check if the user has access to the frogbot repository (to access the resources needed)
58-
frogbotRepoConnection := utils.CheckConnection(outputwriter.FrogbotRepoUrl)
5955

6056
// Build the server configuration file
6157
originalJfrogHomeDir, tempJFrogHomeDir, err := utils.BuildServerConfigFile(frogbotDetails.ServerDetails)
@@ -79,7 +75,7 @@ func Exec(command FrogbotCommand, commandName string) (err error) {
7975

8076
// Invoke the command interface
8177
log.Info(fmt.Sprintf("Running Frogbot %q command", commandName))
82-
err = command.Run(frogbotDetails.Repository, frogbotDetails.GitClient, frogbotRepoConnection)
78+
err = command.Run(frogbotDetails.Repository, frogbotDetails.GitClient)
8379

8480
if err != nil {
8581
if reportError := xsc.ReportError(frogbotDetails.XrayVersion, frogbotDetails.XscVersion, frogbotDetails.ServerDetails, err, "frogbot", frogbotDetails.Repository.JFrogProjectKey); reportError != nil {

scanpullrequest/scanpullrequest.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ type targetPair struct {
3737

3838
type ScanPullRequestCmd struct{}
3939

40-
func (pr *ScanPullRequestCmd) Run(repository utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) (err error) {
41-
repoConfig := &repository
42-
repoConfig.OutputWriter.SetHasInternetConnection(frogbotRepoConnection.IsConnected())
43-
if repoConfig.Params.Git.PullRequestDetails, err = client.GetPullRequestByID(context.Background(),
44-
repoConfig.Params.Git.RepoOwner, repoConfig.Params.Git.RepoName, int(repoConfig.Params.Git.PullRequestDetails.ID)); err != nil {
40+
func (pr *ScanPullRequestCmd) Run(repository utils.Repository, client vcsclient.VcsClient) (err error) {
41+
if repository.Params.Git.PullRequestDetails, err = client.GetPullRequestByID(context.Background(),
42+
repository.Params.Git.RepoOwner, repository.Params.Git.RepoName, int(repository.Params.Git.PullRequestDetails.ID)); err != nil {
4543
return
4644
}
4745
pullRequestDetails := &repository.Params.Git.PullRequestDetails

scanpullrequest/scanpullrequest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func testScanPullRequest(t *testing.T, projectName string) {
262262

263263
// Run "frogbot scan pull request"
264264
var scanPullRequest ScanPullRequestCmd
265-
err := scanPullRequest.Run(config, client, utils.MockHasConnection())
265+
err := scanPullRequest.Run(config, client)
266266
assert.NoError(t, err)
267267

268268
// Check env sanitize

scanrepository/scanrepository.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ type ScanRepositoryCmd struct {
5050
XscVersion string
5151
}
5252

53-
func (sr *ScanRepositoryCmd) Run(repository utils.Repository, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) (err error) {
54-
repository.OutputWriter.SetHasInternetConnection(frogbotRepoConnection.IsConnected())
53+
func (sr *ScanRepositoryCmd) Run(repository utils.Repository, client vcsclient.VcsClient) (err error) {
5554
sr.XrayVersion = repository.Params.XrayVersion
5655
sr.XscVersion = repository.Params.XscVersion
5756
if err = sr.setCommandPrerequisites(&repository, client); err != nil {
@@ -114,8 +113,7 @@ func (sr *ScanRepositoryCmd) setCommandPrerequisites(repository *utils.Repositor
114113
SetResultsContext(repositoryCloneUrl, repository.Params.JFrogPlatform.JFrogProjectKey, false).
115114
SetConfigProfile(repository.Params.ConfigProfile)
116115

117-
// Set the outputwriter interface for the relevant vcs git provider
118-
sr.OutputWriter = outputwriter.GetCompatibleOutputWriter(repository.Params.Git.GitProvider)
116+
sr.OutputWriter = repository.OutputWriter
119117
sr.OutputWriter.SetSizeLimit(client)
120118
// Set the git client to perform git operations
121119
sr.gitManager, err = utils.NewGitManager().

scanrepository/scanrepository_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
205205

206206
// Run
207207
var cmd = ScanRepositoryCmd{XrayVersion: xrayVersion, XscVersion: xscVersion, dryRun: true, dryRunRepoPath: testDir}
208-
err = cmd.Run(repository, client, utils.MockHasConnection())
208+
err = cmd.Run(repository, client)
209209
defer func() {
210210
assert.NoError(t, os.Chdir(baseDir))
211211
}()
@@ -334,7 +334,7 @@ pr body
334334

335335
// Run
336336
var cmd = ScanRepositoryCmd{dryRun: true, dryRunRepoPath: testDir}
337-
err = cmd.Run(repository, client, utils.MockHasConnection())
337+
err = cmd.Run(repository, client)
338338
defer func() {
339339
assert.NoError(t, os.Chdir(baseDir))
340340
}()

utils/getconfiguration.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ type Repository struct {
4040
}
4141

4242
func (r *Repository) setOutputWriterDetails() {
43-
r.OutputWriter = outputwriter.GetCompatibleOutputWriter(r.Params.Git.GitProvider)
43+
// Check if the user has access to the frogbot repository (to access the resources needed)
44+
frogbotRepoConnection := CheckConnection(outputwriter.FrogbotRepoUrl)
45+
r.OutputWriter = outputwriter.GetCompatibleOutputWriter(r.Params.Git.GitProvider, frogbotRepoConnection.IsConnected())
4446
}
4547

4648
type Params struct {

utils/outputwriter/outputwriter.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ func (mo *MarkdownOutput) SetSizeLimit(client vcsclient.VcsClient) {
100100
mo.descriptionSizeLimit = client.GetPullRequestDetailsSizeLimit()
101101
}
102102

103-
func GetCompatibleOutputWriter(provider vcsutils.VcsProvider) OutputWriter {
104-
switch provider {
105-
case vcsutils.BitbucketServer:
106-
return &SimplifiedOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: true}}
107-
default:
108-
return &StandardOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: true}}
103+
func GetCompatibleOutputWriter(provider vcsutils.VcsProvider, hasInternetConnection bool) OutputWriter {
104+
if provider == vcsutils.BitbucketServer {
105+
return &SimplifiedOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: hasInternetConnection}}
109106
}
107+
return &StandardOutput{MarkdownOutput{vcsProvider: provider, hasInternetConnection: hasInternetConnection}}
110108
}
111109

112110
func MarkdownComment(text string) string {

utils/testsutils.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ func SetEnvsAndAssertWithCallback(t *testing.T, envs map[string]string) func() {
6666
}
6767
}
6868

69-
func MockHasConnection() *UrlAccessChecker {
70-
return &UrlAccessChecker{url: "url", connected: true}
71-
}
72-
7369
// Create a temporary directory and copy the content of "testdata/testDir" into it
7470
func CopyTestdataProjectsToTemp(t *testing.T, testDir string) (tmpDir string, restoreFunc func()) {
7571
// Copy project to a temporary directory

0 commit comments

Comments
 (0)