feat: allow projects with branches/envs
This commit is contained in:
17
main.go
17
main.go
@@ -10,7 +10,9 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
|
||||
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
|
||||
)
|
||||
|
||||
@@ -25,11 +27,11 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Scripts))
|
||||
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||
}
|
||||
|
||||
func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
||||
func handlePayload(secret string, projects map[string]map[string]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
||||
return (func(w http.ResponseWriter, r *http.Request) {
|
||||
// Read the request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
@@ -51,16 +53,23 @@ func handlePayload(secret string, scripts map[string]config.ConfigScript) func(w
|
||||
}
|
||||
|
||||
project := r.URL.Query().Get("project")
|
||||
scr, found := scripts[project]
|
||||
proj, found := projects[project]
|
||||
if !found {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
var payload interface{}
|
||||
var payload internal.WebhookPayload
|
||||
if err = json.Unmarshal(body, &payload); err != nil {
|
||||
http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
||||
|
||||
scr, found := proj[branchName]
|
||||
if !found {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user