Skip to content
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

Use server ID in a configuration file #373

Merged
merged 9 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion action/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 29 additions & 6 deletions commands/commands.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
package commands

import (
"errors"
"fmt"
"github.com/jfrog/frogbot/commands/utils"
"github.com/jfrog/froggit-go/vcsclient"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
clitool "github.com/urfave/cli/v2"
"os"
)

type FrogbotCommand interface {
// Run the command
Run(config utils.RepoAggregator, client vcsclient.VcsClient) error
}

func Exec(command FrogbotCommand, name string) error {
// Get frogbotUtils the contains the config, server and VCS client
func Exec(command FrogbotCommand, name string) (err error) {
// Get frogbotUtils that contains the config, server, and VCS client
log.Info("Frogbot version:", utils.FrogbotVersion)
frogbotUtils, err := utils.GetFrogbotUtils()
if err != nil {
return err
}
// Download extractors if the jfrog releases repo environment variable is set
releasesRepo := frogbotUtils.Repositories[0].JfrogReleasesRepo
if err = utils.DownloadExtractorsFromRemoteIfNeeded(frogbotUtils.ServerDetails, "", releasesRepo); err != nil {

// Build the server configuration file
originalJfrogHomeDir, tempJFrogHomeDir, err := utils.BuildServerConfigFile(frogbotUtils.ServerDetails)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, os.Setenv(utils.JfrogHomeDirEnv, originalJfrogHomeDir), fileutils.RemoveTempDir(tempJFrogHomeDir))
}()

// Set releases remote repository env if needed
previousReleasesRepoEnv := os.Getenv(coreutils.ReleasesRemoteEnv)
if frogbotUtils.ReleasesRepo != "" {
if err = os.Setenv(coreutils.ReleasesRemoteEnv, fmt.Sprintf("frogbot/%s", frogbotUtils.ReleasesRepo)); err != nil {
return
}
defer func() {
err = errors.Join(err, os.Setenv(coreutils.ReleasesRemoteEnv, previousReleasesRepoEnv))
}()
}

// Send a usage report
usageReportSent := make(chan error)
go utils.ReportUsage(name, frogbotUtils.ServerDetails, usageReportSent)

// Invoke the command interface
log.Info(fmt.Sprintf("Running Frogbot %q command", name))
err = command.Run(frogbotUtils.Repositories, frogbotUtils.Client)

// Wait for a signal, letting us know that the usage reporting is done.
<-usageReportSent

if err == nil {
log.Info(fmt.Sprintf("Frogbot %q command finished successfully ", name))
log.Info(fmt.Sprintf("Frogbot %q command finished successfully", name))
}
return err
}
Expand Down
1 change: 0 additions & 1 deletion commands/createfixpullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (cfp *CreateFixPullRequestsCmd) scanAndFixRepository(repository *utils.Repo
SetXrayGraphScanParams(repository.Watches, repository.JFrogProjectKey).
SetFailOnInstallationErrors(*repository.FailOnSecurityIssues).
SetBranch(branch).
SetReleasesRepo(repository.JfrogReleasesRepo).
SetFixableOnly(repository.FixableOnly).
SetMinSeverity(repository.MinSeverity)
cfp.aggregateFixes = repository.Git.AggregateFixes
Expand Down
4 changes: 1 addition & 3 deletions commands/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient)
for i := range repoConfig.Projects {
scanDetails := utils.NewScanDetails(client, &repoConfig.Server, &repoConfig.Git).
SetProject(&repoConfig.Projects[i]).
SetReleasesRepo(repoConfig.JfrogReleasesRepo).
SetXrayGraphScanParams(repoConfig.Watches, repoConfig.JFrogProjectKey).
SetMinSeverity(repoConfig.MinSeverity).
SetFixableOnly(repoConfig.FixableOnly)
Expand Down Expand Up @@ -291,8 +290,7 @@ func runInstallAndAudit(scanSetup *utils.ScanDetails, workDirs ...string) (audit
SetUseWrapper(*scanSetup.UseWrapper).
SetDepsRepo(scanSetup.Repository).
SetIgnoreConfigFile(true).
SetServerDetails(scanSetup.ServerDetails).
SetReleasesRepo(scanSetup.ReleasesRepo())
SetServerDetails(scanSetup.ServerDetails)
auditParams := audit.NewAuditParams().
SetXrayGraphScanParams(scanSetup.XrayGraphScanParams).
SetWorkingDirs(workDirs).
Expand Down
77 changes: 0 additions & 77 deletions commands/utils/extractors.go

This file was deleted.

45 changes: 0 additions & 45 deletions commands/utils/extractors_test.go

This file was deleted.

5 changes: 2 additions & 3 deletions commands/utils/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type FrogbotUtils struct {
Repositories RepoAggregator
ServerDetails *coreconfig.ServerDetails
Client vcsclient.VcsClient
ReleasesRepo string
}

type RepoAggregator []Repository
Expand Down Expand Up @@ -107,7 +108,6 @@ type Scan struct {
FailOnSecurityIssues *bool `yaml:"failOnSecurityIssues,omitempty"`
MinSeverity string `yaml:"minSeverity,omitempty"`
Projects []Project `yaml:"projects,omitempty"`
JfrogReleasesRepo string
}

func (s *Scan) setDefaultsIfNeeded() (err error) {
Expand Down Expand Up @@ -145,7 +145,6 @@ func (s *Scan) setDefaultsIfNeeded() (err error) {
return
}
}
s.JfrogReleasesRepo = getTrimmedEnv(jfrogReleasesRepoEnv)
return
}

Expand Down Expand Up @@ -276,7 +275,7 @@ func GetFrogbotUtils() (frogbotUtils *FrogbotUtils, err error) {
if err != nil {
return nil, err
}
return &FrogbotUtils{Repositories: configAggregator, Client: client, ServerDetails: server}, err
return &FrogbotUtils{Repositories: configAggregator, Client: client, ServerDetails: server, ReleasesRepo: os.Getenv(jfrogReleasesRepoEnv)}, err
}

// getConfigAggregator returns a RepoAggregator based on frogbot-config.yml and environment variables.
Expand Down
1 change: 0 additions & 1 deletion commands/utils/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ func TestBuildRepoAggregatorWithEmptyScan(t *testing.T) {
assert.False(t, scan.IncludeAllVulnerabilities)
assert.False(t, scan.FixableOnly)
assert.Empty(t, scan.MinSeverity)
assert.Empty(t, scan.JfrogReleasesRepo)
assert.True(t, *scan.FailOnSecurityIssues)
assert.Len(t, scan.Projects, 1)
project := scan.Projects[0]
Expand Down
10 changes: 0 additions & 10 deletions commands/utils/scandetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type ScanDetails struct {
fixableOnly bool
minSeverityFilter string
branch string
releasesRepo string
}

func NewScanDetails(client vcsclient.VcsClient, server *config.ServerDetails, git *Git) *ScanDetails {
Expand Down Expand Up @@ -53,11 +52,6 @@ func (sc *ScanDetails) SetBranch(branch string) *ScanDetails {
return sc
}

func (sc *ScanDetails) SetReleasesRepo(releasesRepo string) *ScanDetails {
sc.releasesRepo = releasesRepo
return sc
}

func (sc *ScanDetails) Client() vcsclient.VcsClient {
return sc.client
}
Expand All @@ -66,10 +60,6 @@ func (sc *ScanDetails) Branch() string {
return sc.branch
}

func (sc *ScanDetails) ReleasesRepo() string {
return sc.releasesRepo
}

func (sc *ScanDetails) FailOnInstallationErrors() bool {
return sc.failOnInstallationErrors
}
Expand Down
19 changes: 19 additions & 0 deletions commands/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/jfrog/froggit-go/vcsutils"
"github.com/jfrog/gofrog/version"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
audit "github.com/jfrog/jfrog-cli-core/v2/xray/commands/audit/generic"
Expand Down Expand Up @@ -37,6 +38,7 @@ const (
skipIndirectVulnerabilitiesMsg = "%s is an indirect dependency that will not be updated to version %s.\nFixing indirect dependencies can introduce conflicts with other dependencies that rely on the previous version.\nFrogbot skips this to avoid potential incompatibilities."
skipBuildToolDependencyMsg = "Skipping vulnerable package %s since it is not defined in your package descriptor file. " +
"Update %s version to %s to fix this vulnerability."
JfrogHomeDirEnv = "JFROG_CLI_HOME_DIR"
)

var (
Expand Down Expand Up @@ -272,3 +274,20 @@ func validateBranchName(branchName string) error {
}
return nil
}

func BuildServerConfigFile(server *config.ServerDetails) (previousJFrogHomeDir, currentJFrogHomeDir string, err error) {
// Create temp dir to store server config inside
currentJFrogHomeDir, err = fileutils.CreateTempDir()
if err != nil {
return
}
// Save current JFrog Home dir
previousJFrogHomeDir = os.Getenv(JfrogHomeDirEnv)
// Set the temp dir as the JFrog Home dir
if err = os.Setenv(JfrogHomeDirEnv, currentJFrogHomeDir); err != nil {
return
}
cc := commands.NewConfigCommand(commands.AddOrEdit, "frogbot").SetDetails(server)
err = cc.Run()
return
}
Loading
Loading