Skip to content

Commit 1b1471f

Browse files
committed
code review comments
1 parent 24a7fcf commit 1b1471f

File tree

5 files changed

+110
-33
lines changed

5 files changed

+110
-33
lines changed

cmd/skaffold/app/cmd/flags.go

-8
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,6 @@ var flagRegistry = []Flag{
573573
},
574574
IsEnum: true,
575575
},
576-
{
577-
Name: "id",
578-
Usage: "Survey id for survey command to open.",
579-
Value: &surveyID,
580-
DefValue: constants.HaTS,
581-
FlagAddMethod: "StringVar",
582-
DefinedOn: []string{"survey"},
583-
},
584576
}
585577

586578
func methodNameByType(v reflect.Value) string {

cmd/skaffold/app/cmd/survey.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var surveyID string
3030
func NewCmdSurvey() *cobra.Command {
3131
return NewCmd("survey").
3232
WithDescription("Opens a web browser to fill out the Skaffold survey").
33+
WithFlags([]*Flag{
34+
{Value: &surveyID, Name: "id", DefValue: survey.HatsID, Usage: "Survey ID for survey command to open."},
35+
}).
3336
NoArgs(showSurvey)
3437
}
3538

docs/content/en/docs/references/cli/_index.md

+6
Original file line numberDiff line numberDiff line change
@@ -1125,13 +1125,19 @@ Opens a web browser to fill out the Skaffold survey
11251125
```
11261126
11271127
1128+
Options:
1129+
--id='hats': Survey ID for survey command to open.
1130+
11281131
Usage:
11291132
skaffold survey [options]
11301133
11311134
Use "skaffold options" for a list of global command-line options (applies to all commands).
11321135
11331136
11341137
```
1138+
Env vars:
1139+
1140+
* `SKAFFOLD_ID` (same as `--id`)
11351141

11361142
### skaffold test
11371143

pkg/skaffold/survey/config.go

+42-17
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,42 @@ import (
2020
"fmt"
2121
"time"
2222

23-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
23+
sConfig "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2424
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
2525
)
2626

2727
const (
28+
HatsID = "hats"
2829
hatsURL = "https://forms.gle/BMTbGQXLWSdn7vEs6"
2930
)
3031

3132
var (
3233
hats = config{
33-
id: constants.HaTS,
34-
promptText: `Help improve Skaffold with our 2-minute anonymous survey: run 'skaffold survey'
35-
`,
36-
isRelevantFn: func(_ []util.VersionedConfig) bool { return true },
37-
URL: hatsURL,
34+
id: HatsID,
35+
promptText: "Help improve Skaffold with our 2-minute anonymous survey",
36+
isRelevantFn: func([]util.VersionedConfig, sConfig.RunMode) bool {
37+
return true
38+
},
39+
URL: hatsURL,
3840
}
3941
// surveys contains all the skaffold survey information
4042
surveys = []config{hats}
41-
42-
// for testing
43-
today = time.Now()
4443
)
4544

4645
// config defines a survey.
4746
type config struct {
48-
id string
49-
50-
promptText string
51-
47+
id string
48+
// promptText is shown to the user and should be formatted so each line should fit in < 80 characters.
49+
// For example: `As a Helm user, we are requesting your feedback on a proposed change to Skaffold's integration with Helm.`
50+
promptText string
51+
// startsAt mentions the date after the users survey should be prompted. This will ensure, Skaffold team can finalize the survey
52+
// even after release date.
53+
startsAt time.Time
54+
// expiresAt places a time limit of the user survey. As users are only prompted every two weeks
55+
// by design, this time limit should be at least 4 weeks after the upcoming release date to account
56+
// for release propagation lag to Cloud SDK and Cloud Shell.
5257
expiresAt time.Time
53-
isRelevantFn func([]util.VersionedConfig, ) bool
58+
isRelevantFn func([]util.VersionedConfig, sConfig.RunMode) bool
5459
URL string
5560
}
5661

@@ -60,14 +65,26 @@ func (s config) isActive() bool {
6065

6166
func (s config) prompt() string {
6267
if s.id == hats.id {
63-
return s.promptText
68+
return fmt.Sprintf(`%s: run 'skaffold survey'
69+
`, s.promptText)
6470
}
6571
return fmt.Sprintf(`%s: run 'skaffold survey -id %s'
6672
`, s.promptText, s.id)
6773
}
6874

69-
func (s config) isRelevant(cfgs []util.VersionedConfig) bool {
70-
return s.isRelevantFn(cfgs)
75+
func (s config) isRelevant(cfgs []util.VersionedConfig, cmd sConfig.RunMode) bool {
76+
return s.isRelevantFn(cfgs, cmd)
77+
}
78+
79+
func (s config) isValid() bool {
80+
if s.id == HatsID {
81+
return true
82+
}
83+
today := s.startsAt
84+
if today.IsZero() {
85+
today = time.Now()
86+
}
87+
return s.expiresAt.Sub(today) < 60*24*time.Hour
7188
}
7289

7390
func getSurvey(id string) (config, bool) {
@@ -86,3 +103,11 @@ func validKeys() []string {
86103
}
87104
return keys
88105
}
106+
107+
func init() {
108+
for _, s := range surveys {
109+
if !s.isValid() {
110+
panic(fmt.Errorf("survey %q is valid for more than a 60 days - user surveys must be valid for 60 days or less", s.id))
111+
}
112+
}
113+
}

pkg/skaffold/survey/config_test.go

+59-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121
"time"
2222

23+
sConfig "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2324
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
2425
"github.com/GoogleContainerTools/skaffold/testutil"
2526
)
@@ -69,21 +70,20 @@ func TestSurveyActive(t *testing.T) {
6970
description: "expiry in past",
7071
s: config{
7172
id: "expired",
72-
expiresAt: time.Date(2020, time.August, 14, 00, 00, 00, 0, time.UTC),
73+
expiresAt: time.Date(2020, 8, 1, 0, 0, 0, 0, time.UTC),
7374
},
7475
},
7576
{
7677
description: "expiry in future",
7778
s: config{
7879
id: "active",
79-
expiresAt: time.Date(2022, time.August, 14, 00, 00, 00, 0, time.UTC),
80+
expiresAt: time.Now().AddDate(1, 0, 0),
8081
},
8182
expected: true,
8283
},
8384
}
8485
for _, test := range tests {
8586
testutil.Run(t, test.description, func(t *testutil.T) {
86-
t.Override(&today, time.Date(2021, time.August, 14, 0, 0, 0, 0, time.UTC))
8787
t.CheckDeepEqual(test.s.isActive(), test.expected)
8888
})
8989
}
@@ -108,7 +108,7 @@ func TestSurveyRelevant(t *testing.T) {
108108
description: "relevant based on input configs",
109109
s: config{
110110
id: "foo",
111-
isRelevantFn: func(cfgs []util.VersionedConfig) bool {
111+
isRelevantFn: func(cfgs []util.VersionedConfig, _ sConfig.RunMode) bool {
112112
return len(cfgs) > 1
113113
},
114114
},
@@ -119,7 +119,7 @@ func TestSurveyRelevant(t *testing.T) {
119119
description: "not relevant based on config",
120120
s: config{
121121
id: "foo",
122-
isRelevantFn: func(cfgs []util.VersionedConfig) bool {
122+
isRelevantFn: func(cfgs []util.VersionedConfig, _ sConfig.RunMode) bool {
123123
return len(cfgs) > 1
124124
},
125125
},
@@ -129,7 +129,7 @@ func TestSurveyRelevant(t *testing.T) {
129129
description: "contains a config with test version",
130130
s: config{
131131
id: "version-value-test",
132-
isRelevantFn: func(cfgs []util.VersionedConfig) bool {
132+
isRelevantFn: func(cfgs []util.VersionedConfig, _ sConfig.RunMode) bool {
133133
for _, cfg := range cfgs {
134134
if m, ok := cfg.(mockVersionedConfig); ok {
135135
if m.version == "test" {
@@ -147,7 +147,7 @@ func TestSurveyRelevant(t *testing.T) {
147147
description: "does not contains a config with test version",
148148
s: config{
149149
id: "version-value-test",
150-
isRelevantFn: func(cfgs []util.VersionedConfig) bool {
150+
isRelevantFn: func(cfgs []util.VersionedConfig, _ sConfig.RunMode) bool {
151151
for _, cfg := range cfgs {
152152
if m, ok := cfg.(mockVersionedConfig); ok {
153153
if m.version == "test" {
@@ -163,7 +163,58 @@ func TestSurveyRelevant(t *testing.T) {
163163
}
164164
for _, test := range tests {
165165
testutil.Run(t, test.description, func(t *testutil.T) {
166-
t.CheckDeepEqual(test.s.isRelevant(test.cfgs), test.expected)
166+
t.CheckDeepEqual(test.s.isRelevant(test.cfgs, "dev"), test.expected)
167+
})
168+
}
169+
}
170+
171+
func TestIsValid(t *testing.T) {
172+
tests := []struct {
173+
description string
174+
s config
175+
expected bool
176+
}{
177+
{
178+
description: "only hats",
179+
s: hats,
180+
expected: true,
181+
},
182+
{
183+
description: "4 weeks valid survey with start date",
184+
s: config{
185+
id: "invalid",
186+
startsAt: time.Now().AddDate(0, 1, 0),
187+
expiresAt: time.Now().AddDate(0, 2, 0),
188+
},
189+
expected: true,
190+
},
191+
{
192+
description: "4 weeks valid survey without start date",
193+
s: config{
194+
id: "valid",
195+
expiresAt: time.Now().AddDate(0, 1, 0),
196+
},
197+
expected: true,
198+
},
199+
{
200+
description: "90 days invalid survey without start date",
201+
s: config{
202+
id: "invalid",
203+
expiresAt: time.Now().AddDate(0, 0, 90),
204+
},
205+
},
206+
{
207+
description: "90 days invalid survey with start date",
208+
s: config{
209+
id: "invalid",
210+
startsAt: time.Now().AddDate(0, 1, 0),
211+
expiresAt: time.Now().AddDate(0, 1, 90),
212+
},
213+
},
214+
}
215+
for _, test := range tests {
216+
testutil.Run(t, test.description, func(t *testutil.T) {
217+
t.CheckDeepEqual(test.s.isValid(), test.expected)
167218
})
168219
}
169220
}

0 commit comments

Comments
 (0)