From 03105182b715e9a10ac417ca7d3213caefbada1e Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Fri, 20 Dec 2024 22:49:35 +0200 Subject: [PATCH 1/9] Send command name --- .../commands/transferfiles/transfer.go | 10 +-- artifactory/utils/utils.go | 26 ++++++- common/commands/command.go | 75 +++++++++++++------ go.mod | 2 +- go.sum | 14 +--- utils/config/config.go | 17 +++-- utils/usage/usage.go | 14 ++-- 7 files changed, 105 insertions(+), 53 deletions(-) diff --git a/artifactory/commands/transferfiles/transfer.go b/artifactory/commands/transferfiles/transfer.go index 4e2324c48..f303c1ca9 100644 --- a/artifactory/commands/transferfiles/transfer.go +++ b/artifactory/commands/transferfiles/transfer.go @@ -5,8 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/jfrog/gofrog/safeconvert" - "github.com/jfrog/jfrog-client-go/artifactory/services" "os" "os/signal" "path/filepath" @@ -14,6 +12,9 @@ import ( "strings" "syscall" + "github.com/jfrog/gofrog/safeconvert" + "github.com/jfrog/jfrog-client-go/artifactory/services" + "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles/state" "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils/precheckrunner" @@ -266,7 +267,7 @@ func (tdc *TransferFilesCommand) initStateManager(allSourceLocalRepos, sourceBui } func (tdc *TransferFilesCommand) reportTransferFilesUsage() { - log.Debug(usageReporter.ReportUsagePrefix, "Sending Transfer Files info...") + log.Debug(usageReporter.ArtifactoryCallHomePrefix, "Sending Transfer Files info...") sourceStorageInfo, err := tdc.sourceStorageInfoManager.GetStorageInfo() if err != nil { log.Debug(err.Error()) @@ -288,8 +289,7 @@ func (tdc *TransferFilesCommand) reportTransferFilesUsage() { AttributeValue: sourceStorageInfo.BinariesSize, }, } - err = usage.SendReportUsage(coreutils.GetCliUserAgent(), tdc.CommandName(), tdc.targetStorageInfoManager.GetServiceManager(), reportUsageAttributes...) - if err != nil { + if err = usage.NewArtifactoryCallHome().SendUsage(coreutils.GetCliUserAgent(), tdc.CommandName(), tdc.targetStorageInfoManager.GetServiceManager(), reportUsageAttributes...); err != nil { log.Debug(err.Error()) } } diff --git a/artifactory/utils/utils.go b/artifactory/utils/utils.go index f105cae26..68831475e 100644 --- a/artifactory/utils/utils.go +++ b/artifactory/utils/utils.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "errors" - ioutils "github.com/jfrog/gofrog/io" - "github.com/jfrog/jfrog-client-go/evidence" "io" "net/http" "net/url" @@ -16,6 +14,9 @@ import ( "strings" "time" + ioutils "github.com/jfrog/gofrog/io" + "github.com/jfrog/jfrog-client-go/evidence" + "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" "github.com/jfrog/jfrog-client-go/access" @@ -24,6 +25,7 @@ import ( clientConfig "github.com/jfrog/jfrog-client-go/config" "github.com/jfrog/jfrog-client-go/distribution" "github.com/jfrog/jfrog-client-go/http/httpclient" + "github.com/jfrog/jfrog-client-go/jfconnect" "github.com/jfrog/jfrog-client-go/lifecycle" "github.com/jfrog/jfrog-client-go/metadata" clientUtils "github.com/jfrog/jfrog-client-go/utils" @@ -260,6 +262,26 @@ func CreateMetadataServiceManager(serviceDetails *config.ServerDetails, isDryRun return metadata.NewManager(serviceConfig) } +func CreateJfConnectServiceManager(serverDetails *config.ServerDetails) (jfconnect.Manager, error) { + certsPath, err := coreutils.GetJfrogCertsDir() + if err != nil { + return nil, err + } + jfConnectAuth, err := serverDetails.CreateJfConnectAuthConfig() + if err != nil { + return nil, err + } + serviceConfig, err := clientConfig.NewConfigBuilder(). + SetServiceDetails(jfConnectAuth). + SetCertificatesPath(certsPath). + SetInsecureTls(serverDetails.InsecureTls). + Build() + if err != nil { + return nil, err + } + return jfconnect.NewManager(serviceConfig) +} + // This error indicates that the build was scanned by Xray, but Xray found issues with the build. // If Xray failed to scan the build, for example due to a networking issue, a regular error should be returned. var errBuildScan = errors.New("issues found during xray build scan") diff --git a/common/commands/command.go b/common/commands/command.go index 34d4fc6bf..a95cc4632 100644 --- a/common/commands/command.go +++ b/common/commands/command.go @@ -1,9 +1,12 @@ package commands import ( + "sync" + "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" + coreusage "github.com/jfrog/jfrog-cli-core/v2/utils/usage" usageReporter "github.com/jfrog/jfrog-cli-core/v2/utils/usage" "github.com/jfrog/jfrog-client-go/artifactory/usage" "github.com/jfrog/jfrog-client-go/utils/log" @@ -30,29 +33,59 @@ func Exec(command Command) error { } func reportUsage(command Command, channel chan<- bool) { + // When the usage reporting is donw, signal to the channel. defer signalReportUsageFinished(channel) - reportUsage := usageReporter.ShouldReportUsage() - if reportUsage { - serverDetails, err := command.ServerDetails() - if err != nil { - log.Debug(usageReporter.ReportUsagePrefix, err.Error()) - return - } - if serverDetails != nil && serverDetails.ArtifactoryUrl != "" { - log.Debug(usageReporter.ReportUsagePrefix, "Sending info...") - serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false) - if err != nil { - log.Debug(usageReporter.ReportUsagePrefix, err.Error()) - return - } - err = usage.SendReportUsage(coreutils.GetCliUserAgent(), command.CommandName(), serviceManager) - if err != nil { - log.Debug(err.Error()) - return - } + + if !usageReporter.ShouldReportUsage() { + log.Debug("Usage reporting is disabled") + return + } + + serverDetails, err := command.ServerDetails() + if err != nil { + log.Debug("Usage reporting:", err.Error()) + return + } + + if serverDetails != nil { + var wg sync.WaitGroup + + // Repotr the usage to Artifactory's Call Home API. + if serverDetails.ArtifactoryUrl != "" { + wg.Add(1) + go func() { + defer wg.Done() + reportUsageToArtifactoryCallHome(command, serverDetails) + }() } - } else { - log.Debug("Usage info is disabled.") + + // Repotr the usage to the Visibility System. + wg.Add(1) + go func() { + defer wg.Done() + reportUsageToVisibilitySystem(command, serverDetails) + }() + + // Wait for the two report actions to finish. + wg.Wait() + } +} + +func reportUsageToVisibilitySystem(command Command, serverDetails *config.ServerDetails) { + if err := coreusage.NewVisibilitySystemManager(serverDetails).SendUsage(command.CommandName()); err != nil { + log.Debug("Visibility System Usage reporting:", err.Error()) + } +} + +func reportUsageToArtifactoryCallHome(command Command, serverDetails *config.ServerDetails) { + log.Debug(usageReporter.ArtifactoryCallHomePrefix, "Sending info...") + serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false) + if err != nil { + log.Debug(usageReporter.ArtifactoryCallHomePrefix, err.Error()) + return + } + if err = usage.NewArtifactoryCallHome().SendUsage(coreutils.GetCliUserAgent(), command.CommandName(), serviceManager); err != nil { + log.Debug(err.Error()) } } diff --git a/go.mod b/go.mod index e9de07281..3454bcf4f 100644 --- a/go.mod +++ b/go.mod @@ -96,7 +96,7 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) -// replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241103083749-45c13ff7fe16 +replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453 // replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd diff --git a/go.sum b/go.sum index 74666008c..77e4b0110 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,8 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453 h1:yA0LCoCKjZlSUDsh9W7Dsgg6lcvRpRAe9pq1wJojgIA= +github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453/go.mod h1:3rdSCzKoN1l3wklL33D7nu+nB3089rIgxwyoprFdCaw= github.com/forPelevin/gomoji v1.2.0 h1:9k4WVSSkE1ARO/BWywxgEUBvR/jMnao6EZzrql5nxJ8= github.com/forPelevin/gomoji v1.2.0/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -93,8 +95,6 @@ github.com/jfrog/build-info-go v1.10.6 h1:zH1ZhXlVfi5DlFyunygHjrdOcnv5qxfeLqmsfD github.com/jfrog/build-info-go v1.10.6/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.48.3 h1:HJpKGul0f/S2i7Uf7K/GwS1EUGiirt1LWXL1lanKNhU= -github.com/jfrog/jfrog-client-go v1.48.3/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= @@ -222,8 +222,6 @@ go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= -golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo= @@ -234,8 +232,6 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= -golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -256,18 +252,12 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= -golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= -golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= -golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/utils/config/config.go b/utils/config/config.go index 010e2ef0d..c3a15cdfd 100644 --- a/utils/config/config.go +++ b/utils/config/config.go @@ -4,6 +4,12 @@ import ( "bytes" "encoding/json" "errors" + "os" + "path/filepath" + "strconv" + "strings" + "time" + "github.com/buger/jsonparser" biutils "github.com/jfrog/build-info-go/utils" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" @@ -22,11 +28,6 @@ import ( "github.com/jfrog/jfrog-client-go/utils/log" xrayAuth "github.com/jfrog/jfrog-client-go/xray/auth" xscAuth "github.com/jfrog/jfrog-client-go/xsc/auth" - "os" - "path/filepath" - "strconv" - "strings" - "time" ) func init() { @@ -747,6 +748,12 @@ func (serverDetails *ServerDetails) CreateAccessAuthConfig() (auth.ServiceDetail return serverDetails.createAuthConfig(pAuth) } +func (serverDetails *ServerDetails) CreateJfConnectAuthConfig() (auth.ServiceDetails, error) { + pAuth := accessAuth.NewAccessDetails() + pAuth.SetUrl(utils.AddTrailingSlashIfNeeded(serverDetails.Url) + "jfconnect/") + return serverDetails.createAuthConfig(pAuth) +} + func (serverDetails *ServerDetails) CreateLifecycleAuthConfig() (auth.ServiceDetails, error) { lcAuth := lifecycleAuth.NewLifecycleDetails() lcAuth.SetUrl(serverDetails.LifecycleUrl) diff --git a/utils/usage/usage.go b/utils/usage/usage.go index f84dc54ea..b73cfd2b4 100644 --- a/utils/usage/usage.go +++ b/utils/usage/usage.go @@ -18,8 +18,8 @@ import ( ) const ( - ReportUsagePrefix = "Usage Report:" - clientIdAttributeName = "clientId" + ArtifactoryCallHomePrefix = "Artifactory Call Home:" + clientIdAttributeName = "clientId" ) type UsageReporter struct { @@ -57,7 +57,7 @@ func NewUsageReporter(productId string, serverDetails *config.ServerDetails) *Us func ShouldReportUsage() (reportUsage bool) { reportUsage, err := clientutils.GetBoolEnvValue(coreutils.ReportUsage, true) if err != nil { - log.Debug(ReportUsagePrefix + err.Error()) + log.Debug(ArtifactoryCallHomePrefix + err.Error()) return false } return reportUsage @@ -85,10 +85,10 @@ func (ur *UsageReporter) Report(features ...ReportFeature) { return } if len(features) == 0 { - log.Debug(ReportUsagePrefix, "Nothing to send.") + log.Debug(ArtifactoryCallHomePrefix, "Nothing to send.") return } - log.Debug(ReportUsagePrefix, "Sending info...") + log.Debug(ArtifactoryCallHomePrefix, "Sending info...") if ur.sendToEcosystem { ur.reportWaitGroup.Go(func() (err error) { if err = ur.reportToEcosystem(features...); err != nil { @@ -117,7 +117,7 @@ func (ur *UsageReporter) Report(features ...ReportFeature) { func (ur *UsageReporter) WaitForResponses() (err error) { if err = ur.reportWaitGroup.Wait(); err != nil { - err = fmt.Errorf("%s %s", ReportUsagePrefix, err.Error()) + err = fmt.Errorf("%s %s", ArtifactoryCallHomePrefix, err.Error()) } return } @@ -169,7 +169,7 @@ func (ur *UsageReporter) reportToArtifactory(features ...ReportFeature) (err err if err != nil { return } - return usage.ReportUsageToArtifactory(ur.ProductId, serviceManager, converted...) + return usage.NewArtifactoryCallHome().SendUsageToArtifactory(ur.ProductId, serviceManager, converted...) } func convertAttributesToMap(reportFeature ReportFeature) (converted map[string]string) { From 26fe4681fcfd42ad9d2db5e0b6ea9acdfed15a01 Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Fri, 20 Dec 2024 23:02:31 +0200 Subject: [PATCH 2/9] Add missing file --- utils/usage/visibility_system_manager.go | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 utils/usage/visibility_system_manager.go diff --git a/utils/usage/visibility_system_manager.go b/utils/usage/visibility_system_manager.go new file mode 100644 index 000000000..4554e21ec --- /dev/null +++ b/utils/usage/visibility_system_manager.go @@ -0,0 +1,68 @@ +package usage + +import ( + "encoding/json" + "os" + + "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" + "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" +) + +type VisibilitySystemManager struct { + serverDetails *config.ServerDetails +} + +func NewVisibilitySystemManager(serverDetails *config.ServerDetails) *VisibilitySystemManager { + return &VisibilitySystemManager{ + serverDetails: serverDetails, + } +} + +type labels struct { + ProductID string `json:"product_id"` + FeatureID string `json:"feature_id"` + OIDCUsed string `json:"oidc_used"` + JobID string `json:"job_id"` + RunID string `json:"run_id"` + GitRepo string `json:"git_repo"` + GhTokenForCodeScanningAlertsProvided string `json:"gh_token_for_code_scanning_alerts_provided"` +} + +type visibilityMetric struct { + Value int `json:"value"` + MetricsName string `json:"metrics_name"` + Labels labels `json:"labels"` +} + +func createMetric(commandName string) ([]byte, error) { + metricLabels := labels{ + ProductID: coreutils.GetCliUserAgentName(), + FeatureID: commandName, + OIDCUsed: os.Getenv("JFROG_CLI_USAGE_OIDC_USED"), + JobID: os.Getenv("JFROG_CLI_USAGE_JOB_ID"), + RunID: os.Getenv("JFROG_CLI_USAGE_RUN_ID"), + GitRepo: os.Getenv("JFROG_CLI_USAGE_GIT_REPO"), + GhTokenForCodeScanningAlertsProvided: os.Getenv("JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED"), + } + + metric := visibilityMetric{ + Value: 1, + MetricsName: "jfcli_commands_countaa", + Labels: metricLabels, + } + + return json.Marshal(metric) +} + +func (vsm *VisibilitySystemManager) SendUsage(commandName string) error { + manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails) + if err != nil { + return err + } + metric, err := createMetric(commandName) + if err != nil { + return err + } + return manager.LogMetric(metric) +} From 3fc3c0e491cac85ef25e2cb6bacfc83bc0e12f60 Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Fri, 20 Dec 2024 23:22:19 +0200 Subject: [PATCH 3/9] Unit test --- utils/usage/visibility_system_manager_test.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 utils/usage/visibility_system_manager_test.go diff --git a/utils/usage/visibility_system_manager_test.go b/utils/usage/visibility_system_manager_test.go new file mode 100644 index 000000000..b1e99e6d9 --- /dev/null +++ b/utils/usage/visibility_system_manager_test.go @@ -0,0 +1,52 @@ +package usage_test + +import ( + "testing" + + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" + "github.com/jfrog/jfrog-cli-core/v2/utils/testsutils" + "github.com/stretchr/testify/assert" +) + +func TestCreateMetric(t *testing.T) { + // Set environment variables for the test using SetEnvWithCallbackAndAssert + envVars := map[string]string{ + "JFROG_CLI_USAGE_OIDC_USED": "true", + "JFROG_CLI_USAGE_JOB_ID": "job123", + "JFROG_CLI_USAGE_RUN_ID": "run456", + "JFROG_CLI_USAGE_GIT_REPO": "test-repo", + "JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED": "true", + } + cleanupFuncs := []func(){} + for key, value := range envVars { + cleanup := testsutils.SetEnvWithCallbackAndAssert(t, key, value) + cleanupFuncs = append(cleanupFuncs, cleanup) + } + defer func() { + for _, cleanup := range cleanupFuncs { + cleanup() + } + }() + + commandName := "testCommand" + metric, err := createMetric(commandName) + assert.NoError(t, err) + + // Define the expected JSON structure + expectedJSON := `{ + "value": 1, + "metrics_name": "jfcli_commands_countaa", + "labels": { + "product_id": "` + coreutils.GetCliUserAgentName() + `", + "feature_id": "testCommand", + "oidc_used": "true", + "job_id": "job123", + "run_id": "run456", + "git_repo": "https://github.com/jfrog/test-repo", + "gh_token_for_code_scanning_alerts_provided": "true" + } + }` + + // Compare the generated JSON to the expected JSON + assert.JSONEq(t, expectedJSON, string(metric)) +} From 113b625cc90cc9240b1e7d1bcf6945b9a81d2abe Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Fri, 20 Dec 2024 23:41:01 +0200 Subject: [PATCH 4/9] Fix test --- utils/usage/visibility_system_manager.go | 4 ++-- utils/usage/visibility_system_manager_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/usage/visibility_system_manager.go b/utils/usage/visibility_system_manager.go index 4554e21ec..978146eb8 100644 --- a/utils/usage/visibility_system_manager.go +++ b/utils/usage/visibility_system_manager.go @@ -35,7 +35,7 @@ type visibilityMetric struct { Labels labels `json:"labels"` } -func createMetric(commandName string) ([]byte, error) { +func (vsm *VisibilitySystemManager) createMetric(commandName string) ([]byte, error) { metricLabels := labels{ ProductID: coreutils.GetCliUserAgentName(), FeatureID: commandName, @@ -60,7 +60,7 @@ func (vsm *VisibilitySystemManager) SendUsage(commandName string) error { if err != nil { return err } - metric, err := createMetric(commandName) + metric, err := vsm.createMetric(commandName) if err != nil { return err } diff --git a/utils/usage/visibility_system_manager_test.go b/utils/usage/visibility_system_manager_test.go index b1e99e6d9..3f91bc31a 100644 --- a/utils/usage/visibility_system_manager_test.go +++ b/utils/usage/visibility_system_manager_test.go @@ -1,10 +1,10 @@ -package usage_test +package usage import ( "testing" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" - "github.com/jfrog/jfrog-cli-core/v2/utils/testsutils" + testsutils "github.com/jfrog/jfrog-client-go/utils/tests" "github.com/stretchr/testify/assert" ) @@ -29,7 +29,7 @@ func TestCreateMetric(t *testing.T) { }() commandName := "testCommand" - metric, err := createMetric(commandName) + metric, err := NewVisibilitySystemManager(nil).createMetric(commandName) assert.NoError(t, err) // Define the expected JSON structure @@ -42,7 +42,7 @@ func TestCreateMetric(t *testing.T) { "oidc_used": "true", "job_id": "job123", "run_id": "run456", - "git_repo": "https://github.com/jfrog/test-repo", + "git_repo": "test-repo", "gh_token_for_code_scanning_alerts_provided": "true" } }` From 148d6c218aaed5cdea1437430f6e2229a860859b Mon Sep 17 00:00:00 2001 From: Eyal Ben Moshe Date: Sat, 21 Dec 2024 13:05:45 +0200 Subject: [PATCH 5/9] Update common/commands/command.go Co-authored-by: Michael Sverdlov --- common/commands/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/commands/command.go b/common/commands/command.go index a95cc4632..6d50ca844 100644 --- a/common/commands/command.go +++ b/common/commands/command.go @@ -50,7 +50,7 @@ func reportUsage(command Command, channel chan<- bool) { if serverDetails != nil { var wg sync.WaitGroup - // Repotr the usage to Artifactory's Call Home API. + // Report the usage to Artifactory's Call Home API. if serverDetails.ArtifactoryUrl != "" { wg.Add(1) go func() { From 7900dea328629e08da4db4425800e7fbc2bd4f7c Mon Sep 17 00:00:00 2001 From: Eyal Ben Moshe Date: Sat, 21 Dec 2024 13:05:56 +0200 Subject: [PATCH 6/9] Update common/commands/command.go Co-authored-by: Michael Sverdlov --- common/commands/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/commands/command.go b/common/commands/command.go index 6d50ca844..3186244ec 100644 --- a/common/commands/command.go +++ b/common/commands/command.go @@ -59,7 +59,7 @@ func reportUsage(command Command, channel chan<- bool) { }() } - // Repotr the usage to the Visibility System. + // Report the usage to the Visibility System. wg.Add(1) go func() { defer wg.Done() From 1f097723b1b430cd838aa6b56675a8a717a31c6b Mon Sep 17 00:00:00 2001 From: Eyal Ben Moshe Date: Sat, 21 Dec 2024 13:06:15 +0200 Subject: [PATCH 7/9] Update common/commands/command.go Co-authored-by: Michael Sverdlov --- common/commands/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/commands/command.go b/common/commands/command.go index 3186244ec..52bd83cf6 100644 --- a/common/commands/command.go +++ b/common/commands/command.go @@ -33,7 +33,7 @@ func Exec(command Command) error { } func reportUsage(command Command, channel chan<- bool) { - // When the usage reporting is donw, signal to the channel. + // When the usage reporting is done, signal to the channel. defer signalReportUsageFinished(channel) if !usageReporter.ShouldReportUsage() { From e50c984eefc7794632026edda95cac6efbf6fc6b Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Sat, 21 Dec 2024 13:34:50 +0200 Subject: [PATCH 8/9] CR comments --- go.mod | 6 +++--- go.sum | 8 ++++---- utils/usage/visibility_system_manager.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 3454bcf4f..b7a19a152 100644 --- a/go.mod +++ b/go.mod @@ -12,9 +12,9 @@ require ( github.com/google/uuid v1.6.0 github.com/gookit/color v1.5.4 github.com/jedib0t/go-pretty/v6 v6.6.3 - github.com/jfrog/build-info-go v1.10.6 + github.com/jfrog/build-info-go v1.10.7 github.com/jfrog/gofrog v1.7.6 - github.com/jfrog/jfrog-client-go v1.48.3 + github.com/jfrog/jfrog-client-go v1.48.4 github.com/magiconair/properties v1.8.7 github.com/manifoldco/promptui v0.9.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c @@ -96,7 +96,7 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) -replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453 +// replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453 // replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd diff --git a/go.sum b/go.sum index 77e4b0110..02ff60664 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,6 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= -github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453 h1:yA0LCoCKjZlSUDsh9W7Dsgg6lcvRpRAe9pq1wJojgIA= -github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453/go.mod h1:3rdSCzKoN1l3wklL33D7nu+nB3089rIgxwyoprFdCaw= github.com/forPelevin/gomoji v1.2.0 h1:9k4WVSSkE1ARO/BWywxgEUBvR/jMnao6EZzrql5nxJ8= github.com/forPelevin/gomoji v1.2.0/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -91,10 +89,12 @@ github.com/jedib0t/go-pretty/v6 v6.6.3 h1:nGqgS0tgIO1Hto47HSaaK4ac/I/Bu7usmdD3qv github.com/jedib0t/go-pretty/v6 v6.6.3/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI= github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw= -github.com/jfrog/build-info-go v1.10.6 h1:zH1ZhXlVfi5DlFyunygHjrdOcnv5qxfeLqmsfD4+lc4= -github.com/jfrog/build-info-go v1.10.6/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE= +github.com/jfrog/build-info-go v1.10.7 h1:10NVHYg0193gJpQft+S4WQfvYMtj5jlwwhJRvkFJtBE= +github.com/jfrog/build-info-go v1.10.7/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.48.4 h1:uXvBr2ebFKpBRUhWgC9TSSJe32IbSYGlbDp9tDzBcaY= +github.com/jfrog/jfrog-client-go v1.48.4/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec= 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= diff --git a/utils/usage/visibility_system_manager.go b/utils/usage/visibility_system_manager.go index 978146eb8..aa1cd78f2 100644 --- a/utils/usage/visibility_system_manager.go +++ b/utils/usage/visibility_system_manager.go @@ -48,7 +48,7 @@ func (vsm *VisibilitySystemManager) createMetric(commandName string) ([]byte, er metric := visibilityMetric{ Value: 1, - MetricsName: "jfcli_commands_countaa", + MetricsName: "jfcli_commands_count", Labels: metricLabels, } @@ -64,5 +64,5 @@ func (vsm *VisibilitySystemManager) SendUsage(commandName string) error { if err != nil { return err } - return manager.LogMetric(metric) + return manager.PostMetric(metric) } From f7614cacb6452667a8788563c0e823a5f07c11bb Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Sat, 21 Dec 2024 13:40:11 +0200 Subject: [PATCH 9/9] Fix test --- utils/usage/visibility_system_manager_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/usage/visibility_system_manager_test.go b/utils/usage/visibility_system_manager_test.go index 3f91bc31a..d5888fb4d 100644 --- a/utils/usage/visibility_system_manager_test.go +++ b/utils/usage/visibility_system_manager_test.go @@ -35,7 +35,7 @@ func TestCreateMetric(t *testing.T) { // Define the expected JSON structure expectedJSON := `{ "value": 1, - "metrics_name": "jfcli_commands_countaa", + "metrics_name": "jfcli_commands_count", "labels": { "product_id": "` + coreutils.GetCliUserAgentName() + `", "feature_id": "testCommand",