0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-11 12:31:24 +01:00

extract some inline functions related with create comment (#8931)

This commit is contained in:
Lunny Xiao 2019-11-16 02:18:09 +08:00 committed by zeripath
parent c58fba944d
commit e0e4473172
5 changed files with 65 additions and 88 deletions

View file

@ -652,7 +652,16 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
} }
// New action comment // 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 return err
} }
@ -702,8 +711,15 @@ func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
return fmt.Errorf("loadRepo: %v", err) return fmt.Errorf("loadRepo: %v", err)
} }
if _, err = createChangeTitleComment(sess, doer, issue.Repo, issue, oldTitle, issue.Title); err != nil { if _, err = createComment(sess, &CreateCommentOptions{
return fmt.Errorf("createChangeTitleComment: %v", err) 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 { 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 { if err := sess.Begin(); err != nil {
return err 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 return err
} }

View file

@ -120,9 +120,16 @@ func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID in
} }
// Comment // 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 { 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 // if pull request is in the middle of creation - don't call webhook

View file

@ -651,34 +651,6 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
return nil 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) { func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
return createComment(e, &CreateCommentOptions{ return createComment(e, &CreateCommentOptions{
Type: CommentTypeMilestone, 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) { func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
var content string 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 // Creates issue dependency comment
func createIssueDependencyComment(e *xorm.Session, doer *User, issue *Issue, dependentIssue *Issue, add bool) (err error) { func createIssueDependencyComment(e *xorm.Session, doer *User, issue *Issue, dependentIssue *Issue, add bool) (err error) {
cType := CommentTypeAddDependency cType := CommentTypeAddDependency

View file

@ -402,7 +402,14 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
return 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 return err
} }
@ -471,7 +478,13 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
return 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 return err
} }

View file

@ -25,25 +25,6 @@ type crossReferencesContext struct {
OrigComment *Comment 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 { func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
active := make([]*Comment, 0, 10) active := make([]*Comment, 0, 10)
sess := e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens) 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 return err
} }
for _, xref := range xreflist { 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 return err
} }
} }