added code to display CCTray status

This commit is contained in:
Brad Rydzewski 2014-07-31 21:44:25 -07:00
parent f508c27001
commit 4d56ab7878
2 changed files with 72 additions and 1 deletions

View file

@ -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) {

51
shared/model/cctray.go Normal file
View file

@ -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}
}