From 5a438ee3c0303efcb9d1935ff521917fe8a109e8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 24 Sep 2019 13:02:49 +0800 Subject: Move all mail related codes from models to services/mailer (#7200) * move all mail related codes from models to modules/mailer * fix lint * use DBContext instead Engine * use WithContext not WithEngine * Use DBContext instead of Engine * don't use defer when sess.Close() * move DBContext to context.go and add some methods * move mailer from modules/ to services * fix lint * fix tests * fix fmt * add gitea copyright * fix tests * don't expose db functions * make code clear * add DefaultDBContext * fix build * fix bug --- models/context.go | 55 +++++++ models/error.go | 15 ++ models/issue.go | 8 +- models/issue_comment.go | 35 ----- models/issue_mail.go | 159 -------------------- models/issue_user.go | 8 +- models/issue_user_test.go | 2 +- models/mail.go | 217 --------------------------- models/mail_test.go | 87 ----------- modules/mailer/mailer.go | 303 -------------------------------------- modules/notification/mail/mail.go | 11 +- routers/admin/admin.go | 3 +- routers/admin/users.go | 5 +- routers/api/v1/admin/user.go | 6 +- routers/init.go | 2 +- routers/repo/setting.go | 3 +- routers/routes/routes.go | 3 +- routers/user/auth.go | 12 +- routers/user/auth_openid.go | 4 +- routers/user/setting/account.go | 3 +- services/mailer/mail.go | 222 ++++++++++++++++++++++++++++ services/mailer/mail_comment.go | 47 ++++++ services/mailer/mail_issue.go | 159 ++++++++++++++++++++ services/mailer/mail_test.go | 91 ++++++++++++ services/mailer/mailer.go | 303 ++++++++++++++++++++++++++++++++++++++ services/mailer/main_test.go | 16 ++ 26 files changed, 949 insertions(+), 830 deletions(-) create mode 100644 models/context.go delete mode 100644 models/issue_mail.go delete mode 100644 models/mail.go delete mode 100644 models/mail_test.go delete mode 100644 modules/mailer/mailer.go create mode 100644 services/mailer/mail.go create mode 100644 services/mailer/mail_comment.go create mode 100644 services/mailer/mail_issue.go create mode 100644 services/mailer/mail_test.go create mode 100644 services/mailer/mailer.go create mode 100644 services/mailer/main_test.go diff --git a/models/context.go b/models/context.go new file mode 100644 index 0000000000..5f47c595a2 --- /dev/null +++ b/models/context.go @@ -0,0 +1,55 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +// DBContext represents a db context +type DBContext struct { + e Engine +} + +// DefaultDBContext represents a DBContext with default Engine +func DefaultDBContext() DBContext { + return DBContext{x} +} + +// Committer represents an interface to Commit or Close the dbcontext +type Committer interface { + Commit() error + Close() +} + +// TxDBContext represents a transaction DBContext +func TxDBContext() (DBContext, Committer, error) { + sess := x.NewSession() + if err := sess.Begin(); err != nil { + sess.Close() + return DBContext{}, nil, err + } + + return DBContext{sess}, sess, nil +} + +// WithContext represents executing database operations +func WithContext(f func(ctx DBContext) error) error { + return f(DBContext{x}) +} + +// WithTx represents executing database operations on a trasaction +func WithTx(f func(ctx DBContext) error) error { + sess := x.NewSession() + if err := sess.Begin(); err != nil { + sess.Close() + return err + } + + if err := f(DBContext{sess}); err != nil { + sess.Close() + return err + } + + err := sess.Commit() + sess.Close() + return err +} diff --git a/models/error.go b/models/error.go index c025437c50..9974287a0a 100644 --- a/models/error.go +++ b/models/error.go @@ -11,6 +11,21 @@ import ( "code.gitea.io/gitea/modules/git" ) +// ErrNotExist represents a non-exist error. +type ErrNotExist struct { + ID int64 +} + +// IsErrNotExist checks if an error is an ErrNotExist +func IsErrNotExist(err error) bool { + _, ok := err.(ErrNotExist) + return ok +} + +func (err ErrNotExist) Error() string { + return fmt.Sprintf("record does not exist [id: %d]", err.ID) +} + // ErrNameReserved represents a "reserved name" error. type ErrNameReserved struct { Name string diff --git a/models/issue.go b/models/issue.go index bb2db94407..c4b15e9ddf 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1503,7 +1503,7 @@ func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) { // UpdateIssueMentions extracts mentioned people from content and // updates issue-user relations for them. -func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error { +func UpdateIssueMentions(ctx DBContext, issueID int64, mentions []string) error { if len(mentions) == 0 { return nil } @@ -1513,7 +1513,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error { } users := make([]*User, 0, len(mentions)) - if err := e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil { + if err := ctx.e.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil { return fmt.Errorf("find mentioned users: %v", err) } @@ -1525,7 +1525,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error { } memberIDs := make([]int64, 0, user.NumMembers) - orgUsers, err := getOrgUsersByOrgID(e, user.ID) + orgUsers, err := getOrgUsersByOrgID(ctx.e, user.ID) if err != nil { return fmt.Errorf("GetOrgUsersByOrgID [%d]: %v", user.ID, err) } @@ -1537,7 +1537,7 @@ func UpdateIssueMentions(e Engine, issueID int64, mentions []string) error { ids = append(ids, memberIDs...) } - if err := UpdateIssueUsersByMentions(e, issueID, ids); err != nil { + if err := UpdateIssueUsersByMentions(ctx, issueID, ids); err != nil { return fmt.Errorf("UpdateIssueUsersByMentions: %v", err) } diff --git a/models/issue_comment.go b/models/issue_comment.go index 0bb313c30b..6786032f41 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -395,40 +394,6 @@ func (c *Comment) LoadDepIssueDetails() (err error) { return err } -// MailParticipants sends new comment emails to repository watchers -// and mentioned people. -func (c *Comment) MailParticipants(opType ActionType, issue *Issue) (err error) { - return c.mailParticipants(x, opType, issue) -} - -func (c *Comment) mailParticipants(e Engine, opType ActionType, issue *Issue) (err error) { - mentions := markup.FindAllMentions(c.Content) - if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil { - return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err) - } - - if len(c.Content) > 0 { - if err = mailIssueCommentToParticipants(e, issue, c.Poster, c.Content, c, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - } - - switch opType { - case ActionCloseIssue: - ct := fmt.Sprintf("Closed #%d.", issue.Index) - if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - case ActionReopenIssue: - ct := fmt.Sprintf("Reopened #%d.", issue.Index) - if err = mailIssueCommentToParticipants(e, issue, c.Poster, ct, c, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - } - - return nil -} - func (c *Comment) loadReactions(e Engine) (err error) { if c.Reactions != nil { return nil diff --git a/models/issue_mail.go b/models/issue_mail.go deleted file mode 100644 index 87d991e500..0000000000 --- a/models/issue_mail.go +++ /dev/null @@ -1,159 +0,0 @@ -// Copyright 2016 The Gogs Authors. All rights reserved. -// Copyright 2018 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "fmt" - - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/setting" - - "github.com/unknwon/com" -) - -func (issue *Issue) mailSubject() string { - return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index) -} - -// mailIssueCommentToParticipants can be used for both new issue creation and comment. -// This function sends two list of emails: -// 1. Repository watchers and users who are participated in comments. -// 2. Users who are not in 1. but get mentioned in current issue/comment. -func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error { - if !setting.Service.EnableNotifyMail { - return nil - } - - watchers, err := getWatchers(e, issue.RepoID) - if err != nil { - return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err) - } - participants, err := getParticipantsByIssueID(e, issue.ID) - if err != nil { - return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err) - } - - // In case the issue poster is not watching the repository and is active, - // even if we have duplicated in watchers, can be safely filtered out. - err = issue.loadPoster(e) - if err != nil { - return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err) - } - if issue.PosterID != doer.ID && issue.Poster.IsActive && !issue.Poster.ProhibitLogin { - participants = append(participants, issue.Poster) - } - - // Assignees must receive any communications - assignees, err := getAssigneesByIssue(e, issue) - if err != nil { - return err - } - - for _, assignee := range assignees { - if assignee.ID != doer.ID { - participants = append(participants, assignee) - } - } - - tos := make([]string, 0, len(watchers)) // List of email addresses. - names := make([]string, 0, len(watchers)) - for i := range watchers { - if watchers[i].UserID == doer.ID { - continue - } - - to, err := getUserByID(e, watchers[i].UserID) - if err != nil { - return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err) - } - if to.IsOrganization() || to.EmailNotifications() != EmailNotificationsEnabled { - continue - } - - tos = append(tos, to.Email) - names = append(names, to.Name) - } - for i := range participants { - if participants[i].ID == doer.ID || - com.IsSliceContainsStr(names, participants[i].Name) || - participants[i].EmailNotifications() != EmailNotificationsEnabled { - continue - } - - tos = append(tos, participants[i].Email) - names = append(names, participants[i].Name) - } - - if err := issue.loadRepo(e); err != nil { - return err - } - - for _, to := range tos { - SendIssueCommentMail(issue, doer, content, comment, []string{to}) - } - - // Mail mentioned people and exclude watchers. - names = append(names, doer.Name) - tos = make([]string, 0, len(mentions)) // list of user names. - for i := range mentions { - if com.IsSliceContainsStr(names, mentions[i]) { - continue - } - - tos = append(tos, mentions[i]) - } - - emails := getUserEmailsByNames(e, tos) - - for _, to := range emails { - SendIssueMentionMail(issue, doer, content, comment, []string{to}) - } - - return nil -} - -// MailParticipants sends new issue thread created emails to repository watchers -// and mentioned people. -func (issue *Issue) MailParticipants(doer *User, opType ActionType) (err error) { - return issue.mailParticipants(x, doer, opType) -} - -func (issue *Issue) mailParticipants(e Engine, doer *User, opType ActionType) (err error) { - mentions := markup.FindAllMentions(issue.Content) - - if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil { - return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err) - } - - if len(issue.Content) > 0 { - if err = mailIssueCommentToParticipants(e, issue, doer, issue.Content, nil, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - } - - switch opType { - case ActionCreateIssue, ActionCreatePullRequest: - if len(issue.Content) == 0 { - ct := fmt.Sprintf("Created #%d.", issue.Index) - if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - } - case ActionCloseIssue, ActionClosePullRequest: - ct := fmt.Sprintf("Closed #%d.", issue.Index) - if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - case ActionReopenIssue, ActionReopenPullRequest: - ct := fmt.Sprintf("Reopened #%d.", issue.Index) - if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil { - log.Error("mailIssueCommentToParticipants: %v", err) - } - } - - return nil -} diff --git a/models/issue_user.go b/models/issue_user.go index 58eb5117f8..d55a0dc2fb 100644 --- a/models/issue_user.go +++ b/models/issue_user.go @@ -94,22 +94,22 @@ func UpdateIssueUserByRead(uid, issueID int64) error { } // UpdateIssueUsersByMentions updates issue-user pairs by mentioning. -func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error { +func UpdateIssueUsersByMentions(ctx DBContext, issueID int64, uids []int64) error { for _, uid := range uids { iu := &IssueUser{ UID: uid, IssueID: issueID, } - has, err := e.Get(iu) + has, err := ctx.e.Get(iu) if err != nil { return err } iu.IsMentioned = true if has { - _, err = e.ID(iu.ID).Cols("is_mentioned").Update(iu) + _, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu) } else { - _, err = e.Insert(iu) + _, err = ctx.e.Insert(iu) } if err != nil { return err diff --git a/models/issue_user_test.go b/models/issue_user_test.go index a333bc8619..a57ab33f9e 100644 --- a/models/issue_user_test.go +++ b/models/issue_user_test.go @@ -50,7 +50,7 @@ func TestUpdateIssueUsersByMentions(t *testing.T) { issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) uids := []int64{2, 5} - assert.NoError(t, UpdateIssueUsersByMentions(x, issue.ID, uids)) + assert.NoError(t, UpdateIssueUsersByMentions(DefaultDBContext(), issue.ID, uids)) for _, uid := range uids { AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") } diff --git a/models/mail.go b/models/mail.go deleted file mode 100644 index 2c72818cec..0000000000 --- a/models/mail.go +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright 2016 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "bytes" - "fmt" - "html/template" - "path" - - "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/mailer" - "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/markup/markdown" - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/timeutil" - - "gopkg.in/gomail.v2" -) - -const ( - mailAuthActivate base.TplName = "auth/activate" - mailAuthActivateEmail base.TplName = "auth/activate_email" - mailAuthResetPassword base.TplName = "auth/reset_passwd" - mailAuthRegisterNotify base.TplName = "auth/register_notify" - - mailIssueComment base.TplName = "issue/comment" - mailIssueMention base.TplName = "issue/mention" - - mailNotifyCollaborator base.TplName = "notify/collaborator" -) - -var templates *template.Template - -// InitMailRender initializes the mail renderer -func InitMailRender(tmpls *template.Template) { - templates = tmpls -} - -// SendTestMail sends a test mail -func SendTestMail(email string) error { - return gomail.Send(mailer.Sender, mailer.NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message) -} - -// SendUserMail sends a mail to the user -func SendUserMail(language string, u *User, tpl base.TplName, code, subject, info string) { - data := map[string]interface{}{ - "DisplayName": u.DisplayName(), - "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language), - "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language), - "Code": code, - } - - var content bytes.Buffer - - if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil { - log.Error("Template: %v", err) - return - } - - msg := mailer.NewMessage([]string{u.Email}, subject, content.String()) - msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info) - - mailer.SendAsync(msg) -} - -// Locale represents an interface to translation -type Locale interface { - Language() string - Tr(string, ...interface{}) string -} - -// SendActivateAccountMail sends an activation mail to the user (new user registration) -func SendActivateAccountMail(locale Locale, u *User) { - SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account") -} - -// SendResetPasswordMail sends a password reset mail to the user -func SendResetPasswordMail(locale Locale, u *User) { - SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account") -} - -// SendActivateEmailMail sends confirmation email to confirm new email address -func SendActivateEmailMail(locale Locale, u *User, email *EmailAddress) { - data := map[string]interface{}{ - "DisplayName": u.DisplayName(), - "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()), - "Code": u.GenerateEmailActivateCode(email.Email), - "Email": email.Email, - } - - var content bytes.Buffer - - if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil { - log.Error("Template: %v", err) - return - } - - msg := mailer.NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String()) - msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID) - - mailer.SendAsync(msg) -} - -// SendRegisterNotifyMail triggers a notify e-mail by admin created a account. -func SendRegisterNotifyMail(locale Locale, u *User) { - data := map[string]interface{}{ - "DisplayName": u.DisplayName(), - "Username": u.Name, - } - - var content bytes.Buffer - - if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil { - log.Error("Template: %v", err) - return - } - - msg := mailer.NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String()) - msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID) - - mailer.SendAsync(msg) -} - -// SendCollaboratorMail sends mail notification to new collaborator. -func SendCollaboratorMail(u, doer *User, repo *Repository) { - repoName := path.Join(repo.Owner.Name, repo.Name) - subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName) - - data := map[string]interface{}{ - "Subject": subject, - "RepoName": repoName, - "Link": repo.HTMLURL(), - } - - var content bytes.Buffer - - if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil { - log.Error("Template: %v", err) - return - } - - msg := mailer.NewMessage([]string{u.Email}, subject, content.String()) - msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID) - - mailer.SendAsync(msg) -} - -func composeTplData(subject, body, link string) map[string]interface{} { - data := make(map[string]interface{}, 10) - data["Subject"] = subject - data["Body"] = body - data["Link"] = link - return data -} - -func composeIssueCommentMessage(issue *Issue, doer *User, content string, comment *Comment, tplName base.TplName, tos []string, info string) *mailer.Message { - var subject string - if comment != nil { - subject = "Re: " + issue.mailSubject() - } else { - subject = issue.mailSubject() - } - - err := issue.LoadRepo() - if err != nil { - log.Error("LoadRepo: %v", err) - } - body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas())) - - var data = make(map[string]interface{}, 10) - if comment != nil { - data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag()) - } else { - data = composeTplData(subject, body, issue.HTMLURL()) - } - data["Doer"] = doer - - var mailBody bytes.Buffer - - if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil { - log.Error("Template: %v", err) - } - - msg := mailer.NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String()) - msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info) - - // Set Message-ID on first message so replies know what to reference - if comment == nil { - msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">") - } else { - msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">") - msg.SetHeader("References", "<"+issue.ReplyReference()+">") - } - - return msg -} - -// SendIssueCommentMail composes and sends issue comment emails to target receivers. -func SendIssueCommentMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) { - if len(tos) == 0 { - return - } - - mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment")) -} - -// SendIssueMentionMail composes and sends issue mention emails to target receivers. -func SendIssueMentionMail(issue *Issue, doer *User, content string, comment *Comment, tos []string) { - if len(tos) == 0 { - return - } - mailer.SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention")) -} diff --git a/models/mail_test.go b/models/mail_test.go deleted file mode 100644 index 51c52427f0..0000000000 --- a/models/mail_test.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2019 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package models - -import ( - "html/template" - "testing" - - "code.gitea.io/gitea/modules/setting" - - "github.com/stretchr/testify/assert" -) - -const tmpl = ` - - - - - {{.Subject}} - - - -

{{.Body}}

-

- --- -
- View it on Gitea. -

- - -` - -func TestComposeIssueCommentMessage(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - var MailService setting.Mailer - - MailService.From = "test@gitea.com" - setting.MailService = &MailService - - doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository) - issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue) - comment := AssertExistsAndLoadBean(t, &Comment{ID: 2, Issue: issue}).(*Comment) - - email := template.Must(template.New("issue/comment").Parse(tmpl)) - InitMailRender(email) - - tos := []string{"test@gitea.com", "test2@gitea.com"} - msg := composeIssueCommentMessage(issue, doer, "test body", comment, mailIssueComment, tos, "issue comment") - - subject := msg.GetHeader("Subject") - inreplyTo := msg.GetHeader("In-Reply-To") - references := msg.GetHeader("References") - - assert.Equal(t, subject[0], "Re: "+issue.mailSubject(), "Comment reply subject should contain Re:") - assert.Equal(t, inreplyTo[0], "", "In-Reply-To header doesn't match") - assert.Equal(t, references[0], "", "References header doesn't match") - -} - -func TestComposeIssueMessage(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - var MailService setting.Mailer - - MailService.From = "test@gitea.com" - setting.MailService = &MailService - - doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, Owner: doer}).(*Repository) - issue := AssertExistsAndLoadBean(t, &Issue{ID: 1, Repo: repo, Poster: doer}).(*Issue) - - email := template.Must(template.New("issue/comment").Parse(tmpl)) - InitMailRender(email) - - tos := []string{"test@gitea.com", "test2@gitea.com"} - msg := composeIssueCommentMessage(issue, doer, "test body", nil, mailIssueComment, tos, "issue create") - - subject := msg.GetHeader("Subject") - messageID := msg.GetHeader("Message-ID") - - assert.Equal(t, subject[0], issue.mailSubject(), "Subject not equal to issue.mailSubject()") - assert.Nil(t, msg.GetHeader("In-Reply-To")) - assert.Nil(t, msg.GetHeader("References")) - assert.Equal(t, messageID[0], "", "Message-ID header doesn't match") -} diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go deleted file mode 100644 index d19ae7b2f4..0000000000 --- a/modules/mailer/mailer.go +++ /dev/null @@ -1,303 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Copyright 2017 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package mailer - -import ( - "bytes" - "crypto/tls" - "fmt" - "io" - "net" - "net/smtp" - "os" - "os/exec" - "strings" - "time" - - "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" - - "github.com/jaytaylor/html2text" - "gopkg.in/gomail.v2" -) - -// Message mail body and log info -type Message struct { - Info string // Message information for log purpose. - *gomail.Message -} - -// NewMessageFrom creates new mail message object with custom From header. -func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message { - log.Trace("NewMessageFrom (body):\n%s", body) - - msg := gomail.NewMessage() - msg.SetAddressHeader("From", fromAddress, fromDisplayName) - msg.SetHeader("To", to...) - if len(setting.MailService.SubjectPrefix) > 0 { - msg.SetHeader("Subject", setting.MailService.SubjectPrefix+" "+subject) - } else { - msg.SetHeader("Subject", subject) - } - msg.SetDateHeader("Date", time.Now()) - msg.SetHeader("X-Auto-Response-Suppress", "All") - - plainBody, err := html2text.FromString(body) - if err != nil || setting.MailService.SendAsPlainText { - if strings.Contains(base.TruncateString(body, 100), "") { - log.Warn("Mail contains HTML but configured to send as plain text.") - } - msg.SetBody("text/plain", plainBody) - } else { - msg.SetBody("text/plain", plainBody) - msg.AddAlternative("text/html", body) - } - - return &Message{ - Message: msg, - } -} - -// NewMessage creates new mail message object with default From header. -func NewMessage(to []string, subject, body string) *Message { - return NewMessageFrom(to, setting.MailService.FromName, setting.MailService.FromEmail, subject, body) -} - -type loginAuth struct { - username, password string -} - -// LoginAuth SMTP AUTH LOGIN Auth Handler -func LoginAuth(username, password string) smtp.Auth { - return &loginAuth{username, password} -} - -// Start start SMTP login auth -func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { - return "LOGIN", []byte{}, nil -} - -// Next next step of SMTP login auth -func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { - if more { - switch string(fromServer) { - case "Username:": - return []byte(a.username), nil - case "Password:": - return []byte(a.password), nil - default: - return nil, fmt.Errorf("unknown fromServer: %s", string(fromServer)) - } - } - return nil, nil -} - -// Sender SMTP mail sender -type smtpSender struct { -} - -// Send send email -func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error { - opts := setting.MailService - - host, port, err := net.SplitHostPort(opts.Host) - if err != nil { - return err - } - - tlsconfig := &tls.Config{ - InsecureSkipVerify: opts.SkipVerify, - ServerName: host, - } - - if opts.UseCertificate { - cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile) - if err != nil { - return err - } - tlsconfig.Certificates = []tls.Certificate{cert} - } - - conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) - if err != nil { - return err - } - defer conn.Close() - - isSecureConn := opts.IsTLSEnabled || (strings.HasSuffix(port, "465")) - // Start TLS directly if the port ends with 465 (SMTPS protocol) - if isSecureConn { - conn = tls.Client(conn, tlsconfig) - } - - client, err := smtp.NewClient(conn, host) - if err != nil { - return fmt.Errorf("NewClient: %v", err) - } - - if !opts.DisableHelo { - hostname := opts.HeloHostname - if len(hostname) == 0 { - hostname, err = os.Hostname() - if err != nil { - return err - } - } - - if err = client.Hello(hostname); err != nil { - return fmt.Errorf("Hello: %v", err) - } - } - - // If not using SMTPS, always use STARTTLS if available - hasStartTLS, _ := client.Extension("STARTTLS") - if !isSecureConn && hasStartTLS { - if err = client.StartTLS(tlsconfig); err != nil { - return fmt.Errorf("StartTLS: %v", err) - } - } - - canAuth, options := client.Extension("AUTH") - if canAuth && len(opts.User) > 0 { - var auth smtp.Auth - - if strings.Contains(options, "CRAM-MD5") { - auth = smtp.CRAMMD5Auth(opts.User, opts.Passwd) - } else if strings.Contains(options, "PLAIN") { - auth = smtp.PlainAuth("", opts.User, opts.Passwd, host) - } else if strings.Contains(options, "LOGIN") { - // Patch for AUTH LOGIN - auth = LoginAuth(opts.User, opts.Passwd) - } - - if auth != nil { - if err = client.Auth(auth); err != nil { - return fmt.Errorf("Auth: %v", err) - } - } - } - - if err = client.Mail(from); err != nil { - return fmt.Errorf("Mail: %v", err) - } - - for _, rec := range to { - if err = client.Rcpt(rec); err != nil { - return fmt.Errorf("Rcpt: %v", err) - } - } - - w, err := client.Data() - if err != nil { - return fmt.Errorf("Data: %v", err) - } else if _, err = msg.WriteTo(w); err != nil { - return fmt.Errorf("WriteTo: %v", err) - } else if err = w.Close(); err != nil { - return fmt.Errorf("Close: %v", err) - } - - return client.Quit() -} - -// Sender sendmail mail sender -type sendmailSender struct { -} - -// Send send email -func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error { - var err error - var closeError error - var waitError error - - args := []string{"-F", from, "-i"} - args = append(args, setting.MailService.SendmailArgs...) - args = append(args, to...) - log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args) - cmd := exec.Command(setting.MailService.SendmailPath, args...) - pipe, err := cmd.StdinPipe() - - if err != nil { - return err - } - - if err = cmd.Start(); err != nil { - return err - } - - _, err = msg.WriteTo(pipe) - - // we MUST close the pipe or sendmail will hang waiting for more of the message - // Also we should wait on our sendmail command even if something fails - closeError = pipe.Close() - waitError = cmd.Wait() - if err != nil { - return err - } else if closeError != nil { - return closeError - } else { - return waitError - } -} - -// Sender sendmail mail sender -type dummySender struct { -} - -// Send send email -func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error { - buf := bytes.Buffer{} - if _, err := msg.WriteTo(&buf); err != nil { - return err - } - log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String()) - return nil -} - -func processMailQueue() { - for msg := range mailQueue { - log.Trace("New e-mail sending request %s: %s", msg.GetHeader("To"), msg.Info) - if err := gomail.Send(Sender, msg.Message); err != nil { - log.Error("Failed to send emails %s: %s - %v", msg.GetHeader("To"), msg.Info, err) - } else { - log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info) - } - } -} - -var mailQueue chan *Message - -// Sender sender for sending mail synchronously -var Sender gomail.Sender - -// NewContext start mail queue service -func NewContext() { - // Need to check if mailQueue is nil because in during reinstall (user had installed - // before but swithed install lock off), this function will be called again - // while mail queue is already processing tasks, and produces a race condition. - if setting.MailService == nil || mailQueue != nil { - return - } - - switch setting.MailService.MailerType { - case "smtp": - Sender = &smtpSender{} - case "sendmail": - Sender = &sendmailSender{} - case "dummy": - Sender = &dummySender{} - } - - mailQueue = make(chan *Message, setting.MailService.QueueLength) - go processMailQueue() -} - -// SendAsync send mail asynchronous -func SendAsync(msg *Message) { - go func() { - mailQueue <- msg - }() -} diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go index 9d0db4f415..e1ae391f78 100644 --- a/modules/notification/mail/mail.go +++ b/modules/notification/mail/mail.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" + "code.gitea.io/gitea/services/mailer" ) type mailNotifier struct { @@ -36,13 +37,13 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models. act = models.ActionCommentIssue } - if err := comment.MailParticipants(act, issue); err != nil { + if err := mailer.MailParticipantsComment(comment, act, issue); err != nil { log.Error("MailParticipants: %v", err) } } func (m *mailNotifier) NotifyNewIssue(issue *models.Issue) { - if err := issue.MailParticipants(issue.Poster, models.ActionCreateIssue); err != nil { + if err := mailer.MailParticipants(issue, issue.Poster, models.ActionCreateIssue); err != nil { log.Error("MailParticipants: %v", err) } } @@ -63,13 +64,13 @@ func (m *mailNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models. } } - if err := issue.MailParticipants(doer, actionType); err != nil { + if err := mailer.MailParticipants(issue, doer, actionType); err != nil { log.Error("MailParticipants: %v", err) } } func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest) { - if err := pr.Issue.MailParticipants(pr.Issue.Poster, models.ActionCreatePullRequest); err != nil { + if err := mailer.MailParticipants(pr.Issue, pr.Issue.Poster, models.ActionCreatePullRequest); err != nil { log.Error("MailParticipants: %v", err) } } @@ -83,7 +84,7 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models } else if comment.Type == models.CommentTypeComment { act = models.ActionCommentIssue } - if err := comment.MailParticipants(act, pr.Issue); err != nil { + if err := mailer.MailParticipantsComment(comment, act, pr.Issue); err != nil { log.Error("MailParticipants: %v", err) } } diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 496aca375f..538d01f9a4 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/services/mailer" "gitea.com/macaron/macaron" "github.com/unknwon/com" @@ -197,7 +198,7 @@ func Dashboard(ctx *context.Context) { func SendTestMail(ctx *context.Context) { email := ctx.Query("email") // Send a test email to the user's email address and redirect back to Config - if err := models.SendTestMail(email); err != nil { + if err := mailer.SendTestMail(email); err != nil { ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err)) } else { ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email)) diff --git a/routers/admin/users.go b/routers/admin/users.go index aa595edad9..660f116682 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers" + "code.gitea.io/gitea/services/mailer" "github.com/unknwon/com" ) @@ -116,8 +117,8 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) { log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) // Send email notification. - if form.SendNotify && setting.MailService != nil { - models.SendRegisterNotifyMail(ctx.Context, u) + if form.SendNotify { + mailer.SendRegisterNotifyMail(ctx.Locale, u) } ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name)) diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index f4b694aa22..70076b626b 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -9,10 +9,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/convert" "code.gitea.io/gitea/routers/api/v1/user" + "code.gitea.io/gitea/services/mailer" ) func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) { @@ -88,8 +88,8 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) // Send email notification. - if form.SendNotify && setting.MailService != nil { - models.SendRegisterNotifyMail(ctx.Context.Context, u) + if form.SendNotify { + mailer.SendRegisterNotifyMail(ctx.Locale, u) } ctx.JSON(201, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) } diff --git a/routers/init.go b/routers/init.go index fdf90904ce..5c8741f244 100644 --- a/routers/init.go +++ b/routers/init.go @@ -16,11 +16,11 @@ import ( "code.gitea.io/gitea/modules/highlight" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/mailer" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/external" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/ssh" + "code.gitea.io/gitea/services/mailer" "gitea.com/macaron/macaron" ) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 91db519d62..b0af396e7d 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/routers/utils" + "code.gitea.io/gitea/services/mailer" "github.com/unknwon/com" "mvdan.cc/xurls/v2" @@ -549,7 +550,7 @@ func CollaborationPost(ctx *context.Context) { } if setting.Service.EnableNotifyMail { - models.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository) + mailer.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository) } ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success")) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 93ce220b00..e18ac0923e 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -34,6 +34,7 @@ import ( "code.gitea.io/gitea/routers/repo" "code.gitea.io/gitea/routers/user" userSetting "code.gitea.io/gitea/routers/user/setting" + "code.gitea.io/gitea/services/mailer" // to registers all internal adapters _ "code.gitea.io/gitea/modules/session" @@ -166,7 +167,7 @@ func NewMacaron() *macaron.Macaron { )) m.Use(templates.HTMLRenderer()) - models.InitMailRender(templates.Mailer()) + mailer.InitMailRender(templates.Mailer()) localeNames, err := options.Dir("locale") diff --git a/routers/user/auth.go b/routers/user/auth.go index dedb9c7b0d..3def867f64 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/services/mailer" "gitea.com/macaron/captcha" "github.com/markbates/goth" @@ -948,7 +949,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au // Send confirmation email if setting.Service.RegisterEmailConfirm && u.ID > 1 { - models.SendActivateAccountMail(ctx.Context, u) + mailer.SendActivateAccountMail(ctx.Locale, u) + ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) @@ -1094,7 +1096,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo // Send confirmation email, no need for social account. if setting.Service.RegisterEmailConfirm && u.ID > 1 { - models.SendActivateAccountMail(ctx.Context, u) + mailer.SendActivateAccountMail(ctx.Locale, u) + ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) @@ -1125,7 +1128,7 @@ func Activate(ctx *context.Context) { ctx.Data["ResendLimited"] = true } else { ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) - models.SendActivateAccountMail(ctx.Context, ctx.User) + mailer.SendActivateAccountMail(ctx.Locale, ctx.User) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error("Set cache(MailResendLimit) fail: %v", err) @@ -1247,7 +1250,8 @@ func ForgotPasswdPost(ctx *context.Context) { return } - models.SendResetPasswordMail(ctx.Context, u) + mailer.SendResetPasswordMail(ctx.Locale, u) + if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { log.Error("Set cache(MailResendLimit) fail: %v", err) } diff --git a/routers/user/auth_openid.go b/routers/user/auth_openid.go index 832766b16c..ccaea8264f 100644 --- a/routers/user/auth_openid.go +++ b/routers/user/auth_openid.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/recaptcha" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/services/mailer" "gitea.com/macaron/captcha" ) @@ -444,7 +445,8 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si // Send confirmation email, no need for social account. if setting.Service.RegisterEmailConfirm && u.ID > 1 { - models.SendActivateAccountMail(ctx.Context, u) + mailer.SendActivateAccountMail(ctx.Locale, u) + ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email ctx.Data["ActiveCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language()) diff --git a/routers/user/setting/account.go b/routers/user/setting/account.go index 674606cce8..71d98fd3b9 100644 --- a/routers/user/setting/account.go +++ b/routers/user/setting/account.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/services/mailer" ) const ( @@ -130,7 +131,7 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) { // Send confirmation email if setting.Service.RegisterEmailConfirm { - models.SendActivateEmailMail(ctx.Context, ctx.User, email) + mailer.SendActivateEmailMail(ctx.Locale, ctx.User, email) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error("Set cache(MailResendLimit) fail: %v", err) diff --git a/services/mailer/mail.go b/services/mailer/mail.go new file mode 100644 index 0000000000..3f0a789dc4 --- /dev/null +++ b/services/mailer/mail.go @@ -0,0 +1,222 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "bytes" + "fmt" + "html/template" + "path" + + "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/timeutil" + + "gopkg.in/gomail.v2" +) + +const ( + mailAuthActivate base.TplName = "auth/activate" + mailAuthActivateEmail base.TplName = "auth/activate_email" + mailAuthResetPassword base.TplName = "auth/reset_passwd" + mailAuthRegisterNotify base.TplName = "auth/register_notify" + + mailIssueComment base.TplName = "issue/comment" + mailIssueMention base.TplName = "issue/mention" + + mailNotifyCollaborator base.TplName = "notify/collaborator" +) + +var templates *template.Template + +// InitMailRender initializes the mail renderer +func InitMailRender(tmpls *template.Template) { + templates = tmpls +} + +// SendTestMail sends a test mail +func SendTestMail(email string) error { + return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").Message) +} + +// SendUserMail sends a mail to the user +func SendUserMail(language string, u *models.User, tpl base.TplName, code, subject, info string) { + data := map[string]interface{}{ + "DisplayName": u.DisplayName(), + "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, language), + "ResetPwdCodeLives": timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, language), + "Code": code, + } + + var content bytes.Buffer + + if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil { + log.Error("Template: %v", err) + return + } + + msg := NewMessage([]string{u.Email}, subject, content.String()) + msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info) + + SendAsync(msg) +} + +// Locale represents an interface to translation +type Locale interface { + Language() string + Tr(string, ...interface{}) string +} + +// SendActivateAccountMail sends an activation mail to the user (new user registration) +func SendActivateAccountMail(locale Locale, u *models.User) { + SendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateActivateCode(), locale.Tr("mail.activate_account"), "activate account") +} + +// SendResetPasswordMail sends a password reset mail to the user +func SendResetPasswordMail(locale Locale, u *models.User) { + SendUserMail(locale.Language(), u, mailAuthResetPassword, u.GenerateActivateCode(), locale.Tr("mail.reset_password"), "recover account") +} + +// SendActivateEmailMail sends confirmation email to confirm new email address +func SendActivateEmailMail(locale Locale, u *models.User, email *models.EmailAddress) { + data := map[string]interface{}{ + "DisplayName": u.DisplayName(), + "ActiveCodeLives": timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, locale.Language()), + "Code": u.GenerateEmailActivateCode(email.Email), + "Email": email.Email, + } + + var content bytes.Buffer + + if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil { + log.Error("Template: %v", err) + return + } + + msg := NewMessage([]string{email.Email}, locale.Tr("mail.activate_email"), content.String()) + msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID) + + SendAsync(msg) +} + +// SendRegisterNotifyMail triggers a notify e-mail by admin created a account. +func SendRegisterNotifyMail(locale Locale, u *models.User) { + if setting.MailService == nil { + log.Warn("SendRegisterNotifyMail is being invoked but mail service hasn't been initialized") + return + } + + data := map[string]interface{}{ + "DisplayName": u.DisplayName(), + "Username": u.Name, + } + + var content bytes.Buffer + + if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil { + log.Error("Template: %v", err) + return + } + + msg := NewMessage([]string{u.Email}, locale.Tr("mail.register_notify"), content.String()) + msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID) + + SendAsync(msg) +} + +// SendCollaboratorMail sends mail notification to new collaborator. +func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) { + repoName := path.Join(repo.Owner.Name, repo.Name) + subject := fmt.Sprintf("%s added you to %s", doer.DisplayName(), repoName) + + data := map[string]interface{}{ + "Subject": subject, + "RepoName": repoName, + "Link": repo.HTMLURL(), + } + + var content bytes.Buffer + + if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil { + log.Error("Template: %v", err) + return + } + + msg := NewMessage([]string{u.Email}, subject, content.String()) + msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID) + + SendAsync(msg) +} + +func composeTplData(subject, body, link string) map[string]interface{} { + data := make(map[string]interface{}, 10) + data["Subject"] = subject + data["Body"] = body + data["Link"] = link + return data +} + +func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tplName base.TplName, tos []string, info string) *Message { + var subject string + if comment != nil { + subject = "Re: " + mailSubject(issue) + } else { + subject = mailSubject(issue) + } + err := issue.LoadRepo() + if err != nil { + log.Error("LoadRepo: %v", err) + } + body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas())) + + var data = make(map[string]interface{}, 10) + if comment != nil { + data = composeTplData(subject, body, issue.HTMLURL()+"#"+comment.HashTag()) + } else { + data = composeTplData(subject, body, issue.HTMLURL()) + } + data["Doer"] = doer + + var mailBody bytes.Buffer + + if err := templates.ExecuteTemplate(&mailBody, string(tplName), data); err != nil { + log.Error("Template: %v", err) + } + + msg := NewMessageFrom(tos, doer.DisplayName(), setting.MailService.FromEmail, subject, mailBody.String()) + msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info) + + // Set Message-ID on first message so replies know what to reference + if comment == nil { + msg.SetHeader("Message-ID", "<"+issue.ReplyReference()+">") + } else { + msg.SetHeader("In-Reply-To", "<"+issue.ReplyReference()+">") + msg.SetHeader("References", "<"+issue.ReplyReference()+">") + } + + return msg +} + +// SendIssueCommentMail composes and sends issue comment emails to target receivers. +func SendIssueCommentMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) { + if len(tos) == 0 { + return + } + + SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueComment, tos, "issue comment")) +} + +// SendIssueMentionMail composes and sends issue mention emails to target receivers. +func SendIssueMentionMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, tos []string) { + if len(tos) == 0 { + return + } + SendAsync(composeIssueCommentMessage(issue, doer, content, comment, mailIssueMention, tos, "issue mention")) +} diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go new file mode 100644 index 0000000000..cb477f887b --- /dev/null +++ b/services/mailer/mail_comment.go @@ -0,0 +1,47 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" +) + +// MailParticipantsComment sends new comment emails to repository watchers +// and mentioned people. +func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue) error { + return mailParticipantsComment(models.DefaultDBContext(), c, opType, issue) +} + +func mailParticipantsComment(ctx models.DBContext, c *models.Comment, opType models.ActionType, issue *models.Issue) (err error) { + mentions := markup.FindAllMentions(c.Content) + if err = models.UpdateIssueMentions(ctx, c.IssueID, mentions); err != nil { + return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err) + } + + if len(c.Content) > 0 { + if err = mailIssueCommentToParticipants(issue, c.Poster, c.Content, c, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + } + + switch opType { + case models.ActionCloseIssue: + ct := fmt.Sprintf("Closed #%d.", issue.Index) + if err = mailIssueCommentToParticipants(issue, c.Poster, ct, c, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + case models.ActionReopenIssue: + ct := fmt.Sprintf("Reopened #%d.", issue.Index) + if err = mailIssueCommentToParticipants(issue, c.Poster, ct, c, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + } + + return nil +} diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go new file mode 100644 index 0000000000..92d2c5a879 --- /dev/null +++ b/services/mailer/mail_issue.go @@ -0,0 +1,159 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/modules/setting" + + "github.com/unknwon/com" +) + +func mailSubject(issue *models.Issue) string { + return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index) +} + +// mailIssueCommentToParticipants can be used for both new issue creation and comment. +// This function sends two list of emails: +// 1. Repository watchers and users who are participated in comments. +// 2. Users who are not in 1. but get mentioned in current issue/comment. +func mailIssueCommentToParticipants(issue *models.Issue, doer *models.User, content string, comment *models.Comment, mentions []string) error { + if !setting.Service.EnableNotifyMail { + return nil + } + + watchers, err := models.GetWatchers(issue.RepoID) + if err != nil { + return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err) + } + participants, err := models.GetParticipantsByIssueID(issue.ID) + if err != nil { + return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err) + } + + // In case the issue poster is not watching the repository and is active, + // even if we have duplicated in watchers, can be safely filtered out. + err = issue.LoadPoster() + if err != nil { + return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err) + } + if issue.PosterID != doer.ID && issue.Poster.IsActive && !issue.Poster.ProhibitLogin { + participants = append(participants, issue.Poster) + } + + // Assignees must receive any communications + assignees, err := models.GetAssigneesByIssue(issue) + if err != nil { + return err + } + + for _, assignee := range assignees { + if assignee.ID != doer.ID { + participants = append(participants, assignee) + } + } + + tos := make([]string, 0, len(watchers)) // List of email addresses. + names := make([]string, 0, len(watchers)) + for i := range watchers { + if watchers[i].UserID == doer.ID { + continue + } + + to, err := models.GetUserByID(watchers[i].UserID) + if err != nil { + return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err) + } + if to.IsOrganization() || to.EmailNotifications() != models.EmailNotificationsEnabled { + continue + } + + tos = append(tos, to.Email) + names = append(names, to.Name) + } + for i := range participants { + if participants[i].ID == doer.ID || + com.IsSliceContainsStr(names, participants[i].Name) || + participants[i].EmailNotifications() != models.EmailNotificationsEnabled { + continue + } + + tos = append(tos, participants[i].Email) + names = append(names, participants[i].Name) + } + + if err := issue.LoadRepo(); err != nil { + return err + } + + for _, to := range tos { + SendIssueCommentMail(issue, doer, content, comment, []string{to}) + } + + // Mail mentioned people and exclude watchers. + names = append(names, doer.Name) + tos = make([]string, 0, len(mentions)) // list of user names. + for i := range mentions { + if com.IsSliceContainsStr(names, mentions[i]) { + continue + } + + tos = append(tos, mentions[i]) + } + + emails := models.GetUserEmailsByNames(tos) + + for _, to := range emails { + SendIssueMentionMail(issue, doer, content, comment, []string{to}) + } + + return nil +} + +// MailParticipants sends new issue thread created emails to repository watchers +// and mentioned people. +func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType) error { + return mailParticipants(models.DefaultDBContext(), issue, doer, opType) +} + +func mailParticipants(ctx models.DBContext, issue *models.Issue, doer *models.User, opType models.ActionType) (err error) { + mentions := markup.FindAllMentions(issue.Content) + + if err = models.UpdateIssueMentions(ctx, issue.ID, mentions); err != nil { + return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err) + } + + if len(issue.Content) > 0 { + if err = mailIssueCommentToParticipants(issue, doer, issue.Content, nil, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + } + + switch opType { + case models.ActionCreateIssue, models.ActionCreatePullRequest: + if len(issue.Content) == 0 { + ct := fmt.Sprintf("Created #%d.", issue.Index) + if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + } + case models.ActionCloseIssue, models.ActionClosePullRequest: + ct := fmt.Sprintf("Closed #%d.", issue.Index) + if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + case models.ActionReopenIssue, models.ActionReopenPullRequest: + ct := fmt.Sprintf("Reopened #%d.", issue.Index) + if err = mailIssueCommentToParticipants(issue, doer, ct, nil, mentions); err != nil { + log.Error("mailIssueCommentToParticipants: %v", err) + } + } + + return nil +} diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go new file mode 100644 index 0000000000..c7a84d6b33 --- /dev/null +++ b/services/mailer/mail_test.go @@ -0,0 +1,91 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "html/template" + "testing" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/assert" +) + +const tmpl = ` + + + + + {{.Subject}} + + + +

{{.Body}}

+

+ --- +
+ View it on Gitea. +

+ + +` + +func TestComposeIssueCommentMessage(t *testing.T) { + assert.NoError(t, models.PrepareTestDatabase()) + var mailService = setting.Mailer{ + From: "test@gitea.com", + } + + setting.MailService = &mailService + setting.Domain = "localhost" + + doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) + issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) + comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment) + + email := template.Must(template.New("issue/comment").Parse(tmpl)) + InitMailRender(email) + + tos := []string{"test@gitea.com", "test2@gitea.com"} + msg := composeIssueCommentMessage(issue, doer, "test body", comment, mailIssueComment, tos, "issue comment") + + subject := msg.GetHeader("Subject") + inreplyTo := msg.GetHeader("In-Reply-To") + references := msg.GetHeader("References") + + assert.Equal(t, subject[0], "Re: "+mailSubject(issue), "Comment reply subject should contain Re:") + assert.Equal(t, inreplyTo[0], "", "In-Reply-To header doesn't match") + assert.Equal(t, references[0], "", "References header doesn't match") +} + +func TestComposeIssueMessage(t *testing.T) { + assert.NoError(t, models.PrepareTestDatabase()) + var mailService = setting.Mailer{ + From: "test@gitea.com", + } + + setting.MailService = &mailService + setting.Domain = "localhost" + + doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) + issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) + + email := template.Must(template.New("issue/comment").Parse(tmpl)) + InitMailRender(email) + + tos := []string{"test@gitea.com", "test2@gitea.com"} + msg := composeIssueCommentMessage(issue, doer, "test body", nil, mailIssueComment, tos, "issue create") + + subject := msg.GetHeader("Subject") + messageID := msg.GetHeader("Message-ID") + + assert.Equal(t, subject[0], mailSubject(issue), "Subject not equal to issue.mailSubject()") + assert.Nil(t, msg.GetHeader("In-Reply-To")) + assert.Nil(t, msg.GetHeader("References")) + assert.Equal(t, messageID[0], "", "Message-ID header doesn't match") +} diff --git a/services/mailer/mailer.go b/services/mailer/mailer.go new file mode 100644 index 0000000000..d19ae7b2f4 --- /dev/null +++ b/services/mailer/mailer.go @@ -0,0 +1,303 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "bytes" + "crypto/tls" + "fmt" + "io" + "net" + "net/smtp" + "os" + "os/exec" + "strings" + "time" + + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + + "github.com/jaytaylor/html2text" + "gopkg.in/gomail.v2" +) + +// Message mail body and log info +type Message struct { + Info string // Message information for log purpose. + *gomail.Message +} + +// NewMessageFrom creates new mail message object with custom From header. +func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message { + log.Trace("NewMessageFrom (body):\n%s", body) + + msg := gomail.NewMessage() + msg.SetAddressHeader("From", fromAddress, fromDisplayName) + msg.SetHeader("To", to...) + if len(setting.MailService.SubjectPrefix) > 0 { + msg.SetHeader("Subject", setting.MailService.SubjectPrefix+" "+subject) + } else { + msg.SetHeader("Subject", subject) + } + msg.SetDateHeader("Date", time.Now()) + msg.SetHeader("X-Auto-Response-Suppress", "All") + + plainBody, err := html2text.FromString(body) + if err != nil || setting.MailService.SendAsPlainText { + if strings.Contains(base.TruncateString(body, 100), "") { + log.Warn("Mail contains HTML but configured to send as plain text.") + } + msg.SetBody("text/plain", plainBody) + } else { + msg.SetBody("text/plain", plainBody) + msg.AddAlternative("text/html", body) + } + + return &Message{ + Message: msg, + } +} + +// NewMessage creates new mail message object with default From header. +func NewMessage(to []string, subject, body string) *Message { + return NewMessageFrom(to, setting.MailService.FromName, setting.MailService.FromEmail, subject, body) +} + +type loginAuth struct { + username, password string +} + +// LoginAuth SMTP AUTH LOGIN Auth Handler +func LoginAuth(username, password string) smtp.Auth { + return &loginAuth{username, password} +} + +// Start start SMTP login auth +func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { + return "LOGIN", []byte{}, nil +} + +// Next next step of SMTP login auth +func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { + if more { + switch string(fromServer) { + case "Username:": + return []byte(a.username), nil + case "Password:": + return []byte(a.password), nil + default: + return nil, fmt.Errorf("unknown fromServer: %s", string(fromServer)) + } + } + return nil, nil +} + +// Sender SMTP mail sender +type smtpSender struct { +} + +// Send send email +func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error { + opts := setting.MailService + + host, port, err := net.SplitHostPort(opts.Host) + if err != nil { + return err + } + + tlsconfig := &tls.Config{ + InsecureSkipVerify: opts.SkipVerify, + ServerName: host, + } + + if opts.UseCertificate { + cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile) + if err != nil { + return err + } + tlsconfig.Certificates = []tls.Certificate{cert} + } + + conn, err := net.Dial("tcp", net.JoinHostPort(host, port)) + if err != nil { + return err + } + defer conn.Close() + + isSecureConn := opts.IsTLSEnabled || (strings.HasSuffix(port, "465")) + // Start TLS directly if the port ends with 465 (SMTPS protocol) + if isSecureConn { + conn = tls.Client(conn, tlsconfig) + } + + client, err := smtp.NewClient(conn, host) + if err != nil { + return fmt.Errorf("NewClient: %v", err) + } + + if !opts.DisableHelo { + hostname := opts.HeloHostname + if len(hostname) == 0 { + hostname, err = os.Hostname() + if err != nil { + return err + } + } + + if err = client.Hello(hostname); err != nil { + return fmt.Errorf("Hello: %v", err) + } + } + + // If not using SMTPS, always use STARTTLS if available + hasStartTLS, _ := client.Extension("STARTTLS") + if !isSecureConn && hasStartTLS { + if err = client.StartTLS(tlsconfig); err != nil { + return fmt.Errorf("StartTLS: %v", err) + } + } + + canAuth, options := client.Extension("AUTH") + if canAuth && len(opts.User) > 0 { + var auth smtp.Auth + + if strings.Contains(options, "CRAM-MD5") { + auth = smtp.CRAMMD5Auth(opts.User, opts.Passwd) + } else if strings.Contains(options, "PLAIN") { + auth = smtp.PlainAuth("", opts.User, opts.Passwd, host) + } else if strings.Contains(options, "LOGIN") { + // Patch for AUTH LOGIN + auth = LoginAuth(opts.User, opts.Passwd) + } + + if auth != nil { + if err = client.Auth(auth); err != nil { + return fmt.Errorf("Auth: %v", err) + } + } + } + + if err = client.Mail(from); err != nil { + return fmt.Errorf("Mail: %v", err) + } + + for _, rec := range to { + if err = client.Rcpt(rec); err != nil { + return fmt.Errorf("Rcpt: %v", err) + } + } + + w, err := client.Data() + if err != nil { + return fmt.Errorf("Data: %v", err) + } else if _, err = msg.WriteTo(w); err != nil { + return fmt.Errorf("WriteTo: %v", err) + } else if err = w.Close(); err != nil { + return fmt.Errorf("Close: %v", err) + } + + return client.Quit() +} + +// Sender sendmail mail sender +type sendmailSender struct { +} + +// Send send email +func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error { + var err error + var closeError error + var waitError error + + args := []string{"-F", from, "-i"} + args = append(args, setting.MailService.SendmailArgs...) + args = append(args, to...) + log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args) + cmd := exec.Command(setting.MailService.SendmailPath, args...) + pipe, err := cmd.StdinPipe() + + if err != nil { + return err + } + + if err = cmd.Start(); err != nil { + return err + } + + _, err = msg.WriteTo(pipe) + + // we MUST close the pipe or sendmail will hang waiting for more of the message + // Also we should wait on our sendmail command even if something fails + closeError = pipe.Close() + waitError = cmd.Wait() + if err != nil { + return err + } else if closeError != nil { + return closeError + } else { + return waitError + } +} + +// Sender sendmail mail sender +type dummySender struct { +} + +// Send send email +func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error { + buf := bytes.Buffer{} + if _, err := msg.WriteTo(&buf); err != nil { + return err + } + log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String()) + return nil +} + +func processMailQueue() { + for msg := range mailQueue { + log.Trace("New e-mail sending request %s: %s", msg.GetHeader("To"), msg.Info) + if err := gomail.Send(Sender, msg.Message); err != nil { + log.Error("Failed to send emails %s: %s - %v", msg.GetHeader("To"), msg.Info, err) + } else { + log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info) + } + } +} + +var mailQueue chan *Message + +// Sender sender for sending mail synchronously +var Sender gomail.Sender + +// NewContext start mail queue service +func NewContext() { + // Need to check if mailQueue is nil because in during reinstall (user had installed + // before but swithed install lock off), this function will be called again + // while mail queue is already processing tasks, and produces a race condition. + if setting.MailService == nil || mailQueue != nil { + return + } + + switch setting.MailService.MailerType { + case "smtp": + Sender = &smtpSender{} + case "sendmail": + Sender = &sendmailSender{} + case "dummy": + Sender = &dummySender{} + } + + mailQueue = make(chan *Message, setting.MailService.QueueLength) + go processMailQueue() +} + +// SendAsync send mail asynchronous +func SendAsync(msg *Message) { + go func() { + mailQueue <- msg + }() +} diff --git a/services/mailer/main_test.go b/services/mailer/main_test.go new file mode 100644 index 0000000000..bbd121024e --- /dev/null +++ b/services/mailer/main_test.go @@ -0,0 +1,16 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mailer + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models" +) + +func TestMain(m *testing.M) { + models.MainTest(m, filepath.Join("..", "..")) +} -- cgit v1.2.3