diff --git a/server/handler/badge.go b/server/handler/badge.go index 965e4275..3cb35fde 100644 --- a/server/handler/badge.go +++ b/server/handler/badge.go @@ -1,10 +1,12 @@ package handler import ( + "encoding/xml" "net/http" "time" "github.com/drone/drone/server/database" + "github.com/drone/drone/shared/httputil" "github.com/drone/drone/shared/model" "github.com/gorilla/pat" ) @@ -91,7 +93,25 @@ func (h *BadgeHandler) GetCoverage(w http.ResponseWriter, r *http.Request) error } func (h *BadgeHandler) GetCC(w http.ResponseWriter, r *http.Request) error { - return notImplemented{} + host, owner, name := parseRepo(r) + + // get the repository from the database + repo, err := h.repos.FindName(host, owner, name) + if err != nil { + return notFound{err} + } + + // get the latest commits for the repo + commits, err := h.commits.List(repo.ID) + if err != nil || len(commits) == 0 { + return notFound{} + } + commit := commits[0] + + // generate the URL for the repository + url := httputil.GetURL(r) + "/" + repo.Host + "/" + repo.Owner + "/" + repo.Name + proj := model.NewCC(repo, commit, url) + return xml.NewEncoder(w).Encode(proj) } func (h *BadgeHandler) Register(r *pat.Router) { diff --git a/shared/model/cctray.go b/shared/model/cctray.go new file mode 100644 index 00000000..23f95811 --- /dev/null +++ b/shared/model/cctray.go @@ -0,0 +1,51 @@ +package model + +import ( + "encoding/xml" +) + +type CCProjects struct { + XMLName xml.Name `xml:"Projects"` + Project *CCProject `xml:"Project"` +} + +type CCProject struct { + XMLName xml.Name `xml:"Project"` + Name string `xml:"name,attr"` + Activity string `xml:"activity,attr"` + LastBuildStatus string `xml:"lastBuildStatus,attr"` + LastBuildLabel string `xml:"lastBuildLabel,attr"` + LastBuildTime string `xml:"lastBuildTime,attr"` + WebURL string `xml:"webUrl,attr"` +} + +func NewCC(r *Repo, c *Commit, url string) *CCProjects { + proj := &CCProject{ + Name: r.Owner + "/" + r.Name, + WebURL: url, + Activity: "Building", + LastBuildStatus: "Unknown", + LastBuildLabel: "Unknown", + } + + // if the build is not currently running then + // we can return the latest build status. + if c.Status != StatusStarted && + c.Status != StatusEnqueue { + proj.Activity = "Sleeping" + proj.LastBuildStatus = c.Status + proj.LastBuildLabel = c.ShaShort() + proj.LastBuildTime = "" // 2006-01-02T15:04:05.000+0000 + } + + // If the build is not running, and not successful, + // then set to Failure. Not sure CCTray will support + // our custom failure types (ie Killed) + if c.Status != StatusStarted && + c.Status != StatusEnqueue && + c.Status != StatusSuccess { + proj.LastBuildStatus = StatusFailure + } + + return &CCProjects{Project: proj} +}