]> source.dussan.org Git - gitea.git/commitdiff
extract some inline functions related with create comment (#8931)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 15 Nov 2019 18:18:09 +0000 (02:18 +0800)
committerzeripath <art27@cantab.net>
Fri, 15 Nov 2019 18:18:09 +0000 (18:18 +0000)
models/issue.go
models/issue_assignees.go
models/issue_comment.go
models/issue_label.go
models/issue_xref.go

index 389304853380f4751fdfa824228b92f672b1f67f..1fe9140dadd19ccf221b4c7db1dd7e0ec18d25f7 100644 (file)
@@ -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
        }
 
index 70bed039c2e7d52632dff2ff162e7cd7eff9ce9e..4d71cc6e512b911f9f0665ae350598d5526cc55a 100644 (file)
@@ -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
index 37d4d8c11567dd76a7438d47db775a8723bab0e0..7ac8c4df4383a8507441d880ceb3ba6920781e89 100644 (file)
@@ -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
index 497756af5bb567fe9158ba737d35b9aa5127f429..77281e9bd4a659fad6fc4b534672887a69c0e952 100644 (file)
@@ -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
        }
 
index 4b01022bc5753e469d8df52a28ba7f9c19cdc4da..787a0e5119a50e313208f5eb59769d9bda349e85 100644 (file)
@@ -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
                }
        }