Skip to content

Commit d530fa6

Browse files
committed
1 parent a91be46 commit d530fa6

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
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: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ import (
1616

1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/markdown"
19+
"github.com/go-xorm/builder"
1920
)
2021

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,45 +574,68 @@ 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-
Asc("created_unix")
576-
if since > 0 {
577-
sess.And("updated_unix >= ?", since)
577+
// FindCommentsOptions describes the condtions 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})
589+
}
590+
if opts.IssueID > 0 {
591+
cond = cond.And(builder.Eq{"issue.id": opts.IssueID})
578592
}
579-
return comments, sess.Find(&comments)
593+
if opts.Since > 0 {
594+
cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since})
595+
}
596+
if opts.Type > -1 {
597+
cond = cond.And(builder.Eq{"comment.type": opts.Type})
598+
}
599+
return cond
580600
}
581601

582-
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
602+
func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) {
583603
comments := make([]*Comment, 0, 10)
584-
sess := e.Where("issue.repo_id = ?", repoID).
585-
Join("INNER", "issue", "issue.id = comment.issue_id").
586-
Asc("comment.created_unix")
587-
if since > 0 {
588-
sess.And("comment.updated_unix >= ?", since)
589-
}
590-
return comments, sess.Find(&comments)
604+
return comments, e.Join("INNER", "issue", "issue.id = comment.issue_id").
605+
Where(opts.toConds()).
606+
Asc("comment.created_unix").
607+
Find(&comments)
591608
}
592609

593-
func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
594-
return getCommentsByIssueIDSince(e, issueID, -1)
610+
// FindComments returns all comments according options
611+
func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
612+
return findComments(x, opts)
595613
}
596614

597615
// GetCommentsByIssueID returns all comments of an issue.
598616
func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
599-
return getCommentsByIssueID(x, issueID)
617+
return findComments(x, FindCommentsOptions{
618+
IssueID: issueID,
619+
Type: CommentTypeUnknown,
620+
})
600621
}
601622

602623
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
603624
func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
604-
return getCommentsByIssueIDSince(x, issueID, since)
625+
return findComments(x, FindCommentsOptions{
626+
IssueID: issueID,
627+
Type: CommentTypeUnknown,
628+
Since: since,
629+
})
605630
}
606631

607632
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
608633
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
609-
return getCommentsByRepoIDSince(x, repoID, since)
634+
return findComments(x, FindCommentsOptions{
635+
RepoID: repoID,
636+
Type: CommentTypeUnknown,
637+
Since: since,
638+
})
610639
}
611640

612641
// 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)