From 6626bb23e682db0ef6b48276b4f3b36e0d8cc599 Mon Sep 17 00:00:00 2001 From: Dan O'Brien Date: Wed, 28 Apr 2021 14:57:16 -0400 Subject: [PATCH] Details (#12) --- comments/comments.go | 107 ++++++++++++++++++++------------------ comments/comments_test.go | 18 +++---- main.go | 30 +++++------ testdata/test | 3 +- 4 files changed, 83 insertions(+), 75 deletions(-) diff --git a/comments/comments.go b/comments/comments.go index a6fe4c5e..a407366d 100644 --- a/comments/comments.go +++ b/comments/comments.go @@ -7,6 +7,7 @@ import ( "fmt" "html" "html/template" + "log" "reflect" "sort" "strings" @@ -33,7 +34,7 @@ func isNil(a interface{}) bool { return a == nil || reflect.ValueOf(a).IsNil() } -func githubFlagComment(flag ldapi.FeatureFlag, aliases []string, config *config.Config) (string, error) { +func githubFlagComment(flag ldapi.FeatureFlag, aliases []string, config *config.Config) ([]string, error) { commentTemplate := Comment{ Flag: flag, Aliases: aliases, @@ -42,49 +43,56 @@ func githubFlagComment(flag ldapi.FeatureFlag, aliases []string, config *config. LDInstance: config.LdInstance, } var commentBody bytes.Buffer + // 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}}` + "`" + ` -{{- if .Flag.Description}} -*{{trim .Flag.Description}}* -{{- end}} -{{- if .Flag.Tags}} -Tags: {{ range $i, $e := .Flag.Tags }}` + "{{if $i}}, {{end}}`" + `{{$e}}` + "`" + `{{end}} -{{ end}} -Kind: **{{ .Flag.Kind }}** -Temporary: **{{ .Flag.Temporary }}** -{{- if .Aliases }} -{{- if ne (len .Aliases) 0}} -Aliases: {{range $i, $e := .Aliases }}` + "{{if $i}}, {{end}}`" + `{{$e}}` + "`" + `{{end}} -{{- end}} -{{- end}} -{{ "\n" }} -{{- range $key, $env := .Environments }} -Environment: {{ if .EnvironmentName }}**{{ .EnvironmentName }}** {{ end -}}` + "`" + `{{ $key }}` + "`" + ` -| Type | Variation | Weight(if Rollout) | -| --- | --- | --- | -{{- if not (isNil .Fallthrough_.Rollout) }} -{{- if not (isNil .Fallthrough_.Rollout.Variations)}} -| Default | Rollout | | -{{- range .Fallthrough_.Rollout.Variations }} -| |` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .Variation).Value) }}` + "` | `" + `{{ divf .Weight 1000 }}%` + "`|" + ` -{{- end }} -{{- end }} -{{- else }} -| Default | ` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .Fallthrough_.Variation).Value) }}` + "`| |" + ` -{{- end }} -{{- if kindIs "int32" .OffVariation }} -| Off | ` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .OffVariation).Value) }}` + "` | |" + ` -{{- else }} -Off variation: No off variation set. -{{- end }} -{{ end }} + **[{{.Flag.Name}}]({{.LDInstance}}{{.Primary.Site.Href}})** ` + "`" + `{{.Flag.Key}}` + "`" + ` + {{- if .Flag.Description}} + *{{trim .Flag.Description}}* + {{- end}} + {{- if .Flag.Tags}} + Tags: {{ range $i, $e := .Flag.Tags }}` + "{{if $i}}, {{end}}`" + `{{$e}}` + "`" + `{{end}} + {{ end}} + Kind: **{{ .Flag.Kind }}** + Temporary: **{{ .Flag.Temporary }}** + {{- if .Aliases }} + {{- if ne (len .Aliases) 0}} + Aliases: {{range $i, $e := .Aliases }}` + "{{if $i}}, {{end}}`" + `{{$e}}` + "`" + `{{end}} + {{- end}} + {{- end}} + {{ "\n" }} + {{- range $key, $env := .Environments }} + Environment: {{ if .EnvironmentName }}**{{ .EnvironmentName }}** {{ end -}}` + "`" + `{{ $key }}` + "`" + ` + | Type | Variation | Weight(if Rollout) | + | --- | --- | --- | + {{- if not (isNil .Fallthrough_.Rollout) }} + {{- if not (isNil .Fallthrough_.Rollout.Variations)}} + | Default | Rollout | | + {{- range .Fallthrough_.Rollout.Variations }} + | |` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .Variation).Value) }}` + "` | `" + `{{ divf .Weight 1000 }}%` + "`|" + ` + {{- end }} + {{- end }} + {{- else }} + | Default | ` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .Fallthrough_.Variation).Value) }}` + "`| |" + ` + {{- end }} + {{- if kindIs "int32" .OffVariation }} + | Off | ` + "`" + `{{ trunc 50 (toRawJson (index $.Flag.Variations .OffVariation).Value) }}` + "` | |" + ` + {{- else }} + Off variation: No off variation set. + {{- end }} + {{ end }} ` 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 { - return "", err + return []string{}, err } - return html.UnescapeString(commentBody.String()), nil + var commentStr []string + commentStr = append(commentStr, "\n\n") + commentStr = append(commentStr, fmt.Sprintf("-
%s", flag.Name)) + commentStr = append(commentStr, html.UnescapeString(commentBody.String())) + commentStr = append(commentStr, "
") + + return commentStr, nil } func GithubNoFlagComment() *github.IssueComment { @@ -108,18 +116,17 @@ type FlagsRef struct { func BuildFlagComment(buildComment FlagComments, flagsRef FlagsRef, existingComment *github.IssueComment) string { var commentStr []string - commentStr = append(commentStr, "LaunchDarkly Flag Details:") + commentStr = append(commentStr, "LaunchDarkly Flag Details, references to flags have been found in the diff:\n\n") if len(flagsRef.FlagsAdded) > 0 { - commentStr = append(commentStr, "** **Added/Modified** **") + commentStr = append(commentStr, fmt.Sprintf("Flag references: Added/Modified (%d)", len(flagsRef.FlagsAdded))) commentStr = append(commentStr, buildComment.CommentsAdded...) + //commentStr = append(commentStr, "") } if len(flagsRef.FlagsRemoved) > 0 { // Add in divider if there are both removed flags and already added/modified flags - if len(buildComment.CommentsAdded) > 0 { - commentStr = append(commentStr, "---") - } - commentStr = append(commentStr, "** **Removed** **") + commentStr = append(commentStr, fmt.Sprintf("Flag references: Removed (%d)", len(flagsRef.FlagsRemoved))) commentStr = append(commentStr, buildComment.CommentsRemoved...) + //commentStr = append(commentStr, "") } postedComments := strings.Join(commentStr, "\n") allFlagKeys := mergeKeys(flagsRef.FlagsAdded, flagsRef.FlagsRemoved) @@ -133,11 +140,11 @@ func BuildFlagComment(buildComment FlagComments, flagsRef FlagsRef, existingComm hash := md5.Sum([]byte(postedComments)) if existingComment != nil && strings.Contains(*existingComment.Body, hex.EncodeToString(hash[:])) { - fmt.Println("comment already exists") + log.Println("comment already exists") return "" } - postedComments = postedComments + "\n comment hash: " + hex.EncodeToString(hash[:]) + postedComments = postedComments + "\n " return postedComments } @@ -162,9 +169,9 @@ func ProcessFlags(flagsRef FlagsRef, flags ldapi.FeatureFlags, config *lcr.Confi } idx, _ := find(flags.Items, flagKey) createComment, err := githubFlagComment(flags.Items[idx], flagAliases, config) - buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment) + buildComment.CommentsAdded = append(buildComment.CommentsAdded, createComment...) if err != nil { - fmt.Println(err) + log.Println(err) } } removedKeys := make([]string, 0, len(flagsRef.FlagsRemoved)) @@ -182,9 +189,9 @@ func ProcessFlags(flagsRef FlagsRef, flags ldapi.FeatureFlags, config *lcr.Confi } idx, _ := find(flags.Items, flagKey) removedComment, err := githubFlagComment(flags.Items[idx], flagAliases, config) - buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment) + buildComment.CommentsRemoved = append(buildComment.CommentsRemoved, removedComment...) if err != nil { - fmt.Println(err) + log.Println(err) } } diff --git a/comments/comments_test.go b/comments/comments_test.go index 509bfc34..f9fc632a 100644 --- a/comments/comments_test.go +++ b/comments/comments_test.go @@ -139,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** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, []string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tKind: **boolean**\n\tTemporary: **false**\n\t\n\n\tEnvironment: **Production** `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | `true`| |\n\t| Off | `true` | |\n\t\n", "\t
"}, comment, "they should be equal") } func (e *testFlagEnv) Alias(t *testing.T) { @@ -147,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** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, []string([]string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tKind: **boolean**\n\tTemporary: **false**\n\tAliases: `exampleFlag`\n\t\n\n\tEnvironment: **Production** `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | `true`| |\n\t| Off | `true` | |\n\t\n", "\t
"}), comment, "they should be equal") } func (e *testFlagEnv) Tag(t *testing.T) { @@ -156,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** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, []string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tTags: `myTag`\n\t\n\tKind: **boolean**\n\tTemporary: **false**\n\t\n\n\tEnvironment: **Production** `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | `true`| |\n\t| Off | `true` | |\n\t\n", "\t
"}, comment, "they should be equal") } func (e *testFlagEnv) AliasesAndTags(t *testing.T) { @@ -165,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** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n", comment, "they should be equal") + assert.Equal(t, []string([]string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tTags: `myTag`, `otherTag`, `finalTag`\n\t\n\tKind: **boolean**\n\tTemporary: **false**\n\tAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\t\n\n\tEnvironment: **Production** `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | `true`| |\n\t| Off | `true` | |\n\t\n", "\t
"}), comment, "they should be equal") } func (e *testFlagEnv) RolloutFlag(t *testing.T) { @@ -193,21 +193,21 @@ 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, []string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tTags: `myTag`, `otherTag`, `finalTag`\n\t\n\tKind: **boolean**\n\tTemporary: **false**\n\tAliases: `exampleFlag`, `example_flag`, `ExampleFlag`\n\t\n\n\tEnvironment: `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | Rollout | |\n\t| |`true` | `12.345%`|\n\t| |`false` | `87.655%`|\n\t| Off | `true` | |\n\t\n", "\t
"}, comment, "they should be equal") } func (e *testCommentBuilder) AddedOnly(t *testing.T) { e.FlagsRef.FlagsAdded["example-flag"] = []string{} e.Comments.CommentsAdded = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) - assert.Equal(t, "LaunchDarkly Flag Details:\n** **Added/Modified** **\ncomment1\ncomment2\n \n comment hash: 9cb1c3634a34890f4d68da5e76c5e077", comment) + assert.Equal(t, "LaunchDarkly Flag Details, references to flags have been found in the diff:\n\n\nFlag references: Added/Modified (1)\ncomment1\ncomment2\n \n ", comment) } func (e *testCommentBuilder) RemovedOnly(t *testing.T) { e.FlagsRef.FlagsRemoved["example-flag"] = []string{} e.Comments.CommentsRemoved = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) - assert.Equal(t, "LaunchDarkly Flag Details:\n** **Removed** **\ncomment1\ncomment2\n \n comment hash: 433547b56a627f7867f327df688d424a", comment) + assert.Equal(t, "LaunchDarkly Flag Details, references to flags have been found in the diff:\n\n\nFlag references: Removed (1)\ncomment1\ncomment2\n \n ", comment) } func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) { @@ -216,14 +216,14 @@ func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) { e.Comments.CommentsAdded = []string{"comment1", "comment2"} e.Comments.CommentsRemoved = []string{"comment1", "comment2"} comment := BuildFlagComment(e.Comments, e.FlagsRef, nil) - assert.Equal(t, "LaunchDarkly Flag Details:\n** **Added/Modified** **\ncomment1\ncomment2\n---\n** **Removed** **\ncomment1\ncomment2\n \n comment hash: 0efefd950024b3d46ebf96dac1627fd4", comment) + assert.Equal(t, "LaunchDarkly Flag Details, references to flags have been found in the diff:\n\n\nFlag references: Added/Modified (1)\ncomment1\ncomment2\nFlag references: Removed (1)\ncomment1\ncomment2\n \n ", comment) } 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** `production`\n| Type | Variation | Weight(if Rollout) |\n| --- | --- | --- |\n| Default | `true`| |\n| Off | `true` | |\n\n"}, + CommentsAdded: []string{"\n\n", "-
Sample Flag", "\n\t**[Sample Flag](https://example.com/test)** `example-flag`\n\tKind: **boolean**\n\tTemporary: **false**\n\t\n\n\tEnvironment: **Production** `production`\n\t| Type | Variation | Weight(if Rollout) |\n\t| --- | --- | --- |\n\t| Default | `true`| |\n\t| Off | `true` | |\n\t\n", "\t
"}, } assert.Equal(t, expected, processor) } diff --git a/main.go b/main.go index c4ea4fbf..061135c4 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { event, err := parseEvent(os.Getenv("GITHUB_EVENT_PATH")) if err != nil { - fmt.Printf("error parsing GitHub event payload at %q: %v", os.Getenv("GITHUB_EVENT_PATH"), err) + log.Printf("error parsing GitHub event payload at %q: %v", os.Getenv("GITHUB_EVENT_PATH"), err) os.Exit(1) } @@ -45,7 +45,7 @@ func main() { failExit(err) if len(flags.Items) == 0 { - fmt.Println("No flags found.") + log.Println("No flags found.") os.Exit(0) } @@ -70,7 +70,7 @@ func main() { } } if err != nil { - fmt.Println(err) + log.Println(err) } existingComment := checkExistingComments(event, config, ctx) @@ -127,7 +127,7 @@ func getFlags(config *lcr.Config) (ldapi.FeatureFlags, []string, error) { func checkExistingComments(event *github.PullRequestEvent, config *lcr.Config, ctx context.Context) *github.IssueComment { comments, _, err := config.GHClient.Issues.ListComments(ctx, config.Owner, config.Repo[1], *event.PullRequest.Number, nil) if err != nil { - fmt.Println(err) + log.Println(err) } for _, comment := range comments { @@ -150,12 +150,12 @@ func postGithubComments(ctx context.Context, flagsRef ghc.FlagsRef, config *lcr. if existingCommentId > 0 { _, _, err := config.GHClient.Issues.EditComment(ctx, config.Owner, config.Repo[1], existingCommentId, &comment) if err != nil { - fmt.Println(err) + log.Println(err) } } else { _, _, err := config.GHClient.Issues.CreateComment(ctx, config.Owner, config.Repo[1], prNumber, &comment) if err != nil { - fmt.Println(err) + log.Println(err) } } @@ -167,10 +167,10 @@ func postGithubComments(ctx context.Context, flagsRef ghc.FlagsRef, config *lcr. createComment := ghc.GithubNoFlagComment() _, _, err := config.GHClient.Issues.CreateComment(ctx, config.Owner, config.Repo[1], prNumber, createComment) if err != nil { - fmt.Println(err) + log.Println(err) } } else { - fmt.Println("No flags found.") + log.Println("No flags found.") } } @@ -190,11 +190,11 @@ func getAliases(config *lcr.Config, flagKeys []string) (map[string][]string, err err := options.InitYAML() if err != nil { - fmt.Println(err) + log.Println(err) } opts, err := options.GetOptions() if err != nil { - fmt.Println(err) + log.Println(err) } return coderefs.GenerateAliases(flagKeys, opts.Aliases, config.Workspace) @@ -203,7 +203,7 @@ func getAliases(config *lcr.Config, flagKeys []string) (map[string][]string, err func failExit(err error) { if err != nil { - fmt.Println(err) + log.Println(err) os.Exit(1) } } @@ -311,7 +311,7 @@ func processCustomProps(flags ldapi.FeatureFlags, existingComment *github.IssueC } ldClient, err := lc.NewClient(config.ApiToken, config.LdInstance, false) if err != nil { - fmt.Println(err) + log.Println(err) } patchComment := ldapi.PatchComment{ Patch: []ldapi.PatchOperation{patch}, @@ -321,7 +321,7 @@ func processCustomProps(flags ldapi.FeatureFlags, existingComment *github.IssueC return ldClient.Ld.FeatureFlagsApi.PatchFeatureFlag(ldClient.Ctx, config.LdProject, k, patchComment) }) if err != nil { - fmt.Println(err) + log.Println(err) os.Exit(1) } } @@ -353,7 +353,7 @@ func processCustomProps(flags ldapi.FeatureFlags, existingComment *github.IssueC } ldClient, err := lc.NewClient(config.ApiToken, config.LdInstance, false) if err != nil { - fmt.Println(err) + log.Println(err) } patchComment := ldapi.PatchComment{ Patch: []ldapi.PatchOperation{patch}, @@ -363,7 +363,7 @@ func processCustomProps(flags ldapi.FeatureFlags, existingComment *github.IssueC return ldClient.Ld.FeatureFlagsApi.PatchFeatureFlag(ldClient.Ctx, config.LdProject, orphanKey, patchComment) }) if err != nil { - fmt.Println(err) + log.Println(err) os.Exit(1) } } diff --git a/testdata/test b/testdata/test index 9ef0f8bf..1900d3d9 100644 --- a/testdata/test +++ b/testdata/test @@ -1 +1,2 @@ -chatbox +show-widgets +beta-ui \ No newline at end of file