Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/jfrog/jfrog-cli-core into np…
Browse files Browse the repository at this point in the history
…m-login

# Conflicts:
#	go.mod
  • Loading branch information
sverdlov93 committed Nov 17, 2024
2 parents 6e3f8b9 + 24197a7 commit 07af542
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 6 deletions.
7 changes: 7 additions & 0 deletions artifactory/commands/buildinfo/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (bpc *BuildPublishCommand) IsDetailedSummary() bool {
}

func (bpc *BuildPublishCommand) CommandName() string {
autoPublishedTriggered, err := clientutils.GetBoolEnvValue(coreutils.UsageAutoPublishedBuild, false)
if err != nil {
log.Warn("Failed to get the value of the environment variable: " + coreutils.UsageAutoPublishedBuild + ". " + err.Error())
}
if autoPublishedTriggered {
return "rt_build_publish_auto"
}
return "rt_build_publish"
}

Expand Down
4 changes: 3 additions & 1 deletion common/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ func reportUsage(command Command, channel chan<- bool) {

// Set to true when the report usage func exits
func signalReportUsageFinished(ch chan<- bool) {
ch <- true
if ch != nil {
ch <- true
}
}
27 changes: 26 additions & 1 deletion common/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ const (
WebLogin AuthenticationMethod = "Web Login"
)

const (
// Indicates that the config command uses OIDC authentication.
configOidcCommandName = "config_oidc"
// Default config command name.
configCommandName = "config"
)

// Internal golang locking for the same process.
var mutex sync.Mutex

Expand Down Expand Up @@ -143,7 +150,25 @@ func (cc *ConfigCommand) ServerDetails() (*config.ServerDetails, error) {
}

func (cc *ConfigCommand) CommandName() string {
return "config"
oidcConfigured, err := clientUtils.GetBoolEnvValue(coreutils.UsageOidcConfigured, false)
if err != nil {
log.Warn("Failed to get the value of the environment variable: " + coreutils.UsageAutoPublishedBuild + ". " + err.Error())
}
if oidcConfigured {
return configOidcCommandName
}
return configCommandName
}

// ExecAndReportUsage runs the ConfigCommand and then triggers a usage report if needed,
// Report usage only if OIDC integration was used
// Usage must be sent after command execution as we need the server details to be set.
func (cc *ConfigCommand) ExecAndReportUsage() (err error) {
if err = cc.Run(); err != nil {
return
}
reportUsage(cc, nil)
return
}

func (cc *ConfigCommand) config() error {
Expand Down
59 changes: 59 additions & 0 deletions common/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"encoding/json"
testsUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"os"
"testing"

Expand Down Expand Up @@ -344,6 +345,64 @@ func TestImport(t *testing.T) {
assert.Equal(t, "password", serverDetails.GetPassword())
}

func TestCommandName(t *testing.T) {
// Clean up
defer func() {
testsUtils.UnSetEnvAndAssert(t, coreutils.UsageOidcConfigured)
}()
cc := NewConfigCommand(AddOrEdit, testServerId)
// Test when the environment variable is not set
assert.Equal(t, configCommandName, cc.CommandName())
// Test when the environment variable is set
testsUtils.SetEnvWithCallbackAndAssert(t, coreutils.UsageOidcConfigured, "true")
assert.Equal(t, configOidcCommandName, cc.CommandName())
}

func TestConfigCommand_ExecAndReportUsage(t *testing.T) {
defer func() {
testsUtils.UnSetEnvAndAssert(t, coreutils.UsageOidcConfigured)
}()
// Define test scenarios
testCases := []struct {
name string
envVarValue string // Environment variable value to set (or empty to unset)
expectedName string // Expected command name
expectError bool // Whether an error is expected
}{
{
name: "With usage report",
envVarValue: "TRUE",
expectedName: configOidcCommandName,
},
{
name: "Without usage report",
envVarValue: "", // Empty to unset the environment variable
expectedName: configCommandName,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Set or unset the environment variable
if tc.envVarValue != "" {
err := os.Setenv(coreutils.UsageOidcConfigured, tc.envVarValue)
assert.NoError(t, err)
} else {
err := os.Unsetenv(coreutils.UsageOidcConfigured)
assert.NoError(t, err)
}

// Initialize the command and check the expected command name
cc := NewConfigCommand(AddOrEdit, testServerId)
assert.Equal(t, tc.expectedName, cc.CommandName())

// Execute and validate no errors
err := cc.ExecAndReportUsage()
assert.NoError(t, err)
})
}
}

func testExportImport(t *testing.T, inputDetails *config.ServerDetails) {
configToken, err := config.Export(inputDetails)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/jfrog/jfrog-cli-core/v2

go 1.22.9
go 1.23.3

require github.com/c-bata/go-prompt v0.2.5 // Should not be updated to 0.2.6 due to a bug (https://github.com/jfrog/jfrog-cli-core/pull/372)

Expand All @@ -14,7 +14,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.6.1
github.com/jfrog/build-info-go v1.10.5
github.com/jfrog/gofrog v1.7.6
github.com/jfrog/jfrog-client-go v1.47.6
github.com/jfrog/jfrog-client-go v1.48.0
github.com/magiconair/properties v1.8.7
github.com/manifoldco/promptui v0.9.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ github.com/jfrog/build-info-go v1.10.5 h1:cW03JlPlKv7RMUU896uLUxyLWXAmCgR5Y5QX0f
github.com/jfrog/build-info-go v1.10.5/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
github.com/jfrog/jfrog-client-go v1.47.6 h1:nEMwJvjsuuY6LpOV3e33P4c4irPHkG8Qxw27bgeCl/Y=
github.com/jfrog/jfrog-client-go v1.47.6/go.mod h1:jCpvS83DZHAin2aSG7VroTsILJsyq7AOcFfx++P241E=
github.com/jfrog/jfrog-client-go v1.48.0 h1:hx5B7+Wnobmzq4aFVZtALtbEVDFcjpn0Wb4q2m6H4KU=
github.com/jfrog/jfrog-client-go v1.48.0/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
7 changes: 7 additions & 0 deletions utils/coreutils/coreconsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const (
ServerID = "JFROG_CLI_SERVER_ID"
TransitiveDownload = "JFROG_CLI_TRANSITIVE_DOWNLOAD"

// These environment variables are used to adjust command names for more detailed tracking in the usage report.
// Set by the setup-jfrog-cli GitHub Action to identify specific command usage scenarios.
// True if an automatic build publication was triggered.
UsageAutoPublishedBuild = "JFROG_CLI_USAGE_AUTO_BUILD_PUBLISHED"
// True if the JFrog platform was configured using OIDC integration.
UsageOidcConfigured = "JFROG_CLI_USAGE_CONFIG_OIDC"

// Deprecated and replaced with TransitiveDownload
TransitiveDownloadExperimental = "JFROG_CLI_TRANSITIVE_DOWNLOAD_EXPERIMENTAL"
)
Expand Down

0 comments on commit 07af542

Please sign in to comment.