Skip to content

Commit

Permalink
Change email notify way
Browse files Browse the repository at this point in the history
  • Loading branch information
a1012112796 committed Apr 26, 2020
1 parent bd363e5 commit ebde1d3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
49 changes: 0 additions & 49 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 20 additions & 3 deletions modules/notification/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
33 changes: 32 additions & 1 deletion templates/mail/issue/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@

<body>
{{if .IsMention}}<p><b>@{{.Doer.Name}}</b> mentioned you:</p>{{end}}
{{if eq .Comment.Type 29}}
<p>
<b>{{.Doer.Name}}</b>
{{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 <b>{{.Comment.Issue.PullRequest.HeadBranch}}</b> from
<a href="{{$oldCommitLink}}"><b>{{ShortSha .Comment.OldCommit}}</b></a>
to
<a href="{{$newCommitLink}}"><b>{{ShortSha .Comment.NewCommit}}</b></a>.
{{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}}
</p>
{{end}}
<p>
{{if eq .ActionName "close"}}
Closed #{{.Issue.Index}}.
Expand Down Expand Up @@ -46,7 +65,19 @@
<pre>{{.Patch}}</pre>
<div>{{.RenderedContent | Safe}}</div>
</div>
{{end -}}
{{end -}}
{{if eq .Comment.Type 29}}
{{ $r:= List .Comment.Commits}}
<ul>
{{range $r}}
<li>
<a href="{{AppUrl}}{{$.Comment.Issue.PullRequest.BaseRepo.OwnerName}}/{{$.Comment.Issue.PullRequest.BaseRepo.Name}}/commit/{{.ID}}">
{{ShortSha .ID.String}}
</a> - {{.Summary}}
</li>
{{end}}
</ul>
{{end}}
</p>
<div class="footer">
<p>
Expand Down

0 comments on commit ebde1d3

Please sign in to comment.