Skip to content

Commit 9930d47

Browse files
guillep2kzeripath
authored andcommitted
Add review comments to mail notifications (#8996)
1 parent 97dc314 commit 9930d47

File tree

6 files changed

+73
-35
lines changed

6 files changed

+73
-35
lines changed

docs/content/doc/advanced/mail-templates-us.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Currently, the following notification events make use of templates:
3232
| `close` | An issue or pull request was closed. |
3333
| `reopen` | An issue or pull request was reopened. |
3434
| `review` | The head comment of a review in a pull request. |
35+
| `approve` | The head comment of a approving review for a pull request. |
36+
| `reject` | The head comment of a review requesting changes for a pull request. |
3537
| `code` | A single comment on the code of a pull request. |
3638
| `assigned` | Used was assigned to an issue or pull request. |
3739
| `default` | Any action not included in the above categories, or when the corresponding category template is not present. |
@@ -84,22 +86,23 @@ _subject_ and _mail body_ templates requires at least three dashes; no other cha
8486
_Subject_ and _mail body_ are parsed by [Golang's template engine](https://golang.org/pkg/text/template/) and
8587
are provided with a _metadata context_ assembled for each notification. The context contains the following elements:
8688

87-
| Name | Type | Available | Usage |
88-
|--------------------|----------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
89-
| `.FallbackSubject` | string | Always | A default subject line. See Below. |
90-
| `.Subject` | string | Only in body | The _subject_, once resolved. |
91-
| `.Body` | string | Always | The message of the issue, pull request or comment, parsed from Markdown into HTML and sanitized. Do not confuse with the _mail body_ |
92-
| `.Link` | string | Always | The address of the originating issue, pull request or comment. |
93-
| `.Issue` | models.Issue | Always | The issue (or pull request) originating the notification. To get data specific to a pull request (e.g. `HasMerged`), `.Issue.PullRequest` can be used, but care should be taken as this field will be `nil` if the issue is *not* a pull request. |
94-
| `.Comment` | models.Comment | If applicable | If the notification is from a comment added to an issue or pull request, this will contain the information about the comment. |
95-
| `.IsPull` | bool | Always | `true` if the mail notification is associated with a pull request (i.e. `.Issue.PullRequest` is not `nil`). |
96-
| `.Repo` | string | Always | Name of the repository, including owner name (e.g. `mike/stuff`) |
97-
| `.User` | models.User | Always | Owner of the repository from which the event originated. To get the user name (e.g. `mike`),`.User.Name` can be used. |
98-
| `.Doer` | models.User | Always | User that executed the action triggering the notification event. To get the user name (e.g. `rhonda`), `.Doer.Name` can be used. |
99-
| `.IsMention` | bool | Always | `true` if this notification was only generated because the user was mentioned in the comment, while not being subscribed to the source. It will be `false` if the recipient was subscribed to the issue or repository. |
100-
| `.SubjectPrefix` | string | Always | `Re: ` if the notification is about other than issue or pull request creation; otherwise an empty string. |
101-
| `.ActionType` | string | Always | `"issue"` or `"pull"`. Will correspond to the actual _action type_ independently of which template was selected. |
102-
| `.ActionName` | string | Always | It will be one of the action types described above (`new`, `comment`, etc.), and will correspond to the actual _action name_ independently of which template was selected. |
89+
| Name | Type | Available | Usage |
90+
|--------------------|------------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
91+
| `.FallbackSubject` | string | Always | A default subject line. See Below. |
92+
| `.Subject` | string | Only in body | The _subject_, once resolved. |
93+
| `.Body` | string | Always | The message of the issue, pull request or comment, parsed from Markdown into HTML and sanitized. Do not confuse with the _mail body_. |
94+
| `.Link` | string | Always | The address of the originating issue, pull request or comment. |
95+
| `.Issue` | models.Issue | Always | The issue (or pull request) originating the notification. To get data specific to a pull request (e.g. `HasMerged`), `.Issue.PullRequest` can be used, but care should be taken as this field will be `nil` if the issue is *not* a pull request. |
96+
| `.Comment` | models.Comment | If applicable | If the notification is from a comment added to an issue or pull request, this will contain the information about the comment. |
97+
| `.IsPull` | bool | Always | `true` if the mail notification is associated with a pull request (i.e. `.Issue.PullRequest` is not `nil`). |
98+
| `.Repo` | string | Always | Name of the repository, including owner name (e.g. `mike/stuff`) |
99+
| `.User` | models.User | Always | Owner of the repository from which the event originated. To get the user name (e.g. `mike`),`.User.Name` can be used. |
100+
| `.Doer` | models.User | Always | User that executed the action triggering the notification event. To get the user name (e.g. `rhonda`), `.Doer.Name` can be used. |
101+
| `.IsMention` | bool | Always | `true` if this notification was only generated because the user was mentioned in the comment, while not being subscribed to the source. It will be `false` if the recipient was subscribed to the issue or repository. |
102+
| `.SubjectPrefix` | string | Always | `Re: ` if the notification is about other than issue or pull request creation; otherwise an empty string. |
103+
| `.ActionType` | string | Always | `"issue"` or `"pull"`. Will correspond to the actual _action type_ independently of which template was selected. |
104+
| `.ActionName` | string | Always | It will be one of the action types described above (`new`, `comment`, etc.), and will correspond to the actual _action name_ independently of which template was selected. |
105+
| `.ReviewComments` | []models.Comment | Always | List of code comments in a review. The comment text will be in `.RenderedContent` and the referenced code will be in `.Patch`. |
103106

104107
All names are case sensitive.
105108

@@ -254,13 +257,14 @@ This template produces something along these lines:
254257
The template system contains several functions that can be used to further process and format
255258
the messages. Here's a list of some of them:
256259

257-
| Name | Parameters | Available | Usage |
258-
|----------------------|-------------|-----------|---------------------------------------------------------------------|
259-
| `AppUrl` | - | Any | Gitea's URL |
260-
| `AppName` | - | Any | Set from `app.ini`, usually "Gitea" |
261-
| `AppDomain` | - | Any | Gitea's host name |
262-
| `EllipsisString` | string, int | Any | Truncates a string to the specified length; adds ellipsis as needed |
263-
| `Str2html` | string | Body only | Sanitizes text by removing any HTML tags from it. |
260+
| Name | Parameters | Available | Usage |
261+
|----------------------|-------------|-----------|------------------------------------------------------------------------------|
262+
| `AppUrl` | - | Any | Gitea's URL |
263+
| `AppName` | - | Any | Set from `app.ini`, usually "Gitea" |
264+
| `AppDomain` | - | Any | Gitea's host name |
265+
| `EllipsisString` | string, int | Any | Truncates a string to the specified length; adds ellipsis as needed |
266+
| `Str2html` | string | Body only | Sanitizes text by removing any HTML tags from it. |
267+
| `Safe` | string | Body only | Takes the input as HTML; can be used for `.ReviewComments.RenderedContent`. |
264268

265269
These are _functions_, not metadata, so they have to be used:
266270

models/issue_comment.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,6 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
10161016
return nil, err
10171017
}
10181018

1019-
if err := CommentList(comments).loadPosters(e); err != nil {
1020-
return nil, err
1021-
}
1022-
10231019
if err := issue.loadRepo(e); err != nil {
10241020
return nil, err
10251021
}

models/review.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ type Review struct {
6262
}
6363

6464
func (r *Review) loadCodeComments(e Engine) (err error) {
65-
r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r)
65+
if r.CodeComments == nil {
66+
r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r)
67+
}
6668
return
6769
}
6870

modules/notification/mail/mail.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.
4040
}
4141

4242
if err := mailer.MailParticipantsComment(comment, act, issue); err != nil {
43-
log.Error("MailParticipants: %v", err)
43+
log.Error("MailParticipantsComment: %v", err)
4444
}
4545
}
4646

@@ -87,7 +87,7 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models
8787
act = models.ActionCommentIssue
8888
}
8989
if err := mailer.MailParticipantsComment(comment, act, pr.Issue); err != nil {
90-
log.Error("MailParticipants: %v", err)
90+
log.Error("MailParticipantsComment: %v", err)
9191
}
9292
}
9393

services/mailer/mail.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
177177
link string
178178
prefix string
179179
// Fall back subject for bad templates, make sure subject is never empty
180-
fallback string
180+
fallback string
181+
reviewComments []*models.Comment
181182
)
182183

183184
commentType := models.CommentTypeComment
@@ -189,12 +190,26 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
189190
link = issue.HTMLURL()
190191
}
191192

193+
reviewType := models.ReviewTypeComment
194+
if comment != nil && comment.Review != nil {
195+
reviewType = comment.Review.Type
196+
}
197+
192198
fallback = prefix + fallbackMailSubject(issue)
193199

194200
// This is the body of the new issue or comment, not the mail body
195201
body := string(markup.RenderByType(markdown.MarkupName, []byte(content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
196202

197-
actType, actName, tplName := actionToTemplate(issue, actionType, commentType)
203+
actType, actName, tplName := actionToTemplate(issue, actionType, commentType, reviewType)
204+
205+
if comment != nil && comment.Review != nil {
206+
reviewComments = make([]*models.Comment, 0, 10)
207+
for _, lines := range comment.Review.CodeComments {
208+
for _, comments := range lines {
209+
reviewComments = append(reviewComments, comments...)
210+
}
211+
}
212+
}
198213

199214
mailMeta := map[string]interface{}{
200215
"FallbackSubject": fallback,
@@ -210,6 +225,7 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
210225
"SubjectPrefix": prefix,
211226
"ActionType": actType,
212227
"ActionName": actName,
228+
"ReviewComments": reviewComments,
213229
}
214230

215231
var mailSubject bytes.Buffer
@@ -272,7 +288,8 @@ func SendIssueMentionMail(issue *models.Issue, doer *models.User, actionType mod
272288

273289
// actionToTemplate returns the type and name of the action facing the user
274290
// (slightly different from models.ActionType) and the name of the template to use (based on availability)
275-
func actionToTemplate(issue *models.Issue, actionType models.ActionType, commentType models.CommentType) (typeName, name, template string) {
291+
func actionToTemplate(issue *models.Issue, actionType models.ActionType,
292+
commentType models.CommentType, reviewType models.ReviewType) (typeName, name, template string) {
276293
if issue.IsPull {
277294
typeName = "pull"
278295
} else {
@@ -292,7 +309,14 @@ func actionToTemplate(issue *models.Issue, actionType models.ActionType, comment
292309
default:
293310
switch commentType {
294311
case models.CommentTypeReview:
295-
name = "review"
312+
switch reviewType {
313+
case models.ReviewTypeApprove:
314+
name = "approve"
315+
case models.ReviewTypeReject:
316+
name = "reject"
317+
default:
318+
name = "review"
319+
}
296320
case models.CommentTypeCode:
297321
name = "code"
298322
case models.CommentTypeAssignees:

templates/mail/issue/default.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<title>{{.Subject}}</title>
6+
{{if .ReviewComments}}
7+
<style>
8+
.review { padding-left: 1em; margin: 1em 0; }
9+
.review > pre { padding: 1em; border-left: 1px solid grey; }
10+
</style>
11+
{{end}}
612
</head>
713

814
<body>
@@ -15,12 +21,18 @@
1521
Closed #{{.Issue.Index}}.
1622
{{else if eq .ActionName "reopen"}}
1723
Reopened #{{.Issue.Index}}.
18-
{{else}}
24+
{{else if ne .ReviewComments}}
1925
Empty comment on #{{.Issue.Index}}.
2026
{{end}}
2127
{{else}}
2228
{{.Body | Str2html}}
2329
{{end -}}
30+
{{- range .ReviewComments}}
31+
<div class="review">
32+
<pre>{{.Patch}}</pre>
33+
<div>{{.RenderedContent | Safe}}</div>
34+
</div>
35+
{{end -}}
2436
</p>
2537
<p>
2638
---

0 commit comments

Comments
 (0)