fix: update paths to not use 404, only require coverage as form data param

master
ALI Hamza 2021-03-30 22:45:34 +07:00
parent 2783ec1d0b
commit bf3480e9e3
Signed by: hamza
GPG Key ID: 22473A32291F8CB6
4 changed files with 32 additions and 33 deletions

@ -31,7 +31,7 @@ type Pill struct {
} }
// NewPill creates creates a Pill, automatically calculating the colour, width, and formatting the percentage. // 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 { func NewPill(percentDecimal float64) Pill {
square := math.Pow(percentDecimal, 2) square := math.Pow(percentDecimal, 2)
red := modifier + clamp(2-2*square, 0, 1)*(1-modifier) red := modifier + clamp(2-2*square, 0, 1)*(1-modifier)

@ -7,7 +7,6 @@ import (
"gitea.teamortix.com/team-ortix/coverage/db" "gitea.teamortix.com/team-ortix/coverage/db"
"gitea.teamortix.com/team-ortix/coverage/pill" "gitea.teamortix.com/team-ortix/coverage/pill"
"github.com/gorilla/mux"
) )
type endpointData struct { type endpointData struct {
@ -91,26 +90,3 @@ func handlePullBadge(w http.ResponseWriter, r *http.Request) {
}, },
}.run(w, r) }.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
}

@ -20,7 +20,7 @@ func StartServer(port string) {
r.HandleFunc("/badge/pulls/{namespace}/{project}/{pull}", handlePullBadge) r.HandleFunc("/badge/pulls/{namespace}/{project}/{pull}", handlePullBadge)
r.HandleFunc("/upload/branch/{namespace}/{project}/{branch}", uploadBranch) 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) portStr := fmt.Sprintf(":%s", port)
err := http.ListenAndServe(portStr, r) err := http.ListenAndServe(portStr, r)
@ -51,3 +51,26 @@ func handleBadge(w http.ResponseWriter, r *http.Request) {
return 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
}

@ -39,19 +39,19 @@ func upload(w http.ResponseWriter, r *http.Request,
} }
secret := r.Form.Get("secret") secret := r.Form.Get("secret")
namespace := r.Form.Get("namespace") params, ok := getAllParams(w, r, uploadType)
project := r.Form.Get("project")
uploadValue := r.Form.Get(uploadType)
coverage := r.Form.Get("coverage") coverage := r.Form.Get("coverage")
if secretEnv != secret && secretEnv != "" { if secretEnv != secret && secretEnv != "" {
http.Error(w, "invalid secret provided", http.StatusUnauthorized) http.Error(w, "invalid secret provided", http.StatusUnauthorized)
return return
} }
if !ok {
return
}
if namespace == "" || project == "" || uploadValue == "" || coverage == "" { if coverage == "" {
http.Error(w, fmt.Sprintf("request params [namespace project, %s, coverage] must all be present", uploadType), http.Error(w, "request param coverage must be present", http.StatusBadRequest)
http.StatusBadRequest)
return return
} }
@ -66,7 +66,7 @@ func upload(w http.ResponseWriter, r *http.Request,
return return
} }
err = uploadFunc(namespace, project, uploadValue, coverage, string(bytes)) err = uploadFunc(params[0], params[1], params[2], coverage, string(bytes))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("could not upload data to database: %v", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("could not upload data to database: %v", err), http.StatusInternalServerError)
return return