+75
-57
@@ -5,79 +5,97 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.ohea.xyz/cursorius/server/config"
|
||||
"git.ohea.xyz/cursorius/server/database"
|
||||
"git.ohea.xyz/cursorius/server/pipeline_executor"
|
||||
"github.com/go-playground/webhooks/v6/gitea"
|
||||
"github.com/google/uuid"
|
||||
"github.com/op/go-logging"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
|
||||
func CreateWebhookHandler(conf config.Config, mux *http.ServeMux) {
|
||||
mux.HandleFunc("/webhook/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
splitUrl := strings.Split(r.URL.Path, "/")
|
||||
if len(splitUrl) != 3 {
|
||||
log.Errorf("Webhook recieved with invalid url \"%v\", ignoring...", r.URL)
|
||||
return
|
||||
}
|
||||
func webhookHandler(w http.ResponseWriter, r *http.Request, db database.Database, conf config.PipelineConf) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
splitUrl := strings.Split(r.URL.Path, "/")
|
||||
if len(splitUrl) != 4 {
|
||||
log.Errorf("Webhook recieved with invalid url \"%v\", ignoring...", r.URL)
|
||||
return
|
||||
}
|
||||
|
||||
// get URL path after /webhook/
|
||||
// TODO: verify that this handles all valid URL formats
|
||||
webhookJobName := splitUrl[2]
|
||||
// get URL path after /webhook/
|
||||
// TODO: verify that this handles all valid URL formats
|
||||
pipelineUUIDStr := splitUrl[2]
|
||||
webhookUUIDStr := splitUrl[3]
|
||||
|
||||
for jobName, jobConfig := range conf.Jobs {
|
||||
if webhookJobName == jobName {
|
||||
if jobConfig.Webhook == nil {
|
||||
log.Errorf("Matching job does not have webhook configuration, ignoring....")
|
||||
pipelineUUID, err := uuid.Parse(pipelineUUIDStr)
|
||||
if err != nil {
|
||||
log.Errorf("Could not parse pipeline UUID: %v", err)
|
||||
return
|
||||
}
|
||||
webhookUUID, err := uuid.Parse(webhookUUIDStr)
|
||||
if err != nil {
|
||||
log.Errorf("Could not parse webhook UUID: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
pipeline, err := db.GetPipelineById(pipelineUUID)
|
||||
if err != nil {
|
||||
log.Errorf("Could not get webhooks for pipeline with UUID \"%v\": %v", pipelineUUID, err)
|
||||
return
|
||||
}
|
||||
|
||||
webhooks, err := db.GetWebhooksForPipeline(pipelineUUID)
|
||||
if err != nil {
|
||||
log.Errorf("Could not get webhooks for pipeline with UUID \"%v\": %v", webhookUUID, err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(webhooks) < 1 {
|
||||
log.Errorf("No webhooks configured for pipeline with UUID \"%v\"", webhookUUID)
|
||||
return
|
||||
}
|
||||
|
||||
for _, webhook := range webhooks {
|
||||
if webhook.Id == webhookUUID {
|
||||
switch webhook.ServerType {
|
||||
case database.Gitea:
|
||||
hook, err := gitea.New(gitea.Options.Secret(webhook.Secret))
|
||||
if err != nil {
|
||||
log.Errorf("Could not create Gitea webhook handler: %v", err)
|
||||
return
|
||||
}
|
||||
switch jobConfig.Webhook.Sender {
|
||||
case config.Gitea:
|
||||
hook, err := gitea.New(gitea.Options.Secret(jobConfig.Webhook.Secret))
|
||||
if err != nil {
|
||||
log.Errorf("Could not create Gitea webhook handler: %v", err)
|
||||
return
|
||||
payload, err := hook.Parse(r, gitea.PushEvent)
|
||||
if err != nil {
|
||||
if err == gitea.ErrEventNotFound {
|
||||
log.Warning("Got webhook \"%v\" for unexpected event type, ignoring...", webhookUUID)
|
||||
break
|
||||
}
|
||||
payload, err := hook.Parse(r, gitea.PushEvent)
|
||||
if err != nil {
|
||||
if err == gitea.ErrEventNotFound {
|
||||
log.Info("Got webhook for unexpected event type, ignoring...")
|
||||
break
|
||||
}
|
||||
log.Errorf("Could not parse webhook: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch payload.(type) {
|
||||
case gitea.PushPayload:
|
||||
pushPayload := payload.(gitea.PushPayload)
|
||||
|
||||
pe := pipeline_executor.PipelineExecution{
|
||||
Pipeline: pipeline,
|
||||
Ref: pushPayload.Ref,
|
||||
}
|
||||
log.Infof("Got webhook with payload %v", payload)
|
||||
|
||||
switch payload.(type) {
|
||||
case gitea.PushPayload:
|
||||
pushPayload := payload.(gitea.PushPayload)
|
||||
|
||||
pe := pipeline_executor.PipelineExecution{
|
||||
Name: webhookJobName,
|
||||
Job: jobConfig,
|
||||
Ref: pushPayload.Ref,
|
||||
}
|
||||
|
||||
pipeline_executor.ExecutePipeline(pe, conf.PipelineConf)
|
||||
}
|
||||
|
||||
return
|
||||
default:
|
||||
log.Errorf("Job configured with unknown webhook sender \"%v\", igonring...", jobConfig.Webhook.Sender)
|
||||
return
|
||||
|
||||
go pipeline_executor.ExecutePipeline(pe, db, conf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Errorf("Not job configured with name \"%v\", required by webhook with url \"%v\", ignoring...",
|
||||
webhookJobName, r.URL)
|
||||
|
||||
default:
|
||||
log.Errorf("Got request with method \"%v\", ignoring...", r.Method)
|
||||
}
|
||||
})
|
||||
|
||||
log.Errorf("No webhook found with ID \"%v\"", webhookUUID)
|
||||
|
||||
default:
|
||||
log.Errorf("Got request with method \"%v\", ignoring...", r.Method)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateWebhookHandler(db database.Database, conf config.PipelineConf, mux *http.ServeMux) {
|
||||
mux.HandleFunc("/webhook/", func(w http.ResponseWriter, r *http.Request) {
|
||||
webhookHandler(w, r, db, conf)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user