From af6ef4e653e9dfac7742e1b90c95cdcd5f91525a Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Wed, 13 Nov 2019 01:55:37 -0300 Subject: [PATCH 1/3] Remove duplicate code --- models/issue_comment.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index 63f5f6b7788da..64d6c6449a83f 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -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 } From 86f906a4c07eec4e7db25f8409978360bee3e1cb Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Wed, 13 Nov 2019 02:35:38 -0300 Subject: [PATCH 2/3] Fix review notifications and add templates --- modules/notification/mail/mail.go | 8 ++++++- routers/repo/pull_review.go | 7 +++--- services/mailer/mail.go | 39 +++++++++++++++++++++++++++++-- services/pull/review.go | 35 --------------------------- templates/mail/issue/default.tmpl | 8 +++++++ 5 files changed, 55 insertions(+), 42 deletions(-) delete mode 100644 services/pull/review.go diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go index 0900c6dcdf124..a87e31bd2cd46 100644 --- a/modules/notification/mail/mail.go +++ b/modules/notification/mail/mail.go @@ -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) } } diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go index 7d030988fd93a..bad1ceb65ed5f 100644 --- a/routers/repo/pull_review.go +++ b/routers/repo/pull_review.go @@ -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 @@ -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, @@ -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, @@ -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 } diff --git a/services/mailer/mail.go b/services/mailer/mail.go index fc892f6076b1c..2afb5367a6ff6 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -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 @@ -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 @@ -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, @@ -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 @@ -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 { diff --git a/services/pull/review.go b/services/pull/review.go deleted file mode 100644 index e4aae3c0d5885..0000000000000 --- a/services/pull/review.go +++ /dev/null @@ -1,35 +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 pull - -import ( - "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/notification" -) - -// CreateReview creates a new review based on opts -func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) { - review, err := models.CreateReview(opts) - if err != nil { - return nil, err - } - - notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil) - - return review, nil -} - -// UpdateReview updates a review -func UpdateReview(review *models.Review) error { - err := models.UpdateReview(review) - if err != nil { - return err - } - - notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil) - - return nil -} diff --git a/templates/mail/issue/default.tmpl b/templates/mail/issue/default.tmpl index ee15d6d8e1cc0..a49c832014b37 100644 --- a/templates/mail/issue/default.tmpl +++ b/templates/mail/issue/default.tmpl @@ -21,6 +21,14 @@ {{else}} {{.Body | Str2html}} {{end -}} + {{if .ReviewComments}} +
+ {{range .ReviewComments}} + {{.Content | Safe}} +
{{.Patch}}
+ {{end}} +
+ {{end}}

--- From e5bceda25c0244f6c5298862e5c526e52867e3eb Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Wed, 13 Nov 2019 03:29:14 -0300 Subject: [PATCH 3/3] Rearrange review styling --- templates/mail/issue/default.tmpl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/templates/mail/issue/default.tmpl b/templates/mail/issue/default.tmpl index a49c832014b37..f2b5c5baf8bc8 100644 --- a/templates/mail/issue/default.tmpl +++ b/templates/mail/issue/default.tmpl @@ -3,6 +3,12 @@ {{.Subject}} + {{if .ReviewComments}} + + {{end}} @@ -21,12 +27,10 @@ {{else}} {{.Body | Str2html}} {{end -}} - {{if .ReviewComments}} -

- {{range .ReviewComments}} - {{.Content | Safe}} -
{{.Patch}}
- {{end}} + {{range .ReviewComments}} +
+
{{.Patch}}
+
{{.Content | Safe}}
{{end}}