Skip to content

Commit ad0f9e2

Browse files
committed
optimize, lazy init, default style fallback
1 parent 1d846b9 commit ad0f9e2

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

modules/badge/badge.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package badge
55

66
import (
77
"strings"
8+
"sync"
89
"unicode"
910

1011
actions_model "code.gitea.io/gitea/models/actions"
@@ -49,9 +50,6 @@ func (b Badge) Width() int {
4950
return b.Label.width + b.Message.width
5051
}
5152

52-
// Style represents the style of a badge
53-
type Style string
54-
5553
const (
5654
StyleFlat = "flat"
5755
StyleFlatSquare = "flat-square"
@@ -65,8 +63,13 @@ const (
6563
DefaultStyle = StyleFlat
6664
)
6765

68-
var (
69-
StatusColorMap = map[actions_model.Status]string{
66+
var GlobalVars = sync.OnceValue(func() (ret struct {
67+
StatusColorMap map[actions_model.Status]string
68+
DejaVuGlyphWidthData map[rune]uint8
69+
AllStyles []string
70+
},
71+
) {
72+
ret.StatusColorMap = map[actions_model.Status]string{
7073
actions_model.StatusSuccess: "#4c1", // Green
7174
actions_model.StatusSkipped: "#dfb317", // Yellow
7275
actions_model.StatusUnknown: "#97ca00", // Light Green
@@ -76,9 +79,10 @@ var (
7679
actions_model.StatusRunning: "#dfb317", // Yellow
7780
actions_model.StatusBlocked: "#dfb317", // Yellow
7881
}
79-
80-
AllStyles = []Style{StyleFlat, StyleFlatSquare}
81-
)
82+
ret.DejaVuGlyphWidthData = dejaVuGlyphWidthDataFunc()
83+
ret.AllStyles = []string{StyleFlat, StyleFlatSquare}
84+
return ret
85+
})
8286

8387
// GenerateBadge generates badge with given template
8488
func GenerateBadge(label, message, color string) Badge {
@@ -106,7 +110,7 @@ func GenerateBadge(label, message, color string) Badge {
106110

107111
func calculateTextWidth(text string) int {
108112
width := 0
109-
widthData := DejaVuGlyphWidthData()
113+
widthData := GlobalVars().DejaVuGlyphWidthData
110114
for _, char := range strings.TrimSpace(text) {
111115
charWidth, ok := widthData[char]
112116
if !ok {

modules/badge/badge_glyph_width.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package badge
55

6-
import "sync"
7-
86
// DejaVuGlyphWidthData is generated by `sfnt.Face.GlyphAdvance(nil, <rune>, 11, font.HintingNone)` with DejaVu Sans
97
// v2.37 (https://github.com/dejavu-fonts/dejavu-fonts/releases/download/version_2_37/dejavu-sans-ttf-2.37.zip).
108
//
@@ -13,7 +11,7 @@ import "sync"
1311
//
1412
// A devtest page "/devtest/badge-actions-svg" could be used to check the rendered images.
1513

16-
var DejaVuGlyphWidthData = sync.OnceValue(func() map[rune]uint8 {
14+
func dejaVuGlyphWidthDataFunc() map[rune]uint8 {
1715
return map[rune]uint8{
1816
32: 3,
1917
33: 4,
@@ -205,4 +203,4 @@ var DejaVuGlyphWidthData = sync.OnceValue(func() map[rune]uint8 {
205203
254: 7,
206204
255: 7,
207205
}
208-
})
206+
}

routers/web/devtest/devtest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func prepareMockDataBadgeActionsSvg(ctx *context.Context) {
162162
ctx.Data["BadgeSVGs"] = badgeSVGs
163163
ctx.Data["BadgeFontFamilyNames"] = fontFamilyNames
164164
ctx.Data["SelectedFontFamilyName"] = selectedFontFamilyName
165-
ctx.Data["BadgeStyles"] = badge.AllStyles
165+
ctx.Data["BadgeStyles"] = badge.GlobalVars().AllStyles
166166
ctx.Data["SelectedStyle"] = selectedStyle
167167
}
168168

routers/web/repo/actions/badge.go

+9-17
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,25 @@ package actions
55

66
import (
77
"errors"
8-
"fmt"
98
"net/http"
109
"path/filepath"
11-
"slices"
1210
"strings"
1311

1412
actions_model "code.gitea.io/gitea/models/actions"
1513
"code.gitea.io/gitea/modules/badge"
14+
"code.gitea.io/gitea/modules/git"
1615
"code.gitea.io/gitea/modules/util"
1716
"code.gitea.io/gitea/services/context"
1817
)
1918

2019
func GetWorkflowBadge(ctx *context.Context) {
2120
workflowFile := ctx.PathParam("workflow_name")
22-
branch := ctx.Req.URL.Query().Get("branch")
23-
if branch == "" {
24-
branch = ctx.Repo.Repository.DefaultBranch
25-
}
26-
branchRef := fmt.Sprintf("refs/heads/%s", branch)
27-
event := ctx.Req.URL.Query().Get("event")
28-
29-
style := ctx.Req.URL.Query().Get("style")
30-
if !slices.Contains(badge.AllStyles, badge.Style(style)) {
31-
style = badge.DefaultStyle
32-
}
21+
branch := ctx.FormString("branch", ctx.Repo.Repository.DefaultBranch)
22+
event := ctx.FormString("event")
23+
style := ctx.FormString("style")
3324

34-
b, err := getWorkflowBadge(ctx, workflowFile, branchRef, event)
25+
branchRef := git.RefNameFromBranch(branch)
26+
b, err := getWorkflowBadge(ctx, workflowFile, branchRef.String(), event)
3527
if err != nil {
3628
ctx.ServerError("GetWorkflowBadge", err)
3729
return
@@ -40,10 +32,10 @@ func GetWorkflowBadge(ctx *context.Context) {
4032
ctx.Data["Badge"] = b
4133
ctx.RespHeader().Set("Content-Type", "image/svg+xml")
4234
switch style {
43-
case badge.StyleFlat:
44-
ctx.HTML(http.StatusOK, "shared/actions/runner_badge_flat")
4535
case badge.StyleFlatSquare:
4636
ctx.HTML(http.StatusOK, "shared/actions/runner_badge_flat-square")
37+
default: // defaults to badge.StyleFlat
38+
ctx.HTML(http.StatusOK, "shared/actions/runner_badge_flat")
4739
}
4840
}
4941

@@ -59,7 +51,7 @@ func getWorkflowBadge(ctx *context.Context, workflowFile, branchName, event stri
5951
return badge.Badge{}, err
6052
}
6153

62-
color, ok := badge.StatusColorMap[run.Status]
54+
color, ok := badge.GlobalVars().StatusColorMap[run.Status]
6355
if !ok {
6456
return badge.GenerateBadge(workflowName, "unknown status", badge.DefaultColor), nil
6557
}

0 commit comments

Comments
 (0)