Skip to content

Commit d71fad2

Browse files
authored
Fix #2001 and fix issue comments hidden (#2016)
* revert #2001 and fix issue comments hidden * fix #2001 * fix import * improve comment type * reduce unnecessary join * fix comment on FindCommentsOptions
1 parent 0a5dc64 commit d71fad2

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

models/issue.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
174174
}
175175

176176
if issue.Comments == nil {
177-
issue.Comments, err = getCommentsByIssueID(e, issue.ID)
177+
issue.Comments, err = findComments(e, FindCommentsOptions{
178+
IssueID: issue.ID,
179+
Type: CommentTypeUnknown,
180+
})
178181
if err != nil {
179182
return fmt.Errorf("getCommentsByIssueID [%d]: %v", issue.ID, err)
180183
}

models/issue_comment.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/Unknwon/com"
13+
"github.com/go-xorm/builder"
1314
"github.com/go-xorm/xorm"
1415

1516
api "code.gitea.io/sdk/gitea"
@@ -21,6 +22,11 @@ import (
2122
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
2223
type CommentType int
2324

25+
// define unknown comment type
26+
const (
27+
CommentTypeUnknown CommentType = -1
28+
)
29+
2430
// Enumerate all the comment types
2531
const (
2632
// Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
@@ -568,47 +574,71 @@ func GetCommentByID(id int64) (*Comment, error) {
568574
return c, nil
569575
}
570576

571-
func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
572-
comments := make([]*Comment, 0, 10)
573-
sess := e.
574-
Where("issue_id = ?", issueID).
575-
Where("type = ?", CommentTypeComment).
576-
Asc("created_unix")
577-
if since > 0 {
578-
sess.And("updated_unix >= ?", since)
577+
// FindCommentsOptions describes the conditions to Find comments
578+
type FindCommentsOptions struct {
579+
RepoID int64
580+
IssueID int64
581+
Since int64
582+
Type CommentType
583+
}
584+
585+
func (opts *FindCommentsOptions) toConds() builder.Cond {
586+
var cond = builder.NewCond()
587+
if opts.RepoID > 0 {
588+
cond = cond.And(builder.Eq{"issue.repo_id": opts.RepoID})
579589
}
580-
return comments, sess.Find(&comments)
590+
if opts.IssueID > 0 {
591+
cond = cond.And(builder.Eq{"comment.issue_id": opts.IssueID})
592+
}
593+
if opts.Since > 0 {
594+
cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since})
595+
}
596+
if opts.Type != CommentTypeUnknown {
597+
cond = cond.And(builder.Eq{"comment.type": opts.Type})
598+
}
599+
return cond
581600
}
582601

583-
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
602+
func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) {
584603
comments := make([]*Comment, 0, 10)
585-
sess := e.Where("issue.repo_id = ?", repoID).
586-
Where("comment.type = ?", CommentTypeComment).
587-
Join("INNER", "issue", "issue.id = comment.issue_id").
588-
Asc("comment.created_unix")
589-
if since > 0 {
590-
sess.And("comment.updated_unix >= ?", since)
604+
sess := e.Where(opts.toConds())
605+
if opts.RepoID > 0 {
606+
sess.Join("INNER", "issue", "issue.id = comment.issue_id")
591607
}
592-
return comments, sess.Find(&comments)
608+
return comments, sess.
609+
Asc("comment.created_unix").
610+
Find(&comments)
593611
}
594612

595-
func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
596-
return getCommentsByIssueIDSince(e, issueID, -1)
613+
// FindComments returns all comments according options
614+
func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
615+
return findComments(x, opts)
597616
}
598617

599618
// GetCommentsByIssueID returns all comments of an issue.
600619
func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
601-
return getCommentsByIssueID(x, issueID)
620+
return findComments(x, FindCommentsOptions{
621+
IssueID: issueID,
622+
Type: CommentTypeUnknown,
623+
})
602624
}
603625

604626
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
605627
func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
606-
return getCommentsByIssueIDSince(x, issueID, since)
628+
return findComments(x, FindCommentsOptions{
629+
IssueID: issueID,
630+
Type: CommentTypeUnknown,
631+
Since: since,
632+
})
607633
}
608634

609635
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
610636
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
611-
return getCommentsByRepoIDSince(x, repoID, since)
637+
return findComments(x, FindCommentsOptions{
638+
RepoID: repoID,
639+
Type: CommentTypeUnknown,
640+
Since: since,
641+
})
612642
}
613643

614644
// UpdateComment updates information of comment.

routers/api/v1/repo/issue_comment.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func ListIssueComments(ctx *context.APIContext) {
2727
return
2828
}
2929

30-
comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
30+
comments, err := models.FindComments(models.FindCommentsOptions{
31+
IssueID: issue.ID,
32+
Since: since.Unix(),
33+
Type: models.CommentTypeComment,
34+
})
3135
if err != nil {
3236
ctx.Error(500, "GetCommentsByIssueIDSince", err)
3337
return
@@ -47,7 +51,11 @@ func ListRepoIssueComments(ctx *context.APIContext) {
4751
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
4852
}
4953

50-
comments, err := models.GetCommentsByRepoIDSince(ctx.Repo.Repository.ID, since.Unix())
54+
comments, err := models.FindComments(models.FindCommentsOptions{
55+
RepoID: ctx.Repo.Repository.ID,
56+
Since: since.Unix(),
57+
Type: models.CommentTypeComment,
58+
})
5159
if err != nil {
5260
ctx.Error(500, "GetCommentsByRepoIDSince", err)
5361
return

0 commit comments

Comments
 (0)