diff --git a/comments/comments.go b/comments/comments.go index 5fb34836..78abb028 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -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 @@ -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}}
{{- else if eq .ExtinctionsEnabled true}}
{{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}}
{{- else if and .Removed .ExtinctionsEnabled }}
{{- 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}}
{{- else if and .Removed .ExtinctionsEnabled }}
{{- 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 diff --git a/comments/comments_test.go b/comments/comments_test.go index abd3dc74..5ca1a61d 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -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 { @@ -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, } } @@ -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) { @@ -170,7 +180,7 @@ 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) } @@ -178,7 +188,7 @@ 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) } @@ -186,7 +196,7 @@ 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
: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) } @@ -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
: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"} diff --git a/testdata/test b/testdata/test index a82e2f31..613b7907 100644 --- a/testdata/test +++ b/testdata/test @@ -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