Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flat-square action badge style #34062

Merged
merged 8 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/badge/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@ func (b Badge) Width() int {
return b.Label.width + b.Message.width
}

// Style represents the style of a badge
type Style string

const (
StyleFlat = "flat"
StyleFlatSquare = "flat-square"
)

const (
defaultOffset = 10
defaultFontSize = 11
DefaultColor = "#9f9f9f" // Grey
DefaultFontFamily = "DejaVu Sans,Verdana,Geneva,sans-serif"
DefaultStyle = StyleFlat
)

var StatusColorMap = map[actions_model.Status]string{
Expand Down
16 changes: 15 additions & 1 deletion routers/web/devtest/devtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package devtest

import (
"fmt"
"html/template"
"net/http"
"path"
Expand Down Expand Up @@ -127,7 +128,9 @@ func prepareMockDataBadgeCommitSign(ctx *context.Context) {

func prepareMockDataBadgeActionsSvg(ctx *context.Context) {
fontFamilyNames := strings.Split(badge.DefaultFontFamily, ",")
styles := []badge.Style{badge.StyleFlat, badge.StyleFlatSquare}
selectedFontFamilyName := ctx.FormString("font", fontFamilyNames[0])
selectedStyle := ctx.FormString("style", badge.DefaultStyle)
var badges []badge.Badge
badges = append(badges, badge.GenerateBadge("啊啊啊啊啊啊啊啊啊啊啊啊", "🌞🌞🌞🌞🌞", "green"))
for r := rune(0); r < 256; r++ {
Expand All @@ -141,7 +144,16 @@ func prepareMockDataBadgeActionsSvg(ctx *context.Context) {
for i, b := range badges {
b.IDPrefix = "devtest-" + strconv.FormatInt(int64(i), 10) + "-"
b.FontFamily = selectedFontFamilyName
h, err := ctx.RenderToHTML("shared/actions/runner_badge", map[string]any{"Badge": b})
var h template.HTML
var err error
switch selectedStyle {
case badge.StyleFlat:
h, err = ctx.RenderToHTML("shared/actions/runner_badge_flat", map[string]any{"Badge": b})
case badge.StyleFlatSquare:
h, err = ctx.RenderToHTML("shared/actions/runner_badge_flat-square", map[string]any{"Badge": b})
default:
err = fmt.Errorf("unknown badge style: %s", selectedStyle)
}
if err != nil {
ctx.ServerError("RenderToHTML", err)
return
Expand All @@ -151,6 +163,8 @@ func prepareMockDataBadgeActionsSvg(ctx *context.Context) {
ctx.Data["BadgeSVGs"] = badgeSVGs
ctx.Data["BadgeFontFamilyNames"] = fontFamilyNames
ctx.Data["SelectedFontFamilyName"] = selectedFontFamilyName
ctx.Data["BadgeStyles"] = styles
ctx.Data["SelectedStyle"] = selectedStyle
}

func prepareMockData(ctx *context.Context) {
Expand Down
17 changes: 14 additions & 3 deletions routers/web/repo/actions/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"path/filepath"
"slices"
"strings"

actions_model "code.gitea.io/gitea/models/actions"
Expand All @@ -25,15 +26,25 @@ func GetWorkflowBadge(ctx *context.Context) {
branchRef := fmt.Sprintf("refs/heads/%s", branch)
event := ctx.Req.URL.Query().Get("event")

badge, err := getWorkflowBadge(ctx, workflowFile, branchRef, event)
style := ctx.Req.URL.Query().Get("style")
if !slices.Contains([]badge.Style{badge.StyleFlat, badge.StyleFlatSquare}, badge.Style(style)) {
style = badge.DefaultStyle
}

b, err := getWorkflowBadge(ctx, workflowFile, branchRef, event)
if err != nil {
ctx.ServerError("GetWorkflowBadge", err)
return
}

ctx.Data["Badge"] = badge
ctx.Data["Badge"] = b
ctx.RespHeader().Set("Content-Type", "image/svg+xml")
ctx.HTML(http.StatusOK, "shared/actions/runner_badge")
switch style {
case badge.StyleFlat:
ctx.HTML(http.StatusOK, "shared/actions/runner_badge_flat")
case badge.StyleFlatSquare:
ctx.HTML(http.StatusOK, "shared/actions/runner_badge_flat-square")
}
}

func getWorkflowBadge(ctx *context.Context, workflowFile, branchName, event string) (badge.Badge, error) {
Expand Down
13 changes: 10 additions & 3 deletions templates/devtest/badge-actions-svg.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
<div>
<h1>Actions SVG</h1>
<form class="tw-my-3">
{{range $fontName := .BadgeFontFamilyNames}}
<label><input name="font" type="radio" value="{{$fontName}}" {{Iif (eq $.SelectedFontFamilyName $fontName) "checked"}}>{{$fontName}}</label>
{{end}}
<div class="tw-mb-2">
{{range $fontName := .BadgeFontFamilyNames}}
<label><input name="font" type="radio" value="{{$fontName}}" {{Iif (eq $.SelectedFontFamilyName $fontName) "checked"}}>{{$fontName}}</label>
{{end}}
</div>
<div class="tw-mb-2">
{{range $style := .BadgeStyles}}
<label><input name="style" type="radio" value="{{$style}}" {{Iif (eq $.SelectedStyle $style) "checked"}}>{{$style}}</label>
{{end}}
</div>
<button>submit</button>
</form>
<div class="flex-text-block tw-flex-wrap">
Expand Down
15 changes: 15 additions & 0 deletions templates/shared/actions/runner_badge_flat-square.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{{.Badge.Width}}" height="20"
role="img" aria-label="{{.Badge.Label.Text}}: {{.Badge.Message.Text}}">
<title>{{.Badge.Label.Text}}: {{.Badge.Message.Text}}</title>
<g shape-rendering="crispEdges">
<rect width="{{.Badge.Label.Width}}" height="20" fill="#555" />
<rect x="{{.Badge.Label.Width}}" width="{{.Badge.Message.Width}}" height="20" fill="{{.Badge.Color}}" />
</g>
<g fill="#fff" text-anchor="middle" font-family="{{.Badge.FontFamily}}"
text-rendering="geometricPrecision" font-size="{{.Badge.FontSize}}">
<text x="{{.Badge.Label.X}}" y="140"
transform="scale(.1)" fill="#fff" textLength="{{.Badge.Label.TextLength}}">{{.Badge.Label.Text}}</text>
<text x="{{.Badge.Message.X}}" y="140" transform="scale(.1)" fill="#fff"
textLength="{{.Badge.Message.TextLength}}">{{.Badge.Message.Text}}</text>
</g>
</svg>