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.
// 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)

@ -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
}

@ -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
}

@ -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