From e0e44731723332ac49bb22e6d6d84ae4f92be067 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 16 Nov 2019 02:18:09 +0800 Subject: [PATCH] extract some inline functions related with create comment (#8931) --- models/issue.go | 30 +++++++++++++++++--- models/issue_assignees.go | 11 +++++-- models/issue_comment.go | 60 --------------------------------------- models/issue_label.go | 17 +++++++++-- models/issue_xref.go | 35 ++++++++++------------- 5 files changed, 65 insertions(+), 88 deletions(-) diff --git a/models/issue.go b/models/issue.go index 3893048533..1fe9140dad 100644 --- a/models/issue.go +++ b/models/issue.go @@ -652,7 +652,16 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er } // New action comment - if _, err = createStatusComment(e, doer, issue); err != nil { + cmtType := CommentTypeClose + if !issue.IsClosed { + cmtType = CommentTypeReopen + } + if _, err := createComment(e, &CreateCommentOptions{ + Type: cmtType, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + }); err != nil { return err } @@ -702,8 +711,15 @@ func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) { return fmt.Errorf("loadRepo: %v", err) } - if _, err = createChangeTitleComment(sess, doer, issue.Repo, issue, oldTitle, issue.Title); err != nil { - return fmt.Errorf("createChangeTitleComment: %v", err) + if _, err = createComment(sess, &CreateCommentOptions{ + Type: CommentTypeChangeTitle, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + OldTitle: oldTitle, + NewTitle: issue.Title, + }); err != nil { + return fmt.Errorf("createComment: %v", err) } if err = issue.neuterCrossReferences(sess); err != nil { @@ -728,7 +744,13 @@ func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branc if err := sess.Begin(); err != nil { return err } - if _, err := createDeleteBranchComment(sess, doer, repo, issue, branchName); err != nil { + if _, err := createComment(sess, &CreateCommentOptions{ + Type: CommentTypeDeleteBranch, + Doer: doer, + Repo: repo, + Issue: issue, + CommitSHA: branchName, + }); err != nil { return err } diff --git a/models/issue_assignees.go b/models/issue_assignees.go index 70bed039c2..4d71cc6e51 100644 --- a/models/issue_assignees.go +++ b/models/issue_assignees.go @@ -120,9 +120,16 @@ func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID in } // Comment - comment, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed) + comment, err = createComment(sess, &CreateCommentOptions{ + Type: CommentTypeAssignees, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + RemovedAssignee: removed, + AssigneeID: assigneeID, + }) if err != nil { - return false, nil, fmt.Errorf("createAssigneeComment: %v", err) + return false, nil, fmt.Errorf("createComment: %v", err) } // if pull request is in the middle of creation - don't call webhook diff --git a/models/issue_comment.go b/models/issue_comment.go index 37d4d8c115..7ac8c4df43 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -651,34 +651,6 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen return nil } -func createStatusComment(e *xorm.Session, doer *User, issue *Issue) (*Comment, error) { - cmtType := CommentTypeClose - if !issue.IsClosed { - cmtType = CommentTypeReopen - } - return createComment(e, &CreateCommentOptions{ - Type: cmtType, - Doer: doer, - Repo: issue.Repo, - Issue: issue, - }) -} - -func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, label *Label, add bool) (*Comment, error) { - var content string - if add { - content = "1" - } - return createComment(e, &CreateCommentOptions{ - Type: CommentTypeLabel, - Doer: doer, - Repo: repo, - Issue: issue, - Label: label, - Content: content, - }) -} - func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) { return createComment(e, &CreateCommentOptions{ Type: CommentTypeMilestone, @@ -690,17 +662,6 @@ func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue }) } -func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, assigneeID int64, removedAssignee bool) (*Comment, error) { - return createComment(e, &CreateCommentOptions{ - Type: CommentTypeAssignees, - Doer: doer, - Repo: repo, - Issue: issue, - RemovedAssignee: removedAssignee, - AssigneeID: assigneeID, - }) -} - func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) { var content string @@ -733,27 +694,6 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin }) } -func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) { - return createComment(e, &CreateCommentOptions{ - Type: CommentTypeChangeTitle, - Doer: doer, - Repo: repo, - Issue: issue, - OldTitle: oldTitle, - NewTitle: newTitle, - }) -} - -func createDeleteBranchComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, branchName string) (*Comment, error) { - return createComment(e, &CreateCommentOptions{ - Type: CommentTypeDeleteBranch, - Doer: doer, - Repo: repo, - Issue: issue, - CommitSHA: branchName, - }) -} - // Creates issue dependency comment func createIssueDependencyComment(e *xorm.Session, doer *User, issue *Issue, dependentIssue *Issue, add bool) (err error) { cType := CommentTypeAddDependency diff --git a/models/issue_label.go b/models/issue_label.go index 497756af5b..77281e9bd4 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -402,7 +402,14 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err return } - if _, err = createLabelComment(e, doer, issue.Repo, issue, label, true); err != nil { + if _, err = createComment(e, &CreateCommentOptions{ + Type: CommentTypeLabel, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + Label: label, + Content: "1", + }); err != nil { return err } @@ -471,7 +478,13 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) ( return } - if _, err = createLabelComment(e, doer, issue.Repo, issue, label, false); err != nil { + if _, err = createComment(e, &CreateCommentOptions{ + Type: CommentTypeLabel, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + Label: label, + }); err != nil { return err } diff --git a/models/issue_xref.go b/models/issue_xref.go index 4b01022bc5..787a0e5119 100644 --- a/models/issue_xref.go +++ b/models/issue_xref.go @@ -25,25 +25,6 @@ type crossReferencesContext struct { OrigComment *Comment } -func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error { - var refCommentID int64 - if ctx.OrigComment != nil { - refCommentID = ctx.OrigComment.ID - } - _, err := createComment(e, &CreateCommentOptions{ - Type: ctx.Type, - Doer: ctx.Doer, - Repo: xref.Issue.Repo, - Issue: xref.Issue, - RefRepoID: ctx.OrigIssue.RepoID, - RefIssueID: ctx.OrigIssue.ID, - RefCommentID: refCommentID, - RefAction: xref.Action, - RefIsPull: xref.Issue.IsPull, - }) - return err -} - func neuterCrossReferences(e Engine, issueID int64, commentID int64) error { active := make([]*Comment, 0, 10) sess := e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens) @@ -93,7 +74,21 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC return err } for _, xref := range xreflist { - if err = newCrossReference(e, ctx, xref); err != nil { + var refCommentID int64 + if ctx.OrigComment != nil { + refCommentID = ctx.OrigComment.ID + } + if _, err := createComment(e, &CreateCommentOptions{ + Type: ctx.Type, + Doer: ctx.Doer, + Repo: xref.Issue.Repo, + Issue: xref.Issue, + RefRepoID: ctx.OrigIssue.RepoID, + RefIssueID: ctx.OrigIssue.ID, + RefCommentID: refCommentID, + RefAction: xref.Action, + RefIsPull: xref.Issue.IsPull, + }); err != nil { return err } } -- 2.39.5