From ebde1d3a1f06de4e442d94640d4ccc2b5393c3a9 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Sun, 26 Apr 2020 10:27:40 +0800 Subject: [PATCH] Change email notify way --- models/issue_comment.go | 49 ------------------------------- modules/notification/mail/mail.go | 23 +++++++++++++-- templates/mail/issue/default.tmpl | 33 ++++++++++++++++++++- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index 45c6b1a04bf2f..3e517a1c709ab 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -579,55 +579,6 @@ func (c *Comment) LoadPushCommits() (err error) { return err } -// LoadPullPushDefaultNotify load default notify message in Content -// with Mardown style for Pull Push commits comment -func (c *Comment) LoadPullPushDefaultNotify() (err error) { - if c.Type != CommentTypePullPush { - return nil - } - if err = c.LoadIssue(); err != nil { - return - } - if err = c.Issue.LoadRepo(); err != nil { - return - } - if err = c.Issue.LoadPullRequest(); err != nil { - return - } - - message := "@" + c.Poster.Name - if c.IsForcePush { - commitIDs := strings.Split(c.Content, ":") - message += " force-pushed the " + c.Issue.PullRequest.HeadBranch + " branch from " + commitIDs[0] + " to " + commitIDs[1] - } else { - if c.Commits == nil { - repoPath := c.Issue.Repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - return err - } - defer gitRepo.Close() - commitIDs := strings.Split(c.Content, ":") - c.Commits = gitRepo.GetCommitsFromIDs(commitIDs) - } - - if c.Commits.Len() == 1 { - message += fmt.Sprintf(" pushed 1 commit to %s: \n ", c.Issue.Repo.Name) - } else { - message += fmt.Sprintf(" pushed %d commits to %s: \n ", c.Commits.Len(), c.Issue.Repo.Name) - } - - for e := c.Commits.Front(); e != nil; e = e.Next() { - commitMsg := strings.Split(e.Value.(*git.Commit).Message(), "\n")[0] - message += "* " + e.Value.(*git.Commit).ID.String()[0:10] + " - " + commitMsg + " \n" - } - } - - c.Content = message - - return -} - func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) { var LabelID int64 if opts.Label != nil { diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go index 9001307111a43..294bc6023a07e 100644 --- a/modules/notification/mail/mail.go +++ b/modules/notification/mail/mail.go @@ -121,10 +121,27 @@ func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode } func (m *mailNotifier) NotifyPullRequestPushCommits(doer *models.User, pr *models.PullRequest, comment *models.Comment) { - if err := comment.LoadPullPushDefaultNotify(); err != nil { - log.Error("comment.loadPullPushDefaultNotify: %v", err) - comment.Content = " " + var err error + if err = comment.LoadIssue(); err != nil { + log.Error("comment.LoadIssue: %v", err) + return + } + if err = comment.Issue.LoadRepo(); err != nil { + log.Error("comment.Issue.LoadRepo: %v", err) + return + } + if err = comment.Issue.LoadPullRequest(); err != nil { + log.Error("comment.Issue.LoadPullRequest: %v", err) + return + } + if err = comment.Issue.PullRequest.LoadBaseRepo(); err != nil { + log.Error("comment.Issue.PullRequest.LoadBaseRepo: %v", err) + return + } + if err := comment.LoadPushCommits(); err != nil { + log.Error("comment.LoadPushCommits: %v", err) } + comment.Content = "" m.NotifyCreateIssueComment(doer, comment.Issue.Repo, comment.Issue, comment) } diff --git a/templates/mail/issue/default.tmpl b/templates/mail/issue/default.tmpl index 7cd3975277795..00bb561d6969f 100644 --- a/templates/mail/issue/default.tmpl +++ b/templates/mail/issue/default.tmpl @@ -17,6 +17,25 @@ {{if .IsMention}}

@{{.Doer.Name}} mentioned you:

{{end}} + {{if eq .Comment.Type 29}} +

+ {{.Doer.Name}} + {{if .Comment.IsForcePush}} + {{ $oldCommitLink:= printf "%s%s/%s/commit/%s" AppUrl .Comment.Issue.PullRequest.BaseRepo.OwnerName .Comment.Issue.PullRequest.BaseRepo.Name .Comment.OldCommit}} + {{ $newCommitLink:= printf "%s%s/%s/commit/%s" AppUrl .Comment.Issue.PullRequest.BaseRepo.OwnerName .Comment.Issue.PullRequest.BaseRepo.Name .Comment.NewCommit}} + force-pushed the {{.Comment.Issue.PullRequest.HeadBranch}} from + {{ShortSha .Comment.OldCommit}} + to + {{ShortSha .Comment.NewCommit}}. + {{else}} + {{if eq .Comment.Commits.Len 1}} + {{printf "pushed 1 commit to %s:" .Comment.Issue.PullRequest.HeadBranch}} + {{else}} + {{printf "pushed %d commits to %s:" .Comment.Commits.Len .Comment.Issue.PullRequest.HeadBranch}} + {{end}} + {{end}} +

+ {{end}}

{{if eq .ActionName "close"}} Closed #{{.Issue.Index}}. @@ -46,7 +65,19 @@

{{.Patch}}
{{.RenderedContent | Safe}}
- {{end -}} + {{end -}} + {{if eq .Comment.Type 29}} + {{ $r:= List .Comment.Commits}} + + {{end}}