ability to use custom badges. standard badges now using shields.io

This commit is contained in:
Brad Rydzewski 2014-03-01 20:25:10 -07:00
parent 4689d53305
commit f33ebcc21c

View file

@ -7,10 +7,18 @@ import (
"github.com/drone/drone/pkg/database" "github.com/drone/drone/pkg/database"
) )
const (
badgeSuccess = "https://img.shields.io/badge/build-success-brightgreen.png"
badgeFailure = "https://img.shields.io/badge/build-failure-red.png"
badgeUnknown = "https://img.shields.io/badge/build-unknown-lightgray.png"
)
// Display a static badge (png format) for a specific // Display a static badge (png format) for a specific
// repository and an optional branch. // repository and an optional branch.
// TODO this needs to implement basic caching // TODO this needs to implement basic caching
func Badge(w http.ResponseWriter, r *http.Request) error { func Badge(w http.ResponseWriter, r *http.Request) error {
successParam := r.FormValue("success")
failureParam := r.FormValue("failure")
branchParam := r.FormValue("branch") branchParam := r.FormValue("branch")
hostParam := r.FormValue(":host") hostParam := r.FormValue(":host")
ownerParam := r.FormValue(":owner") ownerParam := r.FormValue(":owner")
@ -30,18 +38,30 @@ func Badge(w http.ResponseWriter, r *http.Request) error {
branchParam = repo.DefaultBranch() branchParam = repo.DefaultBranch()
} }
// default badge of "unknown" var badge string
badge := "/img/build_unknown.png"
// get the latest commit from the database // get the latest commit from the database
// for the requested branch // for the requested branch
commit, err := database.GetBranch(repo.ID, branchParam) commit, err := database.GetBranch(repo.ID, branchParam)
if err == nil { if err == nil {
switch commit.Status { switch {
case "Success": case commit.Status == "Success" && len(successParam) == 0:
badge = "/img/build_success.png" // if no success image is provided, we serve a
case "Failing", "Failure": // badge using the shields.io service
badge = "/img/build_failing.png" badge = badgeSuccess
case commit.Status == "Success" && len(successParam) != 0:
// otherwise we serve the user defined success badge
badge = successParam
case commit.Status == "Failure" && len(failureParam) == 0:
// if no failure image is provided, we serve a
// badge using the shields.io service
badge = badgeFailure
case commit.Status == "Failure" && len(failureParam) != 0:
// otherwise we serve the user defined failure badge
badge = failureParam
default:
// otherwise load unknown image
badge = badgeUnknown
} }
} }