From 8f77329aee5d6082534c34177637f1cb2d067846 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 6 Jul 2018 18:24:44 +0200 Subject: [PATCH] Send notifications / actions to users until review gets published Fix diff generation bug Fix wrong hashtag --- models/git_diff.go | 4 +- models/issue_comment.go | 31 +++++++----- models/review.go | 48 +++++++++++++++++++ routers/repo/pull_review.go | 43 ++++++++--------- .../repo/issue/view_content/comments.tmpl | 2 +- 5 files changed, 92 insertions(+), 36 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 4f4389304..1d16b3cb0 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -352,8 +352,8 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi if currentLine == 0 { return "" } - - if len(hunk)-headerLines < numbersOfLine { + // headerLines + hunkLine (1) = totalNonCodeLines + if len(hunk)-headerLines-1 <= numbersOfLine { // No need to cut the hunk => return existing hunk return strings.Join(hunk, "\n") } diff --git a/models/issue_comment.go b/models/issue_comment.go index f75f24b47..4a0dc7ec8 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -459,6 +459,14 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err return nil, err } + if err = sendCreateCommentAction(e, opts, comment); err != nil { + return nil, err + } + + return comment, nil +} + +func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) { // Compose comment action, could be plain comment, close or reopen issue/pull request. // This object will be used to notify watchers in the end of function. act := &Action{ @@ -471,14 +479,18 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err CommentID: comment.ID, IsPrivate: opts.Repo.IsPrivate, } - // Check comment type. switch opts.Type { + case CommentTypeCode: + if comment.Review == nil || comment.Review.Type <= ReviewTypePending { + break + } + fallthrough case CommentTypeComment: act.OpType = ActionCommentIssue if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil { - return nil, err + return err } // Check attachments @@ -489,7 +501,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err if IsErrAttachmentNotExist(err) { continue } - return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err) + return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err) } attachments = append(attachments, attach) } @@ -499,7 +511,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err attachments[i].CommentID = comment.ID // No assign value could be 0, so ignore AllCols(). if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil { - return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) + return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) } } @@ -515,7 +527,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID) } if err != nil { - return nil, err + return err } case CommentTypeClose: @@ -530,15 +542,13 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID) } if err != nil { - return nil, err + return err } } - // update the issue's updated_unix column if err = updateIssueCols(e, opts.Issue, "updated_unix"); err != nil { - return nil, err + return err } - // Notify watchers for whatever action comes in, ignore if no action type. if act.OpType > 0 { if err = notifyWatchers(e, act); err != nil { @@ -548,8 +558,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err log.Error(4, "MailParticipants: %v", err) } } - - return comment, nil + return nil } func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) { diff --git a/models/review.go b/models/review.go index 5a4e0be24..71baa6809 100644 --- a/models/review.go +++ b/models/review.go @@ -5,7 +5,11 @@ package models import ( + "fmt" + + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" + "github.com/go-xorm/xorm" "github.com/go-xorm/builder" ) @@ -97,6 +101,50 @@ func (r *Review) LoadAttributes() error { return r.loadAttributes(x) } +// Publish will send notifications / actions to participants for all code comments; parts are concurrent +func (r *Review) Publish() error { + return r.publish(x) +} + +func (r *Review) publish(e *xorm.Engine) error { + if r.Type == ReviewTypePending || r.Type == ReviewTypeUnknown { + return fmt.Errorf("review cannot be published if type is pending or unknown") + } + if r.Issue == nil { + if err := r.loadIssue(e); err != nil { + return err + } + } + if err := r.Issue.loadRepo(e); err != nil { + return err + } + if len(r.CodeComments) == 0 { + if err := r.loadCodeComments(e); err != nil { + return err + } + } + for _, lines := range r.CodeComments { + for _, comments := range lines { + for _, comment := range comments { + go func() { + sess := x.NewSession() + defer sess.Close() + if err := sendCreateCommentAction(sess, &CreateCommentOptions{ + Doer: comment.Poster, + Issue: r.Issue, + Repo: r.Issue.Repo, + Type: comment.Type, + Content: comment.Content, + }, comment); err != nil { + log.Warn("sendCreateCommentAction: %v", err) + } + }() + } + } + } + return nil +} + func getReviewByID(e Engine, id int64) (*Review, error) { review := new(Review) if has, err := e.ID(id).Get(review); err != nil { diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go index 2dad56c06..153f35f43 100644 --- a/routers/repo/pull_review.go +++ b/routers/repo/pull_review.go @@ -101,25 +101,6 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) { } var review *models.Review var err error - defer func() { - if review != nil { - comm, err := models.CreateComment(&models.CreateCommentOptions{ - Type: models.CommentTypeReview, - Doer: ctx.User, - Content: review.Content, - Issue: issue, - Repo: issue.Repo, - ReviewID: review.ID, - }) - if err != nil || comm == nil { - ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index)) - return - } - ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag())) - } else { - ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index)) - } - }() reviewType := form.ReviewType() if reviewType == models.ReviewTypeUnknown { @@ -142,11 +123,29 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) { ctx.ServerError("CreateReview", err) return } + } else { + review.Content = form.Content + review.Type = reviewType + if err = models.UpdateReview(review); err != nil { + ctx.ServerError("UpdateReview", err) + return + } + } + comm, err := models.CreateComment(&models.CreateCommentOptions{ + Type: models.CommentTypeReview, + Doer: ctx.User, + Content: review.Content, + Issue: issue, + Repo: issue.Repo, + ReviewID: review.ID, + }) + if err != nil || comm == nil { + ctx.ServerError("CreateComment", err) return } - review.Content = form.Content - review.Type = reviewType - if err = models.UpdateReview(review); err != nil { + if err = review.Publish(); err != nil { + ctx.ServerError("Publish", err) return } + ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag())) } diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index a5e82f2f6..e5bc42b8a 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -292,7 +292,7 @@
{{range $comms}} {{ $createdSubStr:= TimeSinceUnix .CreatedUnix $.Lang }} -
+