matrix-prometheus/internal/webhook/handle.go

59 lines
1.3 KiB
Go
Raw Normal View History

2023-01-13 21:24:39 +01:00
package webhook
import (
"bytes"
"encoding/json"
"log"
"net/http"
"maunium.net/go/mautrix/format"
2023-01-13 21:24:39 +01:00
)
const (
messageTemplate = "\U0001f6a8" +
` **Alerts for group "_{{ .GroupKey }}_":**{{ range .Alerts }}
2023-03-07 16:02:40 +01:00
` + "\u2022" + ` {{ if eq .Status "` + StatusResolved + `" }}` + "\u2705" +
`{{ else }}{{ with .Labels.severity }}{{ if eq . "critical" }}` + "\U0001f525" +
`{{ else if eq . "warning" }}` + "\U0001f4e2" +
`{{ else }}` + "\U0001f514" +
`{{ end }}{{ else }}` + "\U0001f514" +
2023-03-07 16:02:40 +01:00
`{{ end }}{{ end }} {{ .Annotations.description }}{{ end }}
@room`
2023-01-13 21:24:39 +01:00
)
func (s Server) handleWebhook(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") != "application/json" {
writeStatus(w, http.StatusBadRequest)
return
}
decoder := json.NewDecoder(r.Body)
var payload Payload
err := decoder.Decode(&payload)
if err != nil {
writeStatus(w, http.StatusBadRequest)
return
}
if payload.Version != "4" {
writeStatus(w, http.StatusBadRequest)
return
}
var message bytes.Buffer
err = s.tmpl.Execute(&message, payload)
if err != nil {
log.Print(err)
writeStatus(w, http.StatusInternalServerError)
return
}
evt := format.RenderMarkdown(message.String(), true, false)
2023-01-13 21:24:39 +01:00
success := s.matrix.Broadcast(&evt)
if !success {
writeStatus(w, http.StatusInternalServerError)
}
}