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 d427168..0000000 Binary files a/database.sqlite and /dev/null differ diff --git a/main.go b/main.go index 87e2344..7ed99af 100644 --- a/main.go +++ b/main.go @@ -2,17 +2,18 @@ package main import ( "fmt" - "github.com/gorilla/mux" "net/http" "strconv" "strings" + + "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() r.HandleFunc("/upload/go", uploadGo) - r.HandleFunc("/badge/commit/{commit}.svg", BadgeFromCommit) - r.HandleFunc("/badge/{project}/{tag}.svg", BadgeFromProject) + r.HandleFunc("/badge/commit/{commit}.svg", badgeFromCommit) + r.HandleFunc("/badge/{project}/{tag}.svg", badgeFromProject) _ = http.ListenAndServe(":8080", r) } diff --git a/pill.go b/pill.go index 027eb6e..7ff2705 100644 --- a/pill.go +++ b/pill.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "math" "strconv" @@ -22,8 +23,8 @@ const svgTemplate string = ` 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)) }