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 jsondevelop
parent
99695a4c4d
commit
9f1e45c1d5
@ -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)
|
|
||||||
}
|
|
@ -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()
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in New Issue