-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: track cli command run events #189
feat: track cli command run events #189
Conversation
cb73d5c
to
17aa87c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests?
a07077a
to
859a723
Compare
cmd/environments/get_test.go
Outdated
t.Run("will track analytics for 'CLI Command Run' event", func(t *testing.T) { | ||
id := "test-id" | ||
mockedTrackingArgs := []interface{}{ | ||
"testAccessToken", | ||
"http://test.com", | ||
"CLI Command Run", | ||
map[string]interface{}{ | ||
"name": "environments", | ||
"action": "get", | ||
"baseURI": "http://test.com", | ||
"id": id, | ||
"flags": []string{"access-token", "base-uri", "environment", "project"}, | ||
}, | ||
} | ||
|
||
client := environments.MockClient{} | ||
client. | ||
On("Get", mockArgs...). | ||
Return([]byte(cmd.ValidResponse), nil) | ||
clients := cmd.APIClients{ | ||
EnvironmentsClient: &client, | ||
} | ||
tracker := analytics.MockTracker{ID: id} | ||
tracker.On("SendEvent", mockedTrackingArgs...) | ||
|
||
args := []string{ | ||
"environments", "get", | ||
"--access-token", "testAccessToken", | ||
"--base-uri", "http://test.com", | ||
"--environment", "test-env", | ||
"--project", "test-proj", | ||
} | ||
|
||
_, err := cmd.CallCmd(t, clients, &tracker, args) | ||
tracker.AssertCalled(t, "SendEvent", mockedTrackingArgs...) | ||
require.NoError(t, err) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests for commands
internal/analytics/event_utils.go
Outdated
|
||
func CmdRunEventProperties(cmd *cobra.Command, name string) map[string]interface{} { | ||
id := uuid.New() | ||
baseURI := viper.GetString(cliflags.BaseURIFlag) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you pass this value in instead of using viper? It removes the dependency.
cmd/projects/list_test.go
Outdated
|
||
assert.EqualError(t, err, "base-uri is invalid"+errorHelp) | ||
}) | ||
|
||
t.Run("will track analytics for 'CLI Command Run' event", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the quotes for these tests? It messes up the test name when running with -v
.
cmd/environments/environments.go
Outdated
cmd.AddCommand(getCmd) | ||
|
||
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you set this when we create cmd
above? Otherwise it seems like it's doing something special that wouldn't work if it were set at the same time as the other fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update this for all the commands?
internal/analytics/event_utils.go
Outdated
@@ -0,0 +1,30 @@ | |||
package analytics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename this file to be events.go
since "utils" doesn't add any meaning?
cmd/cliflags/baseuri.go
Outdated
@@ -0,0 +1,3 @@ | |||
package cliflags | |||
|
|||
const BaseURIDefault = "https://app.launchdarkly.com" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs to be its own file.
cmd/environments/get_test.go
Outdated
|
||
t.Run("will track analytics for 'CLI Command Run' event", func(t *testing.T) { | ||
id := "test-id" | ||
mockedTrackingArgs := []interface{}{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could remove some duplication by extracting this to a helper function.
// analytics/client.go
func MockedTracker(name string, action string, flags []string) *MockTracker {
id := "test-id"
mockedTrackingArgs := []interface{}{
"testAccessToken",
"http://test.com",
"CLI Command Run",
map[string]interface{}{
"action": action,
"baseURI": "http://test.com",
"flags": flags,
"id": id,
"name": name,
},
}
tracker := MockTracker{ID: id}
tracker.On("SendEvent", mockedTrackingArgs...)
return &tracker
}
and call it in the tests like
tracker := analytics.MockedTracker(
"flags",
"get",
[]string{"access-token", "base-uri", "environment", "flag", "project"}, // these need to be alphabetized
)
cmd/cliflags/flags.go
Outdated
@@ -10,3 +11,5 @@ const ( | |||
ProjectFlag = "project" | |||
RoleFlag = "role" | |||
) | |||
|
|||
const BaseURIDefault = "https://app.launchdarkly.com" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you define these with the other constants? It could be after a linebreak but in the same const()
declaration.
cmd/environments/environments.go
Outdated
cmd.AddCommand(getCmd) | ||
|
||
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update this for all the commands?
cmd/flags/update_test.go
Outdated
@@ -285,7 +280,7 @@ func TestToggle(t *testing.T) { | |||
"--environment", "test-env-key", | |||
} | |||
|
|||
_, err := cmd.CallCmd(t, clients, &tracker, args) | |||
_, err := cmd.CallCmd(t, clients, tracker, args) | |||
tracker.AssertCalled(t, "SendEvent", mockedTrackingArgs...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get rid of this assertion since the call in MockedTracker
will fail if it doesn't get the right input. You can test that out by changing the input in the production code to see the test fail without this one. Then you wouldn't need to return the mocked args.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that makes sense. I'll also get rid of mocked args
cmd := &cobra.Command{ | ||
Long: "View and modify specific configuration values", | ||
RunE: run(), | ||
Short: "View and modify specific configuration values", | ||
Use: "config", | ||
PreRun: func(cmd *cobra.Command, args []string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this PreRun while the others are PersistentPreRun?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered leaving a comment. PersistentPreRun from my testing only ran on child cmds attached to the parent cmd. The parent cmd is where PersistentPreRun is defined.
On the other hand PreRun will run on the parent command only. In this case config doesn't have any sub commands similar to the others (flags, environments). Hence why its a PreRun
and not PersistentPreRun
Here's what the properties look like: