2019-05-06 19:12:51 -06:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import "github.com/go-xorm/xorm"
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// InsertMilestones creates milestones of repository.
|
|
|
|
func InsertMilestones(ms ...*Milestone) (err error) {
|
|
|
|
if len(ms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:12:51 -06:00
|
|
|
sess := x.NewSession()
|
2019-06-29 07:38:22 -06:00
|
|
|
defer sess.Close()
|
|
|
|
if err = sess.Begin(); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// to return the id, so we should not use batch insert
|
|
|
|
for _, m := range ms {
|
|
|
|
if _, err = sess.NoAutoTime().Insert(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// InsertIssues insert issues to database
|
|
|
|
func InsertIssues(issues ...*Issue) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
if err := insertIssue(sess, issue); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertIssue(sess *xorm.Session, issue *Issue) error {
|
2019-05-06 19:12:51 -06:00
|
|
|
if _, err := sess.NoAutoTime().Insert(issue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
var issueLabels = make([]IssueLabel, 0, len(issue.Labels))
|
|
|
|
var labelIDs = make([]int64, 0, len(issue.Labels))
|
|
|
|
for _, label := range issue.Labels {
|
2019-05-06 19:12:51 -06:00
|
|
|
issueLabels = append(issueLabels, IssueLabel{
|
|
|
|
IssueID: issue.ID,
|
2019-06-29 07:38:22 -06:00
|
|
|
LabelID: label.ID,
|
2019-05-06 19:12:51 -06:00
|
|
|
})
|
2019-06-29 07:38:22 -06:00
|
|
|
labelIDs = append(labelIDs, label.ID)
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
|
|
|
if _, err := sess.Insert(issueLabels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-03 12:38:42 -06:00
|
|
|
|
|
|
|
cols := make([]string, 0)
|
2019-05-06 19:12:51 -06:00
|
|
|
if !issue.IsPull {
|
|
|
|
sess.ID(issue.RepoID).Incr("num_issues")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_issues")
|
2019-05-06 19:12:51 -06:00
|
|
|
if issue.IsClosed {
|
|
|
|
sess.Incr("num_closed_issues")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_closed_issues")
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sess.ID(issue.RepoID).Incr("num_pulls")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_pulls")
|
2019-05-06 19:12:51 -06:00
|
|
|
if issue.IsClosed {
|
|
|
|
sess.Incr("num_closed_pulls")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_closed_pulls")
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 12:38:42 -06:00
|
|
|
if _, err := sess.NoAutoTime().Cols(cols...).Update(issue.Repo); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = []string{"num_issues"}
|
2019-05-06 19:12:51 -06:00
|
|
|
sess.Incr("num_issues")
|
|
|
|
if issue.IsClosed {
|
|
|
|
sess.Incr("num_closed_issues")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_closed_issues")
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
2019-08-03 12:38:42 -06:00
|
|
|
if _, err := sess.In("id", labelIDs).NoAutoTime().Cols(cols...).Update(new(Label)); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.MilestoneID > 0 {
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = []string{"num_issues"}
|
2019-06-29 07:38:22 -06:00
|
|
|
sess.Incr("num_issues")
|
2019-08-03 12:38:42 -06:00
|
|
|
cl := "num_closed_issues"
|
2019-06-29 07:38:22 -06:00
|
|
|
if issue.IsClosed {
|
|
|
|
sess.Incr("num_closed_issues")
|
2019-08-03 12:38:42 -06:00
|
|
|
cols = append(cols, "num_closed_issues")
|
|
|
|
cl = "(num_closed_issues + 1)"
|
2019-06-29 07:38:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.ID(issue.MilestoneID).
|
2019-08-03 12:38:42 -06:00
|
|
|
SetExpr("completeness", cl+" * 100 / (num_issues + 1)").
|
|
|
|
NoAutoTime().Cols(cols...).
|
2019-06-29 07:38:22 -06:00
|
|
|
Update(new(Milestone)); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// InsertIssueComments inserts many comments of issues.
|
|
|
|
func InsertIssueComments(comments []*Comment) error {
|
|
|
|
if len(comments) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var issueIDs = make(map[int64]bool)
|
|
|
|
for _, comment := range comments {
|
|
|
|
issueIDs[comment.IssueID] = true
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:12:51 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
if _, err := sess.NoAutoTime().Insert(comments); err != nil {
|
2019-05-06 19:12:51 -06:00
|
|
|
return err
|
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// InsertPullRequests inserted pull requests
|
|
|
|
func InsertPullRequests(prs ...*PullRequest) error {
|
2019-05-06 19:12:51 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
for _, pr := range prs {
|
|
|
|
if err := insertIssue(sess, pr.Issue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pr.IssueID = pr.Issue.ID
|
|
|
|
if _, err := sess.NoAutoTime().Insert(pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
2019-06-29 07:38:22 -06:00
|
|
|
|
2019-05-06 19:12:51 -06:00
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
// InsertReleases migrates release
|
|
|
|
func InsertReleases(rels ...*Release) error {
|
2019-05-06 19:12:51 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
for _, rel := range rels {
|
2019-05-06 19:12:51 -06:00
|
|
|
if _, err := sess.NoAutoTime().Insert(rel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
for i := 0; i < len(rel.Attachments); i++ {
|
|
|
|
rel.Attachments[i].ReleaseID = rel.ID
|
|
|
|
}
|
2019-05-06 19:12:51 -06:00
|
|
|
|
2019-06-29 07:38:22 -06:00
|
|
|
if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-06 19:12:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|