package handler import ( "encoding/xml" "net/http" "github.com/drone/drone/server/datastore" "github.com/drone/drone/shared/httputil" "github.com/drone/drone/shared/model" "github.com/goji/context" "github.com/zenazn/goji/web" ) // badges that indicate the current build status for a repository // and branch combination. var ( badgeSuccess = []byte(`buildbuildsuccesssuccess`) badgeFailure = []byte(`buildbuildfailurefailure`) badgeStarted = []byte(`buildbuildstartedstarted`) badgeError = []byte(`buildbuilderrorerror`) badgeNone = []byte(`buildbuildnonenone`) ) // 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/:host/:owner/:name/status.svg // func GetBadge(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var ( host = c.URLParams["host"] owner = c.URLParams["owner"] name = c.URLParams["name"] branch = c.URLParams["branch"] ) repo, err := datastore.GetRepoName(ctx, host, owner, name) if err != nil { w.Write(badgeNone) return } if len(branch) == 0 { branch = model.DefaultBranch } commit, _ := datastore.GetCommitLast(ctx, repo, branch) // if no commit was found then display // the 'none' badge, instead of throwing // an error response if commit == nil { w.Write(badgeNone) return } switch commit.Status { case model.StatusSuccess: w.Write(badgeSuccess) case model.StatusFailure: w.Write(badgeFailure) case model.StatusError: w.Write(badgeError) case model.StatusEnqueue, model.StatusStarted: w.Write(badgeStarted) default: w.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 // func GetCC(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var ( host = c.URLParams["host"] owner = c.URLParams["owner"] name = c.URLParams["name"] ) repo, err := datastore.GetRepoName(ctx, host, owner, name) if err != nil { w.Write(badgeNone) return } commits, err := datastore.GetCommitList(ctx, repo) if err != nil || len(commits) == 0 { w.WriteHeader(http.StatusNotFound) return } var link = httputil.GetURL(r) + "/" + repo.Host + "/" + repo.Owner + "/" + repo.Name var cc = model.NewCC(repo, commits[0], link) xml.NewEncoder(w).Encode(cc) }