Skip to content

Commit

Permalink
rework message
Browse files Browse the repository at this point in the history
  • Loading branch information
jazanne committed Feb 23, 2024
1 parent 1a98420 commit a907951
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions internal/ldclient/flag_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ func CreateFlagLinks(config *lcr.Config, flagsRef flags.ReferenceSummary, event
return nil
}

for key := range flagsRef.FlagsAdded {
link := makeFlagLinkRep(event, key, "added")
numAdded := len(flagsRef.FlagsAdded)
numRemoved := len(flagsRef.FlagsRemoved)

for key, aliases := range flagsRef.FlagsAdded {
message := buildLinkMessage(key, aliases, "added", numAdded, numRemoved)
link := makeFlagLinkRep(event, key, message)
sendFlagRequest(config, *link, key)
}

for key := range flagsRef.FlagsRemoved {
message := "removed"
for key, aliases := range flagsRef.FlagsRemoved {
action := "removed"
if flagsRef.IsExtinct(key) {
message = "extinct"
action = "extinct"
}
message := buildLinkMessage(key, aliases, action, numAdded, numRemoved)
link := makeFlagLinkRep(event, key, message)
sendFlagRequest(config, *link, key)
}
Expand Down Expand Up @@ -84,15 +89,15 @@ func sendFlagRequest(config *lcr.Config, link ldapi.FlagLinkPost, flagKey string
log.Println(string(body))
}

func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, change string) *ldapi.FlagLinkPost {
func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, message string) *ldapi.FlagLinkPost {
pr := event.PullRequest
if pr == nil || pr.HTMLURL == nil || pr.ID == nil {
return nil
}

// TODO update metadata info https://github.com/launchdarkly/integration-framework/blob/main/integrations/slack-app/manifest.json
metadata := map[string]string{
"message": change,
"message": message,
"prNumber": strconv.Itoa(*pr.Number),
"prTitle": *pr.Title,
"state": *pr.State,
Expand Down Expand Up @@ -126,8 +131,9 @@ func makeFlagLinkRep(event *github.PullRequestEvent, flagKey, change string) *ld
IntegrationKey: &integration,
Timestamp: timestamp,
Title: getLinkTitle(event),
Description: pr.Body,
Metadata: &metadata,
// Description: pr.Body, TEMP
Description: &message,
Metadata: &metadata,
}
}

Expand All @@ -152,3 +158,33 @@ func getLinkTitle(event *github.PullRequestEvent) *string {

return &title
}

func buildLinkMessage(key string, aliases []string, action string, added, removed int) string {
builder := new(strings.Builder)
builder.WriteString(fmt.Sprintf("Flag `%s` %s", key, action))
if len(aliases) > 0 {
builder.WriteString(fmt.Sprintf(" (aliases: %s)", strings.Join(aliases, ", ")))
}

if added > 0 {
count := added
if action == "added" {
count--
}
if count > 0 {
builder.WriteString(fmt.Sprintf("\nAdded %d other flags)", count))
}
}

if removed > 0 {
count := removed
if action == "added" {
count--
}
if count > 0 {
builder.WriteString(fmt.Sprintf("\nRemoved %d other flags)", count))
}
}

return builder.String()
}

0 comments on commit a907951

Please sign in to comment.