refactory
This commit is contained in:
parent
ae15e1341e
commit
afdbf52d94
|
@ -8,30 +8,12 @@ import (
|
|||
libHTTP "github.com/genofire/golang-lib/http"
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/runtime"
|
||||
)
|
||||
|
||||
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" {
|
||||
hooks[hook.URL] = hook
|
||||
}
|
||||
}
|
||||
return &Handler{
|
||||
client: client,
|
||||
hooks: hooks,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func init() {
|
||||
runtime.HookRegister["circleci"] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]interface{}
|
||||
libHTTP.Read(r, &body)
|
||||
payload := body["payload"].(map[string]interface{})
|
||||
|
@ -42,12 +24,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
hook, ok := h.hooks[vcsURL]
|
||||
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)
|
||||
return
|
||||
}
|
||||
status := payload["status"].(string)
|
||||
buildNum := payload["build_num"].(float64)
|
||||
buildURL := payload["build_url"].(string)
|
||||
|
@ -55,6 +31,19 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
subject := payload["subject"].(string)
|
||||
msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", vcsURL, status, buildTime/1000, buildNum, subject, buildURL)
|
||||
|
||||
log.Printf("circle ci: %s", msg)
|
||||
ownXMPP.Notify(h.client, hook, msg)
|
||||
ok = false
|
||||
for _, hook := range hooks {
|
||||
if vcsURL != hook.URL {
|
||||
continue
|
||||
}
|
||||
log.Printf("run hook for circleci: %s", msg)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
package config
|
|
@ -4,9 +4,9 @@ webserver_bind = ":8080"
|
|||
host = "fireorbit.de"
|
||||
username = "bot@fireorbit.de"
|
||||
password = "example"
|
||||
startup_notify = "geno@fireorbit.de"
|
||||
startup_notify_user = ["geno@fireorbit.de"]
|
||||
startup_notify_muc = []
|
||||
|
||||
[[hooks]]
|
||||
[[hooks.git]]
|
||||
notify_user = ["geno@fireorbit.de"]
|
||||
type = "git"
|
||||
url = "https://github.com/FreifunkBremen/yanic"
|
||||
|
|
44
git/main.go
44
git/main.go
|
@ -1,38 +1,21 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
libHTTP "github.com/genofire/golang-lib/http"
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/runtime"
|
||||
)
|
||||
|
||||
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 == "git" {
|
||||
hooks[hook.URL] = hook
|
||||
}
|
||||
}
|
||||
return &Handler{
|
||||
client: client,
|
||||
hooks: hooks,
|
||||
}
|
||||
}
|
||||
|
||||
var eventHeader = []string{"X-GitHub-Event", "X-Gogs-Event"}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func init() {
|
||||
runtime.HookRegister["git"] = 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{}
|
||||
event := ""
|
||||
for _, head := range eventHeader {
|
||||
|
@ -52,12 +35,19 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
repository := payload["repository"].(map[string]interface{})
|
||||
url := repository["html_url"].(string)
|
||||
|
||||
hook, ok := h.hooks[url]
|
||||
ok := false
|
||||
for _, hook := range hooks {
|
||||
if url != hook.URL {
|
||||
continue
|
||||
}
|
||||
log.Printf("run hook for git: %s", msg)
|
||||
runtime.Notify(client, hook, msg)
|
||||
ok = true
|
||||
}
|
||||
if !ok {
|
||||
log.Fatalf("No hook found for: '%s'", url)
|
||||
return
|
||||
http.Error(w, fmt.Sprintf("no configuration for git for url %s", url), http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("git: %s", msg)
|
||||
ownXMPP.Notify(h.client, hook, msg)
|
||||
}
|
||||
|
|
|
@ -10,11 +10,11 @@ import (
|
|||
|
||||
"github.com/mattn/go-xmpp"
|
||||
|
||||
"github.com/genofire/hook2xmpp/circleci"
|
||||
configuration "github.com/genofire/hook2xmpp/config"
|
||||
"github.com/genofire/hook2xmpp/git"
|
||||
"github.com/genofire/hook2xmpp/syslog"
|
||||
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
||||
"dev.sum7.eu/genofire/golang-lib/file"
|
||||
|
||||
_ "dev.sum7.eu/genofire/hook2xmpp/circleci"
|
||||
_ "dev.sum7.eu/genofire/hook2xmpp/git"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -22,10 +22,10 @@ func main() {
|
|||
flag.StringVar(&configFile, "config", configFile, "path of configuration file")
|
||||
flag.Parse()
|
||||
|
||||
config := configuration.ReadConfigFile(configFile)
|
||||
config := &runtime.Config{}
|
||||
|
||||
if config.Syslog.Enable {
|
||||
syslog.Bind(config)
|
||||
if err := file.ReadTOML(configFile, config); err != nil {
|
||||
log.Panicf("error on read config: %s", err)
|
||||
}
|
||||
|
||||
// load config
|
||||
|
@ -47,13 +47,14 @@ func main() {
|
|||
log.Printf("Started hock2xmpp with %s", client.JID())
|
||||
|
||||
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"})
|
||||
go ownXMPP.Start(client)
|
||||
go runtime.Start(client)
|
||||
|
||||
circleciHandler := circleci.NewHandler(client, config.Hooks)
|
||||
http.Handle("/circleci", circleciHandler)
|
||||
|
||||
gitHandler := git.NewHandler(client, config.Hooks)
|
||||
http.Handle("/git", gitHandler)
|
||||
for hookType, getHandler := range runtime.HookRegister {
|
||||
hooks, ok := config.Hooks[hookType]
|
||||
if ok {
|
||||
http.HandleFunc(hookType, getHandler(client, hooks))
|
||||
}
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: config.WebserverBind,
|
|
@ -1,19 +1,6 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
package runtime
|
||||
|
||||
type Config struct {
|
||||
Syslog struct {
|
||||
Enable bool `toml:"enable"`
|
||||
Type string `toml:"type"`
|
||||
Address string `toml:"address"`
|
||||
} `toml:"syslog"`
|
||||
|
||||
WebserverBind string `toml:"webserver_bind"`
|
||||
|
||||
XMPP struct {
|
||||
|
@ -28,25 +15,14 @@ type Config struct {
|
|||
StartupNotify string `toml:"startup_notify"`
|
||||
} `toml:"xmpp"`
|
||||
|
||||
Hooks []Hook `toml:"hooks"`
|
||||
StartupNotifyUser []string `toml:"startup_notify_user"`
|
||||
StartupNotifyMuc []string `toml:"startup_notify_muc"`
|
||||
|
||||
Hooks map[string][]Hook `toml:"hooks"`
|
||||
}
|
||||
|
||||
type Hook struct {
|
||||
Type string `toml:"type"`
|
||||
URL string `toml:"url"`
|
||||
NotifyUser []string `toml:"notify_user"`
|
||||
NotifyMuc []string `toml:"notify_muc"`
|
||||
}
|
||||
|
||||
func ReadConfigFile(path string) *Config {
|
||||
config := &Config{}
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
if err := toml.Unmarshal(file, config); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package runtime
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
)
|
||||
|
||||
type HookHandler func(*xmpp.Client, []Hook) func(http.ResponseWriter, *http.Request)
|
||||
|
||||
var HookRegister map[string]HookHandler
|
||||
|
||||
func init() {
|
||||
HookRegister = make(map[string]HookHandler)
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package xmpp
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
)
|
||||
|
||||
|
@ -26,8 +25,22 @@ func Start(client *xmpp.Client) {
|
|||
}
|
||||
}
|
||||
}
|
||||
func NotifyImage(client *xmpp.Client, hook Hook, url string) {
|
||||
msg := fmt.Sprintf(`<message to='%%s' type='%%s'>
|
||||
<body>%s</body>
|
||||
<x xmlns='jabber:x:oob'>
|
||||
<url>%s</url>
|
||||
</x>
|
||||
</message>`, url, url)
|
||||
|
||||
func Notify(client *xmpp.Client, hook config.Hook, msg string) {
|
||||
for _, muc := range hook.NotifyMuc {
|
||||
client.SendOrg(fmt.Sprintf(msg, muc, "groupchat"))
|
||||
}
|
||||
for _, user := range hook.NotifyUser {
|
||||
client.SendOrg(fmt.Sprintf(msg, user, "chat"))
|
||||
}
|
||||
}
|
||||
func Notify(client *xmpp.Client, hook Hook, msg string) {
|
||||
for _, muc := range hook.NotifyMuc {
|
||||
client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg})
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package syslog
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/syslog"
|
||||
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
)
|
||||
|
||||
func Bind(config *config.Config) {
|
||||
sysLog, err := syslog.Dial(config.Syslog.Type, config.Syslog.Address, syslog.LOG_WARNING|syslog.LOG_DAEMON, "hook2xmpp")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.SetOutput(sysLog)
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
package xmpp
|
Loading…
Reference in New Issue