Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/attachments/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -39,6 +40,24 @@ func (f *Flag) Changed() bool {
return f.flag.Changed
}

// RecordTelemetry records how many attachment flags were provided.
func (f *Flag) RecordTelemetry(command string, recorder ghtelemetry.CommandRecorder) {
if recorder == nil || !f.Changed() {
return
}

recorder.SetSampleRate(ghtelemetry.SAMPLE_ALL)
recorder.Record(ghtelemetry.Event{
Type: "attachment_invocation",
Dimensions: ghtelemetry.Dimensions{
"command": command,
},
Measures: ghtelemetry.Measures{
"attach_count": int64(len(f.values)),
},
})
}

// UserAssets validates the files named by the attachment flag, keeping them in
// the order they were written. It returns nothing when the flag was not passed.
func (f *Flag) UserAssets() ([]UserAsset, error) {
Expand Down
54 changes: 54 additions & 0 deletions internal/attachments/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"testing"

"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/telemetry"
"github.com/google/shlex"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -88,6 +90,58 @@ func TestAddFlag(t *testing.T) {
}
}

func TestFlagRecordTelemetry(t *testing.T) {
tests := []struct {
name string
input string
wantEvent bool
wantCount int64
}{
{
name: "flag not passed",
input: "",
},
{
name: "one attachment",
input: "--attach ./first.png",
wantEvent: true,
wantCount: 1,
},
{
name: "several attachments before validation",
input: "--attach ./first.png --attach ./second.png --attach ./third.png",
wantEvent: true,
wantCount: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, attachFlag := attachCmd(t, tt.input)
recorder := &telemetry.CommandRecorderSpy{}

attachFlag.RecordTelemetry("gh issue comment", recorder)

if !tt.wantEvent {
assert.Empty(t, recorder.Events)
assert.Zero(t, recorder.LastSampleRate)
return
}

require.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
require.Equal(t, []ghtelemetry.Event{{
Type: "attachment_invocation",
Dimensions: ghtelemetry.Dimensions{
"command": "gh issue comment",
},
Measures: ghtelemetry.Measures{
"attach_count": tt.wantCount,
},
}}, recorder.Events)
})
}
}

func TestFlagUserAssets(t *testing.T) {
tests := []struct {
name string
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/issue/comment/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package comment
import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/attachments"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/issue/shared"
issueShared "github.com/cli/cli/v2/pkg/cmd/issue/shared"
Expand All @@ -11,7 +12,7 @@ import (
"github.com/spf13/cobra"
)

func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) error) *cobra.Command {
func NewCmdComment(f *cmdutil.Factory, telemetry ghtelemetry.CommandRecorder, runF func(*prShared.CommentableOptions) error) *cobra.Command {
opts := &prShared.CommentableOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Expand Down Expand Up @@ -58,6 +59,8 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*prShared.CommentableOptions) e
`),
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.AttachFlag.RecordTelemetry(cmd.CommandPath(), telemetry)
Comment thread
BagToad marked this conversation as resolved.

opts.RetrieveCommentable = func() (prShared.Commentable, ghrepo.Interface, error) {
// TODO wm: more testing
issueNumber, parsedBaseRepo, err := shared.ParseIssueFromArg(args[0])
Expand Down
17 changes: 16 additions & 1 deletion pkg/cmd/issue/comment/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/telemetry"
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
Expand Down Expand Up @@ -408,7 +410,8 @@ func TestNewCmdComment(t *testing.T) {
assert.NoError(t, err)

var gotOpts *shared.CommentableOptions
cmd := NewCmdComment(f, func(opts *shared.CommentableOptions) error {
recorder := &telemetry.CommandRecorderSpy{}
cmd := NewCmdComment(f, recorder, func(opts *shared.CommentableOptions) error {
gotOpts = opts
return nil
})
Expand All @@ -420,6 +423,18 @@ func TestNewCmdComment(t *testing.T) {
cmd.SetErr(&bytes.Buffer{})

_, err = cmd.ExecuteC()
if cmd.Flags().Changed("attach") {
values, flagErr := cmd.Flags().GetStringArray("attach")
require.NoError(t, flagErr)
require.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
require.Len(t, recorder.Events, 1)
assert.Equal(t, "attachment_invocation", recorder.Events[0].Type)
assert.Equal(t, cmd.CommandPath(), recorder.Events[0].Dimensions["command"])
assert.Equal(t, int64(len(values)), recorder.Events[0].Measures["attach_count"])
} else {
assert.Empty(t, recorder.Events)
assert.Zero(t, recorder.LastSampleRate)
}
if tt.wantsErr {
assert.Error(t, err)
if tt.wantsErrContains != "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cli/cli/v2/internal/browser"
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/internal/text"
Expand Down Expand Up @@ -61,7 +62,7 @@ type CreateOptions struct {
Assets []attachments.UserAsset
}

func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
func NewCmdCreate(f *cmdutil.Factory, telemetry ghtelemetry.CommandRecorder, runF func(*CreateOptions) error) *cobra.Command {
opts := &CreateOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Expand Down Expand Up @@ -120,6 +121,8 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
Args: cmdutil.NoArgsQuoteReminder,
Aliases: []string{"new"},
RunE: func(cmd *cobra.Command, args []string) error {
opts.AttachFlag.RecordTelemetry(cmd.CommandPath(), telemetry)

// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
opts.HasRepoOverride = cmd.Flags().Changed("repo")
Expand Down
19 changes: 17 additions & 2 deletions pkg/cmd/issue/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"github.com/cli/cli/v2/internal/config"
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/cli/v2/internal/telemetry"
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
Expand Down Expand Up @@ -311,7 +313,8 @@ func TestNewCmdCreate(t *testing.T) {
}

var opts *CreateOptions
cmd := NewCmdCreate(f, func(o *CreateOptions) error {
recorder := &telemetry.CommandRecorderSpy{}
cmd := NewCmdCreate(f, recorder, func(o *CreateOptions) error {
opts = o
return nil
})
Expand All @@ -322,6 +325,18 @@ func TestNewCmdCreate(t *testing.T) {
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
_, err = cmd.ExecuteC()
if cmd.Flags().Changed("attach") {
values, flagErr := cmd.Flags().GetStringArray("attach")
require.NoError(t, flagErr)
require.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
require.Len(t, recorder.Events, 1)
assert.Equal(t, "attachment_invocation", recorder.Events[0].Type)
assert.Equal(t, cmd.CommandPath(), recorder.Events[0].Dimensions["command"])
assert.Equal(t, int64(len(values)), recorder.Events[0].Measures["attach_count"])
} else {
assert.Empty(t, recorder.Events)
assert.Zero(t, recorder.LastSampleRate)
}
if tt.wantsErr {
require.Error(t, err)
if tt.wantsErrMsg != "" {
Expand Down Expand Up @@ -1450,7 +1465,7 @@ func runCommandWithRootDirOverridden(rt http.RoundTripper, isTTY bool, cli strin
Prompter: pm,
}

cmd := NewCmdCreate(factory, func(opts *CreateOptions) error {
cmd := NewCmdCreate(factory, &telemetry.NoOpService{}, func(opts *CreateOptions) error {
opts.RootDirOverride = rootDir
opts.Detector = &fd.EnabledDetectorMock{}
return createRun(opts)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/issue/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cli/cli/v2/internal/attachments"
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/text"
issueShared "github.com/cli/cli/v2/pkg/cmd/issue/shared"
Expand Down Expand Up @@ -57,7 +58,7 @@ type EditOptions struct {
prShared.Editable
}

func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command {
func NewCmdEdit(f *cmdutil.Factory, telemetry ghtelemetry.CommandRecorder, runF func(*EditOptions) error) *cobra.Command {
opts := &EditOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Expand Down Expand Up @@ -122,6 +123,8 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.AttachFlag.RecordTelemetry(cmd.CommandPath(), telemetry)

issueNumbers, baseRepo, err := issueShared.ParseIssuesFromArgs(args)
if err != nil {
return err
Expand Down
17 changes: 16 additions & 1 deletion pkg/cmd/issue/edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"github.com/cli/cli/v2/internal/config"
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/cli/v2/internal/telemetry"
prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
Expand Down Expand Up @@ -455,7 +457,8 @@ func TestNewCmdEdit(t *testing.T) {
assert.NoError(t, err)

var gotOpts *EditOptions
cmd := NewCmdEdit(f, func(opts *EditOptions) error {
recorder := &telemetry.CommandRecorderSpy{}
cmd := NewCmdEdit(f, recorder, func(opts *EditOptions) error {
gotOpts = opts
return nil
})
Expand All @@ -467,6 +470,18 @@ func TestNewCmdEdit(t *testing.T) {
cmd.SetErr(&bytes.Buffer{})

_, err = cmd.ExecuteC()
if cmd.Flags().Changed("attach") {
values, flagErr := cmd.Flags().GetStringArray("attach")
require.NoError(t, flagErr)
require.Equal(t, ghtelemetry.SAMPLE_ALL, recorder.LastSampleRate)
require.Len(t, recorder.Events, 1)
assert.Equal(t, "attachment_invocation", recorder.Events[0].Type)
assert.Equal(t, cmd.CommandPath(), recorder.Events[0].Dimensions["command"])
assert.Equal(t, int64(len(values)), recorder.Events[0].Measures["attach_count"])
} else {
assert.Empty(t, recorder.Events)
assert.Zero(t, recorder.LastSampleRate)
}
if tt.wantsErr {
require.Error(t, err)
if tt.wantsErrMsg != "" {
Expand Down
9 changes: 5 additions & 4 deletions pkg/cmd/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package issue

import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
cmdClose "github.com/cli/cli/v2/pkg/cmd/issue/close"
cmdComment "github.com/cli/cli/v2/pkg/cmd/issue/comment"
cmdCreate "github.com/cli/cli/v2/pkg/cmd/issue/create"
Expand All @@ -20,7 +21,7 @@ import (
"github.com/spf13/cobra"
)

func NewCmdIssue(f *cmdutil.Factory) *cobra.Command {
func NewCmdIssue(f *cmdutil.Factory, telemetry ghtelemetry.CommandRecorder) *cobra.Command {
cmd := &cobra.Command{
Use: "issue <command>",
Short: "Manage issues",
Expand All @@ -44,16 +45,16 @@ func NewCmdIssue(f *cmdutil.Factory) *cobra.Command {

cmdutil.AddGroup(cmd, "General commands",
cmdList.NewCmdList(f, nil),
cmdCreate.NewCmdCreate(f, nil),
cmdCreate.NewCmdCreate(f, telemetry, nil),
cmdStatus.NewCmdStatus(f, nil),
)

cmdutil.AddGroup(cmd, "Targeted commands",
cmdView.NewCmdView(f, nil),
cmdComment.NewCmdComment(f, nil),
cmdComment.NewCmdComment(f, telemetry, nil),
cmdClose.NewCmdClose(f, nil),
cmdReopen.NewCmdReopen(f, nil),
cmdEdit.NewCmdEdit(f, nil),
cmdEdit.NewCmdEdit(f, telemetry, nil),
cmdDevelop.NewCmdDevelop(f, nil),
cmdLock.NewCmdLock(f, cmd.Name(), nil),
cmdLock.NewCmdUnlock(f, cmd.Name(), nil),
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/pr/comment/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package comment
import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/attachments"
"github.com/cli/cli/v2/internal/gh/ghtelemetry"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)

func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) error) *cobra.Command {
func NewCmdComment(f *cmdutil.Factory, telemetry ghtelemetry.CommandRecorder, runF func(*shared.CommentableOptions) error) *cobra.Command {
opts := &shared.CommentableOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Expand Down Expand Up @@ -56,6 +57,8 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) err
`),
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.AttachFlag.RecordTelemetry(cmd.CommandPath(), telemetry)
Comment thread
BagToad marked this conversation as resolved.

if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
return cmdutil.FlagErrorf("argument required when using the --repo flag")
}
Expand Down
Loading
Loading