"> repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) { comment, err := CreateComment(&CreateCommentOptions{ Type: CommentTypeComment, Doer: doer, Repo: repo, Issue: issue, Content: content, Attachments: attachments, }) if err != nil { return nil, fmt.Errorf("CreateComment: %v", err) } mode, _ := AccessLevel(doer, repo) if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: issue.APIFormat(), Comment: comment.APIFormat(), Repository: repo.APIFormat(mode), Sender: doer.APIFormat(), }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) } else { go HookQueue.Add(repo.ID) } return comment, nil } // CreateCodeComment creates a plain code comment at the specified line / path func CreateCodeComment(doer *User, repo *Repository, issue *Issue, content, treePath string, line, reviewID int64) (*Comment, error) { var commitID, patch string pr, err := GetPullRequestByIssueID(issue.ID) if err != nil { return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err) } if err := pr.GetBaseRepo(); err != nil { return nil, fmt.Errorf("GetHeadRepo: %v", err) } gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } // FIXME validate treePath // Get latest commit referencing the commented line // No need for get commit for base branch changes if line > 0 { commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line)) if err == nil { commitID = commit.ID.String() } else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") { return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err) } } // Only fetch diff if comment is review comment if reviewID != 0 { headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err) } patchBuf := new(bytes.Buffer) if err := GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, RawDiffNormal, treePath, patchBuf); err != nil { return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath) } patch = CutDiffAroundLine(strings.NewReader(patchBuf.String()), int64((&Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines) } return CreateComment(&CreateCommentOptions{ Type: CommentTypeCode, Doer: doer, Repo: repo, Issue: issue, Content: content, LineNum: line, TreePath: treePath, CommitSHA: commitID, ReviewID: reviewID, Patch: patch, }) } // 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{ Type: CommentTypeCommitRef, IssueID: issue.ID, CommitSHA: commitSHA, }) if err != nil { return fmt.Errorf("check reference comment: %v", err) } else if has { return nil } _, err = CreateComment(&CreateCommentOptions{ Type: CommentTypeCommitRef, 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 { return nil, ErrCommentNotExist{id, 0} } return c, nil } // FindCommentsOptions describes the conditions to Find comments type FindCommentsOptions struct { RepoID int64 IssueID int64 ReviewID int64 Since int64 Type CommentType } func (opts *FindCommentsOptions) toConds() builder.Cond { var cond = builder.NewCond() if opts.RepoID > 0 { cond = cond.And(builder.Eq{"issue.repo_id": opts.RepoID}) } if opts.IssueID > 0 { cond = cond.And(builder.Eq{"comment.issue_id": opts.IssueID}) } if opts.ReviewID > 0 { cond = cond.And(builder.Eq{"comment.review_id": opts.ReviewID}) } if opts.Since > 0 { cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since}) } if opts.Type != CommentTypeUnknown { cond = cond.And(builder.Eq{"comment.type": opts.Type}) } return cond } func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) { comments := make([]*Comment, 0, 10) sess := e.Where(opts.toConds()) if opts.RepoID > 0 { sess.Join("INNER", "issue", "issue.id = comment.issue_id") } return comments, sess. Asc("comment.created_unix"). Asc("comment.id"). Find(&comments) } // FindComments returns all comments according options func FindComments(opts FindCommentsOptions) ([]*Comment, error) { return findComments(x, opts) } // UpdateComment updates information of comment. func UpdateComment(doer *User, c *Comment, oldContent string) error { if _, err := x.ID(c.ID).AllCols().Update(c); err != nil { return err } if err := c.LoadPoster(); err != nil { return err } if err := c.LoadIssue(); err != nil { return err } if err := c.Issue.LoadAttributes(); err != nil { return err } if err := c.loadPoster(x); err != nil { return err } mode, _ := AccessLevel(doer, c.Issue.Repo) if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: c.Issue.APIFormat(), Comment: c.APIFormat(), Changes: &api.ChangesPayload{ Body: &api.ChangesFromPayload{ From: oldContent, }, }, Repository: c.Issue.Repo.APIFormat(mode), Sender: doer.APIFormat(), }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err) } else { go HookQueue.Add(c.Issue.Repo.ID) } return nil } // DeleteComment deletes the comment func DeleteComment(doer *User, comment *Comment) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err } if _, err := sess.Delete(&Comment{ ID: comment.ID, }); err != nil { return err } if comment.Type == CommentTypeComment { if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil { return err } } if _, err := sess.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true}); err != nil { return err } if err := sess.Commit(); err != nil { return err } sess.Close() if err := comment.LoadPoster(); err != nil { return err } if err := comment.LoadIssue(); err != nil { return err } if err := comment.Issue.LoadAttributes(); err != nil { return err } if err := comment.loadPoster(x); err != nil { return err } mode, _ := AccessLevel(doer, comment.Issue.Repo) if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: comment.Issue.APIFormat(), Comment: comment.APIFormat(), Repository: comment.Issue.Repo.APIFormat(mode), Sender: doer.APIFormat(), }); err != nil { log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) } else { go HookQueue.Add(comment.Issue.Repo.ID) } return nil } // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS type CodeComments map[string]map[int64][]*Comment func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (CodeComments, error) { return fetchCodeCommentsByReview(e, issue, currentUser, nil) } func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review *Review) (CodeComments, error) { pathToLineToComment := make(CodeComments) if review == nil { review = &Review{ID: 0} } //Find comments opts := FindCommentsOptions{ Type: CommentTypeCode, IssueID: issue.ID, ReviewID: review.ID, } conds := opts.toConds() if review.ID == 0 { conds = conds.And(builder.Eq{"invalidated": false}) } var comments []*Comment if err := e.Where(conds). Asc("comment.created_unix"). Asc("comment.id"). Find(&comments); err != nil { return nil, err } if err := CommentList(comments).loadPosters(e); err != nil { return nil, err } if err := issue.loadRepo(e); err != nil { return nil, err } if err := CommentList(comments).loadPosters(e); err != nil { return nil, err } // Find all reviews by ReviewID reviews := make(map[int64]*Review) var ids = make([]int64, 0, len(comments)) for _, comment := range comments { if comment.ReviewID != 0 { ids = append(ids, comment.ReviewID) } } if err := e.In("id", ids).Find(&reviews); err != nil { return nil, err } for _, comment := range comments { if re, ok := reviews[comment.ReviewID]; ok && re != nil { // If the review is pending only the author can see the comments (except the review is set) if review.ID == 0 { if re.Type == ReviewTypePending && (currentUser == nil || currentUser.ID != re.ReviewerID) { continue } } comment.Review = re } comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(), issue.Repo.ComposeMetas())) if pathToLineToComment[comment.TreePath] == nil { pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment) } pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment) } return pathToLineToComment, nil } // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line func FetchCodeComments(issue *Issue, currentUser *User) (CodeComments, error) { return fetchCodeComments(x, issue, currentUser) }