Commit b6bf9d11 authored by Julien Schmidt's avatar Julien Schmidt

verbose error logging

parent 0d6ed285
Pipeline #364 passed with stages
in 1 minute and 40 seconds
......@@ -10,6 +10,7 @@ import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
......@@ -28,7 +29,9 @@ type githubRequest struct {
}
func parseGitHubRequest(req *http.Request) (h hook, status int) {
if req.Header.Get("X-GitHub-Event") != "push" {
event := req.Header.Get("X-GitHub-Event")
if event != "push" {
log.Println("wrong event kind:", event)
status = http.StatusBadRequest
return
}
......@@ -36,12 +39,14 @@ func parseGitHubRequest(req *http.Request) (h hook, status int) {
signature := req.Header.Get("X-Hub-Signature")
// signature must have 5 bytes prefix + 40 bytes SHA1 HMAC
if len(signature) != 45 || !strings.HasPrefix(signature, "sha1=") {
log.Println("signature missing")
status = http.StatusBadRequest
return
}
payload, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Println("reading payload error:", err)
status = http.StatusBadRequest
return
}
......@@ -50,6 +55,7 @@ func parseGitHubRequest(req *http.Request) (h hook, status int) {
var jr githubRequest
err = json.Unmarshal(payload, &jr)
if err != nil {
log.Println("decode error:", err)
status = http.StatusBadRequest
return
}
......@@ -57,12 +63,14 @@ func parseGitHubRequest(req *http.Request) (h hook, status int) {
// check if a handler exists for this project
h, ok := cfg.Hooks[jr.Repository.Name]
if !ok {
log.Println("hook not found:", jr.Repository.Name)
status = http.StatusTeapot
return
}
// check if the ref matches
if jr.Ref != h.Ref {
log.Println("ref mismatch:", h.Ref)
status = http.StatusTeapot
return
}
......
......@@ -6,6 +6,7 @@ package main
import (
"encoding/json"
"log"
"net/http"
)
......@@ -22,11 +23,13 @@ func parseGitLabRequest(req *http.Request) (h hook, status int) {
jd := json.NewDecoder(req.Body)
err := jd.Decode(&jr)
if err != nil {
log.Println("decode error:", err)
status = http.StatusBadRequest
return
}
if jr.ObjectKind != "push" {
log.Println("wrong kind:", jr.ObjectKind)
status = http.StatusBadRequest
return
}
......@@ -34,12 +37,14 @@ func parseGitLabRequest(req *http.Request) (h hook, status int) {
// check if a handler exists for this project
h, ok := cfg.Hooks[jr.Project.Name]
if !ok {
log.Println("hook not found:", jr.Project.Name)
status = http.StatusTeapot
return
}
// check if the ref matches
if jr.Ref != h.Ref {
log.Println("ref mismatch:", h.Ref)
status = http.StatusTeapot
return
}
......
......@@ -35,6 +35,7 @@ func sendErr(w http.ResponseWriter, code int) {
func Build(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" || req.URL.Path != "/build" {
log.Println(req.Method, req.URL.Path)
sendErr(w, http.StatusTeapot)
return
}
......@@ -59,6 +60,7 @@ func Build(w http.ResponseWriter, req *http.Request) {
out, err := cmd.CombinedOutput()
log.Println(string(out))
if err != nil {
log.Println("git error:", err)
sendErr(w, http.StatusInternalServerError)
return
}
......@@ -78,6 +80,7 @@ func Build(w http.ResponseWriter, req *http.Request) {
out, err = cmd.CombinedOutput()
log.Println(string(out))
if err != nil {
log.Println("cmd error:", err)
sendErr(w, http.StatusInternalServerError)
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment