From 8d32b17573d8947cb18879a9d9bfef76fecb6c04 Mon Sep 17 00:00:00 2001 From: attiasas Date: Wed, 13 Sep 2023 11:25:04 +0300 Subject: [PATCH 1/3] don't send xray usage until available for all users --- artifactory/utils/dependenciesutils.go | 2 +- utils/usage/usage.go | 3 +++ utils/usage/usage_test.go | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/artifactory/utils/dependenciesutils.go b/artifactory/utils/dependenciesutils.go index 9591c532b..537f0fdf0 100644 --- a/artifactory/utils/dependenciesutils.go +++ b/artifactory/utils/dependenciesutils.go @@ -57,7 +57,7 @@ func DownloadAnalyzerManagerIfNeeded() error { downloadUrl := artDetails.ArtifactoryUrl + remotePath remoteFileDetails, _, err := client.GetRemoteFileDetails(downloadUrl, &httpClientDetails) if err != nil { - return errors.New("couldn't get remote file details for " + downloadUrl) + return errors.New("couldn't get remote file details for " + downloadUrl + ": " + err.Error()) } analyzerManagerDir, err := xrayutils.GetAnalyzerManagerDirAbsolutePath() if err != nil { diff --git a/utils/usage/usage.go b/utils/usage/usage.go index 96b6ae28d..490eb5e69 100644 --- a/utils/usage/usage.go +++ b/utils/usage/usage.go @@ -85,6 +85,9 @@ func (ur *UsageReporter) Report(features ...ReportFeature) { return } log.Debug(ReportUsagePrefix, "Sending info...") + // Xray service can only receive usage from admins. + // disable until fixed + ur.sendToXray = false if ur.sendToEcosystem { ur.reportWaitGroup.Go(func() (err error) { if err = ur.reportToEcosystem(features...); err != nil { diff --git a/utils/usage/usage_test.go b/utils/usage/usage_test.go index d67ceef25..1353ec8dc 100644 --- a/utils/usage/usage_test.go +++ b/utils/usage/usage_test.go @@ -160,6 +160,7 @@ func createArtifactoryUsageHandler(t *testing.T, productName, commandName string } func TestReportXrayUsage(t *testing.T) { + t.Skip() const productName = "test-product" const commandName = "test-command" const clientName = "test-client" @@ -178,6 +179,7 @@ func TestReportXrayUsage(t *testing.T) { } func TestReportXrayError(t *testing.T) { + t.Skip() reporter := NewUsageReporter("", &config.ServerDetails{}).SetSendToEcosystem(false).SetSendToArtifactory(false) reporter.Report(ReportFeature{ FeatureId: "", From d4e83c31e2cf7d63b6d527a4ffd2afbf970e270f Mon Sep 17 00:00:00 2001 From: attiasas Date: Wed, 13 Sep 2023 11:49:50 +0300 Subject: [PATCH 2/3] review changes --- artifactory/utils/dependenciesutils.go | 2 +- utils/usage/usage.go | 11 ----- utils/usage/usage_test.go | 60 -------------------------- 3 files changed, 1 insertion(+), 72 deletions(-) diff --git a/artifactory/utils/dependenciesutils.go b/artifactory/utils/dependenciesutils.go index 537f0fdf0..91fa38cd1 100644 --- a/artifactory/utils/dependenciesutils.go +++ b/artifactory/utils/dependenciesutils.go @@ -57,7 +57,7 @@ func DownloadAnalyzerManagerIfNeeded() error { downloadUrl := artDetails.ArtifactoryUrl + remotePath remoteFileDetails, _, err := client.GetRemoteFileDetails(downloadUrl, &httpClientDetails) if err != nil { - return errors.New("couldn't get remote file details for " + downloadUrl + ": " + err.Error()) + return fmt.Errorf("couldn't get remote file details for %s: %s", downloadUrl, err.Error()) } analyzerManagerDir, err := xrayutils.GetAnalyzerManagerDirAbsolutePath() if err != nil { diff --git a/utils/usage/usage.go b/utils/usage/usage.go index 490eb5e69..3539cbb2a 100644 --- a/utils/usage/usage.go +++ b/utils/usage/usage.go @@ -85,9 +85,6 @@ func (ur *UsageReporter) Report(features ...ReportFeature) { return } log.Debug(ReportUsagePrefix, "Sending info...") - // Xray service can only receive usage from admins. - // disable until fixed - ur.sendToXray = false if ur.sendToEcosystem { ur.reportWaitGroup.Go(func() (err error) { if err = ur.reportToEcosystem(features...); err != nil { @@ -96,14 +93,6 @@ func (ur *UsageReporter) Report(features ...ReportFeature) { return }) } - if ur.sendToXray { - ur.reportWaitGroup.Go(func() (err error) { - if err = ur.reportToXray(features...); err != nil { - err = fmt.Errorf("xray, %s", err.Error()) - } - return - }) - } if ur.sendToArtifactory { ur.reportWaitGroup.Go(func() (err error) { if err = ur.reportToArtifactory(features...); err != nil { diff --git a/utils/usage/usage_test.go b/utils/usage/usage_test.go index 1353ec8dc..657f19ba1 100644 --- a/utils/usage/usage_test.go +++ b/utils/usage/usage_test.go @@ -159,66 +159,6 @@ func createArtifactoryUsageHandler(t *testing.T, productName, commandName string } } -func TestReportXrayUsage(t *testing.T) { - t.Skip() - const productName = "test-product" - const commandName = "test-command" - const clientName = "test-client" - - server := httptest.NewServer(createXrayUsageHandler(t, productName, commandName, clientName)) - defer server.Close() - serverDetails := &config.ServerDetails{XrayUrl: server.URL + "/"} - - reporter := NewUsageReporter(productName, serverDetails).SetSendToEcosystem(false).SetSendToArtifactory(false) - - reporter.Report(ReportFeature{ - FeatureId: commandName, - ClientId: clientName, - }) - assert.NoError(t, reporter.WaitForResponses()) -} - -func TestReportXrayError(t *testing.T) { - t.Skip() - reporter := NewUsageReporter("", &config.ServerDetails{}).SetSendToEcosystem(false).SetSendToArtifactory(false) - reporter.Report(ReportFeature{ - FeatureId: "", - }) - assert.Error(t, reporter.WaitForResponses()) - - server := httptest.NewServer(create404UsageHandler(t)) - defer server.Close() - reporter = NewUsageReporter("", &config.ServerDetails{ArtifactoryUrl: server.URL + "/"}).SetSendToEcosystem(false).SetSendToArtifactory(false) - reporter.Report(ReportFeature{ - FeatureId: "", - }) - assert.Error(t, reporter.WaitForResponses()) -} - -func createXrayUsageHandler(t *testing.T, productId, commandName, clientId string) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/api/v1/system/version" { - w.WriteHeader(http.StatusOK) - _, err := w.Write([]byte(`{"xray_version":"6.9.0"}`)) - assert.NoError(t, err) - return - } - if r.RequestURI == "/api/v1/usage/events/send" { - // Check request - buf := new(bytes.Buffer) - _, err := buf.ReadFrom(r.Body) - assert.NoError(t, err) - featureId := xrayusage.GetExpectedXrayEventName(productId, commandName) - assert.Equal(t, fmt.Sprintf(`[{"data":{"clientId":"%s"},"product_name":"%s","event_name":"%s","origin":"API_CLI"}]`, clientId, productId, featureId), buf.String()) - - // Send response OK - w.WriteHeader(http.StatusOK) - _, err = w.Write([]byte("{}")) - assert.NoError(t, err) - } - } -} - func TestReportEcosystemUsageError(t *testing.T) { // No features reporter := NewUsageReporter("", &config.ServerDetails{}).SetSendToArtifactory(false).SetSendToXray(false) From a26d7643e6abc28836a018159f045aa865f8f0a3 Mon Sep 17 00:00:00 2001 From: attiasas Date: Wed, 13 Sep 2023 11:51:58 +0300 Subject: [PATCH 3/3] review changes --- utils/usage/usage.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/utils/usage/usage.go b/utils/usage/usage.go index 3539cbb2a..30f40f18c 100644 --- a/utils/usage/usage.go +++ b/utils/usage/usage.go @@ -2,8 +2,6 @@ package usage import ( "fmt" - xrayutils "github.com/jfrog/jfrog-cli-core/v2/xray/utils" - "golang.org/x/sync/errgroup" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" @@ -123,23 +121,6 @@ func (ur *UsageReporter) reportToEcosystem(features ...ReportFeature) (err error return ecosysusage.SendEcosystemUsageReports(reports...) } -func (ur *UsageReporter) reportToXray(features ...ReportFeature) (err error) { - if ur.serverDetails.XrayUrl == "" { - err = errorutils.CheckErrorf("Xray Url is not set.") - return - } - serviceManager, err := xrayutils.CreateXrayServiceManager(ur.serverDetails) - if err != nil { - return - } - events := ur.convertAttributesToXrayEvents(features...) - if len(events) == 0 { - err = errorutils.CheckErrorf("Nothing to send.") - return - } - return xrayusage.SendXrayUsageEvents(*serviceManager, events...) -} - func (ur *UsageReporter) reportToArtifactory(features ...ReportFeature) (err error) { if ur.serverDetails.ArtifactoryUrl == "" { err = errorutils.CheckErrorf("Artifactory Url is not set..")