58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
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)
|
|
}
|