coverage/db/upload.go

67 lines
2.0 KiB
Go

package db
import (
"fmt"
"strconv"
)
func UploadBranchData(namespace, project, branch, coverageStr, html string) error {
coverage, err := strconv.ParseFloat(coverageStr, 64)
if err != nil {
return fmt.Errorf("coverage param must be valid float, instead got %s", coverageStr)
}
// Because we don't have have a primary key, we must do the upsert manually.
_, err = QueryByBranch(namespace, project, branch)
if err == ErrNoData {
_, err = db.Exec("INSERT INTO badge (namespace, project_name, branch, coverage, html) VALUES (?, ?, ?, ?, ?)",
namespace, project, branch, coverage, html)
if err != nil {
return fmt.Errorf("could not update database: %v", err)
}
}
if err != nil {
return err
}
_, err = db.Exec("UPDATE badge SET coverage=?, html=? WHERE namespace=? AND project_name=? AND branch=?",
coverage, html, namespace, project, branch)
if err != nil {
return fmt.Errorf("could not update database: %v", err)
}
return nil
}
func UploadPullData(namespace, project, pullStr, coverageStr, html string) error {
pull, err := strconv.Atoi(pullStr)
if err != nil {
return fmt.Errorf("pull param must be valid int, instead got %s", pullStr)
}
coverage, err := strconv.ParseFloat(coverageStr, 64)
if err != nil {
return fmt.Errorf("coverage param must be valid float, instead got %s", coverageStr)
}
// Because we don't have have a primary key, we must do the upsert manually.
_, err = QueryByPull(namespace, project, pullStr)
if err == ErrNoData {
_, err = db.Exec("INSERT INTO badge (namespace, project_name, pull, coverage, html) VALUES (?, ?, ?, ?, ?)",
namespace, project, pull, coverage, html)
if err != nil {
return fmt.Errorf("could not update database: %v", err)
}
}
if err != nil {
return err
}
_, err = db.Exec("UPDATE badge SET coverage=?, html=? WHERE namespace=? AND project_name=? AND pull=?",
coverage, html, namespace, project, pull)
if err != nil {
return fmt.Errorf("could not update database: %v", err)
}
return nil
}