Skip to content

Commit

Permalink
Add env name (#10)
Browse files Browse the repository at this point in the history
* use environment name if available

* remove test flag

* do not append same alias multiple times
  • Loading branch information
InTheCloudDan authored Apr 22, 2021
1 parent 09a4a32 commit 01f76b2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
Expand Down
13 changes: 7 additions & 6 deletions comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newTestAccEnv() *testFlagEnv {

func createFlag(key string) ldapi.FeatureFlag {
environment := ldapi.FeatureFlagConfig{
EnvironmentName: "Production",
Site: &ldapi.Site{
Href: "test",
},
Expand Down Expand Up @@ -138,15 +139,15 @@ 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) {
comment, err := githubFlagComment(e.Flag, []string{"exampleFlag"}, &e.Config)
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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func TestProcessDiffs(t *testing.T) {
+this is for testing
+here is a flag
+exampleFlag
+exampleFlag
+`,
},
}
Expand Down
1 change: 0 additions & 1 deletion testdata/test
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
chatbox
show-widgets

0 comments on commit 01f76b2

Please sign in to comment.