Refactor renders (#15175)

* Refactor renders

* Some performance optimization

* Fix comment

* Transform reader

* Fix csv test

* Fix test

* Fix tests

* Improve optimaziation

* Fix test

* Fix test

* Detect file encoding with reader

* Improve optimaziation

* reduce memory usage

* improve code

* fix build

* Fix test

* Fix for go1.15

* Fix render

* Fix comment

* Fix lint

* Fix test

* Don't use NormalEOF when unnecessary

* revert change on util.go

* Apply suggestions from code review

Co-authored-by: zeripath <art27@cantab.net>

* rename function

* Take NormalEOF back

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2021-04-20 06:25:08 +08:00 committed by GitHub
parent c9cc6698d2
commit 9d99f6ab19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 1027 additions and 627 deletions

View file

@ -95,11 +95,17 @@ func TestCSVDiff(t *testing.T) {
var baseReader *csv.Reader
if len(c.base) > 0 {
baseReader = csv_module.CreateReaderAndGuessDelimiter([]byte(c.base))
baseReader, err = csv_module.CreateReaderAndGuessDelimiter(strings.NewReader(c.base))
if err != nil {
t.Errorf("CreateReaderAndGuessDelimiter failed: %s", err)
}
}
var headReader *csv.Reader
if len(c.head) > 0 {
headReader = csv_module.CreateReaderAndGuessDelimiter([]byte(c.head))
headReader, err = csv_module.CreateReaderAndGuessDelimiter(strings.NewReader(c.head))
if err != nil {
t.Errorf("CreateReaderAndGuessDelimiter failed: %s", err)
}
}
result, err := CreateCsvDiff(diff.Files[0], baseReader, headReader)

View file

@ -174,8 +174,7 @@ func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
SendAsync(msg)
}
func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []string, fromMention bool, info string) []*Message {
func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []string, fromMention bool, info string) ([]*Message, error) {
var (
subject string
link string
@ -199,7 +198,14 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str
}
// This is the body of the new issue or comment, not the mail body
body := string(markup.RenderByType(markdown.MarkupName, []byte(ctx.Content), ctx.Issue.Repo.HTMLURL(), ctx.Issue.Repo.ComposeMetas()))
body, err := markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.Issue.Repo.HTMLURL(),
Metas: ctx.Issue.Repo.ComposeMetas(),
}, ctx.Content)
if err != nil {
return nil, err
}
actType, actName, tplName := actionToTemplate(ctx.Issue, ctx.ActionType, commentType, reviewType)
if actName != "new" {
@ -240,14 +246,13 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str
// TODO: i18n templates?
if err := subjectTemplates.ExecuteTemplate(&mailSubject, string(tplName), mailMeta); err == nil {
subject = sanitizeSubject(mailSubject.String())
if subject == "" {
subject = fallback
}
} else {
log.Error("ExecuteTemplate [%s]: %v", tplName+"/subject", err)
}
if subject == "" {
subject = fallback
}
subject = emoji.ReplaceAliases(subject)
mailMeta["Subject"] = subject
@ -275,7 +280,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, tos []str
msgs = append(msgs, msg)
}
return msgs
return msgs, nil
}
func sanitizeSubject(subject string) string {
@ -288,21 +293,26 @@ func sanitizeSubject(subject string) string {
}
// SendIssueAssignedMail composes and sends issue assigned email
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) {
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) error {
langMap := make(map[string][]string)
for _, user := range recipients {
langMap[user.Language] = append(langMap[user.Language], user.Email)
}
for lang, tos := range langMap {
SendAsyncs(composeIssueCommentMessages(&mailCommentContext{
msgs, err := composeIssueCommentMessages(&mailCommentContext{
Issue: issue,
Doer: doer,
ActionType: models.ActionType(0),
Content: content,
Comment: comment,
}, lang, tos, false, "issue assigned"))
}, lang, tos, false, "issue assigned")
if err != nil {
return err
}
SendAsyncs(msgs)
}
return nil
}
// actionToTemplate returns the type and name of the action facing the user

View file

@ -146,7 +146,11 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visite
// working backwards from the last (possibly) incomplete batch. If len(receivers) can be 0 this
// starting condition will need to be changed slightly
for i := ((len(receivers) - 1) / MailBatchSize) * MailBatchSize; i >= 0; i -= MailBatchSize {
SendAsyncs(composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments"))
msgs, err := composeIssueCommentMessages(ctx, lang, receivers[i:], fromMention, "issue comments")
if err != nil {
return err
}
SendAsyncs(msgs)
receivers = receivers[:i]
}
}

View file

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
@ -48,7 +49,15 @@ func MailNewRelease(rel *models.Release) {
func mailNewRelease(lang string, tos []string, rel *models.Release) {
locale := translation.NewLocale(lang)
rel.RenderedNote = markdown.RenderString(rel.Note, rel.Repo.Link(), rel.Repo.ComposeMetas())
var err error
rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{
URLPrefix: rel.Repo.Link(),
Metas: rel.Repo.ComposeMetas(),
}, rel.Note)
if err != nil {
log.Error("markdown.RenderString(%d): %v", rel.RepoID, err)
return
}
subject := locale.Tr("mail.release.new.subject", rel.TagName, rel.Repo.FullName())
mailMeta := map[string]interface{}{

View file

@ -58,8 +58,9 @@ func TestComposeIssueCommentMessage(t *testing.T) {
InitMailRender(stpl, btpl)
tos := []string{"test@gitea.com", "test2@gitea.com"}
msgs := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue,
msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCommentIssue,
Content: "test body", Comment: comment}, "en-US", tos, false, "issue comment")
assert.NoError(t, err)
assert.Len(t, msgs, 2)
gomailMsg := msgs[0].ToMessage()
mailto := gomailMsg.GetHeader("To")
@ -92,8 +93,9 @@ func TestComposeIssueMessage(t *testing.T) {
InitMailRender(stpl, btpl)
tos := []string{"test@gitea.com", "test2@gitea.com"}
msgs := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue,
msgs, err := composeIssueCommentMessages(&mailCommentContext{Issue: issue, Doer: doer, ActionType: models.ActionCreateIssue,
Content: "test body"}, "en-US", tos, false, "issue create")
assert.NoError(t, err)
assert.Len(t, msgs, 2)
gomailMsg := msgs[0].ToMessage()
@ -218,7 +220,8 @@ func TestTemplateServices(t *testing.T) {
}
func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, tos []string, fromMention bool, info string) *Message {
msgs := composeIssueCommentMessages(ctx, "en-US", tos, fromMention, info)
msgs, err := composeIssueCommentMessages(ctx, "en-US", tos, fromMention, info)
assert.NoError(t, err)
assert.Len(t, msgs, 1)
return msgs[0]
}