Skip to content

Commit e286a07

Browse files
committed
Move patch related codes from models to pull service
1 parent 0e56337 commit e286a07

File tree

14 files changed

+345
-327
lines changed

14 files changed

+345
-327
lines changed

models/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ func WithTx(f func(ctx DBContext) error) error {
5353
sess.Close()
5454
return err
5555
}
56+
57+
// Insert inserts an object to database
58+
func Insert(ctx DBContext, bean interface{}) error {
59+
_, err := ctx.e.Insert(bean)
60+
return err
61+
}

models/issue.go

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ var (
7575

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

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

450-
func (issue *Issue) addLabel(e *xorm.Session, label *Label, doer *User) error {
449+
func (issue *Issue) addLabel(e Engine, label *Label, doer *User) error {
451450
return newIssueLabel(e, issue, label, doer)
452451
}
453452

@@ -843,10 +842,9 @@ type NewIssueOptions struct {
843842
Issue *Issue
844843
LabelIDs []int64
845844
Attachments []string // In UUID format.
846-
IsPull bool
847845
}
848846

849-
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
847+
func newIssue(e Engine, doer *User, opts NewIssueOptions) (err error) {
850848
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
851849

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

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

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

955953
// NewIssue creates new issue with labels for repository.
956-
func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
957-
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
958-
i := 0
959-
for {
960-
if err = newIssueAttempt(repo, issue, labelIDs, uuids); err == nil {
961-
return nil
962-
}
963-
if !IsErrNewIssueInsert(err) {
964-
return err
965-
}
966-
if i++; i == issueMaxDupIndexAttempts {
967-
break
968-
}
969-
log.Error("NewIssue: error attempting to insert the new issue; will retry. Original error: %v", err)
970-
}
971-
return fmt.Errorf("NewIssue: too many errors attempting to insert the new issue. Last error was: %v", err)
972-
}
973-
974-
func newIssueAttempt(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
975-
sess := x.NewSession()
976-
defer sess.Close()
977-
if err = sess.Begin(); err != nil {
978-
return err
979-
}
980-
981-
if err = newIssue(sess, issue.Poster, NewIssueOptions{
954+
func NewIssue(ctx DBContext, repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
955+
return newIssue(ctx.e, issue.Poster, NewIssueOptions{
982956
Repo: repo,
983957
Issue: issue,
984958
LabelIDs: labelIDs,
985959
Attachments: uuids,
986-
}); err != nil {
987-
if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
988-
return err
989-
}
990-
return fmt.Errorf("newIssue: %v", err)
991-
}
992-
993-
if err = sess.Commit(); err != nil {
994-
return fmt.Errorf("Commit: %v", err)
995-
}
996-
997-
return nil
960+
})
998961
}
999962

1000963
// GetIssueByIndex returns raw issue without loading attributes by index in a repository.

models/issue_comment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func (c *Comment) CodeCommentURL() string {
495495
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
496496
}
497497

498-
func createCommentWithNoAction(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
498+
func createCommentWithNoAction(e Engine, opts *CreateCommentOptions) (_ *Comment, err error) {
499499
var LabelID int64
500500
if opts.Label != nil {
501501
LabelID = opts.Label.ID
@@ -546,7 +546,7 @@ func createCommentWithNoAction(e *xorm.Session, opts *CreateCommentOptions) (_ *
546546
return comment, nil
547547
}
548548

549-
func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
549+
func updateCommentInfos(e Engine, opts *CreateCommentOptions, comment *Comment) (err error) {
550550
// Check comment type.
551551
switch opts.Type {
552552
case CommentTypeCode:
@@ -596,7 +596,7 @@ func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Co
596596
return updateIssueCols(e, opts.Issue, "updated_unix")
597597
}
598598

599-
func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, comment *Comment) (err error) {
599+
func sendCreateCommentAction(e Engine, opts *CreateCommentOptions, comment *Comment) (err error) {
600600
// Compose comment action, could be plain comment, close or reopen issue/pull request.
601601
// This object will be used to notify watchers in the end of function.
602602
act := &Action{

models/issue_label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func HasIssueLabel(issueID, labelID int64) bool {
413413
return hasIssueLabel(x, issueID, labelID)
414414
}
415415

416-
func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
416+
func newIssueLabel(e Engine, issue *Issue, label *Label, doer *User) (err error) {
417417
if _, err = e.Insert(&IssueLabel{
418418
IssueID: issue.ID,
419419
LabelID: label.ID,

models/issue_xref.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"code.gitea.io/gitea/modules/references"
1212

1313
"github.com/unknwon/com"
14-
"xorm.io/xorm"
1514
)
1615

1716
type crossReference struct {
@@ -61,7 +60,7 @@ func neuterCrossReferencesIds(e Engine, ids []int64) error {
6160
// \/ \/ \/
6261
//
6362

64-
func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
63+
func (issue *Issue) addCrossReferences(e Engine, doer *User, removeOld bool) error {
6564
var commentType CommentType
6665
if issue.IsPull {
6766
commentType = CommentTypePullRef
@@ -77,7 +76,7 @@ func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bo
7776
return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
7877
}
7978

80-
func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
79+
func (issue *Issue) createCrossReferences(e Engine, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
8180
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, plaincontent, mdcontent)
8281
if err != nil {
8382
return err
@@ -138,7 +137,7 @@ func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesC
138137
return nil
139138
}
140139

141-
func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
140+
func (issue *Issue) getCrossReferences(e Engine, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
142141
xreflist := make([]*crossReference, 0, 5)
143142
var (
144143
refRepo *Repository
@@ -246,7 +245,7 @@ func (issue *Issue) verifyReferencedIssue(e Engine, ctx *crossReferencesContext,
246245
// \/ \/ \/ \/ \/
247246
//
248247

249-
func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
248+
func (comment *Comment) addCrossReferences(e Engine, doer *User, removeOld bool) error {
250249
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
251250
return nil
252251
}

0 commit comments

Comments
 (0)