bench-forgejo/models/issue.go

201 lines
4.7 KiB
Go
Raw Normal View History

2014-03-21 01:34:56 +05:30
// 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 23:20:50 +05:30
import (
2014-03-23 01:30:46 +05:30
"errors"
2014-03-22 23:20:50 +05:30
"strings"
"time"
"github.com/gogits/gogs/modules/base"
)
2014-03-23 01:30:46 +05:30
var (
ErrIssueNotExist = errors.New("Issue does not exist")
)
2014-03-22 23:20:50 +05:30
// Issue represents an issue or pull request of repository.
2014-03-21 01:34:56 +05:30
type Issue struct {
2014-03-22 23:20:50 +05:30
Id int64
Index int64 // Index in one repository.
Name string
RepoId int64 `xorm:"index"`
PosterId int64
2014-03-25 23:34:57 +05:30
Poster *User `xorm:"-"`
2014-03-22 23:20:50 +05:30
MilestoneId int64
AssigneeId int64
IsPull bool // Indicates whether is a pull request or not.
IsClosed bool
2014-03-23 01:30:46 +05:30
Labels string `xorm:"TEXT"`
Mentions string `xorm:"TEXT"`
Content string `xorm:"TEXT"`
2014-03-22 23:20:50 +05:30
NumComments int
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
// CreateIssue creates new issue for repository.
2014-03-23 01:30:46 +05:30
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, content string, isPull bool) (*Issue, error) {
2014-03-22 23:20:50 +05:30
count, err := GetIssueCount(repoId)
if err != nil {
2014-03-23 01:30:46 +05:30
return nil, err
2014-03-22 23:20:50 +05:30
}
2014-03-23 01:30:46 +05:30
// TODO: find out mentions
mentions := ""
issue := &Issue{
2014-03-22 23:20:50 +05:30
Index: count + 1,
Name: name,
RepoId: repoId,
PosterId: userId,
MilestoneId: milestoneId,
AssigneeId: assigneeId,
IsPull: isPull,
Labels: labels,
Mentions: mentions,
Content: content,
2014-03-23 01:30:46 +05:30
}
_, err = orm.Insert(issue)
return issue, err
2014-03-21 01:34:56 +05:30
}
2014-03-22 23:20:50 +05:30
// GetIssueCount returns count of issues in the repository.
func GetIssueCount(repoId int64) (int64, error) {
return orm.Count(&Issue{RepoId: repoId})
2014-03-21 01:34:56 +05:30
}
2014-03-23 01:30:46 +05:30
// GetIssueById returns issue object by given id.
2014-03-24 04:39:11 +05:30
func GetIssueByIndex(repoId, index int64) (*Issue, error) {
issue := &Issue{RepoId: repoId, Index: index}
has, err := orm.Get(issue)
2014-03-23 01:30:46 +05:30
if err != nil {
return nil, err
} else if !has {
return nil, ErrIssueNotExist
}
return issue, nil
}
2014-03-22 23:20:50 +05:30
// 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-23 01:30:46 +05:30
sess := orm.Limit(20, (page-1)*20)
if repoId > 0 {
2014-03-23 15:57:01 +05:30
sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
2014-03-23 01:30:46 +05:30
} else {
2014-03-23 15:57:01 +05:30
sess.Where("is_closed=?", isClosed)
2014-03-23 01:30:46 +05:30
}
2014-03-22 23:20:50 +05:30
if userId > 0 {
2014-03-23 15:57:01 +05:30
sess.And("assignee_id=?", userId)
2014-03-22 23:20:50 +05:30
} else if posterId > 0 {
2014-03-23 15:57:01 +05:30
sess.And("poster_id=?", posterId)
2014-03-22 23:20:50 +05:30
} else if isMention {
2014-03-23 15:57:01 +05:30
sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
2014-03-22 23:20:50 +05:30
}
if milestoneId > 0 {
2014-03-23 15:57:01 +05:30
sess.And("milestone_id=?", milestoneId)
2014-03-22 23:20:50 +05:30
}
if len(labels) > 0 {
for _, label := range strings.Split(labels, ",") {
2014-03-23 15:57:01 +05:30
sess.And("mentions like '%$" + label + "|%'")
2014-03-22 23:20:50 +05:30
}
}
switch sortType {
case "oldest":
2014-03-23 15:57:01 +05:30
sess.Asc("created")
2014-03-22 23:20:50 +05:30
case "recentupdate":
2014-03-23 15:57:01 +05:30
sess.Desc("updated")
2014-03-22 23:20:50 +05:30
case "leastupdate":
2014-03-23 15:57:01 +05:30
sess.Asc("updated")
2014-03-22 23:20:50 +05:30
case "mostcomment":
2014-03-23 15:57:01 +05:30
sess.Desc("num_comments")
2014-03-22 23:20:50 +05:30
case "leastcomment":
2014-03-23 15:57:01 +05:30
sess.Asc("num_comments")
2014-03-22 23:20:50 +05:30
default:
2014-03-23 15:57:01 +05:30
sess.Desc("created")
2014-03-22 23:20:50 +05:30
}
var issues []Issue
err := sess.Find(&issues)
return issues, err
}
2014-03-24 04:39:11 +05:30
// UpdateIssue updates information of issue.
func UpdateIssue(issue *Issue) error {
_, err := orm.Update(issue, &Issue{RepoId: issue.RepoId, Index: issue.Index})
return err
}
func CloseIssue() {
}
func ReopenIssue() {
}
2014-03-22 23:20:50 +05:30
// 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-21 01:34:56 +05:30
type Comment struct {
2014-03-22 23:20:50 +05:30
Id int64
PosterId int64
2014-03-26 22:01:01 +05:30
Poster *User `xorm:"-"`
2014-03-22 23:20:50 +05:30
IssueId int64
CommitId int64
2014-03-26 22:01:01 +05:30
Line int64
2014-03-22 23:20:50 +05:30
Content string
Created time.Time `xorm:"created"`
2014-03-21 01:34:56 +05:30
}
2014-03-26 22:01:01 +05:30
// CreateComment creates comment of issue or commit.
func CreateComment(userId, issueId, commitId, line int64, content string) error {
2014-03-27 02:11:16 +05:30
sess := orm.NewSession()
defer sess.Close()
sess.Begin()
if _, err := orm.Insert(&Comment{PosterId: userId, IssueId: issueId,
CommitId: commitId, Line: line, Content: content,
}); err != nil {
sess.Rollback()
return err
}
rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
if _, err := sess.Exec(rawSql, issueId); err != nil {
sess.Rollback()
return err
}
return sess.Commit()
2014-03-26 22:01:01 +05:30
}
// GetIssueComments returns list of comment by given issue id.
func GetIssueComments(issueId int64) ([]Comment, error) {
comments := make([]Comment, 0, 10)
err := orm.Asc("created").Find(&comments, &Comment{IssueId: issueId})
return comments, err
}