Skip to content

Commit 12170d2

Browse files
lunnylafriks
authored andcommitted
Split sendCreateCommentAction as two parts, one for update comment related informations, another for actions (#8784)
* Split sendCreateCommentAction as two parts, one for update comment related informations, another for actions * fix lint
1 parent 72aa5a2 commit 12170d2

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

models/issue_comment.go

+42-28
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
535535
return nil, err
536536
}
537537

538+
if err = updateCommentInfos(e, opts, comment); err != nil {
539+
return nil, err
540+
}
541+
538542
if err = sendCreateCommentAction(e, opts, comment); err != nil {
539543
return nil, err
540544
}
@@ -546,19 +550,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
546550
return comment, nil
547551
}
548552

549-
func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
550-
// Compose comment action, could be plain comment, close or reopen issue/pull request.
551-
// This object will be used to notify watchers in the end of function.
552-
act := &Action{
553-
ActUserID: opts.Doer.ID,
554-
ActUser: opts.Doer,
555-
Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
556-
RepoID: opts.Repo.ID,
557-
Repo: opts.Repo,
558-
Comment: comment,
559-
CommentID: comment.ID,
560-
IsPrivate: opts.Repo.IsPrivate,
561-
}
553+
func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
562554
// Check comment type.
563555
switch opts.Type {
564556
case CommentTypeCode:
@@ -574,8 +566,6 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
574566
}
575567
fallthrough
576568
case CommentTypeComment:
577-
act.OpType = ActionCommentIssue
578-
579569
if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
580570
return err
581571
}
@@ -601,30 +591,54 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
601591
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
602592
}
603593
}
594+
case CommentTypeReopen, CommentTypeClose:
595+
if err = opts.Issue.updateClosedNum(e); err != nil {
596+
return err
597+
}
598+
}
599+
// update the issue's updated_unix column
600+
return updateIssueCols(e, opts.Issue, "updated_unix")
601+
}
604602

603+
func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
604+
// Compose comment action, could be plain comment, close or reopen issue/pull request.
605+
// This object will be used to notify watchers in the end of function.
606+
act := &Action{
607+
ActUserID: opts.Doer.ID,
608+
ActUser: opts.Doer,
609+
Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
610+
RepoID: opts.Repo.ID,
611+
Repo: opts.Repo,
612+
Comment: comment,
613+
CommentID: comment.ID,
614+
IsPrivate: opts.Repo.IsPrivate,
615+
}
616+
// Check comment type.
617+
switch opts.Type {
618+
case CommentTypeCode:
619+
if comment.ReviewID != 0 {
620+
if comment.Review == nil {
621+
if err := comment.loadReview(e); err != nil {
622+
return err
623+
}
624+
}
625+
if comment.Review.Type <= ReviewTypePending {
626+
return nil
627+
}
628+
}
629+
fallthrough
630+
case CommentTypeComment:
631+
act.OpType = ActionCommentIssue
605632
case CommentTypeReopen:
606633
act.OpType = ActionReopenIssue
607634
if opts.Issue.IsPull {
608635
act.OpType = ActionReopenPullRequest
609636
}
610-
611-
if err = opts.Issue.updateClosedNum(e); err != nil {
612-
return err
613-
}
614-
615637
case CommentTypeClose:
616638
act.OpType = ActionCloseIssue
617639
if opts.Issue.IsPull {
618640
act.OpType = ActionClosePullRequest
619641
}
620-
621-
if err = opts.Issue.updateClosedNum(e); err != nil {
622-
return err
623-
}
624-
}
625-
// update the issue's updated_unix column
626-
if err = updateIssueCols(e, opts.Issue, "updated_unix"); err != nil {
627-
return err
628642
}
629643
// Notify watchers for whatever action comes in, ignore if no action type.
630644
if act.OpType > 0 {

models/review.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,17 @@ func (r *Review) publish(e *xorm.Engine) error {
129129
go func(en *xorm.Engine, review *Review, comm *Comment) {
130130
sess := en.NewSession()
131131
defer sess.Close()
132-
if err := sendCreateCommentAction(sess, &CreateCommentOptions{
132+
opts := &CreateCommentOptions{
133133
Doer: comm.Poster,
134134
Issue: review.Issue,
135135
Repo: review.Issue.Repo,
136136
Type: comm.Type,
137137
Content: comm.Content,
138-
}, comm); err != nil {
138+
}
139+
if err := updateCommentInfos(sess, opts, comm); err != nil {
140+
log.Warn("updateCommentInfos: %v", err)
141+
}
142+
if err := sendCreateCommentAction(sess, opts, comm); err != nil {
139143
log.Warn("sendCreateCommentAction: %v", err)
140144
}
141145
}(e, r, comment)

0 commit comments

Comments
 (0)