2014-02-07 13:10:01 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/drone/drone/pkg/database"
|
|
|
|
)
|
|
|
|
|
2014-03-02 06:25:10 +03:00
|
|
|
const (
|
2014-03-11 21:55:23 +03:00
|
|
|
badgeSuccess = "https://img.shields.io/badge/build-success-brightgreen.svg"
|
|
|
|
badgeFailure = "https://img.shields.io/badge/build-failure-red.svg"
|
|
|
|
badgeUnknown = "https://img.shields.io/badge/build-unknown-lightgray.svg"
|
2014-03-02 06:25:10 +03:00
|
|
|
)
|
|
|
|
|
2014-03-11 21:55:23 +03:00
|
|
|
// Display a static badge (svg format) for a specific
|
2014-02-07 13:10:01 +03:00
|
|
|
// repository and an optional branch.
|
|
|
|
// TODO this needs to implement basic caching
|
|
|
|
func Badge(w http.ResponseWriter, r *http.Request) error {
|
2014-03-02 06:25:10 +03:00
|
|
|
successParam := r.FormValue("success")
|
|
|
|
failureParam := r.FormValue("failure")
|
2014-02-12 22:15:59 +03:00
|
|
|
branchParam := r.FormValue("branch")
|
2014-02-07 13:10:01 +03:00
|
|
|
hostParam := r.FormValue(":host")
|
|
|
|
ownerParam := r.FormValue(":owner")
|
|
|
|
nameParam := r.FormValue(":name")
|
|
|
|
repoSlug := fmt.Sprintf("%s/%s/%s", hostParam, ownerParam, nameParam)
|
|
|
|
|
|
|
|
// get the repo from the database
|
|
|
|
repo, err := database.GetRepoSlug(repoSlug)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the default branch for the repository
|
|
|
|
// if no branch is provided.
|
|
|
|
if len(branchParam) == 0 {
|
|
|
|
branchParam = repo.DefaultBranch()
|
|
|
|
}
|
|
|
|
|
2014-03-02 06:25:10 +03:00
|
|
|
var badge string
|
2014-02-07 13:10:01 +03:00
|
|
|
|
|
|
|
// get the latest commit from the database
|
|
|
|
// for the requested branch
|
|
|
|
commit, err := database.GetBranch(repo.ID, branchParam)
|
|
|
|
if err == nil {
|
2014-03-02 06:25:10 +03:00
|
|
|
switch {
|
|
|
|
case commit.Status == "Success" && len(successParam) == 0:
|
|
|
|
// if no success image is provided, we serve a
|
|
|
|
// badge using the shields.io service
|
|
|
|
badge = badgeSuccess
|
|
|
|
case commit.Status == "Success" && len(successParam) != 0:
|
|
|
|
// otherwise we serve the user defined success badge
|
|
|
|
badge = successParam
|
|
|
|
case commit.Status == "Failure" && len(failureParam) == 0:
|
|
|
|
// if no failure image is provided, we serve a
|
|
|
|
// badge using the shields.io service
|
|
|
|
badge = badgeFailure
|
|
|
|
case commit.Status == "Failure" && len(failureParam) != 0:
|
|
|
|
// otherwise we serve the user defined failure badge
|
|
|
|
badge = failureParam
|
|
|
|
default:
|
|
|
|
// otherwise load unknown image
|
|
|
|
badge = badgeUnknown
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, badge, http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|