2014-03-21 05:04:56 +09: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-23 02:50:50 +09:00
|
|
|
import (
|
2014-03-23 05:00:46 +09:00
|
|
|
"errors"
|
2014-03-23 02:50:50 +09:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/base"
|
|
|
|
)
|
|
|
|
|
2014-03-23 05:00:46 +09:00
|
|
|
var (
|
|
|
|
ErrIssueNotExist = errors.New("Issue does not exist")
|
|
|
|
)
|
|
|
|
|
2014-03-23 02:50:50 +09:00
|
|
|
// Issue represents an issue or pull request of repository.
|
2014-03-21 05:04:56 +09:00
|
|
|
type Issue struct {
|
2014-03-29 23:24:42 +09:00
|
|
|
Id int64
|
|
|
|
Index int64 // Index in one repository.
|
|
|
|
Name string
|
|
|
|
RepoId int64 `xorm:"index"`
|
|
|
|
Repo *Repository `xorm:"-"`
|
|
|
|
PosterId int64
|
|
|
|
Poster *User `xorm:"-"`
|
|
|
|
MilestoneId int64
|
|
|
|
AssigneeId int64
|
|
|
|
IsPull bool // Indicates whether is a pull request or not.
|
|
|
|
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"`
|
2014-03-23 02:50:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIssue creates new issue for repository.
|
2014-03-28 04:24:11 +09:00
|
|
|
func CreateIssue(userId, repoId, milestoneId, assigneeId int64, issueCount int, name, labels, content string, isPull bool) (issue *Issue, err error) {
|
2014-03-23 05:00:46 +09:00
|
|
|
// TODO: find out mentions
|
|
|
|
mentions := ""
|
|
|
|
|
2014-03-28 01:48:29 +09:00
|
|
|
sess := orm.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
sess.Begin()
|
|
|
|
|
2014-03-28 04:24:11 +09:00
|
|
|
issue = &Issue{
|
|
|
|
Index: int64(issueCount) + 1,
|
2014-03-23 02:50:50 +09:00
|
|
|
Name: name,
|
|
|
|
RepoId: repoId,
|
|
|
|
PosterId: userId,
|
|
|
|
MilestoneId: milestoneId,
|
|
|
|
AssigneeId: assigneeId,
|
|
|
|
IsPull: isPull,
|
|
|
|
Labels: labels,
|
|
|
|
Mentions: mentions,
|
|
|
|
Content: content,
|
2014-03-23 05:00:46 +09:00
|
|
|
}
|
2014-03-28 01:48:29 +09:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-01 12:48:01 +09:00
|
|
|
return issue, sess.Commit()
|
2014-03-21 05:04:56 +09:00
|
|
|
}
|
|
|
|
|
2014-03-23 05:00:46 +09:00
|
|
|
// GetIssueById returns issue object by given id.
|
2014-03-24 08:09:11 +09:00
|
|
|
func GetIssueByIndex(repoId, index int64) (*Issue, error) {
|
|
|
|
issue := &Issue{RepoId: repoId, Index: index}
|
|
|
|
has, err := orm.Get(issue)
|
2014-03-23 05:00:46 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrIssueNotExist
|
|
|
|
}
|
|
|
|
return issue, nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 02:50:50 +09: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-23 05:00:46 +09:00
|
|
|
sess := orm.Limit(20, (page-1)*20)
|
|
|
|
|
|
|
|
if repoId > 0 {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Where("repo_id=?", repoId).And("is_closed=?", isClosed)
|
2014-03-23 05:00:46 +09:00
|
|
|
} else {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Where("is_closed=?", isClosed)
|
2014-03-23 05:00:46 +09:00
|
|
|
}
|
|
|
|
|
2014-03-23 02:50:50 +09:00
|
|
|
if userId > 0 {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.And("assignee_id=?", userId)
|
2014-03-23 02:50:50 +09:00
|
|
|
} else if posterId > 0 {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.And("poster_id=?", posterId)
|
2014-03-23 02:50:50 +09:00
|
|
|
} else if isMention {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.And("mentions like '%$" + base.ToStr(userId) + "|%'")
|
2014-03-23 02:50:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if milestoneId > 0 {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.And("milestone_id=?", milestoneId)
|
2014-03-23 02:50:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(labels) > 0 {
|
|
|
|
for _, label := range strings.Split(labels, ",") {
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.And("mentions like '%$" + label + "|%'")
|
2014-03-23 02:50:50 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch sortType {
|
|
|
|
case "oldest":
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Asc("created")
|
2014-03-23 02:50:50 +09:00
|
|
|
case "recentupdate":
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Desc("updated")
|
2014-03-23 02:50:50 +09:00
|
|
|
case "leastupdate":
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Asc("updated")
|
2014-03-23 02:50:50 +09:00
|
|
|
case "mostcomment":
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Desc("num_comments")
|
2014-03-23 02:50:50 +09:00
|
|
|
case "leastcomment":
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Asc("num_comments")
|
2014-03-23 02:50:50 +09:00
|
|
|
default:
|
2014-03-23 19:27:01 +09:00
|
|
|
sess.Desc("created")
|
2014-03-23 02:50:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var issues []Issue
|
|
|
|
err := sess.Find(&issues)
|
|
|
|
return issues, err
|
|
|
|
}
|
|
|
|
|
2014-03-28 05:31:32 +09:00
|
|
|
// GetUserIssueCount returns the number of issues that were created by given user in repository.
|
|
|
|
func GetUserIssueCount(userId, repoId int64) int64 {
|
|
|
|
count, _ := orm.Where("poster_id=?", userId).And("repo_id=?", repoId).Count(new(Issue))
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2014-03-24 08:09:11 +09:00
|
|
|
// UpdateIssue updates information of issue.
|
|
|
|
func UpdateIssue(issue *Issue) error {
|
2014-03-28 04:24:11 +09:00
|
|
|
_, err := orm.Id(issue.Id).AllCols().Update(issue)
|
2014-03-24 08:09:11 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-23 02:50:50 +09:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:50:51 +09:00
|
|
|
// Issue types.
|
|
|
|
const (
|
|
|
|
IT_PLAIN = iota // Pure comment.
|
|
|
|
IT_REOPEN // Issue reopen status change prompt.
|
|
|
|
IT_CLOSE // Issue close status change prompt.
|
|
|
|
)
|
|
|
|
|
2014-03-23 02:50:50 +09:00
|
|
|
// Comment represents a comment in commit and issue page.
|
2014-03-21 05:04:56 +09:00
|
|
|
type Comment struct {
|
2014-03-23 02:50:50 +09:00
|
|
|
Id int64
|
2014-03-30 06:50:51 +09:00
|
|
|
Type int
|
2014-03-23 02:50:50 +09:00
|
|
|
PosterId int64
|
2014-03-27 01:31:01 +09:00
|
|
|
Poster *User `xorm:"-"`
|
2014-03-23 02:50:50 +09:00
|
|
|
IssueId int64
|
|
|
|
CommitId int64
|
2014-03-27 01:31:01 +09:00
|
|
|
Line int64
|
2014-03-23 02:50:50 +09:00
|
|
|
Content string
|
|
|
|
Created time.Time `xorm:"created"`
|
2014-03-21 05:04:56 +09:00
|
|
|
}
|
2014-03-27 01:31:01 +09:00
|
|
|
|
|
|
|
// CreateComment creates comment of issue or commit.
|
2014-03-30 06:50:51 +09:00
|
|
|
func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error {
|
2014-03-27 05:41:16 +09:00
|
|
|
sess := orm.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
sess.Begin()
|
|
|
|
|
2014-04-30 12:12:03 +09:00
|
|
|
if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId,
|
2014-03-28 04:24:11 +09:00
|
|
|
CommitId: commitId, Line: line, Content: content}); err != nil {
|
2014-03-27 05:41:16 +09:00
|
|
|
sess.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:50:51 +09:00
|
|
|
// Check comment type.
|
|
|
|
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
|
|
|
|
}
|
2014-03-27 05:41:16 +09:00
|
|
|
}
|
|
|
|
return sess.Commit()
|
2014-03-27 01:31:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|