Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sc-223627] indicate changed flags that are deprecated #74

Merged
merged 10 commits into from
Mar 25, 2024
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
43 changes: 32 additions & 11 deletions comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import (
)

type Comment struct {
Flag ldapi.FeatureFlag
FlagKey string
FlagName string
Archived bool
ArchivedAt time.Time
Deprecated bool
DeprecatedAt time.Time
Added bool
Removed bool
Extinct bool
Aliases []string
ChangeType string
Expand All @@ -38,40 +43,56 @@ func isNil(a interface{}) bool {
return a == nil || reflect.ValueOf(a).IsNil()
}

// Test go template rendering here https://gotemplate.io/
func githubFlagComment(flag ldapi.FeatureFlag, aliases []string, added, extinct bool, config *lcr.Config) (string, error) {
commentTemplate := Comment{
Flag: flag,
FlagKey: flag.Key,
FlagName: flag.Name,
Archived: flag.Archived,
Deprecated: flag.Deprecated,
Added: added,
Removed: !added,
Extinct: config.CheckExtinctions && extinct,
Aliases: aliases,
Primary: flag.Environments[config.LdEnvironment],
LDInstance: config.LdInstance,
ExtinctionsEnabled: config.CheckExtinctions,
}
var commentBody bytes.Buffer
if flag.ArchivedDate != nil {
commentTemplate.ArchivedAt = time.UnixMilli(*flag.ArchivedDate)
}
if flag.DeprecatedDate != nil {
commentTemplate.DeprecatedAt = time.UnixMilli(*flag.DeprecatedDate)
}

// All whitespace for template is required to be there or it will not render properly nested.
tmplSetup := `| [{{.Flag.Name}}]({{.LDInstance}}{{.Primary.Site.Href}}) | ` +
"`" + `{{.Flag.Key}}` + "` |" +
tmplSetup := `| [{{.FlagName}}]({{.LDInstance}}{{.Primary.Site.Href}}) | ` +
"`" + `{{.FlagKey}}` + "` |" +
`{{- if ne (len .Aliases) 0}}` +
`{{range $i, $e := .Aliases }}` + `{{if $i}},{{end}}` + " `" + `{{$e}}` + "`" + `{{end}}` +
`{{- end}} | ` +
`{{- if eq .Extinct true}} :white_check_mark: all references removed` +
`{{- else if eq .ExtinctionsEnabled true}} :warning: not all references removed {{- end}} ` +
`{{- if eq .Flag.Archived true}}{{- if eq .Extinct true}}<br>{{- else if eq .ExtinctionsEnabled true}}<br>{{end}}{{- if eq .Added true}} :warning:{{else}} :information_source:{{- end}} archived on {{.ArchivedAt | date "2006-01-02"}}{{- end}} |`
`{{- end}} | ` + infoCellTemplate() + ` |`

tmpl := template.Must(template.New("comment").Funcs(template.FuncMap{"trim": strings.TrimSpace, "isNil": isNil}).Funcs(sprig.FuncMap()).Parse(tmplSetup))
err := tmpl.Execute(&commentBody, commentTemplate)
if err != nil {

var commentBody bytes.Buffer
if err := tmpl.Execute(&commentBody, commentTemplate); err != nil {
return "", err
}

commentStr := html.UnescapeString(commentBody.String())

return commentStr, nil
}

// Template for info cell
// Will only show deprecated warning if flag is not archived
func infoCellTemplate() string {
return `{{- if eq .Extinct true}} :white_check_mark: all references removed` +
`{{- else if and .Removed .ExtinctionsEnabled}} :warning: not all references removed {{- end}} ` +
`{{- if eq .Archived true}}{{- if eq .Extinct true}}<br>{{- else if and .Removed .ExtinctionsEnabled }}<br>{{- end}}{{- if eq .Added true}} :warning:{{else}} :information_source:{{- end}} archived on {{.ArchivedAt | date "2006-01-02"}} ` +
`{{- else if eq .Deprecated true}}{{- if eq .Extinct true}}<br>{{- else if and .Removed .ExtinctionsEnabled }}<br>{{- end}}{{- if eq .Added true}} :warning:{{else}} :information_source:{{- end}} deprecated on {{.DeprecatedAt | date "2006-01-02"}}{{- end}}`
}

func GithubNoFlagComment() *github.IssueComment {
commentStr := `## LaunchDarkly flag references

Expand Down
44 changes: 35 additions & 9 deletions comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
func ptr[T any](t T) *T { return &t }

type testFlagEnv struct {
Flag ldapi.FeatureFlag
ArchivedFlag ldapi.FeatureFlag
Config config.Config
Flag ldapi.FeatureFlag
ArchivedFlag ldapi.FeatureFlag
DeprecatedFlag ldapi.FeatureFlag
Config config.Config
}

func newTestAccEnv() *testFlagEnv {
Expand All @@ -30,10 +31,16 @@ func newTestAccEnv() *testFlagEnv {
archivedFlag := createFlag("archived-flag")
archivedFlag.Archived = true
archivedFlag.ArchivedDate = ptr(int64(1691072480000))

deprecatedFlag := createFlag("deprecated-flag")
deprecatedFlag.Deprecated = true
deprecatedFlag.DeprecatedDate = ptr(int64(1691072480000))

return &testFlagEnv{
Flag: flag,
ArchivedFlag: archivedFlag,
Config: config,
Flag: flag,
ArchivedFlag: archivedFlag,
DeprecatedFlag: deprecatedFlag,
Config: config,
}
}

Expand Down Expand Up @@ -140,6 +147,9 @@ func TestGithubFlagComment(t *testing.T) {
t.Run("Archived flag removed", acceptanceTestEnv.ArchivedRemoved)
t.Run("Extinct flag", acceptanceTestEnv.ExtinctFlag)
t.Run("Extinct and Archived flag", acceptanceTestEnv.ExtinctAndArchivedFlag)

t.Run("Deprecated flag added", acceptanceTestEnv.DeprecatedAdded)
t.Run("Deprecated flag removed", acceptanceTestEnv.DeprecatedRemoved)
}

func TestProcessFlags(t *testing.T) {
Expand Down Expand Up @@ -170,23 +180,23 @@ func (e *testFlagEnv) NoAliases(t *testing.T) {
comment, err := githubFlagComment(e.Flag, []string{}, true, false, &e.Config)
require.NoError(t, err)

expected := "| [example flag](https://example.com/test) | `example-flag` | | :warning: not all references removed |"
expected := "| [example flag](https://example.com/test) | `example-flag` | | |"
assert.Equal(t, expected, comment)
}

func (e *testFlagEnv) Alias(t *testing.T) {
comment, err := githubFlagComment(e.Flag, []string{"exampleFlag", "ExampleFlag"}, true, false, &e.Config)
require.NoError(t, err)

expected := "| [example flag](https://example.com/test) | `example-flag` | `exampleFlag`, `ExampleFlag` | :warning: not all references removed |"
expected := "| [example flag](https://example.com/test) | `example-flag` | `exampleFlag`, `ExampleFlag` | |"
assert.Equal(t, expected, comment)
}

func (e *testFlagEnv) ArchivedAdded(t *testing.T) {
comment, err := githubFlagComment(e.ArchivedFlag, []string{}, true, false, &e.Config)
require.NoError(t, err)

expected := "| [archived flag](https://example.com/test) | `archived-flag` | | :warning: not all references removed<br> :warning: archived on 2023-08-03 |"
expected := "| [archived flag](https://example.com/test) | `archived-flag` | | :warning: archived on 2023-08-03 |"
assert.Equal(t, expected, comment)
}

Expand Down Expand Up @@ -214,6 +224,22 @@ func (e *testFlagEnv) ExtinctAndArchivedFlag(t *testing.T) {
assert.Equal(t, expected, comment)
}

func (e *testFlagEnv) DeprecatedAdded(t *testing.T) {
comment, err := githubFlagComment(e.DeprecatedFlag, []string{}, true, false, &e.Config)
require.NoError(t, err)

expected := "| [deprecated flag](https://example.com/test) | `deprecated-flag` | | :warning: deprecated on 2023-08-03 |"
assert.Equal(t, expected, comment)
}

func (e *testFlagEnv) DeprecatedRemoved(t *testing.T) {
comment, err := githubFlagComment(e.DeprecatedFlag, []string{}, false, false, &e.Config)
require.NoError(t, err)

expected := "| [deprecated flag](https://example.com/test) | `deprecated-flag` | | :warning: not all references removed<br> :information_source: deprecated on 2023-08-03 |"
assert.Equal(t, expected, comment)
}

func (e *testCommentBuilder) AddedOnly(t *testing.T) {
e.FlagsRef.FlagsAdded["example-flag"] = []string{}
e.Comments.CommentsAdded = []string{"comment1", "comment2"}
Expand Down
5 changes: 3 additions & 2 deletions testdata/test
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
show-widgets
showWidgets
show_widgets
oldPricingBanner
show-widgets
show_widgets
oldPricingBanner
mobile-app-promo-ios
mobile-app-promo-ios
beta-ui