add other git services

This commit is contained in:
Martin Geno 2017-06-10 16:07:23 +02:00
parent 70109f5645
commit 80af647be1
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
7 changed files with 85 additions and 81 deletions

View File

@ -8,8 +8,8 @@ import (
"github.com/genofire/golang-lib/log" "github.com/genofire/golang-lib/log"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/config" "dev.sum7.eu/genofire/hook2xmpp/config"
ownXMPP "github.com/genofire/hook2xmpp/xmpp" ownXMPP "dev.sum7.eu/genofire/hook2xmpp/xmpp"
) )
type Handler struct { type Handler struct {
@ -22,8 +22,7 @@ func NewHandler(client *xmpp.Client, newHooks []config.Hook) *Handler {
for _, hook := range newHooks { for _, hook := range newHooks {
if hook.Type == "circleci" { if hook.Type == "circleci" {
repoFullName := fmt.Sprintf("%s/%s", hook.CircleCI.Username, hook.CircleCI.Reponame) hooks[hook.URL] = hook
hooks[repoFullName] = hook
} }
} }
return &Handler{ return &Handler{
@ -36,19 +35,17 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var body map[string]interface{} var body map[string]interface{}
libHTTP.Read(r, &body) libHTTP.Read(r, &body)
payload := body["payload"].(map[string]interface{}) payload := body["payload"].(map[string]interface{})
username, ok := payload["username"].(string) vcsURL, ok := payload["vcs_url"].(string)
if !ok { if !ok {
log.Log.Error(r.Body) log.Log.Error(r.Body)
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return return
} }
reponame := payload["reponame"].(string)
repoFullName := fmt.Sprintf("%s/%s", username, reponame)
hook, ok := h.hooks[repoFullName] hook, ok := h.hooks[vcsURL]
if !ok { if !ok {
log.Log.Errorf("No hook found for: '%s'", repoFullName) log.Log.Errorf("No hook found for: '%s'", vcsURL)
http.Error(w, fmt.Sprintf("no configuration for circleci with username %s and reponame %s", username, reponame), http.StatusNotFound) http.Error(w, fmt.Sprintf("no configuration for circleci for url %s", vcsURL), http.StatusNotFound)
return return
} }
status := payload["status"].(string) status := payload["status"].(string)
@ -56,7 +53,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
buildURL := payload["build_url"].(string) buildURL := payload["build_url"].(string)
buildTime := payload["build_time_millis"].(float64) buildTime := payload["build_time_millis"].(float64)
subject := payload["subject"].(string) subject := payload["subject"].(string)
msg := fmt.Sprintf("[%s/%s] %s (%0.fms) - #%0.f: %s \n%s", username, reponame, status, buildTime, buildNum, subject, buildURL) msg := fmt.Sprintf("[%s] %s (%0.fms) - #%0.f: %s \n%s", vcsURL, status, buildTime, buildNum, subject, buildURL)
log.Log.WithField("type", "circleci").Print(msg) log.Log.WithField("type", "circleci").Print(msg)
ownXMPP.Notify(h.client, hook, msg) ownXMPP.Notify(h.client, hook, msg)

View File

@ -10,10 +10,10 @@ import (
"github.com/genofire/golang-lib/log" "github.com/genofire/golang-lib/log"
"github.com/mattn/go-xmpp" "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/circleci" "dev.sum7.eu/genofire/hook2xmpp/circleci"
configuration "github.com/genofire/hook2xmpp/config" configuration "dev.sum7.eu/genofire/hook2xmpp/config"
"github.com/genofire/hook2xmpp/github" "dev.sum7.eu/genofire/hook2xmpp/git"
ownXMPP "github.com/genofire/hook2xmpp/xmpp" ownXMPP "dev.sum7.eu/genofire/hook2xmpp/xmpp"
) )
func main() { func main() {
@ -46,8 +46,8 @@ func main() {
circleciHandler := circleci.NewHandler(client, config.Hooks) circleciHandler := circleci.NewHandler(client, config.Hooks)
http.Handle("/circleci", circleciHandler) http.Handle("/circleci", circleciHandler)
githubHandler := github.NewHandler(client, config.Hooks) gitHandler := git.NewHandler(client, config.Hooks)
http.Handle("/github", githubHandler) http.Handle("/git", gitHandler)
srv := &http.Server{ srv := &http.Server{
Addr: config.WebserverBind, Addr: config.WebserverBind,

View File

@ -27,17 +27,10 @@ type Config struct {
} }
type Hook struct { type Hook struct {
Type string `toml:"type"`
URL string `toml:"url"`
NotifyUser []string `toml:"notify_user"` NotifyUser []string `toml:"notify_user"`
NotifyMuc []string `toml:"notify_muc"` NotifyMuc []string `toml:"notify_muc"`
Type string `toml:"type"`
Github struct {
Project string `toml:"project"`
} `toml:"github"`
CircleCI struct {
Username string `toml:"username"`
Reponame string `toml:"reponame"`
} `toml:"circleci"`
} }
func ReadConfigFile(path string) *Config { func ReadConfigFile(path string) *Config {

View File

@ -6,6 +6,7 @@ username = "bot@fireorbit.de"
password = "example" password = "example"
startup_notify = "geno@fireorbit.de" startup_notify = "geno@fireorbit.de"
[[github.project]] [[hooks]]
repository = "yanic" notify_user = ["geno@fireorbit.de"]
notify_user = geno@fireorbit.de type = "git"
url = "https://github.com/FreifunkBremen/yanic"

63
git/main.go Normal file
View File

@ -0,0 +1,63 @@
package git
import (
"net/http"
libHTTP "github.com/genofire/golang-lib/http"
"github.com/genofire/golang-lib/log"
xmpp "github.com/mattn/go-xmpp"
"dev.sum7.eu/genofire/hook2xmpp/config"
ownXMPP "dev.sum7.eu/genofire/hook2xmpp/xmpp"
)
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) {
var payload map[string]interface{}
event := ""
for _, head := range eventHeader {
event = r.Header.Get(head)
if event != "" {
break
}
}
if event == "status" {
return
}
libHTTP.Read(r, &payload)
msg := PayloadToString(event, payload)
repository := payload["repository"].(map[string]interface{})
url := repository["html_url"].(string)
hook, ok := h.hooks[url]
if !ok {
log.Log.Errorf("No hook found for: '%s'", url)
return
}
log.Log.WithField("type", "git").Print(msg)
ownXMPP.Notify(h.client, hook, msg)
}

View File

@ -1,60 +1,10 @@
package github package git
import ( import (
"fmt" "fmt"
"net/http"
"strings" "strings"
libHTTP "github.com/genofire/golang-lib/http"
"github.com/genofire/golang-lib/log"
xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/config"
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
) )
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 == "github" {
hooks[hook.Github.Project] = hook
}
}
return &Handler{
client: client,
hooks: hooks,
}
}
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)
hook, ok := h.hooks[repoName]
if !ok {
log.Log.Errorf("No hook found for: '%s'", repoName)
return
}
log.Log.WithField("type", "github").Print(msg)
ownXMPP.Notify(h.client, hook, msg)
}
var eventMsg = map[string]string{ var eventMsg = map[string]string{
"commit_comment_created": "Commit comment", "commit_comment_created": "Commit comment",
"status_error": "Commit status: error", "status_error": "Commit status: error",

View File

@ -2,7 +2,7 @@ package xmpp
import ( import (
"github.com/genofire/golang-lib/log" "github.com/genofire/golang-lib/log"
"github.com/genofire/hook2xmpp/config" "dev.sum7.eu/genofire/hook2xmpp/config"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
) )