Skip to content

Commit 727911a

Browse files
authored
Add commands to usage report (#1299)
1 parent ea65564 commit 727911a

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

artifactory/commands/buildinfo/publish.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func (bpc *BuildPublishCommand) IsDetailedSummary() bool {
7070
}
7171

7272
func (bpc *BuildPublishCommand) CommandName() string {
73+
autoPublishedTriggered, err := clientutils.GetBoolEnvValue(coreutils.UsageAutoPublishedBuild, false)
74+
if err != nil {
75+
log.Warn("Failed to get the value of the environment variable: " + coreutils.UsageAutoPublishedBuild + ". " + err.Error())
76+
}
77+
if autoPublishedTriggered {
78+
return "rt_build_publish_auto"
79+
}
7380
return "rt_build_publish"
7481
}
7582

common/commands/command.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ func reportUsage(command Command, channel chan<- bool) {
5858

5959
// Set to true when the report usage func exits
6060
func signalReportUsageFinished(ch chan<- bool) {
61-
ch <- true
61+
if ch != nil {
62+
ch <- true
63+
}
6264
}

common/commands/config.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const (
4141
WebLogin AuthenticationMethod = "Web Login"
4242
)
4343

44+
const (
45+
// Indicates that the config command uses OIDC authentication.
46+
configOidcCommandName = "config_oidc"
47+
// Default config command name.
48+
configCommandName = "config"
49+
)
50+
4451
// Internal golang locking for the same process.
4552
var mutex sync.Mutex
4653

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

145152
func (cc *ConfigCommand) CommandName() string {
146-
return "config"
153+
oidcConfigured, err := clientUtils.GetBoolEnvValue(coreutils.UsageOidcConfigured, false)
154+
if err != nil {
155+
log.Warn("Failed to get the value of the environment variable: " + coreutils.UsageAutoPublishedBuild + ". " + err.Error())
156+
}
157+
if oidcConfigured {
158+
return configOidcCommandName
159+
}
160+
return configCommandName
161+
}
162+
163+
// ExecAndReportUsage runs the ConfigCommand and then triggers a usage report if needed,
164+
// Report usage only if OIDC integration was used
165+
// Usage must be sent after command execution as we need the server details to be set.
166+
func (cc *ConfigCommand) ExecAndReportUsage() (err error) {
167+
if err = cc.Run(); err != nil {
168+
return
169+
}
170+
reportUsage(cc, nil)
171+
return
147172
}
148173

149174
func (cc *ConfigCommand) config() error {

common/commands/config_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"encoding/json"
5+
testsUtils "github.com/jfrog/jfrog-client-go/utils/tests"
56
"os"
67
"testing"
78

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

348+
func TestCommandName(t *testing.T) {
349+
// Clean up
350+
defer func() {
351+
testsUtils.UnSetEnvAndAssert(t, coreutils.UsageOidcConfigured)
352+
}()
353+
cc := NewConfigCommand(AddOrEdit, testServerId)
354+
// Test when the environment variable is not set
355+
assert.Equal(t, configCommandName, cc.CommandName())
356+
// Test when the environment variable is set
357+
testsUtils.SetEnvWithCallbackAndAssert(t, coreutils.UsageOidcConfigured, "true")
358+
assert.Equal(t, configOidcCommandName, cc.CommandName())
359+
}
360+
361+
func TestConfigCommand_ExecAndReportUsage(t *testing.T) {
362+
defer func() {
363+
testsUtils.UnSetEnvAndAssert(t, coreutils.UsageOidcConfigured)
364+
}()
365+
// Define test scenarios
366+
testCases := []struct {
367+
name string
368+
envVarValue string // Environment variable value to set (or empty to unset)
369+
expectedName string // Expected command name
370+
expectError bool // Whether an error is expected
371+
}{
372+
{
373+
name: "With usage report",
374+
envVarValue: "TRUE",
375+
expectedName: configOidcCommandName,
376+
},
377+
{
378+
name: "Without usage report",
379+
envVarValue: "", // Empty to unset the environment variable
380+
expectedName: configCommandName,
381+
},
382+
}
383+
384+
for _, tc := range testCases {
385+
t.Run(tc.name, func(t *testing.T) {
386+
// Set or unset the environment variable
387+
if tc.envVarValue != "" {
388+
err := os.Setenv(coreutils.UsageOidcConfigured, tc.envVarValue)
389+
assert.NoError(t, err)
390+
} else {
391+
err := os.Unsetenv(coreutils.UsageOidcConfigured)
392+
assert.NoError(t, err)
393+
}
394+
395+
// Initialize the command and check the expected command name
396+
cc := NewConfigCommand(AddOrEdit, testServerId)
397+
assert.Equal(t, tc.expectedName, cc.CommandName())
398+
399+
// Execute and validate no errors
400+
err := cc.ExecAndReportUsage()
401+
assert.NoError(t, err)
402+
})
403+
}
404+
}
405+
347406
func testExportImport(t *testing.T, inputDetails *config.ServerDetails) {
348407
configToken, err := config.Export(inputDetails)
349408
assert.NoError(t, err)

utils/coreutils/coreconsts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const (
5050
ServerID = "JFROG_CLI_SERVER_ID"
5151
TransitiveDownload = "JFROG_CLI_TRANSITIVE_DOWNLOAD"
5252

53+
// These environment variables are used to adjust command names for more detailed tracking in the usage report.
54+
// Set by the setup-jfrog-cli GitHub Action to identify specific command usage scenarios.
55+
// True if an automatic build publication was triggered.
56+
UsageAutoPublishedBuild = "JFROG_CLI_USAGE_AUTO_BUILD_PUBLISHED"
57+
// True if the JFrog platform was configured using OIDC integration.
58+
UsageOidcConfigured = "JFROG_CLI_USAGE_CONFIG_OIDC"
59+
5360
// Deprecated and replaced with TransitiveDownload
5461
TransitiveDownloadExperimental = "JFROG_CLI_TRANSITIVE_DOWNLOAD_EXPERIMENTAL"
5562
)

0 commit comments

Comments
 (0)