Skip to content

Commit

Permalink
Respect project env var on build discard and promote (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Jul 13, 2023
1 parent cbcfbf6 commit ef7f99c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
16 changes: 8 additions & 8 deletions artifactory/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ func containerPushCmd(c *cli.Context, containerManagerType containerutils.Contai
targetRepo := c.Args().Get(1)
skipLogin := c.Bool("skip-login")

buildConfiguration, err := buildtools.CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func containerPullCmd(c *cli.Context, containerManagerType containerutils.Contai
imageTag := c.Args().Get(0)
sourceRepo := c.Args().Get(1)
skipLogin := c.Bool("skip-login")
buildConfiguration, err := buildtools.CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return err
}
Expand All @@ -1090,7 +1090,7 @@ func BuildDockerCreateCmd(c *cli.Context) error {
if imageNameWithDigestFile == "" {
return cliutils.PrintHelpAndReturnError("The '--image-file' command option was not provided.", c)
}
buildConfiguration, err := buildtools.CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return err
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ func downloadCmd(c *cli.Context) error {
if err != nil {
return err
}
buildConfiguration, err := buildtools.CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return err
}
Expand Down Expand Up @@ -1274,7 +1274,7 @@ func uploadCmd(c *cli.Context) (err error) {
if err != nil {
return
}
buildConfiguration, err := buildtools.CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return
}
Expand All @@ -1298,7 +1298,7 @@ func uploadCmd(c *cli.Context) (err error) {
"You can avoid this confirmation message by adding --quiet to the command.", false) {
return nil
}
// This error is being checked latter on because we need to generate summary report before return.
// This error is being checked later on because we need to generate summary report before return.
err = progressbar.ExecWithProgress(uploadCmd)
result := uploadCmd.Result()
defer cliutils.CleanupResult(result, &err)
Expand Down Expand Up @@ -2529,7 +2529,7 @@ func createBuildPromoteConfiguration(c *cli.Context) services.PromotionParams {
promotionParamsImpl.IncludeDependencies = c.Bool("include-dependencies")
promotionParamsImpl.Copy = c.Bool("copy")
promotionParamsImpl.Properties = c.String("props")
promotionParamsImpl.ProjectKey = c.String("project")
promotionParamsImpl.ProjectKey = cliutils.GetProject(c)
promotionParamsImpl.FailFast = c.BoolT("fail-fast")

// If the command received 3 args, read the build name, build number
Expand All @@ -2554,7 +2554,7 @@ func createBuildDiscardConfiguration(c *cli.Context) services.DiscardBuildsParam
discardParamsImpl.ExcludeBuilds = c.String("exclude-builds")
discardParamsImpl.Async = c.Bool("async")
discardParamsImpl.BuildName = cliutils.GetBuildName(c.Args().Get(0))
discardParamsImpl.ProjectKey = c.String("project")
discardParamsImpl.ProjectKey = cliutils.GetProject(c)
return discardParamsImpl
}

Expand Down
8 changes: 1 addition & 7 deletions buildtools/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func GoPublishCmd(c *cli.Context) (err error) {
if err != nil {
return err
}
buildConfiguration, err := CreateBuildConfigurationWithModule(c)
buildConfiguration, err := cliutils.CreateBuildConfigurationWithModule(c)
if err != nil {
return err
}
Expand Down Expand Up @@ -663,12 +663,6 @@ func goCmdVerification(c *cli.Context) (string, error) {
return configFilePath, nil
}

func CreateBuildConfigurationWithModule(c *cli.Context) (buildConfigConfiguration *utils.BuildConfiguration, err error) {
buildConfigConfiguration = new(utils.BuildConfiguration)
err = buildConfigConfiguration.SetBuildName(c.String("build-name")).SetBuildNumber(c.String("build-number")).SetProject(c.String("project")).SetModule(c.String("module")).ValidateBuildAndModuleParams()
return
}

func dockerCmd(c *cli.Context) error {
args := cliutils.ExtractCommand(c)
var cmd, image string
Expand Down
17 changes: 12 additions & 5 deletions utils/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ func SetCliExecutableName(executablePath string) {
coreutils.SetCliExecutableName(filepath.Base(executablePath))
}

// Returns build configuration struct using the params provided from the console.
// Returns build configuration struct using the args (build name/number) and options (project) provided by the user.
// Any empty configuration could be later overridden by environment variables if set.
func CreateBuildConfiguration(c *cli.Context) *artifactoryUtils.BuildConfiguration {
buildConfiguration := new(artifactoryUtils.BuildConfiguration)
buildNameArg, buildNumberArg := c.Args().Get(0), c.Args().Get(1)
Expand All @@ -737,6 +738,15 @@ func CreateBuildConfiguration(c *cli.Context) *artifactoryUtils.BuildConfigurati
return buildConfiguration
}

// Returns build configuration struct using the options provided by the user.
// Any empty configuration could be later overridden by environment variables if set.
func CreateBuildConfigurationWithModule(c *cli.Context) (buildConfigConfiguration *artifactoryUtils.BuildConfiguration, err error) {
buildConfigConfiguration = new(artifactoryUtils.BuildConfiguration)
err = buildConfigConfiguration.SetBuildName(c.String("build-name")).SetBuildNumber(c.String("build-number")).
SetProject(c.String("project")).SetModule(c.String("module")).ValidateBuildAndModuleParams()
return
}

func CreateArtifactoryDetailsByFlags(c *cli.Context) (*coreConfig.ServerDetails, error) {
artDetails, err := CreateServerDetailsWithConfigOffer(c, false, Rt)
if err != nil {
Expand Down Expand Up @@ -860,8 +870,5 @@ func doHttpRequest(client *http.Client, req *http.Request) (resp *http.Response,
// Get project key from flag or environment variable
func GetProject(c *cli.Context) string {
projectKey := c.String("project")
if projectKey == "" {
projectKey = os.Getenv(coreutils.Project)
}
return projectKey
return getOrDefaultEnv(projectKey, coreutils.Project)
}

0 comments on commit ef7f99c

Please sign in to comment.