Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract createComment #9125

Merged
merged 5 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,16 +656,18 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (er
if !issue.IsClosed {
cmtType = CommentTypeReopen
}
if _, err := createComment(e, &CreateCommentOptions{

var opts = &CreateCommentOptions{
Type: cmtType,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
}); err != nil {
}
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}

return nil
return sendCreateCommentAction(e, opts, comment)
}

// ChangeStatus changes issue status to open or closed.
Expand Down Expand Up @@ -711,17 +713,21 @@ func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
return fmt.Errorf("loadRepo: %v", err)
}

if _, err = createComment(sess, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: CommentTypeChangeTitle,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
OldTitle: oldTitle,
NewTitle: issue.Title,
}); err != nil {
}
comment, err := createCommentWithNoAction(sess, opts)
if err != nil {
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 {
return err
}
Expand All @@ -740,13 +746,18 @@ func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branc
if err := sess.Begin(); err != nil {
return err
}
if _, err := createComment(sess, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: CommentTypeDeleteBranch,
Doer: doer,
Repo: repo,
Issue: issue,
CommitSHA: branchName,
}); err != nil {
}
comment, err := createCommentWithNoAction(sess, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(sess, opts, comment); err != nil {
return err
}

Expand Down Expand Up @@ -880,7 +891,20 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
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
}
}
Expand Down
10 changes: 7 additions & 3 deletions models/issue_assignees.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,22 @@ func (issue *Issue) toggleAssignee(sess *xorm.Session, doer *User, assigneeID in
return false, nil, fmt.Errorf("loadRepo: %v", err)
}

// Comment
comment, err = createComment(sess, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: CommentTypeAssignees,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
RemovedAssignee: removed,
AssigneeID: assigneeID,
})
}
// Comment
comment, err = createCommentWithNoAction(sess, opts)
if err != nil {
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 isCreate {
Expand Down
72 changes: 45 additions & 27 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -758,7 +753,6 @@ type CreateCommentOptions struct {
RefCommentID int64
RefAction references.XRefAction
RefIsPull bool
NoAction bool
}

// CreateComment creates comment of issue or commit.
Expand All @@ -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
}
Expand Down
18 changes: 14 additions & 4 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,19 @@ func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err
return
}

if _, err = createComment(e, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: CommentTypeLabel,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
Label: label,
Content: "1",
}); err != nil {
}
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err
}

Expand Down Expand Up @@ -478,13 +483,18 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (
return
}

if _, err = createComment(e, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: CommentTypeLabel,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
Label: label,
}); err != nil {
}
comment, err := createCommentWithNoAction(e, opts)
if err != nil {
return err
}
if err = sendCreateCommentAction(e, opts, comment); err != nil {
return err
}

Expand Down
9 changes: 7 additions & 2 deletions models/issue_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
return err
}

_, err := createComment(sess, &CreateCommentOptions{
var opt = &CreateCommentOptions{
Doer: opts.Doer,
Issue: opts.Issue,
Repo: opts.Issue.Repo,
Type: commentType,
Content: opts.Reason,
})
}
comment, err := createCommentWithNoAction(sess, opt)
if err != nil {
return err
}

if err = sendCreateCommentAction(sess, opt, comment); err != nil {
return err
}

return sess.Commit()
}
15 changes: 14 additions & 1 deletion models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,20 @@ func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilesto
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
}
}
Expand Down
9 changes: 7 additions & 2 deletions models/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
if ctx.OrigComment != nil {
refCommentID = ctx.OrigComment.ID
}
if _, err := createComment(e, &CreateCommentOptions{
var opts = &CreateCommentOptions{
Type: ctx.Type,
Doer: ctx.Doer,
Repo: xref.Issue.Repo,
Expand All @@ -126,7 +126,12 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
RefCommentID: refCommentID,
RefAction: xref.Action,
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
}
}
Expand Down
3 changes: 1 addition & 2 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,13 @@ func SubmitReview(doer *User, issue *Issue, reviewType ReviewType, content strin
}
}

comm, err := createComment(sess, &CreateCommentOptions{
comm, err := createCommentWithNoAction(sess, &CreateCommentOptions{
Type: CommentTypeReview,
Doer: doer,
Content: review.Content,
Issue: issue,
Repo: issue.Repo,
ReviewID: review.ID,
NoAction: true,
})
if err != nil || comm == nil {
return nil, nil, err
Expand Down
Loading