kopia lustrzana https://github.com/tmsmr/xmpp-webhook
commit
ba6aa0b0d5
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"channel": "#channel",
|
||||||
|
"icon_emoji": ":heart:",
|
||||||
|
"username": "Flux Deployer",
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"color": "#4286f4",
|
||||||
|
"title": "Applied flux changes to cluster",
|
||||||
|
"title_link": "https://GITURL/USERNAME/kubernetes/commit/COMMITSHA",
|
||||||
|
"text": "Event: Sync: 0f34755, jabber:deployment/test\nCommits:\n\n* \u003chttps://GITURL/USERNAME/kubernetes/commit/COMMITSHA\u003e: change test to test webhook\n\nResources updated:\n\n* jabber:deployment/test"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
83
handler.go
83
handler.go
|
@ -6,8 +6,17 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Message string
|
||||||
|
Code int
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorResponse() Response {
|
||||||
|
return Response{"", http.StatusInternalServerError}
|
||||||
|
}
|
||||||
|
|
||||||
// interface for parser functions (grafana, prometheus, ...)
|
// interface for parser functions (grafana, prometheus, ...)
|
||||||
type parserFunc func(*http.Request) (string, error)
|
type parserFunc func(*http.Request) (string, error, Response)
|
||||||
|
|
||||||
type messageHandler struct {
|
type messageHandler struct {
|
||||||
messages chan<- string // chan to xmpp client
|
messages chan<- string // chan to xmpp client
|
||||||
|
@ -17,13 +26,14 @@ type messageHandler struct {
|
||||||
// http request handler
|
// http request handler
|
||||||
func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// parse/generate message from http request
|
// parse/generate message from http request
|
||||||
m, err := h.parserFunc(r)
|
m, err, response := h.parserFunc(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
// send message to xmpp client
|
// send message to xmpp client
|
||||||
h.messages <- m
|
h.messages <- m
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(response.Code)
|
||||||
|
w.Write([]byte(response.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns new handler with a given parser function
|
// returns new handler with a given parser function
|
||||||
|
@ -37,11 +47,11 @@ func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
|
||||||
/*************
|
/*************
|
||||||
GRAFANA PARSER
|
GRAFANA PARSER
|
||||||
*************/
|
*************/
|
||||||
func grafanaParserFunc(r *http.Request) (string, error) {
|
func grafanaParserFunc(r *http.Request) (string, error, Response) {
|
||||||
// get alert data from request
|
// get alert data from request
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err, errorResponse()
|
||||||
}
|
}
|
||||||
|
|
||||||
// grafana alert struct
|
// grafana alert struct
|
||||||
|
@ -55,7 +65,7 @@ func grafanaParserFunc(r *http.Request) (string, error) {
|
||||||
// parse body into the alert struct
|
// parse body into the alert struct
|
||||||
err = json.Unmarshal(body, &alert)
|
err = json.Unmarshal(body, &alert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err, errorResponse()
|
||||||
}
|
}
|
||||||
|
|
||||||
// contruct alert message
|
// contruct alert message
|
||||||
|
@ -67,5 +77,64 @@ func grafanaParserFunc(r *http.Request) (string, error) {
|
||||||
message = ":( " + alert.Title + "\n" + alert.Message + "\n" + alert.RuleURL
|
message = ":( " + alert.Title + "\n" + alert.Message + "\n" + alert.RuleURL
|
||||||
}
|
}
|
||||||
|
|
||||||
return message, nil
|
return message, nil, Response{"", http.StatusNoContent}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************
|
||||||
|
SLACK PARSER
|
||||||
|
*************/
|
||||||
|
type SlackMessage struct {
|
||||||
|
Channel string `json:"channel"`
|
||||||
|
IconEmoji string `json:"icon_emoji"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
Attachments []SlackAttachment `json:"attachments"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SlackAttachment struct {
|
||||||
|
Color string `json:"color"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
TitleLink string `json:"title_link"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func nonemptyAppendNewline(message string) string {
|
||||||
|
if len(message) == 0 {
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
return message + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
func slackParserFunc(r *http.Request) (string, error, Response) {
|
||||||
|
// get alert data from request
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err, errorResponse()
|
||||||
|
}
|
||||||
|
|
||||||
|
// grafana alert struct
|
||||||
|
alert := SlackMessage{}
|
||||||
|
|
||||||
|
// parse body into the alert struct
|
||||||
|
err = json.Unmarshal(body, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return "", err, Response{"", http.StatusInternalServerError}
|
||||||
|
}
|
||||||
|
|
||||||
|
// contruct alert message
|
||||||
|
message := ""
|
||||||
|
hasText := (alert.Text != "")
|
||||||
|
if hasText {
|
||||||
|
message = alert.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, attachment := range alert.Attachments {
|
||||||
|
message = nonemptyAppendNewline(message)
|
||||||
|
message += attachment.Title + "\n"
|
||||||
|
message += attachment.TitleLink + "\n\n"
|
||||||
|
message += attachment.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
return message, nil, Response{"ok", http.StatusOK}
|
||||||
}
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -115,6 +115,7 @@ func main() {
|
||||||
reply := MessageBody{
|
reply := MessageBody{
|
||||||
Message: stanza.Message{
|
Message: stanza.Message{
|
||||||
To: msg.From.Bare(),
|
To: msg.From.Bare(),
|
||||||
|
From: address,
|
||||||
Type: stanza.ChatMessage,
|
Type: stanza.ChatMessage,
|
||||||
},
|
},
|
||||||
Body: msg.Body,
|
Body: msg.Body,
|
||||||
|
@ -140,6 +141,7 @@ func main() {
|
||||||
_ = xmppSession.Encode(MessageBody{
|
_ = xmppSession.Encode(MessageBody{
|
||||||
Message: stanza.Message{
|
Message: stanza.Message{
|
||||||
To: recipient,
|
To: recipient,
|
||||||
|
From: address,
|
||||||
Type: stanza.ChatMessage,
|
Type: stanza.ChatMessage,
|
||||||
},
|
},
|
||||||
Body: m,
|
Body: m,
|
||||||
|
@ -150,6 +152,7 @@ func main() {
|
||||||
|
|
||||||
// initialize handler for grafana alerts
|
// initialize handler for grafana alerts
|
||||||
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
|
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
|
||||||
|
http.Handle("/slack", newMessageHandler(messages, slackParserFunc))
|
||||||
|
|
||||||
// listen for requests
|
// listen for requests
|
||||||
_ = http.ListenAndServe(":4321", nil)
|
_ = http.ListenAndServe(":4321", nil)
|
||||||
|
|
Ładowanie…
Reference in New Issue