Skip to content

Commit

Permalink
Merge pull request #26 from umomany/master
Browse files Browse the repository at this point in the history
fetch room name from payload if not passed as argument
  • Loading branch information
mr-karan authored Mar 25, 2021
2 parents e4ab7db + ab033e4 commit 3b37afe
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,27 @@ func handleNewAlert(a *App, w http.ResponseWriter, r *http.Request) (code int, m
alertData = alerttemplate.Data{}
n = a.notifier
)
// decode request payload from Alertmanager in a struct
if err := json.NewDecoder(r.Body).Decode(&alertData); err != nil {
errMsg := fmt.Sprintf("Error while decoding alertmanager response: %s", err)
return http.StatusBadRequest, errMsg, nil, excepBadRequest, err
}
// fetch the room_name param. This room_name is used to map the webhook URL from the config.
// just an abstraction, for a more humanised version and to not end up making alertmanager config
// a mess by not flooding with google chat webhook URLs all over the place.
roomName := r.URL.Query().Get("room_name")
if roomName == "" {
return http.StatusBadRequest, "Missing required room_name param", nil, excepBadRequest, err
// Attempt to fetch the room name from the alert payload
roomName = alertData.Alerts[0].Labels["room_name"]
if roomName == "" {
return http.StatusBadRequest, "Missing required room_name param", nil, excepBadRequest, err
}
}
webHookURL := viper.GetString(fmt.Sprintf("app.chat.%s.notification_url", roomName))
if webHookURL == "" {
errMsg := fmt.Sprintf("Webhook URL not configured for room_name: %s", roomName)
return http.StatusBadRequest, errMsg, nil, excepBadRequest, err
}
// decode request payload from Alertmanager in a struct
if err := json.NewDecoder(r.Body).Decode(&alertData); err != nil {
errMsg := fmt.Sprintf("Error while decoding alertmanager response: %s", err)
return http.StatusBadRequest, errMsg, nil, excepBadRequest, err
}
// send notification to chat
err = sendMessageToChat(alertData.Alerts, &n, webHookURL)
if err != nil {
Expand Down

0 comments on commit 3b37afe

Please sign in to comment.