improve logging + xmpp join every muc

This commit is contained in:
Martin/Geno 2019-02-13 03:55:07 +01:00
parent afdbf52d94
commit c650f280f5
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
5 changed files with 83 additions and 32 deletions

View File

@ -2,47 +2,55 @@ package circleci
import (
"fmt"
"log"
"net/http"
"github.com/bdlm/log"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp"
"dev.sum7.eu/genofire/hook2xmpp/runtime"
)
const hookType = "git"
func init() {
runtime.HookRegister["circleci"] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType)
var body map[string]interface{}
libHTTP.Read(r, &body)
payload := body["payload"].(map[string]interface{})
vcsURL, ok := payload["vcs_url"].(string)
url, ok := payload["vcs_url"].(string)
if !ok {
log.Fatal("no readable payload")
logger.Error("no readable payload")
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return
}
logger = logger.WithField("url", url)
status := payload["status"].(string)
buildNum := payload["build_num"].(float64)
buildURL := payload["build_url"].(string)
buildTime := payload["build_time_millis"].(float64)
subject := payload["subject"].(string)
msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", vcsURL, status, buildTime/1000, buildNum, subject, buildURL)
msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", url, status, buildTime/1000, buildNum, subject, buildURL)
logger = logger.WithField("msg", msg)
ok = false
for _, hook := range hooks {
if vcsURL != hook.URL {
if url != hook.URL {
continue
}
log.Printf("run hook for circleci: %s", msg)
logger.Infof("run hook")
runtime.Notify(client, hook, msg)
ok = true
}
if !ok {
log.Fatalf("No hook found for: '%s'", vcsURL)
http.Error(w, fmt.Sprintf("no configuration for circleci for url %s", vcsURL), http.StatusNotFound)
logger.Warnf("no hook found")
http.Error(w, fmt.Sprintf("no configuration for circleci for url: %s", url), http.StatusNotFound)
}
}
}

View File

@ -2,9 +2,10 @@ package git
import (
"fmt"
"log"
"net/http"
"github.com/bdlm/log"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp"
@ -13,10 +14,13 @@ import (
var eventHeader = []string{"X-GitHub-Event", "X-Gogs-Event"}
const hookType = "git"
func init() {
runtime.HookRegister["git"] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
logger := log.WithField("type", hookType)
event := ""
for _, head := range eventHeader {
event = r.Header.Get(head)
@ -30,23 +34,33 @@ func init() {
return
}
libHTTP.Read(r, &payload)
msg := PayloadToString(event, payload)
repository := payload["repository"].(map[string]interface{})
url := repository["html_url"].(string)
var body map[string]interface{}
libHTTP.Read(r, &body)
ok := false
repository := body["repository"].(map[string]interface{})
url, ok := repository["html_url"].(string)
if !ok {
logger.Error("no readable payload")
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return
}
logger = logger.WithField("url", url)
msg := PayloadToString(event, body)
logger = logger.WithField("msg", msg)
ok = false
for _, hook := range hooks {
if url != hook.URL {
continue
}
log.Printf("run hook for git: %s", msg)
logger.Infof("run hook")
runtime.Notify(client, hook, msg)
ok = true
}
if !ok {
log.Fatalf("No hook found for: '%s'", url)
http.Error(w, fmt.Sprintf("no configuration for git for url %s", url), http.StatusNotFound)
logger.Warnf("no hook found")
http.Error(w, fmt.Sprintf("no configuration for git for url: %s", url), http.StatusNotFound)
}
}
}

46
main.go
View File

@ -2,15 +2,14 @@ package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/mattn/go-xmpp"
"dev.sum7.eu/genofire/golang-lib/file"
"github.com/bdlm/log"
"github.com/mattn/go-xmpp"
_ "dev.sum7.eu/genofire/hook2xmpp/circleci"
_ "dev.sum7.eu/genofire/hook2xmpp/git"
@ -41,12 +40,9 @@ func main() {
}
client, err := options.NewClient()
if err != nil {
log.Panic(err)
log.Panicf("error on startup xmpp client: %s",err)
}
log.Printf("Started hock2xmpp with %s", client.JID())
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"})
go runtime.Start(client)
for hookType, getHandler := range runtime.HookRegister {
@ -65,14 +61,46 @@ func main() {
}
}()
var mucs []string
for _, muc := range config.StartupNotifyMuc {
mucs = append(mucs, muc)
client.JoinMUCNoHistory(muc, config.Nickname)
}
for _, hooks := range config.Hooks {
for _, hook := range hooks {
for _, muc := range hook.NotifyMuc {
mucs = append(mucs, muc)
client.JoinMUCNoHistory(muc, config.Nickname)
}
}
}
notify := func (msg string) {
for _, muc := range config.StartupNotifyMuc {
client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg})
}
for _, user := range config.StartupNotifyUser {
client.SendHtml(xmpp.Chat{Remote: user, Type: "chat", Text: msg})
}
}
notify("startup of hock2xmpp")
log.Infof("started hock2xmpp with %s", client.JID())
// Wait for system signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "stopped of hock2xmpp"})
notify("stopped of hock2xmpp")
for _, muc := range mucs {
client.LeaveMUC(muc)
}
srv.Close()
client.Close()
log.Print("received", sig)
log.Infof("closed by receiving: %s", sig)
}

View File

@ -12,8 +12,9 @@ type Config struct {
Session bool `toml:"session"`
Status string `toml:"status"`
StatusMessage string `toml:"status_message"`
StartupNotify string `toml:"startup_notify"`
} `toml:"xmpp"`
Nickname string `toml:"nickname"`
StartupNotifyUser []string `toml:"startup_notify_user"`
StartupNotifyMuc []string `toml:"startup_notify_muc"`

View File

@ -2,8 +2,8 @@ package runtime
import (
"fmt"
"log"
"github.com/bdlm/log"
xmpp "github.com/mattn/go-xmpp"
)
@ -16,7 +16,7 @@ func Start(client *xmpp.Client) {
switch v := m.(type) {
case xmpp.Chat:
if v.Type == "chat" {
log.Printf("from %s: %s", v.Remote, v.Text)
log.Debugf("from %s: %s", v.Remote, v.Text)
}
if v.Type == "groupchat" {
}