Skip to content

Commit

Permalink
Merge pull request #188 from open-sauced/feat/generate-config-telemetry
Browse files Browse the repository at this point in the history
feat: implement PostHog captures for `generate config`
  • Loading branch information
zeucapua authored Sep 13, 2024
2 parents abe4bf1 + eb760b6 commit ea2b4a9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cmd/generate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
},
}

Expand All @@ -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)
}

Expand All @@ -113,27 +127,33 @@ 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
if opts.outputPath == "~/" {
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
}
42 changes: 42 additions & 0 deletions pkg/utils/posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ea2b4a9

Please sign in to comment.