Skip to content

Commit b65034d

Browse files
committed
make the change less
1 parent 3de4b0e commit b65034d

File tree

9 files changed

+62
-59
lines changed

9 files changed

+62
-59
lines changed

models/issue.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func (issue *Issue) IsOverdue() bool {
9595
}
9696

9797
// LoadRepo loads issue's repository
98-
func (issue *Issue) LoadRepo(ctx DBContext) error {
99-
return issue.loadRepo(ctx.e)
98+
func (issue *Issue) LoadRepo() error {
99+
return issue.loadRepo(x)
100100
}
101101

102102
func (issue *Issue) loadRepo(e Engine) (err error) {
@@ -714,11 +714,6 @@ func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
714714
return nil
715715
}
716716

717-
// UpdateIssueCols only updates values of specific columns for given issue.
718-
func UpdateIssueCols(ctx DBContext, issue *Issue, cols ...string) error {
719-
return updateIssueCols(ctx.e, issue, cols...)
720-
}
721-
722717
func (issue *Issue) changeStatus(e *xorm.Session, doer *User, isClosed bool) (err error) {
723718
// Reload the issue
724719
currentIssue, err := getIssueByID(e, issue.ID)
@@ -843,6 +838,38 @@ func (issue *Issue) ChangeStatus(doer *User, isClosed bool) (err error) {
843838
return nil
844839
}
845840

841+
// ChangeTitle changes the title of this issue, as the given user.
842+
func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
843+
sess := x.NewSession()
844+
defer sess.Close()
845+
846+
if err = sess.Begin(); err != nil {
847+
return err
848+
}
849+
850+
if err = updateIssueCols(sess, issue, "name"); err != nil {
851+
return fmt.Errorf("updateIssueCols: %v", err)
852+
}
853+
854+
if err = issue.loadRepo(sess); err != nil {
855+
return fmt.Errorf("loadRepo: %v", err)
856+
}
857+
858+
if _, err = createChangeTitleComment(sess, doer, issue.Repo, issue, oldTitle, issue.Title); err != nil {
859+
return fmt.Errorf("CreateChangeTitleComment: %v", err)
860+
}
861+
862+
if err = issue.neuterCrossReferences(sess); err != nil {
863+
return err
864+
}
865+
866+
if err = issue.addCrossReferences(sess, doer); err != nil {
867+
return err
868+
}
869+
870+
return sess.Commit()
871+
}
872+
846873
// AddDeletePRBranchComment adds delete branch comment for pull request issue
847874
func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branchName string) error {
848875
issue, err := getIssueByID(x, issueID)

models/issue_comment.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func (c *Comment) CodeCommentURL() string {
468468
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
469469
}
470470

471-
func createComment(e Engine, opts *CreateCommentOptions) (_ *Comment, err error) {
471+
func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
472472
var LabelID int64
473473
if opts.Label != nil {
474474
LabelID = opts.Label.ID
@@ -519,7 +519,7 @@ func createComment(e Engine, opts *CreateCommentOptions) (_ *Comment, err error)
519519
return comment, nil
520520
}
521521

522-
func sendCreateCommentAction(e Engine, opts *CreateCommentOptions, comment *Comment) (err error) {
522+
func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
523523
// Compose comment action, could be plain comment, close or reopen issue/pull request.
524524
// This object will be used to notify watchers in the end of function.
525525
act := &Action{
@@ -690,9 +690,8 @@ func createDeadlineComment(e *xorm.Session, doer *User, issue *Issue, newDeadlin
690690
})
691691
}
692692

693-
// CreateChangeTitleComment created a change title comment for issue
694-
func CreateChangeTitleComment(ctx DBContext, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
695-
return createComment(ctx.e, &CreateCommentOptions{
693+
func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
694+
return createComment(e, &CreateCommentOptions{
696695
Type: CommentTypeChangeTitle,
697696
Doer: doer,
698697
Repo: repo,

models/issue_lock.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,33 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
2828
}
2929

3030
opts.Issue.IsLocked = lock
31-
3231
var commentType CommentType
3332
if opts.Issue.IsLocked {
3433
commentType = CommentTypeLock
3534
} else {
3635
commentType = CommentTypeUnlock
3736
}
3837

39-
if err := UpdateIssueCols(DefaultDBContext(), opts.Issue, "is_locked"); err != nil {
38+
sess := x.NewSession()
39+
defer sess.Close()
40+
if err := sess.Begin(); err != nil {
41+
return err
42+
}
43+
44+
if err := updateIssueCols(sess, opts.Issue, "is_locked"); err != nil {
4045
return err
4146
}
4247

43-
_, err := CreateComment(&CreateCommentOptions{
48+
_, err := createComment(sess, &CreateCommentOptions{
4449
Doer: opts.Doer,
4550
Issue: opts.Issue,
4651
Repo: opts.Issue.Repo,
4752
Type: commentType,
4853
Content: opts.Reason,
4954
})
50-
return err
55+
if err != nil {
56+
return err
57+
}
58+
59+
return sess.Commit()
5160
}

models/issue_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestUpdateIssueCols(t *testing.T) {
160160
issue.Content = "This should have no effect"
161161

162162
now := time.Now().Unix()
163-
assert.NoError(t, UpdateIssueCols(DefaultDBContext(), issue, "name"))
163+
assert.NoError(t, updateIssueCols(x, issue, "name"))
164164
then := time.Now().Unix()
165165

166166
updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)

models/issue_xref.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/modules/log"
1212

13+
"github.com/go-xorm/xorm"
1314
"github.com/unknwon/com"
1415
)
1516

@@ -50,7 +51,7 @@ type crossReferencesContext struct {
5051
OrigComment *Comment
5152
}
5253

53-
func newCrossReference(e Engine, ctx *crossReferencesContext, xref *crossReference) error {
54+
func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
5455
var refCommentID int64
5556
if ctx.OrigComment != nil {
5657
refCommentID = ctx.OrigComment.ID
@@ -97,12 +98,7 @@ func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
9798
// \/ \/ \/
9899
//
99100

100-
// AddCrossReferences adds issues cross references
101-
func (issue *Issue) AddCrossReferences(ctx DBContext, doer *User) error {
102-
return issue.addCrossReferences(ctx.e, doer)
103-
}
104-
105-
func (issue *Issue) addCrossReferences(e Engine, doer *User) error {
101+
func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
106102
var commentType CommentType
107103
if issue.IsPull {
108104
commentType = CommentTypePullRef
@@ -117,7 +113,7 @@ func (issue *Issue) addCrossReferences(e Engine, doer *User) error {
117113
return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
118114
}
119115

120-
func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, content string) error {
116+
func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
121117
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
122118
if err != nil {
123119
return err
@@ -130,7 +126,7 @@ func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext,
130126
return nil
131127
}
132128

133-
func (issue *Issue) getCrossReferences(e Engine, ctx *crossReferencesContext, content string) ([]*crossReference, error) {
129+
func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) ([]*crossReference, error) {
134130
xreflist := make([]*crossReference, 0, 5)
135131
var xref *crossReference
136132

@@ -216,11 +212,6 @@ func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContex
216212
}, nil
217213
}
218214

219-
// NeuterCrossReferences updated issues' cross references
220-
func (issue *Issue) NeuterCrossReferences(ctx DBContext) error {
221-
return issue.neuterCrossReferences(ctx.e)
222-
}
223-
224215
func (issue *Issue) neuterCrossReferences(e Engine) error {
225216
return neuterCrossReferences(e, issue.ID, 0)
226217
}
@@ -233,7 +224,7 @@ func (issue *Issue) neuterCrossReferences(e Engine) error {
233224
// \/ \/ \/ \/ \/
234225
//
235226

236-
func (comment *Comment) addCrossReferences(e Engine, doer *User) error {
227+
func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
237228
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
238229
return nil
239230
}

routers/repo/issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ func ViewIssue(ctx *context.Context) {
757757
ctx.ServerError("GetIssueByID", err)
758758
return
759759
}
760-
if err = otherIssue.LoadRepo(models.DefaultDBContext()); err != nil {
760+
if err = otherIssue.LoadRepo(); err != nil {
761761
ctx.ServerError("LoadRepo", err)
762762
return
763763
}

services/issue/issue.go

+1-24
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,7 @@ func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err erro
5151
oldTitle := issue.Title
5252
issue.Title = title
5353

54-
err = models.WithTx(func(ctx models.DBContext) error {
55-
if err = models.UpdateIssueCols(ctx, issue, "name"); err != nil {
56-
return fmt.Errorf("updateIssueCols: %v", err)
57-
}
58-
59-
if err = issue.LoadRepo(ctx); err != nil {
60-
return fmt.Errorf("loadRepo: %v", err)
61-
}
62-
63-
if _, err = models.CreateChangeTitleComment(ctx, doer, issue.Repo, issue, oldTitle, title); err != nil {
64-
return fmt.Errorf("CreateChangeTitleComment: %v", err)
65-
}
66-
67-
if err = issue.NeuterCrossReferences(ctx); err != nil {
68-
return err
69-
}
70-
71-
if err = issue.AddCrossReferences(ctx, doer); err != nil {
72-
return err
73-
}
74-
return nil
75-
})
76-
77-
if err != nil {
54+
if err = issue.ChangeTitle(doer, oldTitle); err != nil {
7855
return
7956
}
8057

services/mailer/mail.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func composeIssueCommentMessage(issue *models.Issue, doer *models.User, content
170170
} else {
171171
subject = mailSubject(issue)
172172
}
173-
err := issue.LoadRepo(models.DefaultDBContext())
173+
err := issue.LoadRepo()
174174
if err != nil {
175175
log.Error("LoadRepo: %v", err)
176176
}

services/mailer/mail_issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(issue *models.Issue, doer *models.User, cont
8888
names = append(names, participants[i].Name)
8989
}
9090

91-
if err := issue.LoadRepo(models.DefaultDBContext()); err != nil {
91+
if err := issue.LoadRepo(); err != nil {
9292
return err
9393
}
9494

0 commit comments

Comments
 (0)