add circle ci

This commit is contained in:
Martin Geno 2017-06-09 14:27:06 +02:00
parent 23900f0350
commit 7fc17fed2d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
8 changed files with 145 additions and 2 deletions

28
.test-coverage Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
# Issue: https://github.com/mattn/goveralls/issues/20
# Source: https://github.com/uber/go-torch/blob/63da5d33a225c195fea84610e2456d5f722f3963/.test-cover.sh
CI=$1
echo "run for $CI"
echo "mode: count" > profile.cov
FAIL=0
# Standard go tooling behavior is to ignore dirs with leading underscors
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
do
if ls $dir/*.go &> /dev/null; then
go test -v -covermode=count -coverprofile=profile.tmp $dir || FAIL=$?
if [ -f profile.tmp ]
then
tail -n +2 < profile.tmp >> profile.cov
rm profile.tmp
fi
fi
done
# Failures have incomplete results, so don't send
if [ "$FAIL" -eq 0 ]; then
goveralls -v -coverprofile=profile.cov -service=$CI -repotoken=$COVERALLS_REPO_TOKEN
fi
exit $FAIL

1
README.md Normal file
View File

@ -0,0 +1 @@
# hook2xmpp [![CircleCI](https://circleci.com/gh/genofire/hook2xmpp/tree/master.svg?style=svg)](https://circleci.com/gh/genofire/hook2xmpp/tree/master)

35
circle.yml Normal file
View File

@ -0,0 +1,35 @@
notify:
webhooks:
- url: https://hook2xmpp.pub.warehost.de/circleci
machine:
environment:
GOROOT: ""
PATH: "/usr/local/go/bin:/usr/local/go_workspace/bin:~/.go_workspace/bin:${PATH}"
GOPATH: "${HOME}/.go_workspace"
dependencies:
override:
- mkdir -p ~/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME}
- ln -s ${HOME}/${CIRCLE_PROJECT_REPONAME} ${HOME}/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}
- go get -t -d -v ./...
- go install github.com/genofire/hook2xmpp/cmd/hook2xmpp
post:
- cp ~/.go_workspace/bin/hook2xmpp hook2xmpp.bin
- tar -cvzf hook2xmpp-builded.tar.gz hook2xmpp.bin config_example.conf
- mv hook2xmpp-builded.tar.gz $CIRCLE_ARTIFACTS
test:
pre:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
override:
- ./.test-coverage circle-ci
deployment:
staging:
branch: master
commands:
- ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING

56
circleci/main.go Normal file
View File

@ -0,0 +1,56 @@
package circleci
import (
"fmt"
"net/http"
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 == "circleci" {
repoFullName := fmt.Sprintf("%s/%s", hook.CircleCI.Username, hook.CircleCI.Reponame)
hooks[repoFullName] = hook
}
}
return &Handler{
client: client,
hooks: hooks,
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var payload map[string]interface{}
libHTTP.Read(r, &payload)
username := payload["username"].(string)
reponame := payload["reponame"].(string)
repoFullName := fmt.Sprintf("%s/%s", username, reponame)
hook, ok := h.hooks[repoFullName]
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)
return
}
status := payload["status"].(string)
buildNum := payload["build_num"].(int)
buildURL := payload["build_url"].(string)
buildTime := payload["build_time_millis"].(int)
subject := payload["subject"].(string)
msg := fmt.Sprintf("[%s/%s] %s (%dms) - #%d: %s \n%s", username, reponame, status, buildTime, buildNum, subject, buildURL)
ownXMPP.Notify(h.client, hook, msg)
}

View File

@ -11,6 +11,7 @@ import (
"github.com/mattn/go-xmpp" "github.com/mattn/go-xmpp"
"github.com/pierrre/githubhook" "github.com/pierrre/githubhook"
"github.com/genofire/hook2xmpp/circleci"
configuration "github.com/genofire/hook2xmpp/config" configuration "github.com/genofire/hook2xmpp/config"
"github.com/genofire/hook2xmpp/github" "github.com/genofire/hook2xmpp/github"
ownXMPP "github.com/genofire/hook2xmpp/xmpp" ownXMPP "github.com/genofire/hook2xmpp/xmpp"
@ -30,7 +31,7 @@ func main() {
log.Log.Infof("Started hock2xmpp with %s", client.JID()) log.Log.Infof("Started hock2xmpp with %s", client.JID())
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "Startup of hock2xmpp"}) client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"})
go ownXMPP.Start(client) go ownXMPP.Start(client)
githubHandler := github.NewHandler(client, config.Hooks) githubHandler := github.NewHandler(client, config.Hooks)
@ -38,6 +39,8 @@ func main() {
Delivery: githubHandler.Deliviery, Delivery: githubHandler.Deliviery,
} }
http.Handle("/github", handler) http.Handle("/github", handler)
circleciHandler := circleci.NewHandler(client, config.Hooks)
http.Handle("/circleci", circleciHandler)
srv := &http.Server{ srv := &http.Server{
Addr: config.WebserverBind, Addr: config.WebserverBind,
@ -53,6 +56,8 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs sig := <-sigs
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "stopped of hock2xmpp"})
srv.Close() srv.Close()
log.Log.Info("received", sig) log.Log.Info("received", sig)

View File

@ -30,6 +30,10 @@ type Hook struct {
Github struct { Github struct {
Project string `toml:"project"` Project string `toml:"project"`
} `toml:"github"` } `toml:"github"`
CircleCI struct {
Username string `toml:"username"`
Reponame string `toml:"reponame"`
} `toml:"circleci"`
} }
func ReadConfigFile(path string) *Config { func ReadConfigFile(path string) *Config {

14
deploy.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
host=$1
port=$2
remote="circleci@${host}"
echo "deploying..."
ssh -p $port $remote sudo systemctl stop hook2xmpp;
RETVAL=$?
[ $RETVAL -ne 0 ] && exit 1
scp -q -P $port ~/.go_workspace/bin/hook2xmpp $remote:~/bin/hook2xmpp;
RETVAL=$?
ssh -p $port $remote sudo systemctl start hook2xmpp;
[ $RETVAL -eq 0 ] && RETVAL=$?
[ $RETVAL -ne 0 ] && exit 1
echo "deployed"

View File

@ -113,7 +113,7 @@ func PayloadToString(event string, payloadOrigin interface{}) string {
} else if event == "issues" || event == "issue_comment" { } else if event == "issues" || event == "issue_comment" {
sender := payload["sender"].(map[string]interface{}) sender := payload["sender"].(map[string]interface{})
issue := payload["issue"].(map[string]interface{}) issue := payload["issue"].(map[string]interface{})
msg = fmt.Sprintf("%s %s - %s action #%f: %s \n %s", msg, sender["login"], payload["action"], issue["number"], issue["title"], issue["html_url"]) msg = fmt.Sprintf("%s %s - %s action #%.0f: %s \n %s", msg, sender["login"], payload["action"], issue["number"], issue["title"], issue["html_url"])
} else { } else {
sender := payload["sender"].(map[string]interface{}) sender := payload["sender"].(map[string]interface{})
text := eventMsg[event] text := eventMsg[event]