2016-03-05 23:28:51 +05:30
|
|
|
// Copyright 2016 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
|
|
|
"github.com/go-xorm/xorm"
|
2016-08-27 02:10:53 +05:30
|
|
|
|
2016-11-11 15:09:44 +05:30
|
|
|
api "code.gitea.io/sdk/gitea"
|
2016-03-05 23:28:51 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/markdown"
|
2016-03-05 23:28:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
|
|
|
|
type CommentType int
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// Enumerate all the comment types
|
2016-03-05 23:28:51 +05:30
|
|
|
const (
|
|
|
|
// Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
|
2016-11-07 22:00:04 +05:30
|
|
|
CommentTypeComment CommentType = iota
|
|
|
|
CommentTypeReopen
|
2016-11-07 22:05:34 +05:30
|
|
|
CommentTypeClose
|
2016-03-05 23:28:51 +05:30
|
|
|
|
|
|
|
// References.
|
2016-11-07 22:05:34 +05:30
|
|
|
CommentTypeIssueRef
|
2016-03-05 23:28:51 +05:30
|
|
|
// Reference from a commit (not part of a pull request)
|
2016-11-07 22:05:34 +05:30
|
|
|
CommentTypeCommitRef
|
2016-03-05 23:28:51 +05:30
|
|
|
// Reference from a comment
|
2016-11-08 02:28:22 +05:30
|
|
|
CommentTypeCommentRef
|
2016-03-05 23:28:51 +05:30
|
|
|
// Reference from a pull request
|
2016-11-07 22:05:34 +05:30
|
|
|
CommentTypePullRef
|
2017-01-30 18:16:45 +05:30
|
|
|
// Labels changed
|
|
|
|
CommentTypeLabel
|
2017-02-01 08:06:08 +05:30
|
|
|
// Milestone changed
|
|
|
|
CommentTypeMilestone
|
2017-02-03 20:39:10 +05:30
|
|
|
// Assignees changed
|
|
|
|
CommentTypeAssignees
|
2017-02-05 20:06:00 +05:30
|
|
|
// Change Title
|
|
|
|
CommentTypeChangeTitle
|
2016-03-05 23:28:51 +05:30
|
|
|
)
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// CommentTag defines comment tag type
|
2016-03-05 23:28:51 +05:30
|
|
|
type CommentTag int
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// Enumerate all the comment tag types
|
2016-03-05 23:28:51 +05:30
|
|
|
const (
|
2016-11-07 22:00:04 +05:30
|
|
|
CommentTagNone CommentTag = iota
|
|
|
|
CommentTagPoster
|
|
|
|
CommentTagWriter
|
2016-11-07 22:05:34 +05:30
|
|
|
CommentTagOwner
|
2016-03-05 23:28:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Comment represents a comment in commit and issue page.
|
|
|
|
type Comment struct {
|
2017-02-03 20:39:10 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Type CommentType
|
|
|
|
PosterID int64 `xorm:"INDEX"`
|
|
|
|
Poster *User `xorm:"-"`
|
|
|
|
IssueID int64 `xorm:"INDEX"`
|
|
|
|
LabelID int64
|
|
|
|
Label *Label `xorm:"-"`
|
|
|
|
OldMilestoneID int64
|
|
|
|
MilestoneID int64
|
|
|
|
OldMilestone *Milestone `xorm:"-"`
|
|
|
|
Milestone *Milestone `xorm:"-"`
|
|
|
|
OldAssigneeID int64
|
|
|
|
AssigneeID int64
|
|
|
|
Assignee *User `xorm:"-"`
|
|
|
|
OldAssignee *User `xorm:"-"`
|
2017-02-05 20:06:00 +05:30
|
|
|
OldTitle string
|
|
|
|
NewTitle string
|
2017-02-03 20:39:10 +05:30
|
|
|
|
2017-02-01 08:06:08 +05:30
|
|
|
CommitID int64
|
2016-03-05 23:28:51 +05:30
|
|
|
Line int64
|
2016-03-10 06:23:30 +05:30
|
|
|
Content string `xorm:"TEXT"`
|
|
|
|
RenderedContent string `xorm:"-"`
|
|
|
|
|
|
|
|
Created time.Time `xorm:"-"`
|
2017-01-06 20:44:33 +05:30
|
|
|
CreatedUnix int64 `xorm:"INDEX"`
|
2016-08-27 02:10:53 +05:30
|
|
|
Updated time.Time `xorm:"-"`
|
2017-01-06 20:44:33 +05:30
|
|
|
UpdatedUnix int64 `xorm:"INDEX"`
|
2016-03-05 23:28:51 +05:30
|
|
|
|
|
|
|
// Reference issue in commit message
|
|
|
|
CommitSHA string `xorm:"VARCHAR(40)"`
|
|
|
|
|
|
|
|
Attachments []*Attachment `xorm:"-"`
|
|
|
|
|
|
|
|
// For view issue page.
|
|
|
|
ShowTag CommentTag `xorm:"-"`
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
|
|
|
// representing this object.
|
2016-03-10 06:23:30 +05:30
|
|
|
func (c *Comment) BeforeInsert() {
|
2016-07-23 17:54:44 +05:30
|
|
|
c.CreatedUnix = time.Now().Unix()
|
2016-08-27 02:10:53 +05:30
|
|
|
c.UpdatedUnix = c.CreatedUnix
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// BeforeUpdate is invoked from XORM before updating this object.
|
2016-08-27 02:10:53 +05:30
|
|
|
func (c *Comment) BeforeUpdate() {
|
|
|
|
c.UpdatedUnix = time.Now().Unix()
|
2016-03-10 06:23:30 +05:30
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// AfterSet is invoked from XORM after setting the value of a field of this object.
|
2016-03-05 23:28:51 +05:30
|
|
|
func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
|
|
|
|
var err error
|
|
|
|
switch colName {
|
|
|
|
case "id":
|
|
|
|
c.Attachments, err = GetAttachmentsByCommentID(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "poster_id":
|
|
|
|
c.Poster, err = GetUserByID(c.PosterID)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrUserNotExist(err) {
|
|
|
|
c.PosterID = -1
|
2016-08-14 16:02:24 +05:30
|
|
|
c.Poster = NewGhostUser()
|
2016-03-05 23:28:51 +05:30
|
|
|
} else {
|
|
|
|
log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 06:23:30 +05:30
|
|
|
case "created_unix":
|
|
|
|
c.Created = time.Unix(c.CreatedUnix, 0).Local()
|
2016-08-27 02:10:53 +05:30
|
|
|
case "updated_unix":
|
|
|
|
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// AfterDelete is invoked from XORM after the object is deleted.
|
2016-03-05 23:28:51 +05:30
|
|
|
func (c *Comment) AfterDelete() {
|
|
|
|
_, err := DeleteAttachmentsByComment(c.ID, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:59:26 +05:30
|
|
|
// HTMLURL formats a URL-string to the issue-comment
|
|
|
|
func (c *Comment) HTMLURL() string {
|
|
|
|
issue, err := GetIssueByID(c.IssueID)
|
|
|
|
if err != nil { // Silently dropping errors :unamused:
|
|
|
|
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s#issuecomment-%d", issue.HTMLURL(), c.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssueURL formats a URL-string to the issue
|
|
|
|
func (c *Comment) IssueURL() string {
|
|
|
|
issue, err := GetIssueByID(c.IssueID)
|
|
|
|
if err != nil { // Silently dropping errors :unamused:
|
|
|
|
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.IsPull {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return issue.HTMLURL()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PRURL formats a URL-string to the pull-request
|
|
|
|
func (c *Comment) PRURL() string {
|
|
|
|
issue, err := GetIssueByID(c.IssueID)
|
|
|
|
if err != nil { // Silently dropping errors :unamused:
|
|
|
|
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !issue.IsPull {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return issue.HTMLURL()
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// APIFormat converts a Comment to the api.Comment format
|
2016-08-26 23:53:21 +05:30
|
|
|
func (c *Comment) APIFormat() *api.Comment {
|
2016-08-27 02:10:53 +05:30
|
|
|
return &api.Comment{
|
2016-12-22 13:59:26 +05:30
|
|
|
ID: c.ID,
|
|
|
|
Poster: c.Poster.APIFormat(),
|
|
|
|
HTMLURL: c.HTMLURL(),
|
|
|
|
IssueURL: c.IssueURL(),
|
|
|
|
PRURL: c.PRURL(),
|
|
|
|
Body: c.Content,
|
|
|
|
Created: c.Created,
|
|
|
|
Updated: c.Updated,
|
2016-08-26 23:53:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:28:51 +05:30
|
|
|
// HashTag returns unique hash tag for comment.
|
|
|
|
func (c *Comment) HashTag() string {
|
|
|
|
return "issuecomment-" + com.ToStr(c.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventTag returns unique event hash tag for comment.
|
|
|
|
func (c *Comment) EventTag() string {
|
|
|
|
return "event-" + com.ToStr(c.ID)
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:16:45 +05:30
|
|
|
// LoadLabel if comment.Type is CommentTypeLabel, then load Label
|
|
|
|
func (c *Comment) LoadLabel() error {
|
|
|
|
var label Label
|
|
|
|
has, err := x.ID(c.LabelID).Get(&label)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return ErrLabelNotExist{
|
|
|
|
LabelID: c.LabelID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Label = &label
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 08:06:08 +05:30
|
|
|
// LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
|
|
|
|
func (c *Comment) LoadMilestone() error {
|
|
|
|
if c.OldMilestoneID > 0 {
|
|
|
|
var oldMilestone Milestone
|
|
|
|
has, err := x.ID(c.OldMilestoneID).Get(&oldMilestone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return ErrMilestoneNotExist{
|
|
|
|
ID: c.OldMilestoneID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.OldMilestone = &oldMilestone
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MilestoneID > 0 {
|
|
|
|
var milestone Milestone
|
|
|
|
has, err := x.ID(c.MilestoneID).Get(&milestone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return ErrMilestoneNotExist{
|
|
|
|
ID: c.MilestoneID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Milestone = &milestone
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:39:10 +05:30
|
|
|
// LoadAssignees if comment.Type is CommentTypeAssignees, then load assignees
|
|
|
|
func (c *Comment) LoadAssignees() error {
|
|
|
|
var err error
|
|
|
|
if c.OldAssigneeID > 0 {
|
|
|
|
c.OldAssignee, err = getUserByID(x, c.OldAssigneeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AssigneeID > 0 {
|
|
|
|
c.Assignee, err = getUserByID(x, c.AssigneeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-15 22:06:39 +05:30
|
|
|
// MailParticipants sends new comment emails to repository watchers
|
|
|
|
// and mentioned people.
|
2016-12-22 14:30:39 +05:30
|
|
|
func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
|
2016-11-28 19:03:09 +05:30
|
|
|
mentions := markdown.FindAllMentions(c.Content)
|
2016-12-22 14:30:39 +05:30
|
|
|
if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
|
2016-11-28 19:03:09 +05:30
|
|
|
return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
|
2016-07-15 22:06:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
switch opType {
|
2016-11-07 21:07:32 +05:30
|
|
|
case ActionCommentIssue:
|
2016-11-28 19:03:09 +05:30
|
|
|
issue.Content = c.Content
|
2016-11-07 21:07:32 +05:30
|
|
|
case ActionCloseIssue:
|
2016-07-15 22:06:39 +05:30
|
|
|
issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
|
2016-11-07 21:07:32 +05:30
|
|
|
case ActionReopenIssue:
|
2016-07-15 22:06:39 +05:30
|
|
|
issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
|
|
|
|
}
|
2016-11-28 19:03:09 +05:30
|
|
|
if err = mailIssueCommentToParticipants(issue, c.Poster, mentions); err != nil {
|
2016-07-15 22:06:39 +05:30
|
|
|
log.Error(4, "mailIssueCommentToParticipants: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:28:51 +05:30
|
|
|
func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
|
2017-01-30 18:16:45 +05:30
|
|
|
var LabelID int64
|
|
|
|
if opts.Label != nil {
|
|
|
|
LabelID = opts.Label.ID
|
|
|
|
}
|
2016-03-05 23:28:51 +05:30
|
|
|
comment := &Comment{
|
2017-02-01 08:06:08 +05:30
|
|
|
Type: opts.Type,
|
|
|
|
PosterID: opts.Doer.ID,
|
|
|
|
Poster: opts.Doer,
|
|
|
|
IssueID: opts.Issue.ID,
|
|
|
|
LabelID: LabelID,
|
|
|
|
OldMilestoneID: opts.OldMilestoneID,
|
|
|
|
MilestoneID: opts.MilestoneID,
|
2017-02-03 20:39:10 +05:30
|
|
|
OldAssigneeID: opts.OldAssigneeID,
|
|
|
|
AssigneeID: opts.AssigneeID,
|
2017-02-01 08:06:08 +05:30
|
|
|
CommitID: opts.CommitID,
|
|
|
|
CommitSHA: opts.CommitSHA,
|
|
|
|
Line: opts.LineNum,
|
|
|
|
Content: opts.Content,
|
2017-02-05 20:06:00 +05:30
|
|
|
OldTitle: opts.OldTitle,
|
|
|
|
NewTitle: opts.NewTitle,
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
if _, err = e.Insert(comment); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:16:45 +05:30
|
|
|
if err = opts.Repo.getOwner(e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-06 04:38:42 +05:30
|
|
|
// Compose comment action, could be plain comment, close or reopen issue/pull request.
|
2016-03-05 23:28:51 +05:30
|
|
|
// This object will be used to notify watchers in the end of function.
|
|
|
|
act := &Action{
|
2016-07-23 22:38:22 +05:30
|
|
|
ActUserID: opts.Doer.ID,
|
2016-03-05 23:28:51 +05:30
|
|
|
ActUserName: opts.Doer.Name,
|
|
|
|
Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
|
|
|
|
RepoID: opts.Repo.ID,
|
|
|
|
RepoUserName: opts.Repo.Owner.Name,
|
|
|
|
RepoName: opts.Repo.Name,
|
|
|
|
IsPrivate: opts.Repo.IsPrivate,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check comment type.
|
|
|
|
switch opts.Type {
|
2016-11-07 22:00:04 +05:30
|
|
|
case CommentTypeComment:
|
2016-11-07 21:07:32 +05:30
|
|
|
act.OpType = ActionCommentIssue
|
2016-03-05 23:28:51 +05:30
|
|
|
|
|
|
|
if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check attachments
|
|
|
|
attachments := make([]*Attachment, 0, len(opts.Attachments))
|
|
|
|
for _, uuid := range opts.Attachments {
|
|
|
|
attach, err := getAttachmentByUUID(e, uuid)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrAttachmentNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-15 22:06:39 +05:30
|
|
|
return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
attachments = append(attachments, attach)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range attachments {
|
|
|
|
attachments[i].IssueID = opts.Issue.ID
|
|
|
|
attachments[i].CommentID = comment.ID
|
|
|
|
// No assign value could be 0, so ignore AllCols().
|
|
|
|
if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
|
2016-07-15 22:06:39 +05:30
|
|
|
return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:00:04 +05:30
|
|
|
case CommentTypeReopen:
|
2016-11-07 21:07:32 +05:30
|
|
|
act.OpType = ActionReopenIssue
|
2016-03-05 23:28:51 +05:30
|
|
|
if opts.Issue.IsPull {
|
2016-11-07 21:07:32 +05:30
|
|
|
act.OpType = ActionReopenPullRequest
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Issue.IsPull {
|
|
|
|
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
|
|
|
|
} else {
|
|
|
|
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-06 04:38:42 +05:30
|
|
|
|
2016-11-07 22:05:34 +05:30
|
|
|
case CommentTypeClose:
|
2016-11-07 21:07:32 +05:30
|
|
|
act.OpType = ActionCloseIssue
|
2016-03-05 23:28:51 +05:30
|
|
|
if opts.Issue.IsPull {
|
2016-11-07 21:07:32 +05:30
|
|
|
act.OpType = ActionClosePullRequest
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Issue.IsPull {
|
|
|
|
_, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
|
|
|
|
} else {
|
|
|
|
_, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-15 22:06:39 +05:30
|
|
|
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
2016-07-15 22:06:39 +05:30
|
|
|
// Notify watchers for whatever action comes in, ignore if no action type.
|
2016-03-11 15:41:58 +05:30
|
|
|
if act.OpType > 0 {
|
|
|
|
if err = notifyWatchers(e, act); err != nil {
|
2016-07-15 22:06:39 +05:30
|
|
|
log.Error(4, "notifyWatchers: %v", err)
|
2016-03-11 15:41:58 +05:30
|
|
|
}
|
2016-12-22 14:30:39 +05:30
|
|
|
if err = comment.MailParticipants(e, act.OpType, opts.Issue); err != nil {
|
|
|
|
log.Error(4, "MailParticipants: %v", err)
|
|
|
|
}
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return comment, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
|
2016-11-07 22:05:34 +05:30
|
|
|
cmtType := CommentTypeClose
|
2016-03-05 23:28:51 +05:30
|
|
|
if !issue.IsClosed {
|
2016-11-07 22:00:04 +05:30
|
|
|
cmtType = CommentTypeReopen
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
return createComment(e, &CreateCommentOptions{
|
|
|
|
Type: cmtType,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:16:45 +05:30
|
|
|
func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, label *Label, add bool) (*Comment, error) {
|
|
|
|
var content string
|
|
|
|
if add {
|
|
|
|
content = "1"
|
|
|
|
}
|
|
|
|
return createComment(e, &CreateCommentOptions{
|
|
|
|
Type: CommentTypeLabel,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
Label: label,
|
|
|
|
Content: content,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-01 08:06:08 +05:30
|
|
|
func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
|
|
|
|
return createComment(e, &CreateCommentOptions{
|
|
|
|
Type: CommentTypeMilestone,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldMilestoneID: oldMilestoneID,
|
|
|
|
MilestoneID: milestoneID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:39:10 +05:30
|
|
|
func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldAssigneeID, assigneeID int64) (*Comment, error) {
|
|
|
|
return createComment(e, &CreateCommentOptions{
|
|
|
|
Type: CommentTypeAssignees,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldAssigneeID: oldAssigneeID,
|
|
|
|
AssigneeID: assigneeID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:06:00 +05:30
|
|
|
func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
|
|
|
|
return createComment(e, &CreateCommentOptions{
|
|
|
|
Type: CommentTypeChangeTitle,
|
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
OldTitle: oldTitle,
|
|
|
|
NewTitle: newTitle,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// CreateCommentOptions defines options for creating comment
|
2016-03-05 23:28:51 +05:30
|
|
|
type CreateCommentOptions struct {
|
|
|
|
Type CommentType
|
|
|
|
Doer *User
|
|
|
|
Repo *Repository
|
|
|
|
Issue *Issue
|
2017-01-30 18:16:45 +05:30
|
|
|
Label *Label
|
2016-03-05 23:28:51 +05:30
|
|
|
|
2017-02-01 08:06:08 +05:30
|
|
|
OldMilestoneID int64
|
|
|
|
MilestoneID int64
|
2017-02-03 20:39:10 +05:30
|
|
|
OldAssigneeID int64
|
|
|
|
AssigneeID int64
|
2017-02-05 20:06:00 +05:30
|
|
|
OldTitle string
|
|
|
|
NewTitle string
|
2017-02-01 08:06:08 +05:30
|
|
|
CommitID int64
|
|
|
|
CommitSHA string
|
|
|
|
LineNum int64
|
|
|
|
Content string
|
|
|
|
Attachments []string // UUIDs of attachments
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// CreateComment creates comment of issue or commit.
|
|
|
|
func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sessionRelease(sess)
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
comment, err = createComment(sess, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return comment, sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIssueComment creates a plain issue comment.
|
|
|
|
func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
|
|
|
|
return CreateComment(&CreateCommentOptions{
|
2016-11-07 22:00:04 +05:30
|
|
|
Type: CommentTypeComment,
|
2016-03-05 23:28:51 +05:30
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
Content: content,
|
|
|
|
Attachments: attachments,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRefComment creates a commit reference comment to issue.
|
|
|
|
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
|
|
|
|
if len(commitSHA) == 0 {
|
|
|
|
return fmt.Errorf("cannot create reference with empty commit SHA")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if same reference from same commit has already existed.
|
|
|
|
has, err := x.Get(&Comment{
|
2016-11-07 22:05:34 +05:30
|
|
|
Type: CommentTypeCommitRef,
|
2016-03-05 23:28:51 +05:30
|
|
|
IssueID: issue.ID,
|
|
|
|
CommitSHA: commitSHA,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("check reference comment: %v", err)
|
|
|
|
} else if has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = CreateComment(&CreateCommentOptions{
|
2016-11-07 22:05:34 +05:30
|
|
|
Type: CommentTypeCommitRef,
|
2016-03-05 23:28:51 +05:30
|
|
|
Doer: doer,
|
|
|
|
Repo: repo,
|
|
|
|
Issue: issue,
|
|
|
|
CommitSHA: commitSHA,
|
|
|
|
Content: content,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommentByID returns the comment by given ID.
|
|
|
|
func GetCommentByID(id int64) (*Comment, error) {
|
|
|
|
c := new(Comment)
|
|
|
|
has, err := x.Id(id).Get(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2016-08-27 02:10:53 +05:30
|
|
|
return nil, ErrCommentNotExist{id, 0}
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-08-27 02:10:53 +05:30
|
|
|
func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
|
2016-03-05 23:28:51 +05:30
|
|
|
comments := make([]*Comment, 0, 10)
|
2016-11-10 20:46:32 +05:30
|
|
|
sess := e.
|
|
|
|
Where("issue_id = ?", issueID).
|
|
|
|
Asc("created_unix")
|
2016-08-27 02:10:53 +05:30
|
|
|
if since > 0 {
|
2016-08-27 02:37:21 +05:30
|
|
|
sess.And("updated_unix >= ?", since)
|
2016-08-27 02:10:53 +05:30
|
|
|
}
|
|
|
|
return comments, sess.Find(&comments)
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:59:26 +05:30
|
|
|
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
|
|
|
|
comments := make([]*Comment, 0, 10)
|
2017-01-14 07:51:30 +05:30
|
|
|
sess := e.Where("issue.repo_id = ?", repoID).
|
|
|
|
Join("INNER", "issue", "issue.id = comment.issue_id").
|
|
|
|
Asc("comment.created_unix")
|
2016-12-22 13:59:26 +05:30
|
|
|
if since > 0 {
|
2017-01-14 07:51:30 +05:30
|
|
|
sess.And("comment.updated_unix >= ?", since)
|
2016-12-22 13:59:26 +05:30
|
|
|
}
|
|
|
|
return comments, sess.Find(&comments)
|
|
|
|
}
|
|
|
|
|
2016-08-27 02:10:53 +05:30
|
|
|
func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
|
|
|
|
return getCommentsByIssueIDSince(e, issueID, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommentsByIssueID returns all comments of an issue.
|
|
|
|
func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
|
|
|
|
return getCommentsByIssueID(x, issueID)
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:03:09 +05:30
|
|
|
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
|
2016-08-27 02:10:53 +05:30
|
|
|
func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
|
|
|
|
return getCommentsByIssueIDSince(x, issueID, since)
|
2016-03-05 23:28:51 +05:30
|
|
|
}
|
|
|
|
|
2016-12-22 13:59:26 +05:30
|
|
|
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
|
|
|
|
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
|
|
|
|
return getCommentsByRepoIDSince(x, repoID, since)
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:28:51 +05:30
|
|
|
// UpdateComment updates information of comment.
|
|
|
|
func UpdateComment(c *Comment) error {
|
|
|
|
_, err := x.Id(c.ID).AllCols().Update(c)
|
|
|
|
return err
|
|
|
|
}
|
2016-07-26 00:18:17 +05:30
|
|
|
|
2017-01-25 08:13:02 +05:30
|
|
|
// DeleteComment deletes the comment
|
|
|
|
func DeleteComment(comment *Comment) error {
|
2016-07-26 00:18:17 +05:30
|
|
|
sess := x.NewSession()
|
|
|
|
defer sessionRelease(sess)
|
2017-01-25 08:13:02 +05:30
|
|
|
if err := sess.Begin(); err != nil {
|
2016-07-26 00:18:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 20:24:52 +05:30
|
|
|
if _, err := sess.Delete(&Comment{
|
|
|
|
ID: comment.ID,
|
|
|
|
}); err != nil {
|
2016-07-26 00:18:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:00:04 +05:30
|
|
|
if comment.Type == CommentTypeComment {
|
2017-01-25 08:13:02 +05:30
|
|
|
if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
|
2016-07-26 00:18:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|