95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type Owner struct {
|
|
Login string `json:"login"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
type Repo struct {
|
|
Name string `json:"name"`
|
|
ID uint64 `json:"id"`
|
|
Owner Owner `json:"owner"`
|
|
}
|
|
|
|
type JsonRoot struct {
|
|
Repo Repo `json:"repository"`
|
|
|
|
}
|
|
|
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
return // Drop
|
|
}
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
http.Error(w, "Could not read request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var JsonRoot JsonRoot
|
|
err = json.Unmarshal(body, &JsonRoot); if err != nil {
|
|
log.Println("Can not Unmarshal body json")
|
|
http.Error(w,"Can not Unmarshal body json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
fmt.Printf("JsonRoot: %v\n", JsonRoot)
|
|
|
|
if JsonRoot.Repo.ID != task.RepoID {
|
|
log.Printf(
|
|
"Repo ID sent: %d, Expecting: %d.",
|
|
JsonRoot.Repo.ID,
|
|
task.RepoID,
|
|
)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
if JsonRoot.Repo.Owner.Login != task.Owner {
|
|
log.Printf(
|
|
"Owner sent: %s, Expecting: %s",
|
|
JsonRoot.Repo.Owner.Login,
|
|
task.Owner,
|
|
)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
DeliveryUUID := findXDeliveryUUID(r)
|
|
if DeliveryUUID == "" {
|
|
log.Println("Could not find Delivery UUID")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
log.Println("Starting New Job", DeliveryUUID)
|
|
go RunJob(&task, DeliveryUUID, log.Default())
|
|
|
|
}
|
|
|
|
func findXDeliveryUUID(r *http.Request) string {
|
|
|
|
var headerVal string
|
|
|
|
possibleHeaders := []string{
|
|
"X-Gitea-Delivery",
|
|
"X-GitHub-Delivery",
|
|
"X-Gogs-Delivery",
|
|
}
|
|
|
|
for _, head := range possibleHeaders {
|
|
headerVal = r.Header.Get(head)
|
|
if headerVal != "" { break }
|
|
}
|
|
return headerVal
|
|
|
|
}
|