Skip to content
Merged
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
73 changes: 27 additions & 46 deletions utils/getconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type FrogbotDetails struct {
}

type Repository struct {
Params `yaml:"params,omitempty"`
Params
OutputWriter outputwriter.OutputWriter
Server coreconfig.ServerDetails
}
Expand All @@ -52,30 +52,26 @@ type Params struct {
type JFrogPlatform struct {
XrayVersion string
XscVersion string
JFrogProjectKey string `yaml:"jfrogProjectKey,omitempty"`
JFrogProjectKey string
}

func (jp *JFrogPlatform) setJfProjectKeyIfExists() (err error) {
e := &ErrMissingEnv{}
if jp.JFrogProjectKey == "" {
if err = readParamFromEnv(jfrogProjectEnv, &jp.JFrogProjectKey); err != nil && !e.IsMissingEnvErr(err) {
return
}
// We don't want to return an error from this function if the error is of type ErrMissingEnv because JFrogPlatform environment variables are not mandatory.
err = nil
if err = readParamFromEnv(jfrogProjectEnv, &jp.JFrogProjectKey); err != nil && !e.IsMissingEnvErr(err) {
return
}
return
return nil
}

type Git struct {
GitProvider vcsutils.VcsProvider
vcsclient.VcsInfo
RepoOwner string
RepoName string `yaml:"repoName,omitempty"`
Branches []string `yaml:"branches,omitempty"`
RepoName string
Branches []string
PullRequestDetails vcsclient.PullRequestInfo
RepositoryCloneUrl string
UploadSbomToVcs *bool `yaml:"uploadSbomToVcs,omitempty"`
UploadSbomToVcs *bool
}

func (g *Git) GetRepositoryHttpsCloneUrl(gitClient vcsclient.VcsClient) (string, error) {
Expand All @@ -96,26 +92,18 @@ func (g *Git) setDefaultsIfNeeded(gitParamsFromEnv *Git, commandName string) (er
g.GitProvider = gitParamsFromEnv.GitProvider
g.VcsInfo = gitParamsFromEnv.VcsInfo
g.PullRequestDetails = gitParamsFromEnv.PullRequestDetails
if g.RepoName == "" {
if gitParamsFromEnv.RepoName == "" {
return fmt.Errorf("repository name is missing. please set the %s environment variable", GitRepoEnv)
}
g.RepoName = gitParamsFromEnv.RepoName
}
g.RepoName = gitParamsFromEnv.RepoName

if commandName == ScanPullRequest {
if gitParamsFromEnv.PullRequestDetails.ID == 0 {
return errors.New("no Pull Request ID has been provided. Please configure it by using the `JF_GIT_PULL_REQUEST_ID` environment variable")
}
}
if commandName == ScanRepository {
noBranchesProvidedViaConfig := len(g.Branches) == 0
noBranchesProvidedViaEnv := len(gitParamsFromEnv.Branches) == 0
if noBranchesProvidedViaConfig {
if noBranchesProvidedViaEnv {
return errors.New("no branches were provided. Please set your branches using the `JF_GIT_BASE_BRANCH` environment variable")
}
g.Branches = gitParamsFromEnv.Branches
if len(gitParamsFromEnv.Branches) == 0 {
return errors.New("no branches were provided. Please set your branches using the `JF_GIT_BASE_BRANCH` environment variable")
}
g.Branches = gitParamsFromEnv.Branches
}
envValue, err := getBoolEnv(GitDependencyGraphSubmissionEnv, true)
if err != nil {
Expand Down Expand Up @@ -191,14 +179,16 @@ func BuildRepositoryFromEnv(xrayVersion, xscVersion string, gitClient vcsclient.
// Create a single repository from environment variables
repository = Repository{}
repository.Server = *server
repository.Params.XrayVersion = xrayVersion
repository.Params.XscVersion = xscVersion
if err = repository.Params.Git.setDefaultsIfNeeded(gitParamsFromEnv, commandName); err != nil {
repository.Params.JFrogPlatform.XrayVersion = xrayVersion
repository.Params.JFrogPlatform.XscVersion = xscVersion
if err = repository.Params.JFrogPlatform.setJfProjectKeyIfExists(); err != nil {
return
}
if err = repository.Params.JFrogPlatform.setJfProjectKeyIfExists(); err != nil {

if err = repository.Params.Git.setDefaultsIfNeeded(gitParamsFromEnv, commandName); err != nil {
return
}

repository.setOutputWriterDetails()
repository.OutputWriter.SetSizeLimit(gitClient)
return repository, nil
Expand Down Expand Up @@ -388,48 +378,39 @@ func extractGitParamsFromEnvs() (*Git, error) {

autoDetectCIEnvVars()

// Branch & Repo names are mandatory variables.
// Must be set as environment variables.
// Validation performed later
// Set the base branch name
// Mandatory as env var for scan-repository, fetched from vcsclient.PullRequestInfo for scan-pr
var branch string
if err = readParamFromEnv(GitBaseBranchEnv, &branch); err != nil && !e.IsMissingEnvErr(err) {
return nil, err
}
if branch != "" {
gitEnvParams.Branches = []string{branch}
}
// Non-mandatory Git Api Endpoint, if not set, default values will be used.

if err = readParamFromEnv(GitRepoEnv, &gitEnvParams.RepoName); err != nil {
return nil, err
}

if err = readParamFromEnv(GitApiEndpointEnv, &gitEnvParams.APIEndpoint); err != nil && !e.IsMissingEnvErr(err) {
return nil, err
}
if err = verifyValidApiEndpoint(gitEnvParams.APIEndpoint); err != nil {
return nil, err
}
// [Mandatory] Set the Git provider
if gitEnvParams.GitProvider, err = extractVcsProviderFromEnv(); err != nil {
return nil, err
}
// [Mandatory] Set the git repository owner name (organization)
if err = readParamFromEnv(GitRepoOwnerEnv, &gitEnvParams.RepoOwner); err != nil {
return nil, err
}
// [Mandatory] Set the access token to the git provider
if err = readParamFromEnv(GitTokenEnv, &gitEnvParams.Token); err != nil {
return nil, err
}

// [Mandatory] Set the repository name, except for multi repository.
if err = readParamFromEnv(GitRepoEnv, &gitEnvParams.RepoName); err != nil {
return nil, err
}

// Set Bitbucket Server username
// Mandatory only for Bitbucket Server, this authentication detail is required for performing git operations.
if err = readParamFromEnv(GitBitBucketUsernameEnv, &gitEnvParams.Username); err != nil && !e.IsMissingEnvErr(err) {
if err = readParamFromEnv(GitBitBucketUsernameEnv, &gitEnvParams.Username); err != nil && gitEnvParams.GitProvider == vcsutils.BitbucketServer {
return nil, err
}
// Set Azure Repos Project name
// Mandatory for Azure Repos only
if err = readParamFromEnv(GitAzureProjectEnv, &gitEnvParams.Project); err != nil && gitEnvParams.GitProvider == vcsutils.AzureRepos {
return nil, err
Expand Down Expand Up @@ -506,7 +487,7 @@ func getBoolEnv(envKey string, defaultValue bool) (bool, error) {
if envValue != "" {
parsedEnv, err := strconv.ParseBool(envValue)
if err != nil {
return false, fmt.Errorf("the value of the %s environment is expected to be either TRUE or FALSE. The value received however is %s", envKey, envValue)
return false, fmt.Errorf("the value of the %s environment is expected to boolean. The value received however is %s", envKey, envValue)
}
return parsedEnv, nil
}
Expand Down
Loading