From 2bb6d24546698e84f332e59c36c22c5b8de764ba Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Fri, 1 Sep 2023 22:39:39 -0600 Subject: [PATCH] fix webhook stuff and start decoding actions --- action/action.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 5 ++++- go.sum | 4 ++++ main.go | 23 +++++++++++++++++++---- webhook/webhook.go | 4 ++-- 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 action/action.go diff --git a/action/action.go b/action/action.go new file mode 100644 index 0000000..161f070 --- /dev/null +++ b/action/action.go @@ -0,0 +1,34 @@ +package action + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +type Action struct { + Build struct { + Cmd string `yaml:"cmd"` + } `yaml:"build,omitempty"` + Deploy struct { + Hosts []string `yaml:"hosts"` + Artifacts map[string][]string `yaml:"artifacts"` + Before map[string]string `yaml:"before,omitempty"` + After map[string]string `yaml:"after,omitempty"` + } `yaml:"deploy,omitempty"` +} + +func Read(filename string) (*Action, error) { + b, err := os.ReadFile(filename) + if err != nil { + return nil, fmt.Errorf("reading action: %w", err) + } + + a := Action{} + if err := yaml.Unmarshal(b, &a); err != nil { + return nil, fmt.Errorf("parsing action: %w", err) + } + + return &a, nil +} \ No newline at end of file diff --git a/go.mod b/go.mod index 611b1c1..03d5fe4 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module forge.lightcrystal.systems/lightcrystal/memnarch go 1.20 -require hacklab.nilfm.cc/quartzgun v0.3.2 +require ( + gopkg.in/yaml.v3 v3.0.1 + hacklab.nilfm.cc/quartzgun v0.3.2 +) diff --git a/go.sum b/go.sum index be61828..ce02516 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= hacklab.nilfm.cc/quartzgun v0.3.2 h1:PmRFZ/IgsXVWyNn1iOsQ/ZeMnOQIQy0PzFakhXBdZoU= hacklab.nilfm.cc/quartzgun v0.3.2/go.mod h1:P6qK4HB0CD/xfyRq8wdEGevAPFDDmv0KCaESSvv93LU= diff --git a/main.go b/main.go index 05becb2..a8e673e 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "hacklab.nilfm.cc/quartzgun/router" . "hacklab.nilfm.cc/quartzgun/util" - + "forge.lightcrystal.systems/lightcrystal/memnarch/action" "forge.lightcrystal.systems/lightcrystal/memnarch/webhook" ) @@ -65,18 +65,33 @@ func runJob(secret string, next http.Handler) http.Handler { // so we run the rest in a goroutine... go func() { // cd and checkout repo - cmd := exec.Command("git", "clone", repoUrl) - cmd.Dir = workingDir + clone := exec.Command("git", "clone", repoUrl) + clone.Dir = workingDir - err := cmd.Run() + err := clone.Run() if err != nil { // clone error - log it and quit return } // read memnarch action file + urlParams := req.Context().Value("params").(map[string]string) + jobName := urlParams["job"] + jobFile := filepath.Join(workingDir, repo, jobName + ".yml") + a, err := action.Read(jobFile) + + if err != nil { + fmt.Println(err.Error()) + } // decode and perform action + // build + buildCmd := exec.Command(a.Build.Cmd) + buildCmd.Dir = filepath.Join(workingDir, repo) + + // pre-deploy + // deploy + // post-deploy }() AddContextValue(req, "data", "job submitted") diff --git a/webhook/webhook.go b/webhook/webhook.go index 3596547..4bbd8a6 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -31,7 +31,7 @@ func (h *Hook) SignedBy(secret []byte) bool { } actual := make([]byte, 20) - hex.Decode(actual, []byte(h.Signature[5:])) + hex.Decode(actual, []byte(h.Signature)) return hmac.Equal(signBody(secret, h.Payload), actual) } @@ -46,7 +46,7 @@ func HookFrom(req *http.Request) (hook *Hook, err error) { return nil, errors.New("Unknown method!") } - if hook.Signature = req.Header.Get("X-Signature-SHA256"); len(hook.Signature) == 0 { + if hook.Signature = req.Header.Get("X-Forgejo-Signature"); len(hook.Signature) == 0 { return nil, errors.New("No signature!") }