Skip to content

Commit

Permalink
move change issue title from models to issue service package
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Oct 11, 2019
1 parent 9ff9f5a commit 3de4b0e
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 99 deletions.
3 changes: 2 additions & 1 deletion integrations/api_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
issue_service "code.gitea.io/gitea/services/issue"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestAPIMergePullWIP(t *testing.T) {
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{Status: models.PullRequestStatusMergeable}, models.Cond("has_merged = ?", false)).(*models.PullRequest)
pr.LoadIssue()
pr.Issue.ChangeTitle(owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title)
issue_service.ChangeTitle(pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title)

// force reload
pr.LoadAttributes()
Expand Down
86 changes: 4 additions & 82 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() error {
return issue.loadRepo(x)
func (issue *Issue) LoadRepo(ctx DBContext) error {
return issue.loadRepo(ctx.e)
}

func (issue *Issue) loadRepo(e Engine) (err error) {
Expand Down Expand Up @@ -715,8 +715,8 @@ func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
}

// UpdateIssueCols only updates values of specific columns for given issue.
func UpdateIssueCols(issue *Issue, cols ...string) error {
return updateIssueCols(x, issue, cols...)
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) {
Expand Down Expand Up @@ -843,84 +843,6 @@ 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, title string) (err error) {
oldTitle := issue.Title
issue.Title = title
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, 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
}

if err = sess.Commit(); err != nil {
return err
}
sess.Close()

mode, _ := AccessLevel(issue.Poster, issue.Repo)
if issue.IsPull {
if err = issue.loadPullRequest(sess); err != nil {
return fmt.Errorf("loadPullRequest: %v", err)
}
issue.PullRequest.Issue = issue
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Title: &api.ChangesFromPayload{
From: oldTitle,
},
},
PullRequest: issue.PullRequest.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
})
} else {
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Title: &api.ChangesFromPayload{
From: oldTitle,
},
},
Issue: issue.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: issue.Poster.APIFormat(),
})
}

if err != nil {
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
} else {
go HookQueue.Add(issue.RepoID)
}

return nil
}

// 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: 5 additions & 4 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 *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
func createComment(e Engine, 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 *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
return comment, nil
}

func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
func sendCreateCommentAction(e Engine, 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,8 +690,9 @@ 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{
// 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{
Type: CommentTypeChangeTitle,
Doer: doer,
Repo: repo,
Expand Down
2 changes: 1 addition & 1 deletion models/issue_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
commentType = CommentTypeUnlock
}

if err := UpdateIssueCols(opts.Issue, "is_locked"); err != nil {
if err := UpdateIssueCols(DefaultDBContext(), opts.Issue, "is_locked"); err != nil {
return err
}

Expand Down
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(issue, "name"))
assert.NoError(t, UpdateIssueCols(DefaultDBContext(), issue, "name"))
then := time.Now().Unix()

updatedIssue := AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue)
Expand Down
21 changes: 15 additions & 6 deletions models/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

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

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

Expand Down Expand Up @@ -51,7 +50,7 @@ type crossReferencesContext struct {
OrigComment *Comment
}

func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
func newCrossReference(e Engine, ctx *crossReferencesContext, xref *crossReference) error {
var refCommentID int64
if ctx.OrigComment != nil {
refCommentID = ctx.OrigComment.ID
Expand Down Expand Up @@ -98,7 +97,12 @@ func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
// \/ \/ \/
//

func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) 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 {
var commentType CommentType
if issue.IsPull {
commentType = CommentTypePullRef
Expand All @@ -113,7 +117,7 @@ func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
}

func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, content string) error {
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
if err != nil {
return err
Expand All @@ -126,7 +130,7 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
return nil
}

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

Expand Down Expand Up @@ -212,6 +216,11 @@ 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 @@ -224,7 +233,7 @@ func (issue *Issue) neuterCrossReferences(e Engine) error {
// \/ \/ \/ \/ \/
//

func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
func (comment *Comment) addCrossReferences(e Engine, doer *User) error {
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions 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(); err != nil {
if err = otherIssue.LoadRepo(models.DefaultDBContext()); err != nil {
ctx.ServerError("LoadRepo", err)
return
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func UpdateIssueTitle(ctx *context.Context) {
}

oldTitle := issue.Title
if err := issue.ChangeTitle(ctx.User, title); err != nil {
if err := issue_service.ChangeTitle(issue, ctx.User, title); err != nil {
ctx.ServerError("ChangeTitle", err)
return
}
Expand Down
74 changes: 74 additions & 0 deletions services/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,77 @@ func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, as

return nil
}

// ChangeTitle changes the title of this issue, as the given user.
func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err error) {
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 {
return
}

mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
if issue.IsPull {
if err = issue.LoadPullRequest(); err != nil {
return fmt.Errorf("loadPullRequest: %v", err)
}
issue.PullRequest.Issue = issue
err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Title: &api.ChangesFromPayload{
From: oldTitle,
},
},
PullRequest: issue.PullRequest.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
})
} else {
err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Title: &api.ChangesFromPayload{
From: oldTitle,
},
},
Issue: issue.APIFormat(),
Repository: issue.Repo.APIFormat(mode),
Sender: issue.Poster.APIFormat(),
})
}

if err != nil {
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
} else {
go models.HookQueue.Add(issue.RepoID)
}

return nil
}
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()
err := issue.LoadRepo(models.DefaultDBContext())
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(); err != nil {
if err := issue.LoadRepo(models.DefaultDBContext()); err != nil {
return err
}

Expand Down

0 comments on commit 3de4b0e

Please sign in to comment.