Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send Xray usage until available for all users #953

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion artifactory/utils/dependenciesutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 fmt.Errorf("couldn't get remote file details for %s: %s", downloadUrl, err.Error())
}
analyzerManagerDir, err := xrayutils.GetAnalyzerManagerDirAbsolutePath()
if err != nil {
Expand Down
27 changes: 0 additions & 27 deletions utils/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -93,14 +91,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 {
Expand Down Expand Up @@ -131,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..")
Expand Down
58 changes: 0 additions & 58 deletions utils/usage/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,64 +159,6 @@ func createArtifactoryUsageHandler(t *testing.T, productName, commandName string
}
}

func TestReportXrayUsage(t *testing.T) {
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) {
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)
Expand Down