From c02a2e5277a39242156de8b8eafefc99899071be Mon Sep 17 00:00:00 2001 From: JulesFaucherre Date: Thu, 13 Jul 2023 09:51:44 +0200 Subject: [PATCH] fix: Added event for info command --- cmd/info/info.go | 18 +++++++++++++++--- cmd/info/info_test.go | 42 +++++++++++++++++++++++++++++++++++++++--- telemetry/events.go | 8 ++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/cmd/info/info.go b/cmd/info/info.go index 1ed3028fe..ade982b97 100644 --- a/cmd/info/info.go +++ b/cmd/info/info.go @@ -2,8 +2,10 @@ package info import ( "github.com/CircleCI-Public/circleci-cli/api/info" + "github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry" "github.com/CircleCI-Public/circleci-cli/cmd/validator" "github.com/CircleCI-Public/circleci-cli/settings" + "github.com/CircleCI-Public/circleci-cli/telemetry" "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" @@ -11,8 +13,9 @@ import ( // infoOptions info command options type infoOptions struct { - cfg *settings.Config - validator validator.Validator + cfg *settings.Config + validator validator.Validator + createTelemetry func() telemetry.Client } // NewInfoCommand information cobra command creation @@ -22,6 +25,9 @@ func NewInfoCommand(config *settings.Config, preRunE validator.Validator) *cobra opts := infoOptions{ cfg: config, validator: preRunE, + createTelemetry: func() telemetry.Client { + return create_telemetry.CreateTelemetry(config) + }, } infoCommand := &cobra.Command{ Use: "info", @@ -41,7 +47,13 @@ func orgInfoCommand(client info.InfoClient, opts infoOptions) *cobra.Command { Long: `View your Organizations' names and ids.`, PreRunE: opts.validator, RunE: func(cmd *cobra.Command, _ []string) error { - return getOrgInformation(cmd, client) + telemetryClient := opts.createTelemetry() + defer telemetryClient.Close() + + err := getOrgInformation(cmd, client) + _ = telemetryClient.Track(telemetry.CreateInfoEvent(create_telemetry.GetCommandInformation(cmd, true), err)) + + return err }, Annotations: make(map[string]string), Example: `circleci info org`, diff --git a/cmd/info/info_test.go b/cmd/info/info_test.go index 19bda1417..50a6854d2 100644 --- a/cmd/info/info_test.go +++ b/cmd/info/info_test.go @@ -7,8 +7,10 @@ import ( "net/http/httptest" "testing" + "github.com/CircleCI-Public/circleci-cli/clitest" "github.com/CircleCI-Public/circleci-cli/cmd/validator" "github.com/CircleCI-Public/circleci-cli/settings" + "github.com/CircleCI-Public/circleci-cli/telemetry" "github.com/spf13/cobra" "gotest.tools/v3/assert" ) @@ -106,6 +108,39 @@ func TestFailedValidator(t *testing.T) { assert.Error(t, err, errorMessage) } +func TestTelemetry(t *testing.T) { + tempSettings := clitest.WithTempSettings() + defer tempSettings.Close() + + // Test server + var serverHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`[{"id":"id", "name":"name"}]`)) + } + server := httptest.NewServer(serverHandler) + defer server.Close() + + // Test command + config := &settings.Config{ + Token: "testtoken", + HTTPClient: http.DefaultClient, + Host: server.URL, + MockTelemetry: tempSettings.TelemetryDestPath, + } + cmd := NewInfoCommand(config, nil) + + // Execute + err := cmd.Execute() + + assert.NilError(t, err) + clitest.CompareTelemetryEvent(tempSettings, []telemetry.Event{ + telemetry.CreateInfoEvent(telemetry.CommandInfo{ + Name: "org", + LocalArgs: map[string]string{"help": "false"}, + }, nil), + }) +} + func defaultValidator(cmd *cobra.Command, args []string) error { return nil } @@ -115,9 +150,10 @@ func scaffoldCMD( validator validator.Validator, ) (*cobra.Command, *bytes.Buffer, *bytes.Buffer) { config := &settings.Config{ - Token: "testtoken", - HTTPClient: http.DefaultClient, - Host: baseURL, + Token: "testtoken", + HTTPClient: http.DefaultClient, + Host: baseURL, + IsTelemetryDisabled: true, } cmd := NewInfoCommand(config, validator) diff --git a/telemetry/events.go b/telemetry/events.go index eb923d1a1..5e8362171 100644 --- a/telemetry/events.go +++ b/telemetry/events.go @@ -115,3 +115,11 @@ func CreateRunnerResourceClassEvent(cmdInfo CommandInfo) Event { func CreateRunnerToken(cmdInfo CommandInfo) Event { return createEventFromCommandInfo("runner-resource-class", cmdInfo) } + +func CreateInfoEvent(cmdInfo CommandInfo, err error) Event { + event := createEventFromCommandInfo("info", cmdInfo) + if err != nil { + event.Properties["error"] = err.Error() + } + return event +}