fix webhook stuff and start decoding actions

This commit is contained in:
Iris Lightshard 2023-09-01 22:39:39 -06:00
parent 2a1845f04c
commit 2bb6d24546
Signed by: Iris Lightshard
GPG key ID: F54E0D40695271D4
5 changed files with 63 additions and 7 deletions

34
action/action.go Normal file
View file

@ -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
}

5
go.mod
View file

@ -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
)

4
go.sum
View file

@ -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=

23
main.go
View file

@ -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")

View file

@ -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!")
}