0
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-11-30 12:14:51 +01:00
gitea/models/issue.go

159 lines
3.6 KiB
Go
Raw Normal View History

2014-03-20 21:04:56 +01:00
// Copyright 2014 The Gogs 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
2014-03-22 18:50:50 +01:00
import (
2014-03-22 21:00:46 +01:00
"errors"
2014-03-22 18:50:50 +01:00
"strings"
"time"
"github.com/gogits/gogs/modules/base"
)
2014-03-22 21:00:46 +01:00
var (
ErrIssueNotExist = errors.New("Issue does not exist")
)
2014-03-22 18:50:50 +01:00
// Issue represents an issue or pull request of repository.
2014-03-20 21:04:56 +01:00
type Issue struct {
2014-03-22 18:50:50 +01:00
Id int64
Index int64 // Index in one repository.
Name string
RepoId int64 `xorm:"index"`
PosterId int64
MilestoneId int64
AssigneeId int64
IsPull bool // Indicates whether is a pull request or not.
IsClosed bool
2014-03-22 21:00:46 +01:00
Labels string `xorm:"TEXT"`
Mentions string `xorm:"TEXT"`
Content string `xorm:"TEXT"`
2014-03-22 18:50:50 +01:00
NumComments int
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
// CreateIssue creates new issue for repository.
2014-03-22 21:00:46 +01:00
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) {
2014-03-22 18:50:50 +01:00
count, err := GetIssueCount(repoId)
if err != nil {
2014-03-22 21:00:46 +01:00
return nil, err
2014-03-22 18:50:50 +01:00
}
2014-03-22 21:00:46 +01:00
// TODO: find out mentions
mentions := ""
issue := &Issue{
2014-03-22 18:50:50 +01:00
Index: count + 1,
Name: name,
RepoId: repoId,
PosterId: userId,
MilestoneId: milestoneId,
AssigneeId: assigneeId,
IsPull: isPull,
Labels: labels,
Mentions: mentions,
Content: content,
2014-03-22 21:00:46 +01:00
}
_, err = orm.Insert(issue)
return issue, err
2014-03-20 21:04:56 +01:00
}
2014-03-22 18:50:50 +01:00
// GetIssueCount returns count of issues in the repository.
func GetIssueCount(repoId int64) (int64, error) {
return orm.Count(&Issue{RepoId: repoId})
2014-03-20 21:04:56 +01:00
}
2014-03-22 21:00:46 +01:00
// GetIssueById returns issue object by given id.
func GetIssueById(id int64) (*Issue, error) {
issue := new(Issue)
has, err := orm.Id(id).Get(issue)
if err != nil {
return nil, err
} else if !has {
return nil, ErrIssueNotExist
}
return issue, nil
}
2014-03-22 18:50:50 +01:00
// GetIssues returns a list of issues by given conditions.
func GetIssues(userId, repoId, posterId, milestoneId int64, page int, isClosed, isMention bool, labels, sortType string) ([]Issue, error) {
2014-03-22 21:00:46 +01:00
sess := orm.Limit(20, (page-1)*20)
if repoId > 0 {
sess = sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
} else {
sess = sess.Where("is_closed=?", isClosed)
}
2014-03-22 18:50:50 +01:00
if userId > 0 {
sess = sess.And("assignee_id=?", userId)
} else if posterId > 0 {
sess = sess.And("poster_id=?", posterId)
} else if isMention {
sess = sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
}
if milestoneId > 0 {
sess = sess.And("milestone_id=?", milestoneId)
}
if len(labels) > 0 {
for _, label := range strings.Split(labels, ",") {
sess = sess.And("mentions like '%$" + label + "|%'")
}
}
switch sortType {
case "oldest":
sess = sess.Asc("created")
case "recentupdate":
sess = sess.Desc("updated")
case "leastupdate":
sess = sess.Asc("updated")
case "mostcomment":
sess = sess.Desc("num_comments")
case "leastcomment":
sess = sess.Asc("num_comments")
default:
sess = sess.Desc("created")
}
var issues []Issue
err := sess.Find(&issues)
return issues, err
}
// Label represents a list of labels of repository for issues.
type Label struct {
Id int64
RepoId int64 `xorm:"index"`
Names string
Colors string
}
// Milestone represents a milestone of repository.
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"`
}
// Comment represents a comment in commit and issue page.
2014-03-20 21:04:56 +01:00
type Comment struct {
2014-03-22 18:50:50 +01:00
Id int64
PosterId int64
IssueId int64
CommitId int64
Line int
Content string
Created time.Time `xorm:"created"`
2014-03-20 21:04:56 +01:00
}