diff --git a/cmd/generate/config/config.go b/cmd/generate/config/config.go index 1d52e77..bc39df4 100644 --- a/cmd/generate/config/config.go +++ b/cmd/generate/config/config.go @@ -11,6 +11,9 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/spf13/cobra" + + "github.com/open-sauced/pizza-cli/pkg/constants" + "github.com/open-sauced/pizza-cli/pkg/utils" ) // Options for the config generation command @@ -26,6 +29,9 @@ type Options struct { // from global config ttyDisabled bool + + // telemetry for capturing CLI events via PostHog + telemetry *utils.PosthogCliClient } const configLongDesc string = `Generates a ".sauced.yaml" configuration file for use with the Pizza CLI's codeowners command. @@ -62,11 +68,17 @@ func NewConfigCommand() *cobra.Command { }, RunE: func(cmd *cobra.Command, _ []string) error { + disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry) + + opts.telemetry = utils.NewPosthogCliClient(!disableTelem) opts.outputPath, _ = cmd.Flags().GetString("output-path") opts.isInteractive, _ = cmd.Flags().GetBool("interactive") opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable") - return run(opts) + err := run(opts) + _ = opts.telemetry.Done() + + return err }, } @@ -81,12 +93,14 @@ func run(opts *Options) error { // Open repo repo, err := git.PlainOpen(opts.path) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error opening repo: %w", err) } commitIter, err := repo.CommitObjects() if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error opening repo commits: %w", err) } @@ -113,11 +127,14 @@ func run(opts *Options) error { // INTERACTIVE: per unique email, set a name (existing or new or ignore) if opts.isInteractive && !opts.ttyDisabled { + _ = opts.telemetry.CaptureConfigGenerateMode("interactive") program := tea.NewProgram(initialModel(opts, uniqueEmails)) if _, err := program.Run(); err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error running interactive mode: %w", err) } } else { + _ = opts.telemetry.CaptureConfigGenerateMode("automatic") // generate an output file // default: `./.sauced.yaml` // fallback for home directories @@ -125,15 +142,18 @@ func run(opts *Options) error { homeDir, _ := os.UserHomeDir() err := generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error generating output file: %w", err) } } else { err := generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap) if err != nil { + _ = opts.telemetry.CaptureFailedConfigGenerate() return fmt.Errorf("error generating output file: %w", err) } } } + _ = opts.telemetry.CaptureConfigGenerate() return nil } diff --git a/pkg/utils/posthog.go b/pkg/utils/posthog.go index 6383c10..9a9fb41 100644 --- a/pkg/utils/posthog.go +++ b/pkg/utils/posthog.go @@ -156,6 +156,48 @@ func (p *PosthogCliClient) CaptureFailedCodeownersGenerateContributorInsight() e return nil } +// CaptureConfigGenerate gathers telemetry on success +func (p *PosthogCliClient) CaptureConfigGenerate() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_generated_config", + }) + } + + return nil +} + +// CaptureConfigGenerateMode gathers what mode a user is in when generating +// either 'Automatic' (default) or 'Interactive' +func (p *PosthogCliClient) CaptureConfigGenerateMode(mode string) error { + properties := make(map[string]interface{}) + + properties["mode"] = mode + + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_generated_config_mode", + Properties: properties, + }) + } + + return nil +} + +// CaptureFailedConfigGenerate gathers telemetry on failed +func (p *PosthogCliClient) CaptureFailedConfigGenerate() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_failed_to_generate_config", + }) + } + + return nil +} + // CaptureInsights gathers telemetry on successful Insights command runs func (p *PosthogCliClient) CaptureInsights() error { if p.activated { diff --git a/telemetry.go b/telemetry.go index 71d910a..7f2c74a 100644 --- a/telemetry.go +++ b/telemetry.go @@ -36,6 +36,21 @@ func main() { panic(err) } + err = client.CaptureConfigGenerate() + if err != nil { + panic(err) + } + + err = client.CaptureFailedConfigGenerate() + if err != nil { + panic(err) + } + + err = client.CaptureConfigGenerateMode("interactive") + if err != nil { + panic(err) + } + err = client.CaptureCodeownersGenerateAuth("test-user") if err != nil { panic(err)