|
|
@@ -1,7 +1,10 @@
|
|
|
package slack
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
+ "net/http"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
@@ -24,7 +27,7 @@ func NewIncidentsNotifier(conf *types.NotificationConfig, slackInts ...*integrat
|
|
|
}
|
|
|
|
|
|
func (s *IncidentsNotifier) NotifyNew(incident *porter_agent.Incident, url string) error {
|
|
|
- blockSet := &goslack.Blocks{}
|
|
|
+ res := []*SlackBlock{}
|
|
|
|
|
|
topSectionMarkdwn := fmt.Sprintf(
|
|
|
":warning: Your application %s crashed on Porter. <%s|View the incident.>",
|
|
|
@@ -35,34 +38,37 @@ func (s *IncidentsNotifier) NotifyNew(incident *porter_agent.Incident, url strin
|
|
|
namespace := strings.Split(incident.ID, ":")[2]
|
|
|
createdAt := time.Unix(incident.CreatedAt, 0).UTC()
|
|
|
|
|
|
- blockSet.BlockSet = append(blockSet.BlockSet, goslack.NewTextBlockObject(
|
|
|
- goslack.MarkdownType, topSectionMarkdwn, false, false,
|
|
|
- ), goslack.NewDividerBlock(), goslack.NewTextBlockObject(
|
|
|
- goslack.MarkdownType,
|
|
|
- fmt.Sprintf("*Name:* %s", "`"+incident.ReleaseName+"`"),
|
|
|
- false, false,
|
|
|
- ), goslack.NewTextBlockObject(
|
|
|
- goslack.MarkdownType,
|
|
|
- fmt.Sprintf("*Namespace:* %s", "`"+namespace+"`"),
|
|
|
- false, false,
|
|
|
- ), goslack.NewTextBlockObject(
|
|
|
- goslack.MarkdownType,
|
|
|
- fmt.Sprintf(
|
|
|
+ res = append(
|
|
|
+ res,
|
|
|
+ getMarkdownBlock(topSectionMarkdwn),
|
|
|
+ getDividerBlock(),
|
|
|
+ getMarkdownBlock(fmt.Sprintf("*Namespace:* %s", "`"+namespace+"`")),
|
|
|
+ getMarkdownBlock(fmt.Sprintf("*Name:* %s", "`"+incident.ReleaseName+"`")),
|
|
|
+ getMarkdownBlock(fmt.Sprintf(
|
|
|
"*Created at:* <!date^%d^Alerted at {date_num} {time_secs}|Alerted at %s>",
|
|
|
createdAt.Unix(),
|
|
|
createdAt.Format("2006-01-02 15:04:05 UTC"),
|
|
|
- ),
|
|
|
- false, false,
|
|
|
- ), goslack.NewTextBlockObject(
|
|
|
- goslack.MarkdownType, fmt.Sprintf("```\n%s\n```", incident.LatestMessage), false, false,
|
|
|
- ))
|
|
|
+ )),
|
|
|
+ getMarkdownBlock(fmt.Sprintf("```\n%s\n```", incident.LatestMessage)),
|
|
|
+ )
|
|
|
+
|
|
|
+ slackPayload := &SlackPayload{
|
|
|
+ Blocks: res,
|
|
|
+ }
|
|
|
+
|
|
|
+ payload, err := json.Marshal(slackPayload)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ reqBody := bytes.NewReader(payload)
|
|
|
+ client := &http.Client{
|
|
|
+ Timeout: time.Second * 5,
|
|
|
+ }
|
|
|
|
|
|
for _, slackInt := range s.slackInts {
|
|
|
- err := goslack.PostWebhook(string(slackInt.Webhook), &goslack.WebhookMessage{
|
|
|
- Username: "Porter Agent",
|
|
|
- Channel: slackInt.Channel,
|
|
|
- Blocks: blockSet,
|
|
|
- })
|
|
|
+ _, err := client.Post(string(slackInt.Webhook), "application/json", reqBody)
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|