123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package models
- import (
- "errors"
- "strings"
- "time"
- "github.com/gogits/gogs/modules/base"
- )
- var (
- ErrIssueNotExist = errors.New("Issue does not exist")
- )
- type Issue struct {
- Id int64
- Index int64
- Name string
- RepoId int64 `xorm:"index"`
- Repo *Repository `xorm:"-"`
- PosterId int64
- Poster *User `xorm:"-"`
- MilestoneId int64
- AssigneeId int64
- IsPull bool
- IsClosed bool
- Labels string `xorm:"TEXT"`
- Mentions string `xorm:"TEXT"`
- Content string `xorm:"TEXT"`
- RenderedContent string `xorm:"-"`
- NumComments int
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
- }
- func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (issue *Issue, err error) {
-
- mentions := ""
- sess := orm.NewSession()
- defer sess.Close()
- sess.Begin()
- issue = &Issue{
- Index: int64(issueCount) + 1,
- Name: name,
- RepoId: repoId,
- PosterId: userId,
- MilestoneId: milestoneId,
- AssigneeId: assigneeId,
- IsPull: isPull,
- Labels: labels,
- Mentions: mentions,
- Content: content,
- }
- if _, err = sess.Insert(issue); err != nil {
- sess.Rollback()
- return nil, err
- }
- rawSql := "UPDATE `repository` SET num_issues = num_issues + 1 WHERE id = ?"
- if _, err = sess.Exec(rawSql, repoId); err != nil {
- sess.Rollback()
- return nil, err
- }
- return issue, sess.Commit()
- }
- func GetIssueByIndex(repoId, index int64) (*Issue, error) {
- issue := &Issue{RepoId: repoId, Index: index}
- has, err := orm.Get(issue)
- if err != nil {
- return nil, err
- } else if !has {
- return nil, ErrIssueNotExist
- }
- return issue, nil
- }
- func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
- sess := orm.Limit(20, (page-1)*20)
- if repoId > 0 {
- sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
- } else {
- sess.Where("is_closed=?", isClosed)
- }
- if userId > 0 {
- sess.And("assignee_id=?", userId)
- } else if posterId > 0 {
- sess.And("poster_id=?", posterId)
- } else if isMention {
- sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
- }
- if milestoneId > 0 {
- sess.And("milestone_id=?", milestoneId)
- }
- if len(labels) > 0 {
- for _, label := range strings.Split(labels, ",") {
- sess.And("mentions like '%$" + label + "|%'")
- }
- }
- switch sortType {
- case "oldest":
- sess.Asc("created")
- case "recentupdate":
- sess.Desc("updated")
- case "leastupdate":
- sess.Asc("updated")
- case "mostcomment":
- sess.Desc("num_comments")
- case "leastcomment":
- sess.Asc("num_comments")
- default:
- sess.Desc("created")
- }
- var issues []Issue
- err := sess.Find(&issues)
- return issues, err
- }
- func GetUserIssueCount(userId, repoId int64) int64 {
- count, _ := orm.Where("poster_id=?", userId).And("repo_id=?", repoId).Count(new(Issue))
- return count
- }
- func UpdateIssue(issue *Issue) error {
- _, err := orm.Id(issue.Id).AllCols().Update(issue)
- return err
- }
- type Label struct {
- Id int64
- RepoId int64 `xorm:"index"`
- Names string
- Colors string
- }
- type Milestone struct {
- Id int64
- Name string
- RepoId int64 `xorm:"index"`
- IsClosed bool
- Content string
- NumIssues int
- DueDate time.Time
- Created time.Time `xorm:"created"`
- }
- const (
- IT_PLAIN = iota
- IT_REOPEN
- IT_CLOSE
- )
- type Comment struct {
- Id int64
- Type int
- PosterId int64
- Poster *User `xorm:"-"`
- IssueId int64
- CommitId int64
- Line int64
- Content string
- Created time.Time `xorm:"created"`
- }
- func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
- sess := orm.NewSession()
- defer sess.Close()
- sess.Begin()
- if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
- CommitId: commitId, Line: line, Content: content}); err != nil {
- sess.Rollback()
- return err
- }
-
- switch cmtType {
- case IT_PLAIN:
- rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
- if _, err := sess.Exec(rawSql, issueId); err != nil {
- sess.Rollback()
- return err
- }
- case IT_REOPEN:
- rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
- if _, err := sess.Exec(rawSql, repoId); err != nil {
- sess.Rollback()
- return err
- }
- case IT_CLOSE:
- rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
- if _, err := sess.Exec(rawSql, repoId); err != nil {
- sess.Rollback()
- return err
- }
- }
- return sess.Commit()
- }
- func GetIssueComments(issueId int64) ([]Comment, error) {
- comments := make([]Comment, 0, 10)
- err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
- return comments, err
- }
|