2017-06-09 14:27:06 +02:00
|
|
|
package circleci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
libHTTP "github.com/genofire/golang-lib/http"
|
|
|
|
"github.com/genofire/golang-lib/log"
|
|
|
|
xmpp "github.com/mattn/go-xmpp"
|
|
|
|
|
2017-06-10 16:31:56 +02:00
|
|
|
"github.com/genofire/hook2xmpp/config"
|
|
|
|
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
2017-06-09 14:27:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
client *xmpp.Client
|
|
|
|
hooks map[string]config.Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(client *xmpp.Client, newHooks []config.Hook) *Handler {
|
|
|
|
hooks := make(map[string]config.Hook)
|
|
|
|
|
|
|
|
for _, hook := range newHooks {
|
|
|
|
if hook.Type == "circleci" {
|
2017-06-10 16:07:23 +02:00
|
|
|
hooks[hook.URL] = hook
|
2017-06-09 14:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Handler{
|
|
|
|
client: client,
|
|
|
|
hooks: hooks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-06-09 15:51:04 +02:00
|
|
|
var body map[string]interface{}
|
|
|
|
libHTTP.Read(r, &body)
|
|
|
|
payload := body["payload"].(map[string]interface{})
|
2017-06-10 16:07:23 +02:00
|
|
|
vcsURL, ok := payload["vcs_url"].(string)
|
2017-06-09 15:51:04 +02:00
|
|
|
if !ok {
|
|
|
|
log.Log.Error(r.Body)
|
|
|
|
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-06-09 14:27:06 +02:00
|
|
|
|
2017-06-10 16:07:23 +02:00
|
|
|
hook, ok := h.hooks[vcsURL]
|
2017-06-09 14:27:06 +02:00
|
|
|
if !ok {
|
2017-06-10 16:07:23 +02:00
|
|
|
log.Log.Errorf("No hook found for: '%s'", vcsURL)
|
|
|
|
http.Error(w, fmt.Sprintf("no configuration for circleci for url %s", vcsURL), http.StatusNotFound)
|
2017-06-09 14:27:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
status := payload["status"].(string)
|
2017-06-09 15:51:04 +02:00
|
|
|
buildNum := payload["build_num"].(float64)
|
2017-06-09 14:27:06 +02:00
|
|
|
buildURL := payload["build_url"].(string)
|
2017-06-09 15:51:04 +02:00
|
|
|
buildTime := payload["build_time_millis"].(float64)
|
2017-06-09 14:27:06 +02:00
|
|
|
subject := payload["subject"].(string)
|
2017-06-10 16:31:56 +02:00
|
|
|
msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", vcsURL, status, buildTime/1000, buildNum, subject, buildURL)
|
2017-06-09 14:27:06 +02:00
|
|
|
|
2017-06-09 15:51:04 +02:00
|
|
|
log.Log.WithField("type", "circleci").Print(msg)
|
2017-06-09 14:27:06 +02:00
|
|
|
ownXMPP.Notify(h.client, hook, msg)
|
|
|
|
}
|