Skip to content

Commit

Permalink
make the change less
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Oct 11, 2019
1 parent 3de4b0e commit b65034d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 59 deletions.
41 changes: 34 additions & 7 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions models/issue_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,33 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
}

opts.Issue.IsLocked = lock

var commentType CommentType
if opts.Issue.IsLocked {
commentType = CommentTypeLock
} else {
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()
}
2 changes: 1 addition & 1 deletion models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 6 additions & 15 deletions models/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

"github.com/go-xorm/xorm"
"github.com/unknwon/com"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 1 addition & 24 deletions services/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion services/mailer/mail_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit b65034d

Please sign in to comment.