2024-04-29 22:24:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-30 07:03:37 +02:00
|
|
|
"context"
|
2024-04-29 22:24:00 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-05-06 17:03:56 +02:00
|
|
|
"log"
|
2024-04-29 22:24:00 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-05-30 07:03:37 +02:00
|
|
|
"os/signal"
|
2024-05-06 17:03:56 +02:00
|
|
|
"path"
|
|
|
|
"runtime"
|
2024-05-24 21:32:05 +02:00
|
|
|
"strings"
|
2024-05-30 07:03:37 +02:00
|
|
|
"syscall"
|
2024-04-29 22:24:00 +02:00
|
|
|
|
2024-05-24 21:32:05 +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() {
|
2024-05-30 07:03:37 +02:00
|
|
|
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 {
|
2024-05-06 17:03:56 +02:00
|
|
|
log.Fatalf("Error loading config: %v", err)
|
2024-04-29 22:24:00 +02:00
|
|
|
}
|
2024-05-24 21:32:05 +02:00
|
|
|
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
2024-05-30 07:03:37 +02:00
|
|
|
|
|
|
|
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)
|
2024-05-30 07:03:37 +02:00
|
|
|
|
|
|
|
<-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()
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-06 17:03:56 +02:00
|
|
|
if !r.URL.Query().Has("project") {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project := r.URL.Query().Get("project")
|
2024-05-24 21:32:05 +02:00
|
|
|
proj, found := projects[project]
|
2024-05-06 17:03:56 +02:00
|
|
|
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)
|
2024-05-06 17:03:56 +02:00
|
|
|
return
|
|
|
|
}
|
2024-05-24 21:32:05 +02:00
|
|
|
var payload internal.WebhookPayload
|
2024-05-06 17:28:48 +02:00
|
|
|
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
|
|
|
|
}
|
2024-05-24 21:32:05 +02:00
|
|
|
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
2024-05-30 12:29:17 +02:00
|
|
|
log.Println("branchName", branchName)
|
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 21:32:05 +02:00
|
|
|
|
2024-05-24 22:07:55 +02:00
|
|
|
}
|
2024-05-24 21:32:05 +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)
|
2024-05-24 21:32:05 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-29 22:24:00 +02:00
|
|
|
|
2024-05-06 17:28:48 +02:00
|
|
|
go func() {
|
2024-05-25 06:31:37 +02:00
|
|
|
if err := execute(scr.Command, scr.Arguments...); err != nil {
|
2024-05-06 17:28:48 +02:00
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-05-30 12:29:17 +02:00
|
|
|
log.Println("script was deployed successful")
|
2024-05-06 17:28:48 +02:00
|
|
|
}()
|
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)
|
|
|
|
}
|
2024-05-30 12:29:17 +02:00
|
|
|
log.Println()
|
2024-04-29 22:24:00 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-30 07:03:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|