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

Move patch related codes from models to pull service #9277

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions models/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ func WithTx(f func(ctx DBContext) error) error {
sess.Close()
return err
}

// Insert inserts an object to database
func Insert(ctx DBContext, bean interface{}) error {
_, err := ctx.e.Insert(bean)
return err
}
53 changes: 8 additions & 45 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ var (

const issueTasksRegexpStr = `(^\s*[-*]\s\[[\sx]\]\s.)|(\n\s*[-*]\s\[[\sx]\]\s.)`
const issueTasksDoneRegexpStr = `(^\s*[-*]\s\[[x]\]\s.)|(\n\s*[-*]\s\[[x]\]\s.)`
const issueMaxDupIndexAttempts = 3

func init() {
issueTasksPat = regexp.MustCompile(issueTasksRegexpStr)
Expand Down Expand Up @@ -447,7 +446,7 @@ func (issue *Issue) ReplyReference() string {
return fmt.Sprintf("%s/%s/%d@%s", issue.Repo.FullName(), path, issue.Index, setting.Domain)
}

func (issue *Issue) addLabel(e *xorm.Session, label *Label, doer *User) error {
func (issue *Issue) addLabel(e Engine, label *Label, doer *User) error {
return newIssueLabel(e, issue, label, doer)
}

Expand Down Expand Up @@ -843,10 +842,9 @@ type NewIssueOptions struct {
Issue *Issue
LabelIDs []int64
Attachments []string // In UUID format.
IsPull bool
}

func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
func newIssue(e Engine, doer *User, opts NewIssueOptions) (err error) {
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)

if opts.Issue.MilestoneID > 0 {
Expand All @@ -864,8 +862,8 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
}

// Milestone validation should happen before insert actual object.
if _, err := e.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
Where("repo_id=?", opts.Issue.RepoID).
if _, err := e.Where("repo_id=?", opts.Issue.RepoID).
SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
Insert(opts.Issue); err != nil {
return ErrNewIssueInsert{err}
}
Expand Down Expand Up @@ -896,7 +894,7 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
}
}

if opts.IsPull {
if opts.Issue.IsPull {
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
} else {
_, err = e.Exec("UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?", opts.Issue.RepoID)
Expand Down Expand Up @@ -953,48 +951,13 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
}

// NewIssue creates new issue with labels for repository.
func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
i := 0
for {
if err = newIssueAttempt(repo, issue, labelIDs, uuids); err == nil {
return nil
}
if !IsErrNewIssueInsert(err) {
return err
}
if i++; i == issueMaxDupIndexAttempts {
break
}
log.Error("NewIssue: error attempting to insert the new issue; will retry. Original error: %v", err)
}
return fmt.Errorf("NewIssue: too many errors attempting to insert the new issue. Last error was: %v", err)
}

func newIssueAttempt(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}

if err = newIssue(sess, issue.Poster, NewIssueOptions{
func NewIssue(ctx DBContext, repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
return newIssue(ctx.e, issue.Poster, NewIssueOptions{
Repo: repo,
Issue: issue,
LabelIDs: labelIDs,
Attachments: uuids,
}); err != nil {
if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
return err
}
return fmt.Errorf("newIssue: %v", err)
}

if err = sess.Commit(); err != nil {
return fmt.Errorf("Commit: %v", err)
}

return nil
})
}

// GetIssueByIndex returns raw issue without loading attributes by index in a repository.
Expand Down
6 changes: 3 additions & 3 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 createCommentWithNoAction(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
func createCommentWithNoAction(e Engine, opts *CreateCommentOptions) (_ *Comment, err error) {
var LabelID int64
if opts.Label != nil {
LabelID = opts.Label.ID
Expand Down Expand Up @@ -546,7 +546,7 @@ func createCommentWithNoAction(e *xorm.Session, opts *CreateCommentOptions) (_ *
return comment, nil
}

func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
func updateCommentInfos(e Engine, opts *CreateCommentOptions, comment *Comment) (err error) {
// Check comment type.
switch opts.Type {
case CommentTypeCode:
Expand Down Expand Up @@ -596,7 +596,7 @@ func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Co
return updateIssueCols(e, opts.Issue, "updated_unix")
}

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
2 changes: 1 addition & 1 deletion models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func HasIssueLabel(issueID, labelID int64) bool {
return hasIssueLabel(x, issueID, labelID)
}

func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
func newIssueLabel(e Engine, issue *Issue, label *Label, doer *User) (err error) {
if _, err = e.Insert(&IssueLabel{
IssueID: issue.ID,
LabelID: label.ID,
Expand Down
2 changes: 1 addition & 1 deletion models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func testInsertIssue(t *testing.T, title, content string) {
Title: title,
Content: content,
}
err := NewIssue(repo, &issue, nil, nil)
err := NewIssue(DefaultDBContext(), repo, &issue, nil, nil)
assert.NoError(t, err)

var newIssue Issue
Expand Down
9 changes: 4 additions & 5 deletions models/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"code.gitea.io/gitea/modules/references"

"github.com/unknwon/com"
"xorm.io/xorm"
)

type crossReference struct {
Expand Down Expand Up @@ -61,7 +60,7 @@ func neuterCrossReferencesIds(e Engine, ids []int64) error {
// \/ \/ \/
//

func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
func (issue *Issue) addCrossReferences(e Engine, doer *User, removeOld bool) error {
var commentType CommentType
if issue.IsPull {
commentType = CommentTypePullRef
Expand All @@ -77,7 +76,7 @@ func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bo
return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
}

func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, plaincontent, mdcontent)
if err != nil {
return err
Expand Down Expand Up @@ -138,7 +137,7 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
return nil
}

func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
func (issue *Issue) getCrossReferences(e Engine, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
xreflist := make([]*crossReference, 0, 5)
var (
refRepo *Repository
Expand Down Expand Up @@ -246,7 +245,7 @@ func (issue *Issue) verifyReferencedIssue(e Engine, ctx *crossReferencesContext,
// \/ \/ \/ \/ \/
//

func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
func (comment *Comment) addCrossReferences(e Engine, doer *User, removeOld bool) error {
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
return nil
}
Expand Down
Loading