diff --git a/models/issue.go b/models/issue.go index 2345e6f672de..3fcdab6164c6 100644 --- a/models/issue.go +++ b/models/issue.go @@ -95,8 +95,8 @@ func (issue *Issue) IsOverdue() bool { } // LoadRepo loads issue's repository -func (issue *Issue) LoadRepo(ctx DBContext) error { - return issue.loadRepo(ctx.e) +func (issue *Issue) LoadRepo() error { + return issue.loadRepo(x) } func (issue *Issue) loadRepo(e Engine) (err error) { @@ -714,11 +714,6 @@ func updateIssueCols(e Engine, issue *Issue, cols ...string) error { return nil } -// UpdateIssueCols only updates values of specific columns for given issue. -func UpdateIssueCols(ctx DBContext, issue *Issue, cols ...string) error { - return updateIssueCols(ctx.e, issue, cols...) -} - func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (err error) { // Reload the issue currentIssue, err := getIssueByID(e, issue.ID) @@ -843,6 +838,38 @@ func (issue *Issue) ChangeStatus(doer *User, isClosed bool) (err error) { return nil } +// ChangeTitle changes the title of this issue, as the given user. +func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) { + sess := x.NewSession() + defer sess.Close() + + if err = sess.Begin(); err != nil { + return err + } + + if err = updateIssueCols(sess, issue, "name"); err != nil { + return fmt.Errorf("updateIssueCols: %v", err) + } + + if err = issue.loadRepo(sess); err != nil { + 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 = issue.neuterCrossReferences(sess); err != nil { + return err + } + + if err = issue.addCrossReferences(sess, doer); err != nil { + return err + } + + return sess.Commit() +} + // AddDeletePRBranchComment adds delete branch comment for pull request issue func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branchName string) error { issue, err := getIssueByID(x, issueID) diff --git a/models/issue_comment.go b/models/issue_comment.go index 8e2fa43f86c1..e8043c1ec7f3 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -468,7 +468,7 @@ func (c *Comment) CodeCommentURL() string { return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag()) } -func createComment(e Engine, opts *CreateCommentOptions) (_ *Comment, err error) { +func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) { var LabelID int64 if opts.Label != nil { LabelID = opts.Label.ID @@ -519,7 +519,7 @@ func createComment(e Engine, opts *CreateCommentOptions) (_ *Comment, err error) return comment, nil } -func sendCreateCommentAction(e Engine, opts *CreateCommentOptions, comment *Comment) (err error) { +func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) { // Compose comment action, could be plain comment, close or reopen issue/pull request. // This object will be used to notify watchers in the end of function. act := &Action{ @@ -690,9 +690,8 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin }) } -// CreateChangeTitleComment created a change title comment for issue -func CreateChangeTitleComment(ctx DBContext, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) { - return createComment(ctx.e, &CreateCommentOptions{ +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, diff --git a/models/issue_lock.go b/models/issue_lock.go index b927bab093c1..dc6655ad3b51 100644 --- a/models/issue_lock.go +++ b/models/issue_lock.go @@ -28,7 +28,6 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error { } opts.Issue.IsLocked = lock - var commentType CommentType if opts.Issue.IsLocked { commentType = CommentTypeLock @@ -36,16 +35,26 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error { commentType = CommentTypeUnlock } - if err := UpdateIssueCols(DefaultDBContext(), opts.Issue, "is_locked"); err != nil { + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + if err := updateIssueCols(sess, opts.Issue, "is_locked"); err != nil { return err } - _, err := CreateComment(&CreateCommentOptions{ + _, err := createComment(sess, &CreateCommentOptions{ Doer: opts.Doer, Issue: opts.Issue, Repo: opts.Issue.Repo, Type: commentType, Content: opts.Reason, }) - return err + if err != nil { + return err + } + + return sess.Commit() } diff --git a/models/issue_test.go b/models/issue_test.go index fe5af5c190a8..0be3f6880825 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -160,7 +160,7 @@ func TestUpdateIssueCols(t *testing.T) { issue.Content = "This should have no effect" now := time.Now().Unix() - assert.NoError(t, UpdateIssueCols(DefaultDBContext(), issue, "name")) + assert.NoError(t, updateIssueCols(x, issue, "name")) then := time.Now().Unix() updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) diff --git a/models/issue_xref.go b/models/issue_xref.go index 3f02741bef3d..1cc0bcfe6a11 100644 --- a/models/issue_xref.go +++ b/models/issue_xref.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/modules/log" + "github.com/go-xorm/xorm" "github.com/unknwon/com" ) @@ -50,7 +51,7 @@ type crossReferencesContext struct { OrigComment *Comment } -func newCrossReference(e Engine, ctx *crossReferencesContext, xref *crossReference) error { +func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error { var refCommentID int64 if ctx.OrigComment != nil { refCommentID = ctx.OrigComment.ID @@ -97,12 +98,7 @@ func neuterCrossReferences(e Engine, issueID int64, commentID int64) error { // \/ \/ \/ // -// AddCrossReferences adds issues cross references -func (issue *Issue) AddCrossReferences(ctx DBContext, doer *User) error { - return issue.addCrossReferences(ctx.e, doer) -} - -func (issue *Issue) addCrossReferences(e Engine, doer *User) error { +func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error { var commentType CommentType if issue.IsPull { commentType = CommentTypePullRef @@ -117,7 +113,7 @@ func (issue *Issue) addCrossReferences(e Engine, doer *User) error { return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content) } -func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, content string) error { +func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error { xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content) if err != nil { return err @@ -130,7 +126,7 @@ func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, return nil } -func (issue *Issue) getCrossReferences(e Engine, ctx *crossReferencesContext, content string) ([]*crossReference, error) { +func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) ([]*crossReference, error) { xreflist := make([]*crossReference, 0, 5) var xref *crossReference @@ -216,11 +212,6 @@ func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContex }, nil } -// NeuterCrossReferences updated issues' cross references -func (issue *Issue) NeuterCrossReferences(ctx DBContext) error { - return issue.neuterCrossReferences(ctx.e) -} - func (issue *Issue) neuterCrossReferences(e Engine) error { return neuterCrossReferences(e, issue.ID, 0) } @@ -233,7 +224,7 @@ func (issue *Issue) neuterCrossReferences(e Engine) error { // \/ \/ \/ \/ \/ // -func (comment *Comment) addCrossReferences(e Engine, doer *User) error { +func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error { if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment { return nil } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index bb11bb4719ea..16a049c7aa6d 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -757,7 +757,7 @@ func ViewIssue(ctx *context.Context) { ctx.ServerError("GetIssueByID", err) return } - if err = otherIssue.LoadRepo(models.DefaultDBContext()); err != nil { + if err = otherIssue.LoadRepo(); err != nil { ctx.ServerError("LoadRepo", err) return } diff --git a/services/issue/issue.go b/services/issue/issue.go index 6efe616c3e2b..a28916a7f9b5 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -51,30 +51,7 @@ func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err erro oldTitle := issue.Title issue.Title = title - err = models.WithTx(func(ctx models.DBContext) error { - if err = models.UpdateIssueCols(ctx, issue, "name"); err != nil { - return fmt.Errorf("updateIssueCols: %v", err) - } - - if err = issue.LoadRepo(ctx); err != nil { - return fmt.Errorf("loadRepo: %v", err) - } - - if _, err = models.CreateChangeTitleComment(ctx, doer, issue.Repo, issue, oldTitle, title); err != nil { - return fmt.Errorf("CreateChangeTitleComment: %v", err) - } - - if err = issue.NeuterCrossReferences(ctx); err != nil { - return err - } - - if err = issue.AddCrossReferences(ctx, doer); err != nil { - return err - } - return nil - }) - - if err != nil { + if err = issue.ChangeTitle(doer, oldTitle); err != nil { return } diff --git a/services/mailer/mail.go b/services/mailer/mail.go index cdb95e9c47fa..3f0a789dc4cd 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -170,7 +170,7 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content } else { subject = mailSubject(issue) } - err := issue.LoadRepo(models.DefaultDBContext()) + err := issue.LoadRepo() if err != nil { log.Error("LoadRepo: %v", err) } diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index bdbbddc12b0a..da0249d595da 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(issue *models.Issue, doer *models.User, cont names = append(names, participants[i].Name) } - if err := issue.LoadRepo(models.DefaultDBContext()); err != nil { + if err := issue.LoadRepo(); err != nil { return err }