[BugFix] remove nil inserts in models (#11096)

* Fix InsertReleases Nil Insert on Attachments

* FIX "No element on slice when insert" & smal refactor

* again

* impruve

* rm useles newline

* Apply suggestions from code review

Co-Authored-By: zeripath <art27@cantab.net>

* process insert as a whole

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
6543 2020-04-17 19:42:57 +02:00 committed by GitHub
parent 0010fde8a2
commit 12960b9d18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 17 deletions

View file

@ -64,16 +64,21 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
})
labelIDs = append(labelIDs, label.ID)
}
if len(issueLabels) > 0 {
if _, err := sess.Insert(issueLabels); err != nil {
return err
}
}
for _, reaction := range issue.Reactions {
reaction.IssueID = issue.ID
}
if len(issue.Reactions) > 0 {
if _, err := sess.Insert(issue.Reactions); err != nil {
return err
}
}
cols := make([]string, 0)
if !issue.IsPull {
@ -151,10 +156,12 @@ func InsertIssueComments(comments []*Comment) error {
reaction.IssueID = comment.IssueID
reaction.CommentID = comment.ID
}
if len(comment.Reactions) > 0 {
if _, err := sess.Insert(comment.Reactions); err != nil {
return err
}
}
}
for issueID := range issueIDs {
if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
@ -196,7 +203,8 @@ func InsertReleases(rels ...*Release) error {
return err
}
for i := 0; i < len(rel.Attachments); i++ {
if len(rel.Attachments) > 0 {
for i := range rel.Attachments {
rel.Attachments[i].ReleaseID = rel.ID
}
@ -204,6 +212,7 @@ func InsertReleases(rels ...*Release) error {
return err
}
}
}
return sess.Commit()
}

View file

@ -393,13 +393,16 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
iss = append(iss, &is)
}
err := models.InsertIssues(iss...)
if err != nil {
if len(iss) > 0 {
if err := models.InsertIssues(iss...); err != nil {
return err
}
for _, is := range iss {
g.issues.Store(is.Index, is.ID)
}
}
return nil
}
@ -478,6 +481,9 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
cms = append(cms, &cm)
}
if len(cms) == 0 {
return nil
}
return models.InsertIssueComments(cms)
}