Skip to content

Commit

Permalink
fix: Added event for info command
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Jul 13, 2023
1 parent 884408e commit 455a8a7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
17 changes: 14 additions & 3 deletions cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ 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"
)

// 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
Expand All @@ -26,6 +29,8 @@ func NewInfoCommand(config *settings.Config, preRunE validator.Validator) *cobra
infoCommand := &cobra.Command{
Use: "info",
Short: "Check information associated to your user account.",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
},
}
orgInfoCmd := orgInfoCommand(client, opts)
infoCommand.AddCommand(orgInfoCmd)
Expand All @@ -41,7 +46,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`,
Expand Down
42 changes: 39 additions & 3 deletions cmd/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions telemetry/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 455a8a7

Please sign in to comment.