From 26d3508dc05834f066989e4454ae4dcaf46e5e62 Mon Sep 17 00:00:00 2001 From: Hamza Ali Date: Tue, 13 Aug 2019 16:23:23 +0700 Subject: [PATCH] Implement endpoints /badge/commit/{commit} and /badge/{project_name}/{project_tag}, and show unknown coverage when database record is missing, and formalize all commit messages in /upload/go to json --- Badge.go | 57 ------------------------------------- badge.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ database.sqlite | Bin 20480 -> 0 bytes main.go | 7 +++-- pill.go | 18 ++++++++++-- upload.go | 29 +++++++++++++------ 6 files changed, 114 insertions(+), 71 deletions(-) delete mode 100644 Badge.go create mode 100644 badge.go delete mode 100644 database.sqlite diff --git a/Badge.go b/Badge.go deleted file mode 100644 index d834aee..0000000 --- a/Badge.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "fmt" - "github.com/gorilla/mux" - "net/http" - "text/template" -) - -var baseTemplate *template.Template - -func init() { - baseTemplate = template.New("svgTemplate") - baseTemplate, _ = baseTemplate.Parse(svgTemplate) -} - -func BadgeFromCommit(w http.ResponseWriter, r *http.Request) { - fmt.Println(mux.Vars(r)) - commitID := mux.Vars(r)["commit"] - result, _ := db.Query("SELECT percentage FROM badge WHERE commit_hash=?", commitID) - defer result.Close() - - w.Header().Set("Content-Type", "image/svg+xml") - - if !result.Next() { - http.Error(w, "Request commit hash has no corresponding badge", 400) - //TODO send unknown svg instead - return - } - - var percentage float64 - result.Scan(&percentage) - - fillColour := percentageToRGB(percentage) - - template.New("svgConv") - pill := CoveragePill{ - toOneDecimal(percentage * 100), - baseColour, - fillColour, - } - baseTemplate.Execute(w, pill) -} - -func BadgeFromProject(w http.ResponseWriter, r *http.Request) { - projectName := mux.Vars(r)["project"] - tagName := mux.Vars(r)["tag"] - - result, _ := db.Query("SELECT commit_hash FROM alias WHERE project_name=? AND project_tag=?", projectName, tagName) - defer result.Close() - - var commit_hash string - result.Scan(&commit_hash) - fmt.Println(commit_hash) - mux.Vars(r)["commit"] = commit_hash - BadgeFromCommit(w, r) -} diff --git a/badge.go b/badge.go new file mode 100644 index 0000000..e4ad7e4 --- /dev/null +++ b/badge.go @@ -0,0 +1,74 @@ +package main + +import ( + "bytes" + "fmt" + "net/http" + "text/template" + + "github.com/gorilla/mux" +) + +var baseTemplate *template.Template + +func init() { + baseTemplate = template.New("svgTemplate") + baseTemplate, _ = baseTemplate.Parse(svgTemplate) +} + +func badgeFromCommit(w http.ResponseWriter, r *http.Request) { + commitID := mux.Vars(r)["commit"] + result := svgFromHash(commitID) + + w.Header().Set("Content-Type", "image/svg+xml") + fmt.Fprint(w, result) +} + +func badgeFromProject(w http.ResponseWriter, r *http.Request) { + projectName := mux.Vars(r)["project"] + tagName := mux.Vars(r)["tag"] + commitRow, err := db.Query("SELECT commit_hash FROM alias WHERE project_name=? AND project_tag=?", projectName, tagName) + + if err != nil { + panic(err) + } + if !commitRow.Next() { + fmt.Fprint(w, unknownCoveragePill()) + return + } + + var commitHash string + commitRow.Scan(&commitHash) + commitRow.Close() + + result := svgFromHash(commitHash) + + w.Header().Set("Content-Type", "image/svg+xml") + fmt.Fprint(w, result) +} + +func svgFromHash(commit string) string { + result, err := db.Query("SELECT percentage FROM badge WHERE commit_hash=?", commit) + defer result.Close() + if err != nil { + panic(err) + } + + if !result.Next() { + return unknownCoveragePill() + } + + var percentage float64 + result.Scan(&percentage) + + fillColour := percentageToRGB(percentage) + pill := CoveragePill{ + toOneDecimal(percentage*100) + "%", + baseColour, + fillColour, + } + + buf := &bytes.Buffer{} + baseTemplate.Execute(buf, pill) + return buf.String() +} diff --git a/database.sqlite b/database.sqlite deleted file mode 100644 index d42716859e10bbbc891ce4f3859a9cc8b719f63d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI&yHDFd9Ki9rBoH12R~_ns5HB-9$@st6N^Nh+AEOLByuJWAq9h>h;d z{b#!H2k;N@Hz0MY#K`3Y0tPyC4Byked_Kwd`}h_=_R4W9GwQI{@90b|i?>2b@mVP$ zMA4QpTOLx*b_x#%wv|u%FLjGzab;r0yA_4m3*nW$tJw>0{-w!`BY*$`2q1s}0tg_0 z00IcS5`pvg`J(Up^5=A>Yi*<3Ej@5mVIzscN~9{`dK@V?q!#MEPN$WnM|yCizJj6B=-`9;-tQfodX{!|$EZrQZ>Qc*e>|7z z=JQiQP#E{ud?{MphB+OawCy8vs()lXcbvNQq)V#&xS{_I z!Sw5yxma~!(BCFi}Y@U%_Ia6KmY**5I_I{1Q0*~ z0R#{j0fBciKUdVXdczzxKmGZAy?XiP*OZM*dVht@Bm@vZ009ILKmY**5I_I{1P~Y{ zfvNo5 coverage coverage - {{.Percentage}}% - {{.Percentage}}% + {{.Percentage}} + {{.Percentage}} ` @@ -34,12 +35,25 @@ const ( blue = 55 ) +//Pill used for template type CoveragePill struct { Percentage string BaseColour string FillColour string } +func unknownCoveragePill() string { + pill := CoveragePill{ + "?", + baseColour, + baseColour, // Using the base colour for fill colour because unknown + } + + buf := &bytes.Buffer{} + baseTemplate.Execute(buf, pill) + return buf.String() +} + func percentageToRGB(percentage float64) string { red := modifier + clamp(2-2*math.Pow(percentage, 2), 0, 1)*(1-modifier) green := modifier + clamp(2*math.Pow(percentage, 2), 0, 1)*(1-modifier) diff --git a/upload.go b/upload.go index b20aa4f..418c3e9 100644 --- a/upload.go +++ b/upload.go @@ -1,6 +1,7 @@ package main import ( + "database/sql" "fmt" "io/ioutil" "net/http" @@ -10,36 +11,46 @@ func uploadGo(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { return } + w.Header().Set("Content-Type", "application/json") err := r.ParseForm() if err != nil { - http.Error(w, "Request parameters not parsed", 500) + http.Error(w, `{"success": false, "message": "Request parameters not parsed"}`, 500) return } err = r.ParseMultipartForm(5 << 20) if err != nil { - http.Error(w, "Request parameters not parsed", 500) + http.Error(w, `{"success": false, "message": "Request parameters too large"}`, 500) return } - project := r.Form.Get("project") + name := r.Form.Get("project-name") tag := r.Form.Get("tag") id := r.Form.Get("id") - if project == "" || tag == "" || id == "" { - http.Error(w, "Request parameters [secret, project, tag, id] not found", 400) + if name == "" || tag == "" || id == "" { + http.Error(w, `{"success": false, "message": "Request parameters [secret, name, tag, id] not found"}`, 400) return } file, _, err := r.FormFile("file") if err != nil { - http.Error(w, "Request parameter file not found", 400) + http.Error(w, `{"success": false, "message": "Request parameter file not found"}`, 400) return } b, _ := ioutil.ReadAll(file) coverage := parseCoverage(string(b)) db.Exec("INSERT INTO badge (commit_hash, percentage) VALUES (?, ?)", id, coverage) - db.Exec("INSERT INTO alias (commit_hash, project_name, project_tag) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE commit=?", id, project, tag, id) - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"success": "true", "coverage": `+toOneDecimal(coverage*100)+`}`) + + _, err = db.Query("SELECT commit_hash FROM alias WHERE project_name=? AND project_tag=?", name, tag) + + if err == nil { + db.Exec("UPDATE ALIAS SET commit_hash=? WHERE project_name=? AND project_tag=?", id, name, tag) + } else if err == sql.ErrNoRows { + db.Exec("INSERT INTO alias (commit_hash, project_name, project_tag VALUES (?, ?, ?)", id, name, tag) + } else { + panic(err) + } + + fmt.Fprintf(w, `{"success": "true", "coverage": %s%%}`, toOneDecimal(coverage*100)) } -- 2.38.4