From 051386b32668b4bef1e8d7988c8aed093851ce1f Mon Sep 17 00:00:00 2001 From: JulesFaucherre Date: Tue, 11 Jul 2023 16:08:31 +0200 Subject: [PATCH] chore: Added events for policy --- cmd/policy/policy.go | 18 +++++++++++++++--- cmd/policy/policy_test.go | 11 ++++++----- telemetry/events.go | 4 ++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cmd/policy/policy.go b/cmd/policy/policy.go index 693bfb0c3..cbd246e57 100644 --- a/cmd/policy/policy.go +++ b/cmd/policy/policy.go @@ -24,17 +24,29 @@ import ( "github.com/CircleCI-Public/circleci-cli/api/policy" "github.com/CircleCI-Public/circleci-cli/api/rest" + "github.com/CircleCI-Public/circleci-cli/cmd/create_telemetry" "github.com/CircleCI-Public/circleci-cli/cmd/validator" "github.com/CircleCI-Public/circleci-cli/config" + "github.com/CircleCI-Public/circleci-cli/telemetry" + "github.com/CircleCI-Public/circleci-cli/settings" ) // NewCommand creates the root policy command with all policy subcommands attached. func NewCommand(globalConfig *settings.Config, preRunE validator.Validator) *cobra.Command { cmd := &cobra.Command{ - Use: "policy", - PersistentPreRunE: preRunE, - Short: "Manage security policies", + Use: "policy", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + telemetryClient := create_telemetry.CreateTelemetry(globalConfig) + defer telemetryClient.Close() + telemetryClient.Track(telemetry.CreatePolicyEvent(create_telemetry.GetCommandInformation(cmd, true))) + + if preRunE != nil { + return preRunE(cmd, args) + } + return nil + }, + Short: "Manage security policies", Long: `Policies ensures security of build configs via security policy management framework. This group of commands allows the management of polices to be verified against build configs.`, } diff --git a/cmd/policy/policy_test.go b/cmd/policy/policy_test.go index 39b414649..ac7fab59c 100644 --- a/cmd/policy/policy_test.go +++ b/cmd/policy/policy_test.go @@ -57,7 +57,7 @@ func TestPushPolicyWithPrompt(t *testing.T) { })) defer svr.Close() - config := &settings.Config{Token: "testtoken", HTTPClient: http.DefaultClient} + config := &settings.Config{Token: "testtoken", HTTPClient: http.DefaultClient, IsTelemetryDisabled: true} cmd := NewCommand(config, nil) buffer := makeSafeBuffer() @@ -1382,10 +1382,11 @@ func TestTestRunner(t *testing.T) { func makeCMD(circleHost string, token string) (*cobra.Command, *bytes.Buffer, *bytes.Buffer) { config := &settings.Config{ - Host: circleHost, - Token: token, - RestEndpoint: "/api/v2", - HTTPClient: http.DefaultClient, + Host: circleHost, + Token: token, + RestEndpoint: "/api/v2", + HTTPClient: http.DefaultClient, + IsTelemetryDisabled: true, } cmd := NewCommand(config, nil) diff --git a/telemetry/events.go b/telemetry/events.go index 52620823c..4278968ee 100644 --- a/telemetry/events.go +++ b/telemetry/events.go @@ -91,3 +91,7 @@ func CreateNamespaceEvent(cmdInfo CommandInfo) Event { func CreateOrbEvent(cmdInfo CommandInfo) Event { return createEventFromCommandInfo("orb", cmdInfo) } + +func CreatePolicyEvent(cmdInfo CommandInfo) Event { + return createEventFromCommandInfo("policy", cmdInfo) +}