Extract createComment (#9125)

* Extract createComment

* fix lint

* fix lint
This commit is contained in:
Lunny Xiao 2019-12-01 10:44:39 +08:00 committed by techknowlogick
parent 7c6f2e27be
commit 2011a5b818
9 changed files with 130 additions and 53 deletions

View file

@ -656,16 +656,18 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
if !issue.IsClosed { if !issue.IsClosed {
cmtType = CommentTypeReopen cmtType = CommentTypeReopen
} }
if _, err := createComment(e, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: cmtType, Type: cmtType,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
}); err != nil { }
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err return err
} }
return sendCreateCommentAction(e, opts, comment)
return nil
} }
// ChangeStatus changes issue status to open or closed. // ChangeStatus changes issue status to open or closed.
@ -711,17 +713,21 @@ func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
return fmt.Errorf("loadRepo: %v", err) return fmt.Errorf("loadRepo: %v", err)
} }
if _, err = createComment(sess, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: CommentTypeChangeTitle, Type: CommentTypeChangeTitle,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
OldTitle: oldTitle, OldTitle: oldTitle,
NewTitle: issue.Title, NewTitle: issue.Title,
}); err != nil { }
comment, err := createCommentWithNoAction(sess, opts)
if err != nil {
return fmt.Errorf("createComment: %v", err) return fmt.Errorf("createComment: %v", err)
} }
if err = sendCreateCommentAction(sess, opts, comment); err != nil {
return err
}
if err = issue.addCrossReferences(sess, doer, true); err != nil { if err = issue.addCrossReferences(sess, doer, true); err != nil {
return err return err
} }
@ -740,13 +746,18 @@ func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branc
if err := sess.Begin(); err != nil { if err := sess.Begin(); err != nil {
return err return err
} }
if _, err := createComment(sess, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: CommentTypeDeleteBranch, Type: CommentTypeDeleteBranch,
Doer: doer, Doer: doer,
Repo: repo, Repo: repo,
Issue: issue, Issue: issue,
CommitSHA: branchName, CommitSHA: branchName,
}); err != nil { }
comment, err := createCommentWithNoAction(sess, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(sess, opts, comment); err != nil {
return err return err
} }
@ -880,7 +891,20 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
return err return err
} }
if _, err = createMilestoneComment(e, doer, opts.Repo, opts.Issue, 0, opts.Issue.MilestoneID); err != nil { var opts = &CreateCommentOptions{
Type: CommentTypeMilestone,
Doer: doer,
Repo: opts.Repo,
Issue: opts.Issue,
OldMilestoneID: 0,
MilestoneID: opts.Issue.MilestoneID,
}
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err return err
} }
} }

View file

@ -131,18 +131,22 @@ func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID in
return false, nil, fmt.Errorf("loadRepo: %v", err) return false, nil, fmt.Errorf("loadRepo: %v", err)
} }
// Comment var opts = &CreateCommentOptions{
comment, err = createComment(sess, &CreateCommentOptions{
Type: CommentTypeAssignees, Type: CommentTypeAssignees,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
RemovedAssignee: removed, RemovedAssignee: removed,
AssigneeID: assigneeID, AssigneeID: assigneeID,
}) }
// Comment
comment, err = createCommentWithNoAction(sess, opts)
if err != nil { if err != nil {
return false, nil, fmt.Errorf("createComment: %v", err) return false, nil, fmt.Errorf("createComment: %v", err)
} }
if err = sendCreateCommentAction(sess, opts, comment); err != nil {
return false, nil, err
}
// if pull request is in the middle of creation - don't call webhook // if pull request is in the middle of creation - don't call webhook
if isCreate { if isCreate {

View file

@ -495,7 +495,7 @@ func (c *Comment) CodeCommentURL() string {
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) 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 var LabelID int64
if opts.Label != nil { if opts.Label != nil {
LabelID = opts.Label.ID LabelID = opts.Label.ID
@ -539,12 +539,6 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
return nil, 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 { if err = comment.addCrossReferences(e, opts.Doer, false); err != nil {
return nil, err return nil, err
} }
@ -651,19 +645,7 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
return nil 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) { func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
var content string var content string
var commentType CommentType var commentType CommentType
@ -685,13 +667,18 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin
return nil, err return nil, err
} }
return createComment(e, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: commentType, Type: commentType,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
Content: content, Content: content,
}) }
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return nil, err
}
return comment, sendCreateCommentAction(e, opts, comment)
} }
// Creates issue dependency 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 // Make two comments, one in each issue
_, err = createComment(e, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: cType, Type: cType,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
DependentIssueID: dependentIssue.ID, DependentIssueID: dependentIssue.ID,
}) }
comment, err := createCommentWithNoAction(e, opts)
if err != nil { if err != nil {
return return
} }
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err
}
_, err = createComment(e, &CreateCommentOptions{ opts = &CreateCommentOptions{
Type: cType, Type: cType,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: dependentIssue, Issue: dependentIssue,
DependentIssueID: issue.ID, DependentIssueID: issue.ID,
}) }
comment, err = createCommentWithNoAction(e, opts)
if err != nil { if err != nil {
return return
} }
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err
}
return return
} }
@ -758,7 +753,6 @@ type CreateCommentOptions struct {
RefCommentID int64 RefCommentID int64
RefAction references.XRefAction RefAction references.XRefAction
RefIsPull bool RefIsPull bool
NoAction bool
} }
// CreateComment creates comment of issue or commit. // CreateComment creates comment of issue or commit.
@ -769,7 +763,31 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -406,14 +406,19 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
return return
} }
if _, err = createComment(e, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: CommentTypeLabel, Type: CommentTypeLabel,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
Label: label, Label: label,
Content: "1", Content: "1",
}); err != nil { }
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err return err
} }
@ -482,13 +487,18 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
return return
} }
if _, err = createComment(e, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: CommentTypeLabel, Type: CommentTypeLabel,
Doer: doer, Doer: doer,
Repo: issue.Repo, Repo: issue.Repo,
Issue: issue, Issue: issue,
Label: label, Label: label,
}); err != nil { }
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err return err
} }

View file

@ -45,16 +45,21 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
return err return err
} }
_, err := createComment(sess, &CreateCommentOptions{ var opt = &CreateCommentOptions{
Doer: opts.Doer, Doer: opts.Doer,
Issue: opts.Issue, Issue: opts.Issue,
Repo: opts.Issue.Repo, Repo: opts.Issue.Repo,
Type: commentType, Type: commentType,
Content: opts.Reason, Content: opts.Reason,
}) }
comment, err := createCommentWithNoAction(sess, opt)
if err != nil { if err != nil {
return err return err
} }
if err = sendCreateCommentAction(sess, opt, comment); err != nil {
return err
}
return sess.Commit() return sess.Commit()
} }

View file

@ -386,7 +386,20 @@ func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilesto
return err return err
} }
if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil { var opts = &CreateCommentOptions{
Type: CommentTypeMilestone,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
OldMilestoneID: oldMilestoneID,
MilestoneID: issue.MilestoneID,
}
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err := sendCreateCommentAction(e, opts, comment); err != nil {
return err return err
} }
} }

View file

@ -116,7 +116,7 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
if ctx.OrigComment != nil { if ctx.OrigComment != nil {
refCommentID = ctx.OrigComment.ID refCommentID = ctx.OrigComment.ID
} }
if _, err := createComment(e, &CreateCommentOptions{ var opts = &CreateCommentOptions{
Type: ctx.Type, Type: ctx.Type,
Doer: ctx.Doer, Doer: ctx.Doer,
Repo: xref.Issue.Repo, Repo: xref.Issue.Repo,
@ -126,7 +126,12 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
RefCommentID: refCommentID, RefCommentID: refCommentID,
RefAction: xref.Action, RefAction: xref.Action,
RefIsPull: ctx.OrigIssue.IsPull, RefIsPull: ctx.OrigIssue.IsPull,
}); err != nil { }
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err return err
} }
} }

View file

@ -291,14 +291,13 @@ func SubmitReview(doer *User, issue *Issue, reviewType ReviewType, content strin
} }
} }
comm, err := createComment(sess, &CreateCommentOptions{ comm, err := createCommentWithNoAction(sess, &CreateCommentOptions{
Type: CommentTypeReview, Type: CommentTypeReview,
Doer: doer, Doer: doer,
Content: review.Content, Content: review.Content,
Issue: issue, Issue: issue,
Repo: issue.Repo, Repo: issue.Repo,
ReviewID: review.ID, ReviewID: review.ID,
NoAction: true,
}) })
if err != nil || comm == nil { if err != nil || comm == nil {
return nil, nil, err return nil, nil, err

View file

@ -143,7 +143,7 @@ func createCodeComment(doer *models.User, repo *models.Repository, issue *models
} }
patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines) patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
} }
return models.CreateComment(&models.CreateCommentOptions{ return models.CreateCommentWithNoAction(&models.CreateCommentOptions{
Type: models.CommentTypeCode, Type: models.CommentTypeCode,
Doer: doer, Doer: doer,
Repo: repo, Repo: repo,
@ -154,7 +154,6 @@ func createCodeComment(doer *models.User, repo *models.Repository, issue *models
CommitSHA: commitID, CommitSHA: commitID,
ReviewID: reviewID, ReviewID: reviewID,
Patch: patch, Patch: patch,
NoAction: true,
}) })
} }