export xmpp options

This commit is contained in:
Martin Geno 2017-06-09 15:51:04 +02:00
parent 7fc17fed2d
commit 70109f5645
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 45 additions and 17 deletions

View File

@ -33,9 +33,15 @@ func NewHandler(client *xmpp.Client, newHooks []config.Hook) *Handler {
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
libHTTP.Read(r, &payload)
username := payload["username"].(string)
var body map[string]interface{}
libHTTP.Read(r, &body)
payload := body["payload"].(map[string]interface{})
username, ok := payload["username"].(string)
if !ok {
log.Log.Error(r.Body)
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return
}
reponame := payload["reponame"].(string)
repoFullName := fmt.Sprintf("%s/%s", username, reponame)
@ -46,11 +52,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
status := payload["status"].(string)
buildNum := payload["build_num"].(int)
buildNum := payload["build_num"].(float64)
buildURL := payload["build_url"].(string)
buildTime := payload["build_time_millis"].(int)
buildTime := payload["build_time_millis"].(float64)
subject := payload["subject"].(string)
msg := fmt.Sprintf("[%s/%s] %s (%dms) - #%d: %s \n%s", username, reponame, status, buildTime, buildNum, subject, buildURL)
msg := fmt.Sprintf("[%s/%s] %s (%0.fms) - #%0.f: %s \n%s", username, reponame, status, buildTime, buildNum, subject, buildURL)
log.Log.WithField("type", "circleci").Print(msg)
ownXMPP.Notify(h.client, hook, msg)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/genofire/golang-lib/log"
"github.com/mattn/go-xmpp"
"github.com/pierrre/githubhook"
"github.com/genofire/hook2xmpp/circleci"
configuration "github.com/genofire/hook2xmpp/config"
@ -24,7 +23,17 @@ func main() {
// load config
config := configuration.ReadConfigFile(configFile)
client, err := xmpp.NewClientNoTLS(config.XMPP.Host, config.XMPP.Username, config.XMPP.Password, config.XMPP.Debug)
options := xmpp.Options{
Host: config.XMPP.Host,
User: config.XMPP.Username,
Password: config.XMPP.Password,
NoTLS: config.XMPP.NoTLS,
Debug: config.XMPP.Debug,
Session: config.XMPP.Session,
Status: config.XMPP.Status,
StatusMessage: config.XMPP.StatusMessage,
}
client, err := options.NewClient()
if err != nil {
log.Log.Panic(err)
}
@ -34,14 +43,12 @@ func main() {
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"})
go ownXMPP.Start(client)
githubHandler := github.NewHandler(client, config.Hooks)
handler := &githubhook.Handler{
Delivery: githubHandler.Deliviery,
}
http.Handle("/github", handler)
circleciHandler := circleci.NewHandler(client, config.Hooks)
http.Handle("/circleci", circleciHandler)
githubHandler := github.NewHandler(client, config.Hooks)
http.Handle("/github", githubHandler)
srv := &http.Server{
Addr: config.WebserverBind,
}

View File

@ -16,6 +16,10 @@ type Config struct {
Username string `toml:"username"`
Password string `toml:"password"`
Debug bool `toml:"debug"`
NoTLS bool `toml:"no_tls"`
Session bool `toml:"session"`
Status string `toml:"status"`
StatusMessage string `toml:"status_message"`
StartupNotify string `toml:"startup_notify"`
} `toml:"xmpp"`

View File

@ -2,12 +2,14 @@ package github
import (
"fmt"
"net/http"
"strings"
libHTTP "github.com/genofire/golang-lib/http"
"github.com/genofire/golang-lib/log"
"github.com/genofire/hook2xmpp/config"
xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/config"
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
)
@ -30,9 +32,16 @@ func NewHandler(client *xmpp.Client, newHooks []config.Hook) *Handler {
}
}
func (h *Handler) Deliviery(event string, deliveryID string, payloadOrigin interface{}) {
msg := PayloadToString(event, payloadOrigin)
payload := payloadOrigin.(map[string]interface{})
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
event := r.Header.Get("X-GitHub-Event")
if event == "status" {
return
}
libHTTP.Read(r, &payload)
msg := PayloadToString(event, payload)
repository := payload["repository"].(map[string]interface{})
repoName := repository["full_name"].(string)
@ -42,6 +51,7 @@ func (h *Handler) Deliviery(event string, deliveryID string, payloadOrigin inter
return
}
log.Log.WithField("type", "github").Print(msg)
ownXMPP.Notify(h.client, hook, msg)
}