some fixes + xmpp update

This commit is contained in:
Martin/Geno 2019-02-13 04:52:00 +01:00
parent caf0dfe497
commit 5d57274202
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
6 changed files with 23 additions and 17 deletions

View File

@ -6,16 +6,15 @@ import (
"net/http" "net/http"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/mitchellh/mapstructure"
libHTTP "github.com/genofire/golang-lib/http" libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure"
"dev.sum7.eu/genofire/hook2xmpp/runtime" "dev.sum7.eu/genofire/hook2xmpp/runtime"
) )
const hookType = "circleci" const hookType = "circleci"
type requestBody struct { type requestBody struct {
Payload struct { Payload struct {
VCSURL string `mapstructure:"vcs_url"` VCSURL string `mapstructure:"vcs_url"`
@ -33,6 +32,7 @@ func (r requestBody) String() string {
func init() { func init() {
runtime.HookRegister[hookType] = 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) {
log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

View File

@ -6,19 +6,20 @@ import (
"net/http" "net/http"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/mitchellh/mapstructure"
libHTTP "github.com/genofire/golang-lib/http" libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure"
"dev.sum7.eu/genofire/hook2xmpp/runtime" "dev.sum7.eu/genofire/hook2xmpp/runtime"
) )
var eventHeader = []string{"X-GitHub-Event", "X-Gogs-Event"} var eventHeader = []string{"X-GitHub-Event", "X-Gogs-Event", "X-Gitlab-Event"}
const hookType = "git" const hookType = "git"
func init() { func init() {
runtime.HookRegister[hookType] = 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) {
log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)
@ -31,7 +32,7 @@ func init() {
} }
} }
if event == "status" { if event == "" || event == "status" {
return return
} }
@ -45,13 +46,13 @@ func init() {
return return
} }
logger = logger.WithFields(map[string]interface{}{ logger = logger.WithFields(map[string]interface{}{
"url": request.Repository.HTMLURL, "url": request.Repository.URL,
"msg": request.String(event), "msg": request.String(event),
}) })
ok := false ok := false
for _, hook := range hooks { for _, hook := range hooks {
if request.Repository.HTMLURL != hook.URL { if request.Repository.URL != hook.URL {
continue continue
} }
logger.Infof("run hook") logger.Infof("run hook")
@ -60,7 +61,7 @@ func init() {
} }
if !ok { if !ok {
logger.Warnf("no hook found") logger.Warnf("no hook found")
http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.Repository.HTMLURL), http.StatusNotFound) http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.Repository.URL), http.StatusNotFound)
} }
} }
} }

View File

@ -49,9 +49,9 @@ var eventMsg = map[string]string{
"gollum_edited": "Wiki: edited page", "gollum_edited": "Wiki: edited page",
} }
type requestBody struct { type requestBody struct {
Repository struct { Repository struct {
URL string `mapstructure:"url"`
HTMLURL string `mapstructure:"html_url"` HTMLURL string `mapstructure:"html_url"`
FullName string `mapstructure:"full_name"` FullName string `mapstructure:"full_name"`
} `mapstructure:"repository"` } `mapstructure:"repository"`

View File

@ -2,20 +2,19 @@ package circleci
import ( import (
"fmt" "fmt"
"net/url"
"net/http" "net/http"
"net/url"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/mitchellh/mapstructure"
libHTTP "github.com/genofire/golang-lib/http" libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure"
"dev.sum7.eu/genofire/hook2xmpp/runtime" "dev.sum7.eu/genofire/hook2xmpp/runtime"
) )
const hookType = "grafana" const hookType = "grafana"
type evalMatch struct { type evalMatch struct {
Tags map[string]string `mapstructure:"tags,omitempty"` Tags map[string]string `mapstructure:"tags,omitempty"`
Metric string `mapstructure:"metric"` Metric string `mapstructure:"metric"`
@ -43,6 +42,7 @@ func (r requestBody) String() string {
func init() { func init() {
runtime.HookRegister[hookType] = 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) {
log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

11
main.go
View File

@ -13,6 +13,7 @@ import (
_ "dev.sum7.eu/genofire/hook2xmpp/circleci" _ "dev.sum7.eu/genofire/hook2xmpp/circleci"
_ "dev.sum7.eu/genofire/hook2xmpp/git" _ "dev.sum7.eu/genofire/hook2xmpp/git"
_ "dev.sum7.eu/genofire/hook2xmpp/grafana"
"dev.sum7.eu/genofire/hook2xmpp/runtime" "dev.sum7.eu/genofire/hook2xmpp/runtime"
) )
@ -31,7 +32,9 @@ func main() {
options := xmpp.Options{ options := xmpp.Options{
Host: config.XMPP.Host, Host: config.XMPP.Host,
User: config.XMPP.Username, User: config.XMPP.Username,
Resource: config.XMPP.Resource,
Password: config.XMPP.Password, Password: config.XMPP.Password,
StartTLS: config.XMPP.StartTLS,
NoTLS: config.XMPP.NoTLS, NoTLS: config.XMPP.NoTLS,
Debug: config.XMPP.Debug, Debug: config.XMPP.Debug,
Session: config.XMPP.Session, Session: config.XMPP.Session,
@ -40,7 +43,7 @@ func main() {
} }
client, err := options.NewClient() client, err := options.NewClient()
if err != nil { if err != nil {
log.Panicf("error on startup xmpp client: %s",err) log.Panicf("error on startup xmpp client: %s", err)
} }
go runtime.Start(client) go runtime.Start(client)
@ -48,7 +51,7 @@ func main() {
for hookType, getHandler := range runtime.HookRegister { for hookType, getHandler := range runtime.HookRegister {
hooks, ok := config.Hooks[hookType] hooks, ok := config.Hooks[hookType]
if ok { if ok {
http.HandleFunc(hookType, getHandler(client, hooks)) http.HandleFunc("/"+hookType, getHandler(client, hooks))
} }
} }
@ -75,7 +78,7 @@ func main() {
} }
} }
notify := func (msg string) { notify := func(msg string) {
for _, muc := range config.StartupNotifyMuc { for _, muc := range config.StartupNotifyMuc {
client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg}) client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg})
} }
@ -94,7 +97,7 @@ func main() {
sig := <-sigs sig := <-sigs
notify("stopped of hock2xmpp") notify("stopped of hock2xmpp")
for _, muc := range mucs { for _, muc := range mucs {
client.LeaveMUC(muc) client.LeaveMUC(muc)
} }

View File

@ -6,14 +6,16 @@ type Config struct {
XMPP struct { XMPP struct {
Host string `toml:"host"` Host string `toml:"host"`
Username string `toml:"username"` Username string `toml:"username"`
Resource string `toml:"resource"`
Password string `toml:"password"` Password string `toml:"password"`
Debug bool `toml:"debug"` Debug bool `toml:"debug"`
NoTLS bool `toml:"no_tls"` NoTLS bool `toml:"no_tls"`
StartTLS bool `toml:"start_tls"`
Session bool `toml:"session"` Session bool `toml:"session"`
Status string `toml:"status"` Status string `toml:"status"`
StatusMessage string `toml:"status_message"` StatusMessage string `toml:"status_message"`
} `toml:"xmpp"` } `toml:"xmpp"`
Nickname string `toml:"nickname"` Nickname string `toml:"nickname"`
StartupNotifyUser []string `toml:"startup_notify_user"` StartupNotifyUser []string `toml:"startup_notify_user"`