fix: support Gitea 28.0.0 mail templates
Release / Validate Templates (push) Canceled after 0s
Release / Package & Release (push) Canceled after 0s

This commit is contained in:
KenanZhu committed 2026-10-02 19:44:20 +08:00
1 parent ffb674669b
commit 92d2ba9889
18 files changed
+135 -42

No files matched your search

+17 -5
View File
@@ -15,10 +15,10 @@ var TemplateFuncs = template.FuncMap{
"QueryEscape": url.QueryEscape,
"ShortSha": ShortSha,
"PathEscapeSegments": PathEscapeSegments,
"FileSize": FileSize,
"FormatByteSize": previewFormatByteSize,
"HTMLFormat": HTMLFormat,
"Dict": Dict,
"gt": Gt, // override built-in for JSON float64/int coercion
"gt": Gt, // override built-in for JSON float64/int coercion
"lt": Lt,
"ge": Ge,
"le": Le,
@@ -41,8 +41,20 @@ func PathEscapeSegments(s string) string {
return strings.Join(parts, "/")
}
// FileSize formats a byte count as a human-readable string (e.g. "2.5 MB").
func FileSize(n int64) string {
// previewFormatByteSize converts JSON fixture numbers to Gitea's int64 input type.
func previewFormatByteSize(size any) (string, error) {
switch n := size.(type) {
case int64:
return FormatByteSize(n), nil
case float64:
return FormatByteSize(int64(n)), nil
default:
return "", fmt.Errorf("FormatByteSize: unsupported size type %T", size)
}
}
// FormatByteSize mirrors Gitea 28's IEC attachment size formatting.
func FormatByteSize(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
@@ -52,7 +64,7 @@ func FileSize(n int64) string {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %s", float64(n)/float64(div), []string{"KB", "MB", "GB", "TB"}[exp])
return fmt.Sprintf("%.1f %s", float64(n)/float64(div), []string{"KiB", "MiB", "GiB", "TiB"}[exp])
}
// HTMLFormat substitutes %s/%d placeholders with HTML-escaped values.
+53
View File
@@ -0,0 +1,53 @@
package preview
import (
"html/template"
"os"
"path/filepath"
"strings"
"testing"
"gitea-mail-templates/tools/config"
)
// Gitea 28 removes FileSize, so render an attachment in every release theme.
func TestReleaseAttachmentsGitea28(t *testing.T) {
if _, ok := TemplateFuncs["FileSize"]; ok {
t.Fatal("FileSize is not available in Gitea 28 mail templates")
}
cfg, err := config.Load(filepath.Join("..", "data", "templates_config.json"))
if err != nil {
t.Fatal(err)
}
data := cfg.Templates["release"].Params
data["locale"] = Locale{}
markSafeHTML(data)
themesDir := filepath.Join("..", "..", "themes")
themes, err := os.ReadDir(themesDir)
if err != nil {
t.Fatal(err)
}
for _, theme := range themes {
if !theme.IsDir() {
continue
}
t.Run(theme.Name(), func(t *testing.T) {
content, err := os.ReadFile(filepath.Join(themesDir, theme.Name(), "mail", "repo", "release.tmpl"))
if err != nil {
t.Fatal(err)
}
tmpl, err := template.New("release").Funcs(TemplateFuncs).Parse(string(content))
if err != nil {
t.Fatal(err)
}
var rendered strings.Builder
if err := tmpl.Execute(&rendered, data); err != nil {
t.Fatal(err)
}
if !strings.Contains(rendered.String(), "release-notes.txt") || !strings.Contains(rendered.String(), "1.5 KiB") {
t.Fatal("release attachment or formatted size missing from email")
}
})
}
}