@@ -10,6 +10,7 @@ import (
10
10
"time"
11
11
12
12
"github.com/Unknwon/com"
13
+ "github.com/go-xorm/builder"
13
14
"github.com/go-xorm/xorm"
14
15
15
16
api "code.gitea.io/sdk/gitea"
@@ -21,6 +22,11 @@ import (
21
22
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
22
23
type CommentType int
23
24
25
+ // define unknown comment type
26
+ const (
27
+ CommentTypeUnknown CommentType = - 1
28
+ )
29
+
24
30
// Enumerate all the comment types
25
31
const (
26
32
// 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) {
568
574
return c , nil
569
575
}
570
576
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 })
579
589
}
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
581
600
}
582
601
583
- func getCommentsByRepoIDSince (e Engine , repoID , since int64 ) ([]* Comment , error ) {
602
+ func findComments (e Engine , opts FindCommentsOptions ) ([]* Comment , error ) {
584
603
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" )
591
607
}
592
- return comments , sess .Find (& comments )
608
+ return comments , sess .
609
+ Asc ("comment.created_unix" ).
610
+ Find (& comments )
593
611
}
594
612
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 )
597
616
}
598
617
599
618
// GetCommentsByIssueID returns all comments of an issue.
600
619
func GetCommentsByIssueID (issueID int64 ) ([]* Comment , error ) {
601
- return getCommentsByIssueID (x , issueID )
620
+ return findComments (x , FindCommentsOptions {
621
+ IssueID : issueID ,
622
+ Type : CommentTypeUnknown ,
623
+ })
602
624
}
603
625
604
626
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
605
627
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
+ })
607
633
}
608
634
609
635
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
610
636
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
+ })
612
642
}
613
643
614
644
// UpdateComment updates information of comment.
0 commit comments