mirror of
https://github.com/gogs/gogs.git
synced 2026-02-27 16:50:58 +01:00
Convert milestone.go from XORM to GORM
Co-authored-by: unknwon <2946214+unknwon@users.noreply.github.com>
This commit is contained in:
@@ -4,11 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "unknwon.dev/clog/v2"
|
|
||||||
"xorm.io/xorm"
|
|
||||||
|
|
||||||
"github.com/cockroachdb/errors"
|
"github.com/cockroachdb/errors"
|
||||||
api "github.com/gogs/go-gogs-client"
|
api "github.com/gogs/go-gogs-client"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
log "unknwon.dev/clog/v2"
|
||||||
|
|
||||||
"gogs.io/gogs/internal/conf"
|
"gogs.io/gogs/internal/conf"
|
||||||
"gogs.io/gogs/internal/errutil"
|
"gogs.io/gogs/internal/errutil"
|
||||||
@@ -17,29 +16,30 @@ import (
|
|||||||
// Milestone represents a milestone of repository.
|
// Milestone represents a milestone of repository.
|
||||||
type Milestone struct {
|
type Milestone struct {
|
||||||
ID int64
|
ID int64
|
||||||
RepoID int64 `xorm:"INDEX"`
|
RepoID int64 `gorm:"index"`
|
||||||
Name string
|
Name string
|
||||||
Content string `xorm:"TEXT"`
|
Content string `gorm:"type:text"`
|
||||||
RenderedContent string `xorm:"-" json:"-" gorm:"-"`
|
RenderedContent string `gorm:"-" json:"-"`
|
||||||
IsClosed bool
|
IsClosed bool
|
||||||
NumIssues int
|
NumIssues int
|
||||||
NumClosedIssues int
|
NumClosedIssues int
|
||||||
NumOpenIssues int `xorm:"-" json:"-" gorm:"-"`
|
NumOpenIssues int `gorm:"-" json:"-"`
|
||||||
Completeness int // Percentage(1-100).
|
Completeness int // Percentage(1-100).
|
||||||
IsOverDue bool `xorm:"-" json:"-" gorm:"-"`
|
IsOverDue bool `gorm:"-" json:"-"`
|
||||||
|
|
||||||
DeadlineString string `xorm:"-" json:"-" gorm:"-"`
|
DeadlineString string `gorm:"-" json:"-"`
|
||||||
Deadline time.Time `xorm:"-" json:"-" gorm:"-"`
|
Deadline time.Time `gorm:"-" json:"-"`
|
||||||
DeadlineUnix int64
|
DeadlineUnix int64
|
||||||
ClosedDate time.Time `xorm:"-" json:"-" gorm:"-"`
|
ClosedDate time.Time `gorm:"-" json:"-"`
|
||||||
ClosedDateUnix int64
|
ClosedDateUnix int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Milestone) BeforeInsert() {
|
func (m *Milestone) BeforeCreate(tx *gorm.DB) error {
|
||||||
m.DeadlineUnix = m.Deadline.Unix()
|
m.DeadlineUnix = m.Deadline.Unix()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Milestone) BeforeUpdate() {
|
func (m *Milestone) BeforeUpdate(tx *gorm.DB) error {
|
||||||
if m.NumIssues > 0 {
|
if m.NumIssues > 0 {
|
||||||
m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
|
m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
|
||||||
} else {
|
} else {
|
||||||
@@ -48,27 +48,22 @@ func (m *Milestone) BeforeUpdate() {
|
|||||||
|
|
||||||
m.DeadlineUnix = m.Deadline.Unix()
|
m.DeadlineUnix = m.Deadline.Unix()
|
||||||
m.ClosedDateUnix = m.ClosedDate.Unix()
|
m.ClosedDateUnix = m.ClosedDate.Unix()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
|
func (m *Milestone) AfterFind(tx *gorm.DB) error {
|
||||||
switch colName {
|
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
|
||||||
case "num_closed_issues":
|
|
||||||
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
|
|
||||||
|
|
||||||
case "deadline_unix":
|
|
||||||
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
|
|
||||||
if m.Deadline.Year() == 9999 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
|
||||||
|
if m.Deadline.Year() != 9999 {
|
||||||
m.DeadlineString = m.Deadline.Format("2006-01-02")
|
m.DeadlineString = m.Deadline.Format("2006-01-02")
|
||||||
if time.Now().Local().After(m.Deadline) {
|
if time.Now().Local().After(m.Deadline) {
|
||||||
m.IsOverDue = true
|
m.IsOverDue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
case "closed_date_unix":
|
|
||||||
m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// State returns string representation of milestone status.
|
// State returns string representation of milestone status.
|
||||||
@@ -102,30 +97,24 @@ func (m *Milestone) APIFormat() *api.Milestone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Milestone) CountIssues(isClosed, includePulls bool) int64 {
|
func (m *Milestone) CountIssues(isClosed, includePulls bool) int64 {
|
||||||
sess := x.Where("milestone_id = ?", m.ID).And("is_closed = ?", isClosed)
|
query := db.Model(new(Issue)).Where("milestone_id = ? AND is_closed = ?", m.ID, isClosed)
|
||||||
if !includePulls {
|
if !includePulls {
|
||||||
sess.And("is_pull = ?", false)
|
query = query.Where("is_pull = ?", false)
|
||||||
}
|
}
|
||||||
count, _ := sess.Count(new(Issue))
|
var count int64
|
||||||
|
query.Count(&count)
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMilestone creates new milestone of repository.
|
// NewMilestone creates new milestone of repository.
|
||||||
func NewMilestone(m *Milestone) (err error) {
|
func NewMilestone(m *Milestone) (err error) {
|
||||||
sess := x.NewSession()
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
defer sess.Close()
|
if err := tx.Create(m).Error; err != nil {
|
||||||
if err = sess.Begin(); err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = sess.Insert(m); err != nil {
|
return tx.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID).Error
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sess.Commit()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ errutil.NotFound = (*ErrMilestoneNotExist)(nil)
|
var _ errutil.NotFound = (*ErrMilestoneNotExist)(nil)
|
||||||
@@ -147,74 +136,73 @@ func (ErrMilestoneNotExist) NotFound() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
|
func getMilestoneByRepoID(e *gorm.DB, repoID, id int64) (*Milestone, error) {
|
||||||
m := &Milestone{
|
m := &Milestone{}
|
||||||
ID: id,
|
err := e.Where("id = ? AND repo_id = ?", id, repoID).First(m).Error
|
||||||
RepoID: repoID,
|
|
||||||
}
|
|
||||||
has, err := e.Get(m)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, ErrMilestoneNotExist{args: map[string]any{"repoID": repoID, "milestoneID": id}}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
|
||||||
return nil, ErrMilestoneNotExist{args: map[string]any{"repoID": repoID, "milestoneID": id}}
|
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWebhookByRepoID returns the milestone in a repository.
|
// GetWebhookByRepoID returns the milestone in a repository.
|
||||||
func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
|
func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
|
||||||
return getMilestoneByRepoID(x, repoID, id)
|
return getMilestoneByRepoID(db, repoID, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMilestonesByRepoID returns all milestones of a repository.
|
// GetMilestonesByRepoID returns all milestones of a repository.
|
||||||
func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
|
func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) {
|
||||||
miles := make([]*Milestone, 0, 10)
|
miles := make([]*Milestone, 0, 10)
|
||||||
return miles, x.Where("repo_id = ?", repoID).Find(&miles)
|
return miles, db.Where("repo_id = ?", repoID).Find(&miles).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMilestones returns a list of milestones of given repository and status.
|
// GetMilestones returns a list of milestones of given repository and status.
|
||||||
func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
|
func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
|
||||||
miles := make([]*Milestone, 0, conf.UI.IssuePagingNum)
|
miles := make([]*Milestone, 0, conf.UI.IssuePagingNum)
|
||||||
sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
|
query := db.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
|
||||||
if page > 0 {
|
if page > 0 {
|
||||||
sess = sess.Limit(conf.UI.IssuePagingNum, (page-1)*conf.UI.IssuePagingNum)
|
query = query.Limit(conf.UI.IssuePagingNum).Offset((page - 1) * conf.UI.IssuePagingNum)
|
||||||
}
|
}
|
||||||
return miles, sess.Find(&miles)
|
return miles, query.Find(&miles).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateMilestone(e Engine, m *Milestone) error {
|
func updateMilestone(e *gorm.DB, m *Milestone) error {
|
||||||
_, err := e.ID(m.ID).AllCols().Update(m)
|
return e.Model(m).Where("id = ?", m.ID).Updates(m).Error
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMilestone updates information of given milestone.
|
// UpdateMilestone updates information of given milestone.
|
||||||
func UpdateMilestone(m *Milestone) error {
|
func UpdateMilestone(m *Milestone) error {
|
||||||
return updateMilestone(x, m)
|
return updateMilestone(db, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func countRepoMilestones(e Engine, repoID int64) int64 {
|
func countRepoMilestones(e *gorm.DB, repoID int64) int64 {
|
||||||
count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
|
var count int64
|
||||||
|
e.Model(new(Milestone)).Where("repo_id = ?", repoID).Count(&count)
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountRepoMilestones returns number of milestones in given repository.
|
// CountRepoMilestones returns number of milestones in given repository.
|
||||||
func CountRepoMilestones(repoID int64) int64 {
|
func CountRepoMilestones(repoID int64) int64 {
|
||||||
return countRepoMilestones(x, repoID)
|
return countRepoMilestones(db, repoID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func countRepoClosedMilestones(e Engine, repoID int64) int64 {
|
func countRepoClosedMilestones(e *gorm.DB, repoID int64) int64 {
|
||||||
closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
|
var count int64
|
||||||
return closed
|
e.Model(new(Milestone)).Where("repo_id = ? AND is_closed = ?", repoID, true).Count(&count)
|
||||||
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountRepoClosedMilestones returns number of closed milestones in given repository.
|
// CountRepoClosedMilestones returns number of closed milestones in given repository.
|
||||||
func CountRepoClosedMilestones(repoID int64) int64 {
|
func CountRepoClosedMilestones(repoID int64) int64 {
|
||||||
return countRepoClosedMilestones(x, repoID)
|
return countRepoClosedMilestones(db, repoID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MilestoneStats returns number of open and closed milestones of given repository.
|
// MilestoneStats returns number of open and closed milestones of given repository.
|
||||||
func MilestoneStats(repoID int64) (open, closed int64) {
|
func MilestoneStats(repoID int64) (open, closed int64) {
|
||||||
open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
|
db.Model(new(Milestone)).Where("repo_id = ? AND is_closed = ?", repoID, false).Count(&open)
|
||||||
return open, CountRepoClosedMilestones(repoID)
|
return open, CountRepoClosedMilestones(repoID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,26 +215,19 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
defer sess.Close()
|
m.IsClosed = isClosed
|
||||||
if err = sess.Begin(); err != nil {
|
if err := updateMilestone(tx, m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.IsClosed = isClosed
|
repo.NumMilestones = int(countRepoMilestones(tx, repo.ID))
|
||||||
if err = updateMilestone(sess, m); err != nil {
|
repo.NumClosedMilestones = int(countRepoClosedMilestones(tx, repo.ID))
|
||||||
return err
|
return tx.Model(repo).Where("id = ?", repo.ID).Updates(repo).Error
|
||||||
}
|
})
|
||||||
|
|
||||||
repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
|
|
||||||
repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
|
|
||||||
if _, err = sess.ID(repo.ID).AllCols().Update(repo); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sess.Commit()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
|
func changeMilestoneIssueStats(e *gorm.DB, issue *Issue) error {
|
||||||
if issue.MilestoneID == 0 {
|
if issue.MilestoneID == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -270,20 +251,12 @@ func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
|
|||||||
// ChangeMilestoneIssueStats updates the open/closed issues counter and progress
|
// ChangeMilestoneIssueStats updates the open/closed issues counter and progress
|
||||||
// for the milestone associated with the given issue.
|
// for the milestone associated with the given issue.
|
||||||
func ChangeMilestoneIssueStats(issue *Issue) (err error) {
|
func ChangeMilestoneIssueStats(issue *Issue) (err error) {
|
||||||
sess := x.NewSession()
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
defer sess.Close()
|
return changeMilestoneIssueStats(tx, issue)
|
||||||
if err = sess.Begin(); err != nil {
|
})
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = changeMilestoneIssueStats(sess, issue); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return sess.Commit()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64) error {
|
func changeMilestoneAssign(e *gorm.DB, issue *Issue, oldMilestoneID int64) error {
|
||||||
if oldMilestoneID > 0 {
|
if oldMilestoneID > 0 {
|
||||||
m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
|
m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -297,7 +270,9 @@ func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64)
|
|||||||
|
|
||||||
if err = updateMilestone(e, m); err != nil {
|
if err = updateMilestone(e, m); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID); err != nil {
|
}
|
||||||
|
|
||||||
|
if err = e.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE issue_id = ?", issue.ID).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,7 +292,9 @@ func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64)
|
|||||||
|
|
||||||
if err = updateMilestone(e, m); err != nil {
|
if err = updateMilestone(e, m); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID); err != nil {
|
}
|
||||||
|
|
||||||
|
if err = e.Exec("UPDATE `issue_user` SET milestone_id = ? WHERE issue_id = ?", m.ID, issue.ID).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,18 +306,11 @@ func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64)
|
|||||||
|
|
||||||
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
||||||
func ChangeMilestoneAssign(doer *User, issue *Issue, oldMilestoneID int64) (err error) {
|
func ChangeMilestoneAssign(doer *User, issue *Issue, oldMilestoneID int64) (err error) {
|
||||||
sess := x.NewSession()
|
err = db.Transaction(func(tx *gorm.DB) error {
|
||||||
defer sess.Close()
|
return changeMilestoneAssign(tx, issue, oldMilestoneID)
|
||||||
if err = sess.Begin(); err != nil {
|
})
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return errors.Newf("transaction: %v", err)
|
||||||
|
|
||||||
if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = sess.Commit(); err != nil {
|
|
||||||
return errors.Newf("commit: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var hookAction api.HookIssueAction
|
var hookAction api.HookIssueAction
|
||||||
@@ -394,26 +364,21 @@ func DeleteMilestoneOfRepoByID(repoID, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
defer sess.Close()
|
if err := tx.Where("id = ?", m.ID).Delete(new(Milestone)).Error; err != nil {
|
||||||
if err = sess.Begin(); err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
|
repo.NumMilestones = int(countRepoMilestones(tx, repo.ID))
|
||||||
return err
|
repo.NumClosedMilestones = int(countRepoClosedMilestones(tx, repo.ID))
|
||||||
}
|
if err := tx.Model(repo).Where("id = ?", repo.ID).Updates(repo).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
|
if err := tx.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID).Error; err != nil {
|
||||||
repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
|
return err
|
||||||
if _, err = sess.ID(repo.ID).AllCols().Update(repo); err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
return tx.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID).Error
|
||||||
|
})
|
||||||
if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
|
|
||||||
return err
|
|
||||||
} else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sess.Commit()
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user