Skip to content

Commit

Permalink
Add commands to usage report (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored Nov 13, 2024
1 parent ea65564 commit 727911a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 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
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 727911a

Please sign in to comment.