hook2xmpp/circleci/main.go

67 lines
1.7 KiB
Go
Raw Permalink Normal View History

2017-06-09 14:27:06 +02:00
package circleci
import (
"fmt"
2019-02-13 03:55:07 +01:00
2017-06-09 14:27:06 +02:00
"net/http"
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
2019-02-13 03:55:07 +01:00
"github.com/bdlm/log"
2019-07-15 23:57:26 +02:00
"gosrc.io/xmpp"
2017-06-09 14:27:06 +02:00
2019-08-08 15:49:25 +02:00
"dev.sum7.eu/sum7/hook2xmpp/runtime"
2017-06-09 14:27:06 +02:00
)
2019-02-13 04:32:06 +01:00
const hookType = "circleci"
type requestBody struct {
Payload struct {
VCSURL string `mapstructure:"vcs_url"`
Status string `mapstructure:"status"`
BuildNum float64 `mapstructure:"build_num"`
BuildURL string `mapstructure:"build_url"`
BuildTime float64 `mapstructure:"build_time_millis"`
Subject string `mapstructure:"subject"`
} `mapstructure:"payload"`
}
func (r requestBody) String() string {
return fmt.Sprintf("#%0.f (%0.fs): %s", r.Payload.BuildNum, r.Payload.BuildTime/1000, r.Payload.Subject)
}
2019-02-13 03:55:07 +01:00
2019-02-13 03:24:38 +01:00
func init() {
2019-07-15 23:57:26 +02:00
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
2019-02-13 04:52:00 +01:00
log.WithField("type", hookType).Info("loaded")
2019-02-13 03:24:38 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2019-02-13 03:55:07 +01:00
logger := log.WithField("type", hookType)
2019-02-13 04:32:06 +01:00
var request requestBody
2019-02-14 05:05:03 +01:00
if err := libHTTP.Read(r, &request); err != nil {
2019-02-13 04:32:06 +01:00
logger.Errorf("no readable payload: %s", err)
2019-02-13 03:24:38 +01:00
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return
}
2019-02-13 04:32:06 +01:00
logger = logger.WithFields(map[string]interface{}{
"url": request.Payload.VCSURL,
"msg": request.String(),
})
2019-02-13 03:24:38 +01:00
2019-02-13 04:32:06 +01:00
ok := false
2019-07-15 23:57:26 +02:00
msg := request.String()
2019-02-13 03:24:38 +01:00
for _, hook := range hooks {
if request.Payload.VCSURL != hook.Secret {
2019-02-13 03:24:38 +01:00
continue
}
2019-02-13 03:55:07 +01:00
logger.Infof("run hook")
2019-07-15 23:57:26 +02:00
runtime.Notify(client, hook, msg, msg)
2019-02-13 03:24:38 +01:00
ok = true
}
if !ok {
2019-02-13 03:55:07 +01:00
logger.Warnf("no hook found")
2019-02-13 04:32:06 +01:00
http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.Payload.VCSURL), http.StatusNotFound)
2019-02-13 03:24:38 +01:00
}
2017-06-09 14:27:06 +02:00
}
}
}