Refactor webhook handling logic into seperate pkg

This commit is contained in:
2022-12-24 17:20:09 -07:00
parent e16444f3f1
commit 176de76581
2 changed files with 84 additions and 67 deletions
+2 -67
View File
@@ -3,11 +3,10 @@ package listen
import (
"fmt"
"net/http"
"strings"
"git.ohea.xyz/cursorius/server/config"
"git.ohea.xyz/cursorius/server/jobscheduler"
"github.com/go-playground/webhooks/v6/gitea"
"git.ohea.xyz/cursorius/server/webhook"
"github.com/op/go-logging"
"nhooyr.io/websocket"
)
@@ -16,71 +15,7 @@ var log = logging.MustGetLogger("cursorius-server")
func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration,
jobs map[string]config.Job) {
http.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
}
// get URL path after /webhook/
// TODO: verify that this handles all valid URL formats
webhookJobName := splitUrl[2]
for jobName, jobConfig := range jobs {
if webhookJobName == jobName {
if jobConfig.Webhook == nil {
log.Errorf("Matching job does not have webhook configuration, ignoring....")
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.Info("Got webhook for unexpected event type, ignoring...")
break
}
log.Errorf("Could not parse webhook: %v", err)
return
}
log.Infof("Got webhook with payload %v", payload)
switch payload.(type) {
case gitea.PushPayload:
pushPayload := payload.(gitea.PushPayload)
runCh <- jobscheduler.Run{
JobName: jobName,
JobConfig: jobConfig,
Ref: pushPayload.Ref,
}
}
return
default:
log.Errorf("Job configured with unknown webhook sender \"%v\", igonring...", jobConfig.Webhook.Sender)
return
}
}
}
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)
}
})
webhook.CreateWebhookHandler(runCh, jobs)
http.HandleFunc("/runner", func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
+82
View File
@@ -0,0 +1,82 @@
package webhook
import (
"net/http"
"strings"
"git.ohea.xyz/cursorius/server/config"
"git.ohea.xyz/cursorius/server/jobscheduler"
"github.com/go-playground/webhooks/v6/gitea"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("cursorius-server")
func CreateWebhookHandler(runCh chan jobscheduler.Run, jobs map[string]config.Job) {
http.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
}
// get URL path after /webhook/
// TODO: verify that this handles all valid URL formats
webhookJobName := splitUrl[2]
for jobName, jobConfig := range jobs {
if webhookJobName == jobName {
if jobConfig.Webhook == nil {
log.Errorf("Matching job does not have webhook configuration, ignoring....")
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.Info("Got webhook for unexpected event type, ignoring...")
break
}
log.Errorf("Could not parse webhook: %v", err)
return
}
log.Infof("Got webhook with payload %v", payload)
switch payload.(type) {
case gitea.PushPayload:
pushPayload := payload.(gitea.PushPayload)
runCh <- jobscheduler.Run{
JobName: jobName,
JobConfig: jobConfig,
Ref: pushPayload.Ref,
}
}
return
default:
log.Errorf("Job configured with unknown webhook sender \"%v\", igonring...", jobConfig.Webhook.Sender)
return
}
}
}
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)
}
})
}