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

View File

@ -6,19 +6,20 @@ import (
"net/http"
"github.com/bdlm/log"
"github.com/mitchellh/mapstructure"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure"
"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"
func init() {
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) {
logger := log.WithField("type", hookType)
@ -31,7 +32,7 @@ func init() {
}
}
if event == "status" {
if event == "" || event == "status" {
return
}
@ -45,13 +46,13 @@ func init() {
return
}
logger = logger.WithFields(map[string]interface{}{
"url": request.Repository.HTMLURL,
"url": request.Repository.URL,
"msg": request.String(event),
})
ok := false
for _, hook := range hooks {
if request.Repository.HTMLURL != hook.URL {
if request.Repository.URL != hook.URL {
continue
}
logger.Infof("run hook")
@ -60,7 +61,7 @@ func init() {
}
if !ok {
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",
}
type requestBody struct {
Repository struct {
URL string `mapstructure:"url"`
HTMLURL string `mapstructure:"html_url"`
FullName string `mapstructure:"full_name"`
} `mapstructure:"repository"`

View File

@ -2,20 +2,19 @@ package circleci
import (
"fmt"
"net/url"
"net/http"
"net/url"
"github.com/bdlm/log"
"github.com/mitchellh/mapstructure"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure"
"dev.sum7.eu/genofire/hook2xmpp/runtime"
)
const hookType = "grafana"
type evalMatch struct {
Tags map[string]string `mapstructure:"tags,omitempty"`
Metric string `mapstructure:"metric"`
@ -43,6 +42,7 @@ func (r requestBody) String() string {
func init() {
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) {
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/git"
_ "dev.sum7.eu/genofire/hook2xmpp/grafana"
"dev.sum7.eu/genofire/hook2xmpp/runtime"
)
@ -31,7 +32,9 @@ func main() {
options := xmpp.Options{
Host: config.XMPP.Host,
User: config.XMPP.Username,
Resource: config.XMPP.Resource,
Password: config.XMPP.Password,
StartTLS: config.XMPP.StartTLS,
NoTLS: config.XMPP.NoTLS,
Debug: config.XMPP.Debug,
Session: config.XMPP.Session,
@ -40,7 +43,7 @@ func main() {
}
client, err := options.NewClient()
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)
@ -48,7 +51,7 @@ func main() {
for hookType, getHandler := range runtime.HookRegister {
hooks, ok := config.Hooks[hookType]
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 {
client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg})
}
@ -94,7 +97,7 @@ func main() {
sig := <-sigs
notify("stopped of hock2xmpp")
for _, muc := range mucs {
client.LeaveMUC(muc)
}

View File

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