package server
import (
"github.com/gin-gonic/gin"
"github.com/drone/drone/common"
"github.com/drone/drone/common/ccmenu"
"github.com/drone/drone/common/httputil"
)
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)
// 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
if repo.Last == nil {
c.Writer.Write(badgeNone)
return
}
switch repo.Last.State {
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)
last, err := store.BuildLast(repo.FullName)
if err != nil {
c.Fail(404, err)
return
}
link := httputil.GetURL(c.Request) + "/" + repo.FullName
cc := ccmenu.NewCC(repo, last, link)
c.Writer.Header().Set("Content-Type", "application/xml")
c.XML(200, cc)
}