Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 03:06:10 +01:00

feat(i18n): include authorlink in translations

Co-authored-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
0ko 2024-11-19 12:11:04 +05:00
parent f90928507a
commit 0b2750714c
4 changed files with 92 additions and 5 deletions

View file

@ -6,6 +6,8 @@
package templates package templates
import ( import (
"bytes"
"context"
"fmt" "fmt"
"html" "html"
"html/template" "html/template"
@ -29,6 +31,23 @@ func NewFuncMap() template.FuncMap {
return map[string]any{ return map[string]any{
"ctx": func() any { return nil }, // template context function "ctx": func() any { return nil }, // template context function
"ExecuteTemplate": func(ctx context.Context, tmplName string, args any) template.HTML {
h := HTMLRenderer()
tmpl, err := h.TemplateLookup(tmplName, ctx)
if err != nil {
panic("Template not found: " + tmplName)
}
buf := bytes.Buffer{}
if err := tmpl.Execute(&buf, args); err != nil {
panic("Error while executing template")
}
// We can safely return this as `template.HTML` as html/template will
// already make sure it's sanitized.
return template.HTML(buf.String())
},
"DumpVar": dumpVar, "DumpVar": dumpVar,
// ----------------------------------------------------------------- // -----------------------------------------------------------------

View file

@ -1746,8 +1746,8 @@ issues.add_time_sum_to_small = No time was entered.
issues.time_spent_total = Total time spent issues.time_spent_total = Total time spent
issues.time_spent_from_all_authors = `Total time spent: %s` issues.time_spent_from_all_authors = `Total time spent: %s`
issues.due_date = Due date issues.due_date = Due date
issues.push_commit_1 = added %d commit %s discussion.added_commits_one = %[1]s added %[2]d commit %[3]s
issues.push_commits_n = added %d commits %s discussion.added_commits_few = %[1]s added %[2]d commits %[3]s
issues.force_push_codes = `force-pushed %[1]s from <a class="%[7]s" href="%[3]s"><code>%[2]s</code></a> to <a class="%[7]s" href="%[5]s"><code>%[4]s</code></a> %[6]s` issues.force_push_codes = `force-pushed %[1]s from <a class="%[7]s" href="%[3]s"><code>%[2]s</code></a> to <a class="%[7]s" href="%[5]s"><code>%[4]s</code></a> %[6]s`
issues.force_push_compare = Compare issues.force_push_compare = Compare
issues.due_date_form = yyyy-mm-dd issues.due_date_form = yyyy-mm-dd

View file

@ -549,11 +549,10 @@
<div class="timeline-item event" id="{{.HashTag}}"> <div class="timeline-item event" id="{{.HashTag}}">
<span class="badge">{{svg "octicon-repo-push"}}</span> <span class="badge">{{svg "octicon-repo-push"}}</span>
<span class="text grey muted-links"> <span class="text grey muted-links">
{{template "shared/user/authorlink" .Poster}}
{{if .IsForcePush}} {{if .IsForcePush}}
{{ctx.Locale.Tr "repo.issues.force_push_codes" $.Issue.PullRequest.HeadBranch (ShortSha .OldCommit) ($.Issue.Repo.CommitLink .OldCommit) (ShortSha .NewCommit) ($.Issue.Repo.CommitLink .NewCommit) $createdStr "ui sha"}} {{template "shared/user/authorlink" .Poster}} {{ctx.Locale.Tr "repo.issues.force_push_codes" $.Issue.PullRequest.HeadBranch (ShortSha .OldCommit) ($.Issue.Repo.CommitLink .OldCommit) (ShortSha .NewCommit) ($.Issue.Repo.CommitLink .NewCommit) $createdStr "ui sha"}}
{{else}} {{else}}
{{ctx.Locale.TrN (len .Commits) "repo.issues.push_commit_1" "repo.issues.push_commits_n" (len .Commits) $createdStr}} {{ctx.Locale.TrN (len .Commits) "repo.discussion.added_commits_one" "repo.discussion.added_commits_few" (ExecuteTemplate ctx "shared/user/authorlink" .Poster) (len .Commits) $createdStr}}
{{end}} {{end}}
</span> </span>
{{if and .IsForcePush $.Issue.PullRequest.BaseRepo.Name}} {{if and .IsForcePush $.Issue.PullRequest.BaseRepo.Name}}

View file

@ -0,0 +1,69 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"fmt"
"net/http"
"net/url"
"path"
"regexp"
"strings"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
// TestDiscussionEvents is a test for various events displayed in the timelines of pulls and issues
func TestDiscussionEvents(t *testing.T) {
repoName := "discussion-timeline-tests"
user2 := "user2"
description := "This PR will be used for testing events in discussions"
// Expected branch name when initializing repo automatically
defaultBranch := "master"
htmlCleaner := regexp.MustCompile(`[\t\n]`)
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
sessionUser2 := loginUser(t, user2)
tokenUser2 := getTokenForLoggedInUser(t, sessionUser2, auth_model.AccessTokenScopeAll)
// Create test repo
var repo api.Repository
resp := sessionUser2.MakeRequest(t, NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{
Name: repoName,
AutoInit: true,
}).AddTokenAuth(tokenUser2), http.StatusCreated)
DecodeJSON(t, resp, &repo)
// == Test pulls ==
// Open a new PR as user2
testEditFileToNewBranch(t, sessionUser2, user2, repo.Name, defaultBranch, "comment-labels", "README.md", description)
sessionUser2.MakeRequest(t, NewRequestWithValues(t, "POST", path.Join(repo.FullName, "compare", fmt.Sprintf("%s...comment-labels", defaultBranch)),
map[string]string{
"_csrf": GetCSRF(t, sessionUser2, path.Join(repo.FullName, "compare", fmt.Sprintf("%s...comment-labels", defaultBranch))),
"title": description,
},
), http.StatusOK)
// Pull number, expected to be 1 in a fresh repo
testPullID := "1"
// Get the PR page and find all events
response := sessionUser2.MakeRequest(t, NewRequest(t, "GET", path.Join(repo.FullName, "pulls", testPullID)), http.StatusOK)
page := NewHTMLParser(t, response.Body)
events := page.Find(".timeline .timeline-item.event .text")
// Check the event. Should contain: "<username> added 1 commit <relative-time>"
event := events.Eq(0)
eventHTML, _ := event.Html()
eventText := htmlCleaner.ReplaceAllString(strings.TrimSpace(event.Text()), "")
assert.Contains(t, eventHTML, `href="/user2">user2</a>`)
assert.Contains(t, eventHTML, `<relative-time`)
assert.Contains(t, eventText, `user2 added 1 commit`)
})
}