|
@@ -7,6 +7,7 @@ package models
|
|
|
import (
|
|
|
"bytes"
|
|
|
"errors"
|
|
|
+ "html/template"
|
|
|
"os"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -25,6 +26,7 @@ var (
|
|
|
ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
|
|
|
ErrAttachmentNotExist = errors.New("Attachment does not exist")
|
|
|
ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
|
|
|
+ ErrMissingIssueNumber = errors.New("No issue number specified")
|
|
|
)
|
|
|
|
|
|
|
|
@@ -140,6 +142,29 @@ func NewIssue(issue *Issue) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+func GetIssueByRef(ref string) (issue *Issue, err error) {
|
|
|
+ var issueNumber int64
|
|
|
+ var repo *Repository
|
|
|
+
|
|
|
+ n := strings.IndexByte(ref, byte('#'))
|
|
|
+
|
|
|
+ if n == -1 {
|
|
|
+ return nil, ErrMissingIssueNumber
|
|
|
+ }
|
|
|
+
|
|
|
+ if issueNumber, err = strconv.ParseInt(ref[n+1:], 10, 64); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if repo, err = GetRepositoryByRef(ref[:n]); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return GetIssueByIndex(repo.Id, issueNumber)
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
func GetIssueByIndex(rid, index int64) (*Issue, error) {
|
|
|
issue := &Issue{RepoId: rid, Index: index}
|
|
@@ -418,6 +443,11 @@ func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
|
|
|
|
|
|
func UpdateIssue(issue *Issue) error {
|
|
|
_, err := x.Id(issue.Id).AllCols().Update(issue)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
return err
|
|
|
}
|
|
|
|
|
@@ -688,6 +718,32 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
|
|
|
return sess.Commit()
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+func ChangeMilestoneIssueStats(issue *Issue) error {
|
|
|
+ if issue.MilestoneId == 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ m, err := GetMilestoneById(issue.MilestoneId)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if issue.IsClosed {
|
|
|
+ m.NumOpenIssues--
|
|
|
+ m.NumClosedIssues++
|
|
|
+ } else {
|
|
|
+ m.NumOpenIssues++
|
|
|
+ m.NumClosedIssues--
|
|
|
+ }
|
|
|
+
|
|
|
+ m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
|
|
|
+
|
|
|
+ return UpdateMilestone(m)
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
|
|
|
sess := x.NewSession()
|
|
@@ -711,6 +767,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
|
|
|
} else {
|
|
|
m.Completeness = 0
|
|
|
}
|
|
|
+
|
|
|
if _, err = sess.Id(m.Id).Update(m); err != nil {
|
|
|
sess.Rollback()
|
|
|
return err
|
|
@@ -728,6 +785,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
m.NumIssues++
|
|
|
if issue.IsClosed {
|
|
|
m.NumClosedIssues++
|
|
@@ -749,6 +807,7 @@ func ChangeMilestoneAssign(oldMid, mid int64, issue *Issue) (err error) {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return sess.Commit()
|
|
|
}
|
|
|
|
|
@@ -792,17 +851,33 @@ func DeleteMilestone(m *Milestone) (err error) {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+type CommentType int
|
|
|
+
|
|
|
const (
|
|
|
- IT_PLAIN = iota
|
|
|
- IT_REOPEN
|
|
|
- IT_CLOSE
|
|
|
+
|
|
|
+ COMMENT CommentType = iota
|
|
|
+
|
|
|
+
|
|
|
+ REOPEN
|
|
|
+
|
|
|
+
|
|
|
+ CLOSE
|
|
|
+
|
|
|
+
|
|
|
+ ISSUE
|
|
|
+
|
|
|
+
|
|
|
+ COMMIT
|
|
|
+
|
|
|
+
|
|
|
+ PULL
|
|
|
)
|
|
|
|
|
|
|
|
|
type Comment struct {
|
|
|
Id int64
|
|
|
- Type int
|
|
|
+ Type CommentType
|
|
|
PosterId int64
|
|
|
Poster *User `xorm:"-"`
|
|
|
IssueId int64
|
|
@@ -813,7 +888,7 @@ type Comment struct {
|
|
|
}
|
|
|
|
|
|
|
|
|
-func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, 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()
|
|
|
defer sess.Close()
|
|
|
if err := sess.Begin(); err != nil {
|
|
@@ -830,7 +905,7 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, c
|
|
|
|
|
|
|
|
|
switch cmtType {
|
|
|
- case IT_PLAIN:
|
|
|
+ case COMMENT:
|
|
|
rawSql := "UPDATE `issue` SET num_comments = num_comments + 1 WHERE id = ?"
|
|
|
if _, err := sess.Exec(rawSql, issueId); err != nil {
|
|
|
sess.Rollback()
|
|
@@ -851,13 +926,13 @@ func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, c
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
- case IT_REOPEN:
|
|
|
+ case 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 nil, err
|
|
|
}
|
|
|
- case IT_CLOSE:
|
|
|
+ case CLOSE:
|
|
|
rawSql := "UPDATE `repository` SET num_closed_issues = num_closed_issues + 1 WHERE id = ?"
|
|
|
if _, err := sess.Exec(rawSql, repoId); err != nil {
|
|
|
sess.Rollback()
|
|
@@ -876,6 +951,10 @@ func GetCommentById(commentId int64) (*Comment, error) {
|
|
|
return c, err
|
|
|
}
|
|
|
|
|
|
+func (c *Comment) ContentHtml() template.HTML {
|
|
|
+ return template.HTML(c.Content)
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
func GetIssueComments(issueId int64) ([]Comment, error) {
|
|
|
comments := make([]Comment, 0, 10)
|