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

feat: track cli command run events #189

Merged
merged 32 commits into from
Apr 22, 2024

Conversation

sunnyguduru
Copy link
Contributor

@sunnyguduru sunnyguduru commented Apr 18, 2024

Here's what the properties look like:
CleanShot 2024-04-18 at 16 53 41@2x

cmd/environments/get.go Outdated Show resolved Hide resolved
@sunnyguduru sunnyguduru force-pushed the sunny/sc-240591/track-cli-command-run-events branch from cb73d5c to 17aa87c Compare April 18, 2024 21:51
@sunnyguduru sunnyguduru marked this pull request as ready for review April 18, 2024 23:56
cmd/utils/utils.go Outdated Show resolved Hide resolved
Copy link
Contributor

@dbolson dbolson left a 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?

cmd/utils/utils.go Outdated Show resolved Hide resolved
cmd/utils/utils.go Outdated Show resolved Hide resolved
cmd/utils/utils.go Outdated Show resolved Hide resolved
cmd/utils/utils.go Outdated Show resolved Hide resolved
cmd/utils/utils.go Outdated Show resolved Hide resolved
@sunnyguduru sunnyguduru force-pushed the sunny/sc-240591/track-cli-command-run-events branch from a07077a to 859a723 Compare April 19, 2024 21:41
Comment on lines 147 to 183
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)
})
Copy link
Contributor Author

@sunnyguduru sunnyguduru Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for commands


func CmdRunEventProperties(cmd *cobra.Command, name string) map[string]interface{} {
id := uuid.New()
baseURI := viper.GetString(cliflags.BaseURIFlag)
Copy link
Contributor

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.


assert.EqualError(t, err, "base-uri is invalid"+errorHelp)
})

t.Run("will track analytics for 'CLI Command Run' event", func(t *testing.T) {
Copy link
Contributor

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.AddCommand(getCmd)

cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
Copy link
Contributor

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.

Copy link
Contributor

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?

@@ -0,0 +1,30 @@
package analytics
Copy link
Contributor

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?

@@ -0,0 +1,3 @@
package cliflags

const BaseURIDefault = "https://app.launchdarkly.com"
Copy link
Contributor

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.


t.Run("will track analytics for 'CLI Command Run' event", func(t *testing.T) {
id := "test-id"
mockedTrackingArgs := []interface{}{
Copy link
Contributor

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
)

@@ -10,3 +11,5 @@ const (
ProjectFlag = "project"
RoleFlag = "role"
)

const BaseURIDefault = "https://app.launchdarkly.com"
Copy link
Contributor

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.AddCommand(getCmd)

cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
Copy link
Contributor

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?

@@ -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...)
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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

@sunnyguduru sunnyguduru merged commit fd98b42 into main Apr 22, 2024
2 checks passed
@sunnyguduru sunnyguduru deleted the sunny/sc-240591/track-cli-command-run-events branch April 22, 2024 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants