Skip to content

WIP: Fix review notifications and add them to mail templates #8948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,6 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
return nil, err
}

if err := CommentList(comments).loadPosters(e); err != nil {
return nil, err
}

if err := issue.loadRepo(e); err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion modules/notification/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models
} else if comment.Type == models.CommentTypeComment {
act = models.ActionCommentIssue
}
if r != nil {
comment.Review = r
if err := r.LoadCodeComments(); err != nil {
log.Error("LoadCodeComments: %v", err)
}
}
if err := mailer.MailParticipantsComment(comment, act, pr.Issue); err != nil {
log.Error("MailParticipants: %v", err)
log.Error("MailParticipantsComment: %v", err)
}
}

Expand Down
7 changes: 3 additions & 4 deletions routers/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
comment_service "code.gitea.io/gitea/services/comments"
pull_service "code.gitea.io/gitea/services/pull"
)

// CreateCodeComment will create a code comment including an pending review if required
Expand Down Expand Up @@ -55,7 +54,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
}
// No pending review exists
// Create a new pending review for this issue & user
if review, err = pull_service.CreateReview(models.CreateReviewOptions{
if review, err = models.CreateReview(models.CreateReviewOptions{
Type: models.ReviewTypePending,
Reviewer: ctx.User,
Issue: issue,
Expand Down Expand Up @@ -157,7 +156,7 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
return
}
// No current review. Create a new one!
if review, err = pull_service.CreateReview(models.CreateReviewOptions{
if review, err = models.CreateReview(models.CreateReviewOptions{
Type: reviewType,
Issue: issue,
Reviewer: ctx.User,
Expand All @@ -169,7 +168,7 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {
} else {
review.Content = form.Content
review.Type = reviewType
if err = pull_service.UpdateReview(review); err != nil {
if err = models.UpdateReview(review); err != nil {
ctx.ServerError("UpdateReview", err)
return
}
Expand Down
39 changes: 37 additions & 2 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ var (
subjectRemoveSpaces = regexp.MustCompile(`[\s]+`)
)

// CodeComment contains a single comment within a review
type CodeComment struct {
Path string
Line int64
Content string
Patch string
Author *models.User
}

// InitMailRender initializes the mail renderer
func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) {
subjectTemplates = subjectTpl
Expand Down Expand Up @@ -177,7 +186,8 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
link string
prefix string
// Fall back subject for bad templates, make sure subject is never empty
fallback string
fallback string
reviewComments []CodeComment
)

commentType := models.CommentTypeComment
Expand All @@ -190,12 +200,18 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
}

fallback = prefix + fallbackMailSubject(issue)
url := issue.Repo.HTMLURL()
metas := issue.Repo.ComposeMetas()

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

actType, actName, tplName := actionToTemplate(issue, actionType, commentType)

if actName == "review" && comment.Review != nil {
reviewComments = getReviewComments(comment.Review)
}

mailMeta := map[string]interface{}{
"FallbackSubject": fallback,
"Body": body,
Expand All @@ -210,6 +226,7 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
"SubjectPrefix": prefix,
"ActionType": actType,
"ActionName": actName,
"ReviewComments": reviewComments,
}

var mailSubject bytes.Buffer
Expand Down Expand Up @@ -244,6 +261,24 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, actionTy
return msg
}

func getReviewComments(r *models.Review) []CodeComment {
reviewComments := make([]CodeComment, 0, 10)
for _, lines := range r.CodeComments {
for _, comments := range lines {
for _, comment := range comments {
reviewComments = append(reviewComments, CodeComment{
Path: comment.TreePath,
Line: comment.Line,
Content: comment.RenderedContent,
Patch: comment.Patch,
Author: comment.Poster,
})
}
}
}
return reviewComments
}

func sanitizeSubject(subject string) string {
runes := []rune(strings.TrimSpace(subjectRemoveSpaces.ReplaceAllLiteralString(subject, " ")))
if len(runes) > mailMaxSubjectRunes {
Expand Down
35 changes: 0 additions & 35 deletions services/pull/review.go

This file was deleted.

12 changes: 12 additions & 0 deletions templates/mail/issue/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{.Subject}}</title>
{{if .ReviewComments}}
<style>
.review { padding-left: 1em; margin: 1em 0; }
.review > pre { padding: 1em; border-left: 1px solid grey; }
</style>
{{end}}
</head>

<body>
Expand All @@ -21,6 +27,12 @@
{{else}}
{{.Body | Str2html}}
{{end -}}
{{range .ReviewComments}}
<div class="review">
<pre>{{.Patch}}</pre>
<div>{{.Content | Safe}}</div>
</div>
{{end}}
</p>
<p>
---
Expand Down