@ -14,7 +14,6 @@ import (
"time"
"time"
"github.com/Unknwon/com"
"github.com/Unknwon/com"
"github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/modules/setting"
@ -73,7 +72,7 @@ func (i *Issue) GetLabels() error {
strIds := strings . Split ( strings . TrimSuffix ( i . LabelIds [ 1 : ] , "|" ) , "|$" )
strIds := strings . Split ( strings . TrimSuffix ( i . LabelIds [ 1 : ] , "|" ) , "|$" )
i . Labels = make ( [ ] * Label , 0 , len ( strIds ) )
i . Labels = make ( [ ] * Label , 0 , len ( strIds ) )
for _ , strId := range strIds {
for _ , strId := range strIds {
id , _ := com . StrTo ( strId ) . Int64 ( )
id := com . StrTo ( strId ) . Must Int64( )
if id > 0 {
if id > 0 {
l , err := GetLabelById ( id )
l , err := GetLabelById ( id )
if err != nil {
if err != nil {
@ -186,29 +185,29 @@ func GetIssueById(id int64) (*Issue, error) {
}
}
// GetIssues returns a list of issues by given conditions.
// GetIssues returns a list of issues by given conditions.
func GetIssues ( uid , rid , pid , mid int64 , page int , isClosed bool , labelIds , sortType string ) ( [ ] Issue , error ) {
func GetIssues ( uid , assigneeID , repoID , posterID , milestoneID int64 , page int , isClosed , isMention bool , labelIds , sortType string ) ( [ ] Issue , error ) {
sess := x . Limit ( setting . IssuePagingNum , ( page - 1 ) * setting . IssuePagingNum )
sess := x . Limit ( setting . IssuePagingNum , ( page - 1 ) * setting . IssuePagingNum )
if rid > 0 {
if repoID > 0 {
sess . Where ( "repo_id=?" , rid ) . And ( "is_closed=?" , isClosed )
sess . Where ( "issue. repo_id=?" , repoID ) . And ( "issue. is_closed=?" , isClosed )
} else {
} else {
sess . Where ( "is_closed=?" , isClosed )
sess . Where ( "issue.is _closed=?" , isClosed )
}
}
if uid > 0 {
if assigneeID > 0 {
sess . And ( "assignee_id=?" , uid )
sess . And ( "issue. assignee_id=?" , assigneeID )
} else if pid > 0 {
} else if posterID > 0 {
sess . And ( "poster_id=?" , pid )
sess . And ( "issue. poster_id=?" , posterID )
}
}
if mid > 0 {
if milestoneID > 0 {
sess . And ( "milestone_id=?" , mid )
sess . And ( "issue. milestone_id=?" , milestoneID )
}
}
if len ( labelIds ) > 0 {
if len ( labelIds ) > 0 {
for _ , label := range strings . Split ( labelIds , "," ) {
for _ , label := range strings . Split ( labelIds , "," ) {
if com . StrTo ( label ) . MustInt ( ) > 0 {
if com . StrTo ( label ) . MustInt ( ) > 0 {
sess . And ( "label_ids like ?" , "' %$" + label + "|%' " )
sess . And ( "label_ids like ?" , "%$" + label + "|%" )
}
}
}
}
}
}
@ -230,6 +229,14 @@ func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labelIds, sort
sess . Desc ( "created" )
sess . Desc ( "created" )
}
}
if isMention {
queryStr := "issue.id == issue_user.issue_id AND issue_user.is_mentioned=1"
if uid > 0 {
queryStr += " AND issue_user.uid = " + com . ToStr ( uid )
}
sess . Join ( "INNER" , "issue_user" , queryStr )
}
var issues [ ] Issue
var issues [ ] Issue
return issues , sess . Find ( & issues )
return issues , sess . Find ( & issues )
}
}
@ -394,53 +401,42 @@ type IssueStats struct {
// Filter modes.
// Filter modes.
const (
const (
FM_ASSIGN = iota + 1
FM_ALL = iota
FM_ASSIGN
FM_CREATE
FM_CREATE
FM_MENTION
FM_MENTION
)
)
// GetIssueStats returns issue statistic information by given conditions.
// GetIssueStats returns issue statistic information by given conditions.
func GetIssueStats ( rid , uid int64 , isShowClosed bool , filterMode int ) * IssueStats {
func GetIssueStats ( repoID , uid , labelID int64 , isShowClosed bool , filterMode int ) * IssueStats {
stats := & IssueStats { }
stats := & IssueStats { }
issue := new ( Issue )
issue := new ( Issue )
tmpSess := & xorm . Session { }
queryStr := "repo_id=? AND is_closed=?"
sess := x . Where ( "repo_id=?" , rid )
switch filterMode {
* tmpSess = * sess
case FM_ALL :
stats . OpenCount , _ = tmpSess . And ( "is_closed=?" , false ) . Count ( issue )
stats . OpenCount , _ = x . Where ( queryStr , repoID , false ) . Count ( issue )
* tmpSess = * sess
stats . ClosedCount , _ = x . Where ( queryStr , repoID , true ) . Count ( issue )
stats . ClosedCount , _ = tmpSess . And ( "is_closed=?" , true ) . Count ( issue )
return stats
if isShowClosed {
stats . AllCount = stats . ClosedCount
case FM_ASSIGN :
} else {
queryStr += " AND assignee_id=?"
stats . AllCount = stats . OpenCount
stats . OpenCount , _ = x . Where ( queryStr , repoID , false , uid ) . Count ( issue )
}
stats . ClosedCount , _ = x . Where ( queryStr , repoID , true , uid ) . Count ( issue )
return stats
if filterMode != FM_MENTION {
sess = x . Where ( "repo_id=?" , rid )
case FM_CREATE :
switch filterMode {
queryStr += " AND poster_id=?"
case FM_ASSIGN :
stats . OpenCount , _ = x . Where ( queryStr , repoID , false , uid ) . Count ( issue )
sess . And ( "assignee_id=?" , uid )
stats . ClosedCount , _ = x . Where ( queryStr , repoID , true , uid ) . Count ( issue )
case FM_CREATE :
return stats
sess . And ( "poster_id=?" , uid )
default :
case FM_MENTION :
goto nofilter
queryStr += " AND uid=? AND is_mentioned=?"
}
stats . OpenCount , _ = x . Where ( queryStr , repoID , false , uid , true ) . Count ( new ( IssueUser ) )
* tmpSess = * sess
stats . ClosedCount , _ = x . Where ( queryStr , repoID , true , uid , true ) . Count ( new ( IssueUser ) )
stats . OpenCount , _ = tmpSess . And ( "is_closed=?" , false ) . Count ( issue )
return stats
* tmpSess = * sess
}
stats . ClosedCount , _ = tmpSess . And ( "is_closed=?" , true ) . Count ( issue )
} else {
sess := x . Where ( "repo_id=?" , rid ) . And ( "uid=?" , uid ) . And ( "is_mentioned=?" , true )
* tmpSess = * sess
stats . OpenCount , _ = tmpSess . And ( "is_closed=?" , false ) . Count ( new ( IssueUser ) )
* tmpSess = * sess
stats . ClosedCount , _ = tmpSess . And ( "is_closed=?" , true ) . Count ( new ( IssueUser ) )
}
nofilter :
stats . AssignCount , _ = x . Where ( "repo_id=?" , rid ) . And ( "is_closed=?" , isShowClosed ) . And ( "assignee_id=?" , uid ) . Count ( issue )
stats . CreateCount , _ = x . Where ( "repo_id=?" , rid ) . And ( "is_closed=?" , isShowClosed ) . And ( "poster_id=?" , uid ) . Count ( issue )
stats . MentionCount , _ = x . Where ( "repo_id=?" , rid ) . And ( "uid=?" , uid ) . And ( "is_closed=?" , isShowClosed ) . And ( "is_mentioned=?" , true ) . Count ( new ( IssueUser ) )
return stats
return stats
}
}
@ -894,7 +890,7 @@ type Comment struct {
// CreateComment creates comment of issue or commit.
// CreateComment creates comment of issue or commit.
func CreateComment ( userId , repoId , issueId , commitId , line int64 , cmtType CommentType , content string , attachments [ ] int64 ) ( * Comment , error ) {
func CreateComment ( userId , repoId , issueId , commitId , line int64 , cmtType CommentType , content string , attachments [ ] int64 ) ( * Comment , error ) {
sess := x . NewSession ( )
sess := x . NewSession ( )
defer sess . Close ( )
defer sessionRelease ( sess )
if err := sess . Begin ( ) ; err != nil {
if err := sess . Begin ( ) ; err != nil {
return nil , err
return nil , err
}
}
@ -903,7 +899,6 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType Commen
CommitId : commitId , Line : line , Content : content }
CommitId : commitId , Line : line , Content : content }
if _ , err := sess . Insert ( comment ) ; err != nil {
if _ , err := sess . Insert ( comment ) ; err != nil {
sess . Rollback ( )
return nil , err
return nil , err
}
}
@ -912,7 +907,6 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType Commen
case COMMENT_TYPE_COMMENT :
case COMMENT_TYPE_COMMENT :
rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
if _ , err := sess . Exec ( rawSql , issueId ) ; err != nil {
if _ , err := sess . Exec ( rawSql , issueId ) ; err != nil {
sess . Rollback ( )
return nil , err
return nil , err
}
}
@ -926,20 +920,17 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType Commen
}
}
if _ , err := sess . Exec ( rawSql , comment . Id , strings . Join ( astrs , "," ) ) ; err != nil {
if _ , err := sess . Exec ( rawSql , comment . Id , strings . Join ( astrs , "," ) ) ; err != nil {
sess . Rollback ( )
return nil , err
return nil , err
}
}
}
}
case COMMENT_TYPE_REOPEN :
case COMMENT_TYPE_REOPEN :
rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues - 1 WHERE id = ?"
if _ , err := sess . Exec ( rawSql , repoId ) ; err != nil {
if _ , err := sess . Exec ( rawSql , repoId ) ; err != nil {
sess . Rollback ( )
return nil , err
return nil , err
}
}
case COMMENT_TYPE_CLOSE :
case COMMENT_TYPE_CLOSE :
rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
if _ , err := sess . Exec ( rawSql , repoId ) ; err != nil {
if _ , err := sess . Exec ( rawSql , repoId ) ; err != nil {
sess . Rollback ( )
return nil , err
return nil , err
}
}
}
}