fixed notifications dispatch
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
This commit is contained in:
@@ -6,23 +6,50 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type parseMode string
|
||||
|
||||
const (
|
||||
parseModeUnset parseMode = ""
|
||||
parseModeMarkdown parseMode = "markdown"
|
||||
parseModeMarkdownV2 parseMode = "markdownV2"
|
||||
parseModeHTML parseMode = "HTML"
|
||||
)
|
||||
|
||||
func normalizeParseMode(value string) parseMode {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "markdown":
|
||||
return parseModeMarkdown
|
||||
case "markdownv2":
|
||||
return parseModeMarkdownV2
|
||||
case "html":
|
||||
return parseModeHTML
|
||||
default:
|
||||
return parseModeUnset
|
||||
}
|
||||
}
|
||||
|
||||
func (pm parseMode) String() string {
|
||||
return string(pm)
|
||||
}
|
||||
|
||||
type messageField struct {
|
||||
label string
|
||||
value string
|
||||
}
|
||||
|
||||
type messageTemplate struct {
|
||||
title string
|
||||
fields []messageField
|
||||
title string
|
||||
fields []messageField
|
||||
emphasize []string
|
||||
}
|
||||
|
||||
func (mt messageTemplate) Format(parseMode string) string {
|
||||
func (mt messageTemplate) Format(mode parseMode) string {
|
||||
var builder strings.Builder
|
||||
builder.WriteString(mt.title)
|
||||
builder.WriteString(formatTitle(mode, mt.title, mt.emphasize))
|
||||
builder.WriteString("\n")
|
||||
builder.WriteString("-----------------------------\n")
|
||||
|
||||
formatter := selectValueFormatter(parseMode)
|
||||
formatter := selectValueFormatter(mode)
|
||||
for _, field := range mt.fields {
|
||||
appendMessageField(&builder, field.label, field.value, formatter)
|
||||
}
|
||||
@@ -31,6 +58,53 @@ func (mt messageTemplate) Format(parseMode string) string {
|
||||
|
||||
type valueFormatter func(string) string
|
||||
|
||||
func formatTitle(mode parseMode, title string, emphasize []string) string {
|
||||
switch mode {
|
||||
case parseModeMarkdown:
|
||||
return highlightMarkdown(title, emphasize, escapeMarkdown)
|
||||
case parseModeMarkdownV2:
|
||||
return highlightMarkdown(title, emphasize, escapeMarkdownV2)
|
||||
case parseModeHTML:
|
||||
return highlightHTML(title, emphasize)
|
||||
default:
|
||||
return title
|
||||
}
|
||||
}
|
||||
|
||||
func highlightMarkdown(title string, emphasize []string, esc func(string) string) string {
|
||||
if len(emphasize) == 0 {
|
||||
return title
|
||||
}
|
||||
result := title
|
||||
for _, word := range emphasize {
|
||||
word = strings.TrimSpace(word)
|
||||
if word == "" {
|
||||
continue
|
||||
}
|
||||
escaped := esc(word)
|
||||
replacement := fmt.Sprintf("*%s*", escaped)
|
||||
result = strings.ReplaceAll(result, word, replacement)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func highlightHTML(title string, emphasize []string) string {
|
||||
if len(emphasize) == 0 {
|
||||
return html.EscapeString(title)
|
||||
}
|
||||
result := html.EscapeString(title)
|
||||
for _, word := range emphasize {
|
||||
word = strings.TrimSpace(word)
|
||||
if word == "" {
|
||||
continue
|
||||
}
|
||||
escaped := html.EscapeString(word)
|
||||
replacement := fmt.Sprintf("<b>%s</b>", escaped)
|
||||
result = strings.ReplaceAll(result, escaped, replacement)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func appendMessageField(builder *strings.Builder, label, value string, formatter valueFormatter) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
@@ -41,17 +115,17 @@ func appendMessageField(builder *strings.Builder, label, value string, formatter
|
||||
fmt.Fprintf(builder, "• %s: %s\n", label, value)
|
||||
}
|
||||
|
||||
func selectValueFormatter(parseMode string) valueFormatter {
|
||||
switch strings.ToLower(parseMode) {
|
||||
case "markdown":
|
||||
func selectValueFormatter(mode parseMode) valueFormatter {
|
||||
switch mode {
|
||||
case parseModeMarkdown:
|
||||
return func(value string) string {
|
||||
return fmt.Sprintf("*%s*", escapeMarkdown(value))
|
||||
}
|
||||
case "markdownv2":
|
||||
case parseModeMarkdownV2:
|
||||
return func(value string) string {
|
||||
return fmt.Sprintf("*%s*", escapeMarkdownV2(value))
|
||||
}
|
||||
case "html":
|
||||
case parseModeHTML:
|
||||
return func(value string) string {
|
||||
return fmt.Sprintf("<b>%s</b>", html.EscapeString(value))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user