github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/shield/shield.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package shield 18 19 // shield provides a local version of the shields.io service. 20 21 import ( 22 "bytes" 23 "html/template" 24 ) 25 26 var svg = `<svg xmlns="http://www.w3.org/2000/svg" width="{{.Width}}" height="20"> 27 <linearGradient id="a" x2="0" y2="100%"> 28 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> 29 <stop offset="1" stop-opacity=".1"/> 30 </linearGradient> 31 <rect rx="3" width="100%" height="20" fill="#555"/> 32 <g fill="{{.Color}}"> 33 <rect rx="3" x="{{.RightStart}}" width="{{.RightWidth}}" height="20"/> 34 <path d="M{{.RightStart}} 0h4v20h-4z"/> 35 </g> 36 <rect rx="3" width="100%" height="20" fill="url(#a)"/> 37 <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> 38 <g fill="#010101" opacity=".3"> 39 <text x="{{.XposLeft}}" y="15">{{.Subject}}</text> 40 <text x="{{.XposRight}}" y="15">{{.Status}}</text> 41 </g> 42 <text x="{{.XposLeft}}" y="14">{{.Subject}}</text> 43 <text x="{{.XposRight}}" y="14">{{.Status}}</text> 44 </g> 45 </svg>` 46 47 var svgTemplate = template.Must(template.New("svg").Parse(svg)) 48 49 // Make a small SVG badge that looks like `[subject | status]`, with the status 50 // text in the given color. 51 func Make(subject, status, color string) []byte { 52 // TODO(rmmh): Use better font-size metrics for prettier badges-- estimating 53 // character widths as 6px isn't very accurate. 54 // See also: https://github.com/badges/shields/blob/master/measure-text.js 55 p := struct { 56 Width, RightStart, RightWidth int 57 XposLeft, XposRight float64 58 Subject, Status string 59 Color string 60 }{ 61 Subject: subject, 62 Status: status, 63 RightStart: 13 + 6*len(subject), 64 RightWidth: 13 + 6*len(status), 65 } 66 p.Width = p.RightStart + p.RightWidth 67 p.XposLeft = float64(p.RightStart) * 0.5 68 p.XposRight = float64(p.RightStart) + float64(p.RightWidth-2)*0.5 69 switch color { 70 case "brightgreen": 71 p.Color = "#4c1" 72 case "red": 73 p.Color = "#e05d44" 74 default: 75 panic("Invalid color " + color) 76 } 77 var buf bytes.Buffer 78 svgTemplate.Execute(&buf, p) 79 return buf.Bytes() 80 }