Add WIP proper webhook support (for gitea)
This commit is contained in:
+9
-1
@@ -2,16 +2,24 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.ohea.xyz/golang/config"
|
||||
)
|
||||
|
||||
type WebhookSender string
|
||||
|
||||
const (
|
||||
Gitea WebhookSender = "gitea"
|
||||
)
|
||||
|
||||
type Webhook struct {
|
||||
Sender WebhookSender
|
||||
Secret string
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
URL string
|
||||
Webhook Webhook
|
||||
Webhook *Webhook
|
||||
Cron *string
|
||||
PollInterval uint64
|
||||
}
|
||||
|
||||
@@ -4,29 +4,39 @@ go 1.19
|
||||
|
||||
require (
|
||||
git.ohea.xyz/golang/config v0.0.0-20220915224621-b9debd233173
|
||||
github.com/go-git/go-git/v5 v5.4.2
|
||||
github.com/go-git/go-git/v5 v5.4.3-0.20220529141257-bc1f419cebcf
|
||||
github.com/go-playground/webhooks/v6 v6.0.0-00010101000000-000000000000
|
||||
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
|
||||
nhooyr.io/websocket v1.8.7
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Microsoft/go-winio v0.4.16 // indirect
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
|
||||
code.gitea.io/gitea v1.17.4 // indirect
|
||||
github.com/Microsoft/go-winio v0.6.0 // indirect
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20220930113650-c6815a8c17ad // indirect
|
||||
github.com/acomagu/bufpipe v1.0.3 // indirect
|
||||
github.com/emirpasic/gods v1.12.0 // indirect
|
||||
github.com/cloudflare/circl v1.2.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/go-git/gcfg v1.5.0 // indirect
|
||||
github.com/go-git/go-billy/v5 v5.3.1 // indirect
|
||||
github.com/gobwas/ws v1.1.0 // indirect
|
||||
github.com/imdario/mergo v0.3.12 // indirect
|
||||
github.com/imdario/mergo v0.3.13 // indirect
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
|
||||
github.com/klauspost/compress v1.10.3 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||
github.com/klauspost/compress v1.15.11 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||
github.com/sergi/go-diff v1.1.0 // indirect
|
||||
github.com/xanzy/ssh-agent v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
|
||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
|
||||
github.com/sergi/go-diff v1.2.0 // indirect
|
||||
github.com/xanzy/ssh-agent v0.3.2 // indirect
|
||||
golang.org/x/crypto v0.2.1-0.20221112162523-6fad3dfc1891 // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/net v0.2.0 // indirect
|
||||
golang.org/x/sys v0.2.0 // indirect
|
||||
golang.org/x/tools v0.1.12 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/go-playground/webhooks/v6 => git.ohea.xyz/cursorius/webhooks/v6 v6.0.2-0.20221224045412-1c899982c188
|
||||
|
||||
+53
-8
@@ -2,20 +2,65 @@ package listen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.ohea.xyz/cursorius/server/jobscheduler"
|
||||
"github.com/op/go-logging"
|
||||
"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"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
|
||||
func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration) {
|
||||
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
|
||||
func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration,
|
||||
webhookConfig map[string]config.Webhook) {
|
||||
http.HandleFunc("/webhook/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
log.Info("Got webhook")
|
||||
runCh <- jobscheduler.Run{Name: "test"}
|
||||
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 webhookConfig {
|
||||
if webhookJobName == jobName {
|
||||
switch jobConfig.Sender {
|
||||
case config.Gitea:
|
||||
hook, err := gitea.New(gitea.Options.Secret(jobConfig.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)
|
||||
runCh <- jobscheduler.Run{Name: "test"}
|
||||
return
|
||||
default:
|
||||
log.Errorf("Job configured with unkown webhook sender \"%v\", igonring...", jobConfig.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)
|
||||
}
|
||||
@@ -30,9 +75,9 @@ func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.R
|
||||
})
|
||||
}
|
||||
|
||||
func Listen(address string, port int, runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration) {
|
||||
func Listen(address string, port int, runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration, webhookConfig map[string]config.Webhook) {
|
||||
|
||||
setupHTTPServer(runCh, registerCh)
|
||||
setupHTTPServer(runCh, registerCh, webhookConfig)
|
||||
|
||||
connect_string := fmt.Sprintf("%v:%v", address, port)
|
||||
log.Noticef("Launching HTTP server on %v\n", connect_string)
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.ohea.xyz/cursorius/server/config"
|
||||
"git.ohea.xyz/cursorius/server/jobscheduler"
|
||||
"git.ohea.xyz/cursorius/server/listen"
|
||||
"git.ohea.xyz/cursorius/server/poll"
|
||||
"git.ohea.xyz/cursorius/server/util"
|
||||
"github.com/op/go-logging"
|
||||
"os"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
@@ -37,6 +39,7 @@ func main() {
|
||||
|
||||
poll.StartPolling(configData.Config.Jobs, runCh)
|
||||
|
||||
listen.Listen(configData.Config.Address, configData.Config.Port, runCh, registerCh)
|
||||
webhookConfig := util.CreateWebhookConfig(configData.Config.Jobs)
|
||||
listen.Listen(configData.Config.Address, configData.Config.Port, runCh, registerCh, webhookConfig)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package util
|
||||
|
||||
import "git.ohea.xyz/cursorius/server/config"
|
||||
|
||||
// Generated map for webhook handler config
|
||||
func CreateWebhookConfig(conf map[string]config.Job) map[string]config.Webhook {
|
||||
webhookConfig := make(map[string]config.Webhook, 0)
|
||||
for k, v := range conf {
|
||||
if v.Webhook != nil {
|
||||
webhookConfig[k] = *v.Webhook
|
||||
}
|
||||
}
|
||||
return webhookConfig
|
||||
}
|
||||
Reference in New Issue
Block a user