gitea-webhook-listener/main.go

136 lines
3.0 KiB
Go
Raw Normal View History

2024-04-29 22:24:00 +02:00
package main
import (
"context"
2024-04-29 22:24:00 +02:00
"encoding/json"
"fmt"
"io"
"log"
2024-04-29 22:24:00 +02:00
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"runtime"
"strings"
"syscall"
2024-04-29 22:24:00 +02:00
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
2024-04-29 22:24:00 +02:00
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
)
func main() {
var err error
ctx, cancel := context.WithCancel(signalContext(context.Background()))
defer cancel()
2024-05-06 17:19:58 +02:00
cfgFile := os.Getenv("CONFIG_FILE")
if cfgFile == "" {
// Get root path
_, filename, _, _ := runtime.Caller(0)
cfgFile = path.Join(path.Dir(filename), "configs", "app.yml")
}
cfg, err := config.LoadConfig(cfgFile)
2024-04-29 22:24:00 +02:00
if err != nil {
log.Fatalf("Error loading config: %v", err)
2024-04-29 22:24:00 +02:00
}
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
go func() {
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
}()
2024-05-25 06:47:21 +02:00
log.Printf("server is up on %d\n", cfg.Port)
<-ctx.Done()
if err != nil {
log.Println("some error happened", err)
}
log.Println("server shutdown")
2024-04-29 22:24:00 +02:00
}
2024-05-24 22:07:55 +02:00
func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
2024-04-29 22:24:00 +02:00
return (func(w http.ResponseWriter, r *http.Request) {
// Read the request body
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
authHeader := r.Header.Get("Authorization")
if authHeader != secret {
2024-04-29 22:24:00 +02:00
http.Error(w, "Signatures didn't match", http.StatusUnauthorized)
return
}
if !r.URL.Query().Has("project") {
http.Error(w, "", http.StatusBadRequest)
return
}
project := r.URL.Query().Get("project")
proj, found := projects[project]
if !found {
http.Error(w, "not found", http.StatusNotFound)
2024-05-25 06:47:21 +02:00
log.Printf("project %s not found\n", project)
return
}
var payload internal.WebhookPayload
if err = json.Unmarshal(body, &payload); err != nil {
2024-04-29 22:24:00 +02:00
http.Error(w, "Failed to parse JSON payload", http.StatusBadRequest)
return
}
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
2024-05-24 22:07:55 +02:00
found = false
var scr config.ConfigScript
for i := range proj {
if proj[i].Environment == branchName {
found = true
scr = proj[i]
break
}
2024-05-24 22:07:55 +02:00
}
if !found {
http.Error(w, "not found", http.StatusNotFound)
2024-05-25 06:47:21 +02:00
log.Printf("project %s with branch %s not found\n", project, branchName)
return
}
2024-04-29 22:24:00 +02:00
go func() {
2024-05-25 06:31:37 +02:00
if err := execute(scr.Command, scr.Arguments...); err != nil {
log.Println(err)
}
}()
2024-04-29 22:24:00 +02:00
})
}
2024-05-25 06:31:37 +02:00
func execute(command string, args ...string) error {
cmd := exec.Command(command, args...)
2024-04-29 22:24:00 +02:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("cmd.Run %w", err)
}
return nil
}
func signalContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
signal.Stop(sigs)
close(sigs)
cancel()
}()
return ctx
}