add other git services
This commit is contained in:
parent
70109f5645
commit
80af647be1
|
@ -8,8 +8,8 @@ import (
|
|||
"github.com/genofire/golang-lib/log"
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/config"
|
||||
ownXMPP "dev.sum7.eu/genofire/hook2xmpp/xmpp"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
|
@ -22,8 +22,7 @@ func NewHandler(client *xmpp.Client, newHooks []config.Hook) *Handler {
|
|||
|
||||
for _, hook := range newHooks {
|
||||
if hook.Type == "circleci" {
|
||||
repoFullName := fmt.Sprintf("%s/%s", hook.CircleCI.Username, hook.CircleCI.Reponame)
|
||||
hooks[repoFullName] = hook
|
||||
hooks[hook.URL] = hook
|
||||
}
|
||||
}
|
||||
return &Handler{
|
||||
|
@ -36,19 +35,17 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
var body map[string]interface{}
|
||||
libHTTP.Read(r, &body)
|
||||
payload := body["payload"].(map[string]interface{})
|
||||
username, ok := payload["username"].(string)
|
||||
vcsURL, ok := payload["vcs_url"].(string)
|
||||
if !ok {
|
||||
log.Log.Error(r.Body)
|
||||
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
reponame := payload["reponame"].(string)
|
||||
repoFullName := fmt.Sprintf("%s/%s", username, reponame)
|
||||
|
||||
hook, ok := h.hooks[repoFullName]
|
||||
hook, ok := h.hooks[vcsURL]
|
||||
if !ok {
|
||||
log.Log.Errorf("No hook found for: '%s'", repoFullName)
|
||||
http.Error(w, fmt.Sprintf("no configuration for circleci with username %s and reponame %s", username, reponame), http.StatusNotFound)
|
||||
log.Log.Errorf("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)
|
||||
|
@ -56,7 +53,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
buildURL := payload["build_url"].(string)
|
||||
buildTime := payload["build_time_millis"].(float64)
|
||||
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)
|
||||
ownXMPP.Notify(h.client, hook, msg)
|
||||
|
|
|
@ -10,10 +10,10 @@ import (
|
|||
"github.com/genofire/golang-lib/log"
|
||||
"github.com/mattn/go-xmpp"
|
||||
|
||||
"github.com/genofire/hook2xmpp/circleci"
|
||||
configuration "github.com/genofire/hook2xmpp/config"
|
||||
"github.com/genofire/hook2xmpp/github"
|
||||
ownXMPP "github.com/genofire/hook2xmpp/xmpp"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/circleci"
|
||||
configuration "dev.sum7.eu/genofire/hook2xmpp/config"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/git"
|
||||
ownXMPP "dev.sum7.eu/genofire/hook2xmpp/xmpp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -46,8 +46,8 @@ func main() {
|
|||
circleciHandler := circleci.NewHandler(client, config.Hooks)
|
||||
http.Handle("/circleci", circleciHandler)
|
||||
|
||||
githubHandler := github.NewHandler(client, config.Hooks)
|
||||
http.Handle("/github", githubHandler)
|
||||
gitHandler := git.NewHandler(client, config.Hooks)
|
||||
http.Handle("/git", gitHandler)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: config.WebserverBind,
|
||||
|
|
|
@ -27,17 +27,10 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Hook struct {
|
||||
Type string `toml:"type"`
|
||||
URL string `toml:"url"`
|
||||
NotifyUser []string `toml:"notify_user"`
|
||||
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 {
|
||||
|
|
|
@ -6,6 +6,7 @@ username = "bot@fireorbit.de"
|
|||
password = "example"
|
||||
startup_notify = "geno@fireorbit.de"
|
||||
|
||||
[[github.project]]
|
||||
repository = "yanic"
|
||||
notify_user = geno@fireorbit.de
|
||||
[[hooks]]
|
||||
notify_user = ["geno@fireorbit.de"]
|
||||
type = "git"
|
||||
url = "https://github.com/FreifunkBremen/yanic"
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -1,60 +1,10 @@
|
|||
package github
|
||||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"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{
|
||||
"commit_comment_created": "Commit comment",
|
||||
"status_error": "Commit status: error",
|
|
@ -2,7 +2,7 @@ package xmpp
|
|||
|
||||
import (
|
||||
"github.com/genofire/golang-lib/log"
|
||||
"github.com/genofire/hook2xmpp/config"
|
||||
"dev.sum7.eu/genofire/hook2xmpp/config"
|
||||
|
||||
xmpp "github.com/mattn/go-xmpp"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue