Skip to content

Commit

Permalink
Details (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
InTheCloudDan authored Apr 28, 2021
1 parent c334411 commit 6626bb2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 75 deletions.
107 changes: 57 additions & 50 deletions comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"html"
"html/template"
"log"
"reflect"
"sort"
"strings"
Expand All @@ -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,
Expand All @@ -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("- <details><summary> %s</summary>", flag.Name))
commentStr = append(commentStr, html.UnescapeString(commentBody.String()))
commentStr = append(commentStr, " </details>")

return commentStr, nil
}

func GithubNoFlagComment() *github.IssueComment {
Expand All @@ -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, "</details>")
}
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, "</details>")
}
postedComments := strings.Join(commentStr, "\n")
allFlagKeys := mergeKeys(flagsRef.FlagsAdded, flagsRef.FlagsRemoved)
Expand All @@ -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 <!-- comment hash: " + hex.EncodeToString(hash[:]) + " -->"
return postedComments
}

Expand All @@ -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))
Expand All @@ -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)
}
}

Expand Down
18 changes: 9 additions & 9 deletions comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,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** `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", "- <details><summary> Sample Flag</summary>", "\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</details>"}, 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** `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", "- <details><summary> Sample Flag</summary>", "\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</details>"}), comment, "they should be equal")
}

func (e *testFlagEnv) Tag(t *testing.T) {
Expand All @@ -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", "- <details><summary> Sample Flag</summary>", "\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</details>"}, comment, "they should be equal")
}

func (e *testFlagEnv) AliasesAndTags(t *testing.T) {
Expand All @@ -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", "- <details><summary> Sample Flag</summary>", "\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</details>"}), comment, "they should be equal")
}

func (e *testFlagEnv) RolloutFlag(t *testing.T) {
Expand Down Expand Up @@ -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", "- <details><summary> Sample Flag</summary>", "\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</details>"}, 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 <!-- flags:example-flag -->\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 <!-- flags:example-flag -->\n <!-- comment hash: b47fc43b30d97bf647a43d48ce1b85fd -->", 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 <!-- flags:example-flag -->\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 <!-- flags:example-flag -->\n <!-- comment hash: e39d16108df3e5369f43176ab1a12323 -->", comment)
}

func (e *testCommentBuilder) AddedAndRemoved(t *testing.T) {
Expand All @@ -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 <!-- flags:example-flag -->\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 <!-- flags:example-flag -->\n <!-- comment hash: e9b00faf6005ae2ed28911472366fcc0 -->", 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", "- <details><summary> Sample Flag</summary>", "\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</details>"},
}
assert.Equal(t, expected, processor)
}
30 changes: 15 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -70,7 +70,7 @@ func main() {
}
}
if err != nil {
fmt.Println(err)
log.Println(err)
}

existingComment := checkExistingComments(event, config, ctx)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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.")
}
}

Expand All @@ -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)
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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},
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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},
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion testdata/test
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
chatbox
show-widgets
beta-ui

0 comments on commit 6626bb2

Please sign in to comment.