From b2de3d71c567e170f12da8a92f7865df77545b1e Mon Sep 17 00:00:00 2001 From: "Kim \"BKC\" Carlbäcker" Date: Wed, 21 Dec 2016 07:21:15 +0100 Subject: More Issue-Comments API-endpoints (#3624) * ListAllInRepo & Delete Issue-Comments * Moar data in issue-comments --- models/issue_comment.go | 64 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 6 deletions(-) (limited to 'models') diff --git a/models/issue_comment.go b/models/issue_comment.go index ccfa765f..63d4cbf6 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -115,13 +115,51 @@ func (c *Comment) AfterDelete() { } } +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) +} + +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() +} + +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() +} + func (c *Comment) APIFormat() *api.Comment { return &api.Comment{ - ID: c.ID, - Poster: c.Poster.APIFormat(), - Body: c.Content, - Created: c.Created, - Updated: c.Updated, + 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, } } @@ -363,6 +401,15 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro return comments, sess.Find(&comments) } +func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) { + comments := make([]*Comment, 0, 10) + sess := e.Where("issue.repo_id = ?", repoID).Join("INNER", "issue", "issue.id = comment.issue_id", repoID).Asc("created_unix") + if since > 0 { + sess.And("updated_unix >= ?", since) + } + return comments, sess.Find(&comments) +} + func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) { return getCommentsByIssueIDSince(e, issueID, -1) } @@ -372,11 +419,16 @@ func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { return getCommentsByIssueID(x, issueID) } -// GetCommentsByIssueID returns a list of comments of an issue since a given time point. +// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point. func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) { return getCommentsByIssueIDSince(x, issueID, since) } +// 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) +} + // UpdateComment updates information of comment. func UpdateComment(c *Comment) error { _, err := x.Id(c.ID).AllCols().Update(c) -- cgit v1.2.3