From 399ac6835636153fb6ea21383ce3c94956bbc0e0 Mon Sep 17 00:00:00 2001 From: Markus Ast Date: Wed, 29 Oct 2014 12:06:11 +0100 Subject: [PATCH] add flat and flat-square badge styles --- server/handler/badge.go | 57 ++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/server/handler/badge.go b/server/handler/badge.go index 98a502de..5fe9d2ff 100644 --- a/server/handler/badge.go +++ b/server/handler/badge.go @@ -13,13 +13,34 @@ import ( // 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`) -) +type badge struct { + success, failure, started, err, none []byte +} + +var defaultBadge = badge{ + []byte(`buildbuildsuccesssuccess`), + []byte(`buildbuildfailurefailure`), + []byte(`buildbuildstartedstarted`), + []byte(`buildbuilderrorerror`), + []byte(`buildbuildnonenone`), +} + +var badgeStyles = map[string]badge{ + "flat": badge{ + []byte(`buildbuildsuccesssuccess`), + []byte(`buildbuildfailurefailure`), + []byte(`buildbuildstartedstarted`), + []byte(`buildbuilderrorerror`), + []byte(`buildbuildnonenone`), + }, + "flat-square": badge{ + []byte(`buildsuccess`), + []byte(`buildfailure`), + []byte(`buildstarted`), + []byte(`builderror`), + []byte(`buildnone`), + }, +} // GetBadge accepts a request to retrieve the named // repo and branhes latest build details from the datastore @@ -34,15 +55,21 @@ func GetBadge(c web.C, w http.ResponseWriter, r *http.Request) { owner = c.URLParams["owner"] name = c.URLParams["name"] branch = r.FormValue("branch") + style = r.FormValue("style") ) // an SVG response is always served, even when error, so // we can go ahead and set the content type appropriately. w.Header().Set("Content-Type", "image/svg+xml") + badge, ok := badgeStyles[style] + if !ok { + badge = defaultBadge + } + repo, err := datastore.GetRepoName(ctx, host, owner, name) if err != nil { - w.Write(badgeNone) + w.Write(badge.none) return } if len(branch) == 0 { @@ -54,21 +81,21 @@ func GetBadge(c web.C, w http.ResponseWriter, r *http.Request) { // the 'none' badge, instead of throwing // an error response if commit == nil { - w.Write(badgeNone) + w.Write(badge.none) return } switch commit.Status { case model.StatusSuccess: - w.Write(badgeSuccess) + w.Write(badge.success) case model.StatusFailure: - w.Write(badgeFailure) + w.Write(badge.failure) case model.StatusError: - w.Write(badgeError) + w.Write(badge.err) case model.StatusEnqueue, model.StatusStarted: - w.Write(badgeStarted) + w.Write(badge.started) default: - w.Write(badgeNone) + w.Write(badge.none) } } @@ -88,7 +115,7 @@ func GetCC(c web.C, w http.ResponseWriter, r *http.Request) { repo, err := datastore.GetRepoName(ctx, host, owner, name) if err != nil { - w.Write(badgeNone) + w.Write(defaultBadge.none) return } commits, err := datastore.GetCommitList(ctx, repo)