forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.go
124 lines (105 loc) · 3.29 KB
/
badge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package jobserver
import (
"bytes"
"fmt"
"net/http"
"text/template"
"github.com/concourse/atc/db"
)
var (
badgePassing = badge{width: 88, fillColor: `#44cc11`, status: `passing`}
badgeFailing = badge{width: 80, fillColor: `#e05d44`, status: `failing`}
badgeUnknown = badge{width: 98, fillColor: `#9f9f9f`, status: `unknown`}
badgeAborted = badge{width: 90, fillColor: `#8f4b2d`, status: `aborted`}
badgeErrored = badge{width: 88, fillColor: `#fe7d37`, status: `errored`}
)
type badge struct {
width int
fillColor string
status string
}
func (b *badge) statusWidth() int {
return b.width - 37
}
func (b *badge) statusTextWidth() string {
return fmt.Sprintf("%.1f", float64(b.width)/2+17.5)
}
func (b *badge) String() string {
tmpl, err := template.New("badge").Parse(badgeTemplate)
if err != nil {
panic(err)
}
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, badgeTemplateConfig{
Width: b.width,
FillColor: b.fillColor,
Status: b.status,
StatusWidth: b.statusWidth(),
StatusTextWidth: b.statusTextWidth(),
})
return buffer.String()
}
func badgeForBuild(build db.Build) *badge {
switch {
case build == nil:
return &badgeUnknown
case build.Status() == db.StatusSucceeded:
return &badgePassing
case build.Status() == db.StatusFailed:
return &badgeFailing
case build.Status() == db.StatusAborted:
return &badgeAborted
case build.Status() == db.StatusErrored:
return &badgeErrored
default:
return &badgeUnknown
}
}
const badgeTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="{{ .Width }}" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1" />
<stop offset="1" stop-opacity=".1" />
</linearGradient>
<mask id="a">
<rect width="{{ .Width }}" height="20" rx="3" fill="#fff" />
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h37v20H0z" />
<path fill="{{ .FillColor }}" d="M37 0h{{ .StatusWidth }}v20H37z" />
<path fill="url(#b)" d="M0 0h{{ .Width }}v20H0z" />
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="18.5" y="15" fill="#010101" fill-opacity=".3">build</text>
<text x="18.5" y="14">build</text>
<text x="{{ .StatusTextWidth }}" y="15" fill="#010101" fill-opacity=".3">{{ .Status }}</text>
<text x="{{ .StatusTextWidth }}" y="14">{{ .Status }}</text>
</g>
</svg>`
type badgeTemplateConfig struct {
Width int
StatusWidth int
StatusTextWidth string
Status string
FillColor string
}
func (s *Server) JobBadge(pipelineDB db.PipelineDB) http.Handler {
logger := s.logger.Session("job-badge")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
jobName := r.FormValue(":job_name")
_, found := pipelineDB.Config().Jobs.Lookup(jobName)
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
build, _, err := pipelineDB.GetJobFinishedAndNextBuild(jobName)
if err != nil {
logger.Error("could-not-get-job-finished-and-next-build", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, badgeForBuild(build))
})
}