diff --git a/comments/comments.go b/comments/comments.go index 488a04f0..a6fe4c5e 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -59,7 +59,7 @@ Aliases: {{range $i, $e := .Aliases }}` + "{{if $i}}, {{end}}`" + `{{$e}}` + "`" {{- end}} {{ "\n" }} {{- range $key, $env := .Environments }} -Environment: **{{ $key }}** +Environment: {{ if .EnvironmentName }}**{{ .EnvironmentName }}** {{ end -}}` + "`" + `{{ $key }}` + "`" + ` | Type | Variation | Weight(if Rollout) | | --- | --- | --- | {{- if not (isNil .Fallthrough_.Rollout) }} diff --git a/comments/comments_test.go b/comments/comments_test.go index 4216c444..509bfc34 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -30,6 +30,7 @@ func newTestAccEnv() *testFlagEnv { func createFlag(key string) ldapi.FeatureFlag { environment := ldapi.FeatureFlagConfig{ + EnvironmentName: "Production", Site: &ldapi.Site{ Href: "test", }, @@ -138,7 +139,7 @@ func (e *testFlagEnv) noAliasesNoTags(t *testing.T) { if err != nil { t.Fatalf("err:%v", err) } - assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **Production** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") } func (e *testFlagEnv) Alias(t *testing.T) { @@ -146,7 +147,7 @@ func (e *testFlagEnv) Alias(t *testing.T) { if err != nil { t.Fatalf("err:%v", err) } - assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`\n\n\nEnvironment: **Production** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") } func (e *testFlagEnv) Tag(t *testing.T) { @@ -155,7 +156,7 @@ func (e *testFlagEnv) Tag(t *testing.T) { if err != nil { t.Fatalf("err:%v", err) } - assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`\n\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`\n\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **Production** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") } func (e *testFlagEnv) AliasesAndTags(t *testing.T) { @@ -164,7 +165,7 @@ func (e *testFlagEnv) AliasesAndTags(t *testing.T) { if err != nil { t.Fatalf("err:%v", err) } - assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`, `otherTag`, `finalTag`\n\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`, `otherTag`, `finalTag`\n\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\n\nEnvironment: **Production** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") } func (e *testFlagEnv) RolloutFlag(t *testing.T) { @@ -192,7 +193,7 @@ func (e *testFlagEnv) RolloutFlag(t *testing.T) { if err != nil { t.Fatalf("err:%v", err) } - assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`, `otherTag`, `finalTag`\n\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | Rollout | |\n| |`true` | `12.345%`|\n| |`false` | `87.655%`|\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, "\n**[Sample Flag](https://example.com/test)** `example-flag`\nTags: `myTag`, `otherTag`, `finalTag`\n\nKind: **boolean**\nTemporary: **false**\nAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\n\nEnvironment: `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | Rollout | |\n| |`true` | `12.345%`|\n| |`false` | `87.655%`|\n| Off | `true` | |\n\n", comment, "they should be equal") } func (e *testCommentBuilder) AddedOnly(t *testing.T) { @@ -222,7 +223,7 @@ func (e *testProcessor) Basic(t *testing.T) { e.FlagsRef.FlagsAdded["example-flag"] = []string{""} processor := ProcessFlags(e.FlagsRef, e.Flags, &e.Config) expected := FlagComments{ - CommentsAdded: []string{"\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **production**\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n"}, + CommentsAdded: []string{"\n**[Sample Flag](https://example.com/test)** `example-flag`\nKind: **boolean**\nTemporary: **false**\n\n\nEnvironment: **Production** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n"}, } assert.Equal(t, expected, processor) } diff --git a/diff/diff.go b/diff/diff.go index 31ea8c79..74304122 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -61,9 +61,16 @@ func ProcessDiffs(raw *diff.Hunk, flagsRef ghc.FlagsRef, flags ldapi.FeatureFlag flagsRef.FlagsAdded[flag.Key] = currentKeys } if len(aliases[flag.Key]) > 0 { + CheckAliasAdded: for _, alias := range aliases[flag.Key] { if strings.Contains(row, alias) { currentKeys := flagsRef.FlagsAdded[flag.Key] + for i, _ := range currentKeys { + if alias == currentKeys[i] { + // If key already exists we do not want to add it + continue CheckAliasAdded + } + } currentKeys = append(currentKeys, alias) flagsRef.FlagsAdded[flag.Key] = currentKeys } @@ -78,9 +85,16 @@ func ProcessDiffs(raw *diff.Hunk, flagsRef ghc.FlagsRef, flags ldapi.FeatureFlag flagsRef.FlagsRemoved[flag.Key] = currentKeys } if len(aliases[flag.Key]) > 0 { + CheckAliasRemoved: for _, alias := range aliases[flag.Key] { if strings.Contains(row, alias) { currentKeys := flagsRef.FlagsRemoved[flag.Key] + for i, _ := range currentKeys { + // If key already exists we do not want to add it + if alias == currentKeys[i] { + continue CheckAliasRemoved + } + } currentKeys = append(currentKeys, alias) flagsRef.FlagsRemoved[flag.Key] = currentKeys } diff --git a/diff/diff_test.go b/diff/diff_test.go index ababd21d..2efa7282 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -178,6 +178,7 @@ func TestProcessDiffs(t *testing.T) { +this is for testing +here is a flag +exampleFlag ++exampleFlag +`, }, } diff --git a/testdata/test b/testdata/test index 58cd6b7f..9ef0f8bf 100644 --- a/testdata/test +++ b/testdata/test @@ -1,2 +1 @@ chatbox -show-widgets