mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -535,6 +535,10 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err | |||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if err = updateCommentInfos(e, opts, comment); err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if err = sendCreateCommentAction(e, opts, comment); err != nil { | 	if err = sendCreateCommentAction(e, opts, comment); err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| @@ -546,6 +550,56 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err | |||||||
| 	return comment, nil | 	return comment, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) { | ||||||
|  | 	// Check comment type. | ||||||
|  | 	switch opts.Type { | ||||||
|  | 	case CommentTypeCode: | ||||||
|  | 		if comment.ReviewID != 0 { | ||||||
|  | 			if comment.Review == nil { | ||||||
|  | 				if err := comment.loadReview(e); err != nil { | ||||||
|  | 					return err | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			if comment.Review.Type <= ReviewTypePending { | ||||||
|  | 				return nil | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		fallthrough | ||||||
|  | 	case CommentTypeComment: | ||||||
|  | 		if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// Check attachments | ||||||
|  | 		attachments := make([]*Attachment, 0, len(opts.Attachments)) | ||||||
|  | 		for _, uuid := range opts.Attachments { | ||||||
|  | 			attach, err := getAttachmentByUUID(e, uuid) | ||||||
|  | 			if err != nil { | ||||||
|  | 				if IsErrAttachmentNotExist(err) { | ||||||
|  | 					continue | ||||||
|  | 				} | ||||||
|  | 				return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err) | ||||||
|  | 			} | ||||||
|  | 			attachments = append(attachments, attach) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		for i := range attachments { | ||||||
|  | 			attachments[i].IssueID = opts.Issue.ID | ||||||
|  | 			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 fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	case CommentTypeReopen, CommentTypeClose: | ||||||
|  | 		if err = opts.Issue.updateClosedNum(e); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	// update the issue's updated_unix column | ||||||
|  | 	return updateIssueCols(e, opts.Issue, "updated_unix") | ||||||
|  | } | ||||||
|  |  | ||||||
| func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) { | func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) { | ||||||
| 	// Compose comment action, could be plain comment, close or reopen issue/pull request. | 	// 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. | 	// This object will be used to notify watchers in the end of function. | ||||||
| @@ -575,56 +629,16 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen | |||||||
| 		fallthrough | 		fallthrough | ||||||
| 	case CommentTypeComment: | 	case CommentTypeComment: | ||||||
| 		act.OpType = ActionCommentIssue | 		act.OpType = ActionCommentIssue | ||||||
|  |  | ||||||
| 		if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Check attachments |  | ||||||
| 		attachments := make([]*Attachment, 0, len(opts.Attachments)) |  | ||||||
| 		for _, uuid := range opts.Attachments { |  | ||||||
| 			attach, err := getAttachmentByUUID(e, uuid) |  | ||||||
| 			if err != nil { |  | ||||||
| 				if IsErrAttachmentNotExist(err) { |  | ||||||
| 					continue |  | ||||||
| 				} |  | ||||||
| 				return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err) |  | ||||||
| 			} |  | ||||||
| 			attachments = append(attachments, attach) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		for i := range attachments { |  | ||||||
| 			attachments[i].IssueID = opts.Issue.ID |  | ||||||
| 			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 fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 	case CommentTypeReopen: | 	case CommentTypeReopen: | ||||||
| 		act.OpType = ActionReopenIssue | 		act.OpType = ActionReopenIssue | ||||||
| 		if opts.Issue.IsPull { | 		if opts.Issue.IsPull { | ||||||
| 			act.OpType = ActionReopenPullRequest | 			act.OpType = ActionReopenPullRequest | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if err = opts.Issue.updateClosedNum(e); err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 	case CommentTypeClose: | 	case CommentTypeClose: | ||||||
| 		act.OpType = ActionCloseIssue | 		act.OpType = ActionCloseIssue | ||||||
| 		if opts.Issue.IsPull { | 		if opts.Issue.IsPull { | ||||||
| 			act.OpType = ActionClosePullRequest | 			act.OpType = ActionClosePullRequest | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if err = opts.Issue.updateClosedNum(e); err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	// update the issue's updated_unix column |  | ||||||
| 	if err = updateIssueCols(e, opts.Issue, "updated_unix"); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} | 	} | ||||||
| 	// Notify watchers for whatever action comes in, ignore if no action type. | 	// Notify watchers for whatever action comes in, ignore if no action type. | ||||||
| 	if act.OpType > 0 { | 	if act.OpType > 0 { | ||||||
|   | |||||||
| @@ -129,13 +129,17 @@ func (r *Review) publish(e *xorm.Engine) error { | |||||||
| 				go func(en *xorm.Engine, review *Review, comm *Comment) { | 				go func(en *xorm.Engine, review *Review, comm *Comment) { | ||||||
| 					sess := en.NewSession() | 					sess := en.NewSession() | ||||||
| 					defer sess.Close() | 					defer sess.Close() | ||||||
| 					if err := sendCreateCommentAction(sess, &CreateCommentOptions{ | 					opts := &CreateCommentOptions{ | ||||||
| 						Doer:    comm.Poster, | 						Doer:    comm.Poster, | ||||||
| 						Issue:   review.Issue, | 						Issue:   review.Issue, | ||||||
| 						Repo:    review.Issue.Repo, | 						Repo:    review.Issue.Repo, | ||||||
| 						Type:    comm.Type, | 						Type:    comm.Type, | ||||||
| 						Content: comm.Content, | 						Content: comm.Content, | ||||||
| 					}, comm); err != nil { | 					} | ||||||
|  | 					if err := updateCommentInfos(sess, opts, comm); err != nil { | ||||||
|  | 						log.Warn("updateCommentInfos: %v", err) | ||||||
|  | 					} | ||||||
|  | 					if err := sendCreateCommentAction(sess, opts, comm); err != nil { | ||||||
| 						log.Warn("sendCreateCommentAction: %v", err) | 						log.Warn("sendCreateCommentAction: %v", err) | ||||||
| 					} | 					} | ||||||
| 				}(e, r, comment) | 				}(e, r, comment) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user