diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-12-01 10:44:39 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-11-30 21:44:39 -0500 |
commit | 2011a5b8183fe227f9f57f861dbe42abbd7abf42 (patch) | |
tree | cc34a5a20b3d8851f4dd139ce3a8356817883e1b /models/issue_comment.go | |
parent | 7c6f2e27becb7ab43fe266d2882a0d80977c2ef7 (diff) | |
download | gitea-2011a5b8183fe227f9f57f861dbe42abbd7abf42.tar.gz gitea-2011a5b8183fe227f9f57f861dbe42abbd7abf42.zip |
Extract createComment (#9125)
* Extract createComment
* fix lint
* fix lint
Diffstat (limited to 'models/issue_comment.go')
-rw-r--r-- | models/issue_comment.go | 72 |
1 files changed, 45 insertions, 27 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go index 73fd9c8c83..1fad141d92 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -495,7 +495,7 @@ func (c *Comment) CodeCommentURL() string { return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) } -func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) { +func createCommentWithNoAction(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) { var LabelID int64 if opts.Label != nil { LabelID = opts.Label.ID @@ -539,12 +539,6 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err return nil, err } - if !opts.NoAction { - if err = sendCreateCommentAction(e, opts, comment); err != nil { - return nil, err - } - } - if err = comment.addCrossReferences(e, opts.Doer, false); err != nil { return nil, err } @@ -651,19 +645,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen return nil } -func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) { - return createComment(e, &CreateCommentOptions{ - Type: CommentTypeMilestone, - Doer: doer, - Repo: repo, - Issue: issue, - OldMilestoneID: oldMilestoneID, - MilestoneID: milestoneID, - }) -} - func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) { - var content string var commentType CommentType @@ -685,13 +667,18 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin return nil, err } - return createComment(e, &CreateCommentOptions{ + var opts = &CreateCommentOptions{ Type: commentType, Doer: doer, Repo: issue.Repo, Issue: issue, Content: content, - }) + } + comment, err := createCommentWithNoAction(e, opts) + if err != nil { + return nil, err + } + return comment, sendCreateCommentAction(e, opts, comment) } // Creates issue dependency comment @@ -705,27 +692,35 @@ func createIssueDependencyComment(e *xorm.Session, doer *User, issue *Issue, dep } // Make two comments, one in each issue - _, err = createComment(e, &CreateCommentOptions{ + var opts = &CreateCommentOptions{ Type: cType, Doer: doer, Repo: issue.Repo, Issue: issue, DependentIssueID: dependentIssue.ID, - }) + } + comment, err := createCommentWithNoAction(e, opts) if err != nil { return } + if err = sendCreateCommentAction(e, opts, comment); err != nil { + return err + } - _, err = createComment(e, &CreateCommentOptions{ + opts = &CreateCommentOptions{ Type: cType, Doer: doer, Repo: issue.Repo, Issue: dependentIssue, DependentIssueID: issue.ID, - }) + } + comment, err = createCommentWithNoAction(e, opts) if err != nil { return } + if err = sendCreateCommentAction(e, opts, comment); err != nil { + return err + } return } @@ -758,7 +753,6 @@ type CreateCommentOptions struct { RefCommentID int64 RefAction references.XRefAction RefIsPull bool - NoAction bool } // CreateComment creates comment of issue or commit. @@ -769,7 +763,31 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { return nil, err } - comment, err = createComment(sess, opts) + comment, err = createCommentWithNoAction(sess, opts) + if err != nil { + return nil, err + } + + if err = sendCreateCommentAction(sess, opts, comment); err != nil { + return nil, err + } + + if err = sess.Commit(); err != nil { + return nil, err + } + + return comment, nil +} + +// CreateCommentWithNoAction creates comment of issue or commit with no action created +func CreateCommentWithNoAction(opts *CreateCommentOptions) (comment *Comment, err error) { + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return nil, err + } + + comment, err = createCommentWithNoAction(sess, opts) if err != nil { return nil, err } |