Skip to content

Commit

Permalink
feat: Posthog events bootstrapping
Browse files Browse the repository at this point in the history
Signed-off-by: John McBride <[email protected]>
  • Loading branch information
jpmcb committed Sep 6, 2024
1 parent 19b1711 commit 2bd7b4a
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 78 deletions.
7 changes: 4 additions & 3 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ func NewLoginCommand() *cobra.Command {
disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry)

opts.telemetry = utils.NewPosthogCliClient(!disableTelem)
defer opts.telemetry.Done()

username, err := run()

if err != nil {
opts.telemetry.CaptureFailedLogin()
_ = opts.telemetry.CaptureFailedLogin()
} else {
opts.telemetry.CaptureLogin(username)
_ = opts.telemetry.CaptureLogin(username)
}

_ = opts.telemetry.Done()

return err
},
}
Expand Down
27 changes: 15 additions & 12 deletions cmd/generate/codeowners/codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func NewCodeownersCommand() *cobra.Command {
disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry)

opts.telemetry = utils.NewPosthogCliClient(!disableTelem)
defer opts.telemetry.Done()

configPath, _ := cmd.Flags().GetString("config")
opts.config, err = config.LoadConfig(configPath, filepath.Join(opts.path, ".sauced.yaml"))
Expand All @@ -112,7 +111,11 @@ func NewCodeownersCommand() *cobra.Command {
opts.loglevel = logging.LogDebug
}

return run(opts, cmd)
err = run(opts, cmd)

_ = opts.telemetry.Done()

return err
},
}

Expand All @@ -135,7 +138,7 @@ func run(opts *Options, cmd *cobra.Command) error {

repo, err := git.PlainOpen(opts.path)
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerate()
_ = opts.telemetry.CaptureFailedCodeownersGenerate()
return fmt.Errorf("error opening repo: %w", err)
}
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Opened repo at: %s\n", opts.path)
Expand All @@ -150,7 +153,7 @@ func run(opts *Options, cmd *cobra.Command) error {

codeowners, err := processOptions.process()
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerate()
_ = opts.telemetry.CaptureFailedCodeownersGenerate()
return fmt.Errorf("error traversing git log: %w", err)
}

Expand All @@ -165,11 +168,11 @@ func run(opts *Options, cmd *cobra.Command) error {
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", outputPath)
err = generateOutputFile(codeowners, outputPath, opts, cmd)
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerate()
_ = opts.telemetry.CaptureFailedCodeownersGenerate()
return fmt.Errorf("error generating github style codeowners file: %w", err)
}
opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Finished generating file: %s\n", outputPath)
opts.telemetry.CaptureCodeownersGenerate()
_ = opts.telemetry.CaptureCodeownersGenerate()

// ignore the interactive prompts for CI/CD environments
if opts.tty {
Expand Down Expand Up @@ -209,11 +212,11 @@ func run(opts *Options, cmd *cobra.Command) error {
case "y", "Y", "yes":
user, err := authenticator.Login()
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerateAuth()
_ = opts.telemetry.CaptureFailedCodeownersGenerateAuth()
opts.logger.V(logging.LogInfo).Style(0, colors.FgRed).Infof("Error logging in\n")
return fmt.Errorf("could not log in: %w", err)
}
opts.telemetry.CaptureCodeownersGenerateAuth(user)
_ = opts.telemetry.CaptureCodeownersGenerateAuth(user)
opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Logged in as: %s\n", user)

case "n", "N", "no":
Expand All @@ -226,7 +229,7 @@ func run(opts *Options, cmd *cobra.Command) error {

opts.token, err = authenticator.GetSessionToken()
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
_ = opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
opts.logger.V(logging.LogInfo).Style(0, colors.FgRed).Infof("Error getting session token\n")
return fmt.Errorf("could not get session token: %w", err)
}
Expand All @@ -236,7 +239,7 @@ func run(opts *Options, cmd *cobra.Command) error {
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Looking up OpenSauced workspace: Pizza CLI\n")
workspace, err := findCreatePizzaCliWorkspace(opts)
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
_ = opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
opts.logger.V(logging.LogInfo).Style(0, colors.FgRed).Infof("Error finding Workspace: Pizza CLI\n")
return fmt.Errorf("could not find Pizza CLI workspace: %w", err)
}
Expand All @@ -245,13 +248,13 @@ func run(opts *Options, cmd *cobra.Command) error {
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Looking up Contributor Insight for local repository: %s\n", listName)
userList, err := updateCreateLocalWorkspaceUserList(opts, listName, workspace, codeowners)
if err != nil {
opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
_ = opts.telemetry.CaptureFailedCodeownersGenerateContributorInsight()
opts.logger.V(logging.LogInfo).Style(0, colors.FgRed).Infof("Error finding Workspace Contributor Insight: %s\n", listName)
return fmt.Errorf("could not find Workspace Contributor Insight: %s - %w", listName, err)
}
opts.logger.V(logging.LogDebug).Style(0, colors.FgGreen).Infof("Updated Contributor Insight for local repository: %s\n", listName)
opts.logger.V(logging.LogInfo).Style(0, colors.FgCyan).Infof("Access list on OpenSauced:\n%s\n", fmt.Sprintf("https://app.opensauced.pizza/workspaces/%s/contributor-insights/%s", workspace.ID, userList.ID))
opts.telemetry.CaptureCodeownersGenerateContributorInsight()
_ = opts.telemetry.CaptureCodeownersGenerateContributorInsight()

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/insights/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func NewContributorsCommand() *cobra.Command {
disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry)

opts.telemetry = utils.NewPosthogCliClient(!disableTelem)
defer opts.telemetry.Done()

endpointURL, _ := cmd.Flags().GetString(constants.FlagNameEndpoint)
opts.APIClient = api.NewClient(endpointURL)
Expand All @@ -67,11 +66,13 @@ func NewContributorsCommand() *cobra.Command {
err := opts.run()

if err != nil {
opts.telemetry.CaptureInsights()
_ = opts.telemetry.CaptureInsights()
} else {
opts.telemetry.CaptureFailedInsights()
_ = opts.telemetry.CaptureFailedInsights()
}

_ = opts.telemetry.Done()

return err
},
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/insights/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func NewRepositoriesCommand() *cobra.Command {
disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry)

opts.telemetry = utils.NewPosthogCliClient(!disableTelem)
defer opts.telemetry.Done()

endpointURL, _ := cmd.Flags().GetString(constants.FlagNameEndpoint)
opts.APIClient = api.NewClient(endpointURL)
Expand All @@ -67,11 +66,13 @@ func NewRepositoriesCommand() *cobra.Command {
err := opts.run()

if err != nil {
opts.telemetry.CaptureInsights()
_ = opts.telemetry.CaptureInsights()
} else {
opts.telemetry.CaptureFailedInsights()
_ = opts.telemetry.CaptureFailedInsights()
}

_ = opts.telemetry.Done()

return err
},
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/cli/browser v1.3.0
github.com/go-git/go-git/v5 v5.12.0
github.com/jpmcb/gopherlogs v0.2.0
github.com/posthog/posthog-go v1.2.19
github.com/posthog/posthog-go v1.2.21
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -40,6 +40,7 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -103,6 +105,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posthog/posthog-go v1.2.19 h1:0udGG2do4LjOzE0D/ik7S3uM2wwFKwzSqswBfdcQ1y4=
github.com/posthog/posthog-go v1.2.19/go.mod h1:uYC2l1Yktc8E+9FAHJ9QZG4vQf/NHJPD800Hsm7DzoM=
github.com/posthog/posthog-go v1.2.21 h1:p2ea0l+Qwtk+VC2LCAI87Dz36vwj9i+QHw5s6CpRikA=
github.com/posthog/posthog-go v1.2.21/go.mod h1:uYC2l1Yktc8E+9FAHJ9QZG4vQf/NHJPD800Hsm7DzoM=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
16 changes: 16 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ gen-docs:

# Runs all the dev tasks (like formatting, linting, building, etc.)
dev: format lint test build-all

# Calls the various Posthog capture events to add the Insights to the database
bootstrap-telemetry:
#!/usr/bin/env sh
echo "Building telemetry-oneshot"
go build \
-tags telemetry \
-ldflags="-s -w" \
-ldflags="-X 'github.com/open-sauced/pizza-cli/pkg/utils.writeOnlyPublicPosthogKey=${POSTHOG_PUBLIC_API_KEY}'" \
-o build/telemetry-oneshot \
telemetry.go

./build/telemetry-oneshot

rm ./build/telemetry-oneshot
Loading

0 comments on commit 2bd7b4a

Please sign in to comment.