diff --git a/pill/pill.go b/pill/pill.go index 3a57ef1..5edf9db 100644 --- a/pill/pill.go +++ b/pill/pill.go @@ -31,7 +31,7 @@ type Pill struct { } // NewPill creates creates a Pill, automatically calculating the colour, width, and formatting the percentage. -// Note: %02.0f gives us a float with 0 decimals and 0 padding for 2 numbers less than 10. +// Note: %.0f gives us a float with 0 decimals and 0 padding for 2 numbers less than 10. func NewPill(percentDecimal float64) Pill { square := math.Pow(percentDecimal, 2) red := modifier + clamp(2-2*square, 0, 1)*(1-modifier) diff --git a/server/query.go b/server/query.go index 7d57172..a73d6a0 100644 --- a/server/query.go +++ b/server/query.go @@ -7,7 +7,6 @@ import ( "gitea.teamortix.com/team-ortix/coverage/db" "gitea.teamortix.com/team-ortix/coverage/pill" - "github.com/gorilla/mux" ) type endpointData struct { @@ -91,26 +90,3 @@ func handlePullBadge(w http.ResponseWriter, r *http.Request) { }, }.run(w, r) } - -func getAllParams(w http.ResponseWriter, r *http.Request, thirdParam string) ([3]string, bool) { - vars := mux.Vars(r) - namespace, ok := vars["namespace"] - if !ok { - http.Error(w, "Path 'namespace' not found", http.StatusBadRequest) - return [3]string{}, false - } - - project, ok := vars["project"] - if !ok { - http.Error(w, "Path 'project' not found", http.StatusBadRequest) - return [3]string{}, false - } - - third, ok := vars[thirdParam] - if !ok { - http.Error(w, fmt.Sprintf("Path '%s' not found", thirdParam), http.StatusBadRequest) - return [3]string{}, false - } - - return [3]string{namespace, project, third}, true -} diff --git a/server/server.go b/server/server.go index daae28e..2fe3953 100644 --- a/server/server.go +++ b/server/server.go @@ -20,7 +20,7 @@ func StartServer(port string) { r.HandleFunc("/badge/pulls/{namespace}/{project}/{pull}", handlePullBadge) r.HandleFunc("/upload/branch/{namespace}/{project}/{branch}", uploadBranch) - r.HandleFunc("/upload/pulls/{namespace}/{project}/{branch}", uploadPull) + r.HandleFunc("/upload/pulls/{namespace}/{project}/{pull}", uploadPull) portStr := fmt.Sprintf(":%s", port) err := http.ListenAndServe(portStr, r) @@ -51,3 +51,26 @@ func handleBadge(w http.ResponseWriter, r *http.Request) { return } } + +func getAllParams(w http.ResponseWriter, r *http.Request, thirdParam string) ([3]string, bool) { + vars := mux.Vars(r) + namespace, ok := vars["namespace"] + if !ok { + http.Error(w, "Path 'namespace' not found", http.StatusBadRequest) + return [3]string{}, false + } + + project, ok := vars["project"] + if !ok { + http.Error(w, "Path 'project' not found", http.StatusBadRequest) + return [3]string{}, false + } + + third, ok := vars[thirdParam] + if !ok { + http.Error(w, fmt.Sprintf("Path '%s' not found", thirdParam), http.StatusBadRequest) + return [3]string{}, false + } + + return [3]string{namespace, project, third}, true +} diff --git a/server/upload.go b/server/upload.go index 75b26fd..98c2ec5 100644 --- a/server/upload.go +++ b/server/upload.go @@ -39,19 +39,19 @@ func upload(w http.ResponseWriter, r *http.Request, } secret := r.Form.Get("secret") - namespace := r.Form.Get("namespace") - project := r.Form.Get("project") - uploadValue := r.Form.Get(uploadType) + params, ok := getAllParams(w, r, uploadType) coverage := r.Form.Get("coverage") if secretEnv != secret && secretEnv != "" { http.Error(w, "invalid secret provided", http.StatusUnauthorized) return } + if !ok { + return + } - if namespace == "" || project == "" || uploadValue == "" || coverage == "" { - http.Error(w, fmt.Sprintf("request params [namespace project, %s, coverage] must all be present", uploadType), - http.StatusBadRequest) + if coverage == "" { + http.Error(w, "request param coverage must be present", http.StatusBadRequest) return } @@ -66,7 +66,7 @@ func upload(w http.ResponseWriter, r *http.Request, return } - err = uploadFunc(namespace, project, uploadValue, coverage, string(bytes)) + err = uploadFunc(params[0], params[1], params[2], coverage, string(bytes)) if err != nil { http.Error(w, fmt.Sprintf("could not upload data to database: %v", err), http.StatusInternalServerError) return