package server
import (
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
"github.com/drone/drone/pkg/ccmenu"
common "github.com/drone/drone/pkg/types"
)
var (
badgeSuccess = []byte(``)
badgeFailure = []byte(``)
badgeStarted = []byte(``)
badgeError = []byte(``)
badgeNone = []byte(``)
)
// GetBadge accepts a request to retrieve the named
// repo and branhes latest build details from the datastore
// and return an SVG badges representing the build results.
//
// GET /api/badge/:owner/:name/status.svg
//
func GetBadge(c *gin.Context) {
var repo = ToRepo(c)
var store = ToDatastore(c)
var branch = c.Request.FormValue("branch")
if len(branch) == 0 {
branch = repo.Branch
}
// an SVG response is always served, even when error, so
// we can go ahead and set the content type appropriately.
c.Writer.Header().Set("Content-Type", "image/svg+xml")
// if no commit was found then display
// the 'none' badge, instead of throwing
// an error response
build, err := store.BuildLast(repo, branch)
if err != nil {
c.Writer.Write(badgeNone)
return
}
switch build.Status {
case common.StateSuccess:
c.Writer.Write(badgeSuccess)
case common.StateFailure:
c.Writer.Write(badgeFailure)
case common.StateError, common.StateKilled:
c.Writer.Write(badgeError)
case common.StatePending, common.StateRunning:
c.Writer.Write(badgeStarted)
default:
c.Writer.Write(badgeNone)
}
}
// GetCC accepts a request to retrieve the latest build
// status for the given repository from the datastore and
// in CCTray XML format.
//
// GET /api/badge/:host/:owner/:name/cc.xml
//
// TODO(bradrydzewski) this will not return in-progress builds, which it should
func GetCC(c *gin.Context) {
store := ToDatastore(c)
repo := ToRepo(c)
list, err := store.BuildList(repo, 1, 0)
if err != nil || len(list) == 0 {
c.AbortWithStatus(404)
return
}
cc := ccmenu.NewCC(repo, list[0])
c.Writer.Header().Set("Content-Type", "application/xml")
c.XML(200, cc)
}