Skip to content

Commit 8923dd7

Browse files
authored
Merge pull request #33 from factorysh/features/dynamic-badges
Add dynamic badges for BadgeMyTask
2 parents e304cbe + 014c298 commit 8923dd7

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

application/badge.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010

11+
"github.com/factorysh/microdensity/badge"
1112
_badge "github.com/factorysh/microdensity/badge"
1213
"github.com/go-chi/chi/v5"
1314
"go.uber.org/zap"
@@ -30,23 +31,27 @@ func (a *Application) BadgeMyTaskHandler(latest bool) http.HandlerFunc {
3031
commit := chi.URLParam(r, "commit")
3132
bdg := chi.URLParam(r, "badge")
3233

34+
// get the task
3335
t, err := a.storage.GetByCommit(service, project, branch, commit, latest)
3436
if err != nil {
3537
l.Warn("Task get error", zap.Error(err))
3638
w.WriteHeader(http.StatusNotFound)
3739
return
3840
}
3941

42+
// try to get the output badge for this task in this service
4043
p := filepath.Join(a.storage.GetVolumePath(t), "/data", fmt.Sprintf("%s.badge", bdg))
41-
4244
_, err = os.Stat(p)
45+
46+
// if not found
4347
if err != nil {
44-
l.Warn("Task get error", zap.Error(err))
48+
// fallback to status badge
4549
if os.IsNotExist(err) {
46-
w.WriteHeader(http.StatusNotFound)
47-
} else {
48-
w.WriteHeader(http.StatusBadRequest)
50+
// use the service name, task status and colors from badge pkg
51+
badge.WriteBadge(service, t.State.String(), _badge.Colors.Get(t.State), w)
52+
return
4953
}
54+
w.WriteHeader(http.StatusBadRequest)
5055
return
5156
}
5257
l = l.With(zap.String("path", p))

badge/badge.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
"github.com/narqo/go-badge"
1111
)
1212

13-
var colors = statusToColors{
13+
// Colors is used to harmonize status colors for all badges
14+
var Colors = statusToColors{
1415
c: map[task.State]badge.Color{
1516
// blue - lapis
1617
task.Ready: "#2832C2",
@@ -20,8 +21,8 @@ var colors = statusToColors{
2021
task.Running: "#DD571C",
2122
// red - ruby
2223
task.Failed: "#900603",
23-
// green - emerald
24-
task.Done: "#028A0F",
24+
// green
25+
task.Done: "#4ec820",
2526
},
2627
// blue
2728
Default: "#527284",
@@ -67,7 +68,7 @@ func StatusBadge(s storage.Storage, latest bool) func(http.ResponseWriter, *http
6768
t, err := s.GetByCommit(service, project, branch, commit, latest)
6869

6970
if t == nil || err != nil {
70-
err = writeBadge("status", "?!", colors.Default, w)
71+
err = WriteBadge("status", "?!", Colors.Default, w)
7172
if err != nil {
7273
panic(err)
7374
}
@@ -79,15 +80,15 @@ func StatusBadge(s storage.Storage, latest bool) func(http.ResponseWriter, *http
7980
return
8081
}
8182

82-
writeBadge(fmt.Sprintf("status : %s", service), t.State.String(), colors.Get(t.State), w)
83+
WriteBadge(fmt.Sprintf("status : %s", service), t.State.String(), Colors.Get(t.State), w)
8384
if err != nil {
8485
panic(err)
8586
}
8687
}
8788
}
8889

89-
// writeBadge is a wrapper use to write a badge into an http response
90-
func writeBadge(label string, content string, color badge.Color, w http.ResponseWriter) error {
90+
// WriteBadge is a wrapper use to write a badge into an http response
91+
func WriteBadge(label string, content string, color badge.Color, w http.ResponseWriter) error {
9192
w.Header().Set("content-type", "image/svg+xml")
9293
return badge.Render(label, content, color, w)
9394
}

0 commit comments

Comments
 (0)