Skip to content

Commit df7fa4e

Browse files
bkcsoftlunny
authored andcommitted
issue comment api fix (#449)
* ListAllInRepo & Delete Issue-Comments * Moar data in issue-comments
1 parent d5d21b6 commit df7fa4e

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed

models/issue_comment.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,55 @@ func (c *Comment) AfterDelete() {
123123
}
124124
}
125125

126+
// HTMLURL formats a URL-string to the issue-comment
127+
func (c *Comment) HTMLURL() string {
128+
issue, err := GetIssueByID(c.IssueID)
129+
if err != nil { // Silently dropping errors :unamused:
130+
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
131+
return ""
132+
}
133+
return fmt.Sprintf("%s#issuecomment-%d", issue.HTMLURL(), c.ID)
134+
}
135+
136+
// IssueURL formats a URL-string to the issue
137+
func (c *Comment) IssueURL() string {
138+
issue, err := GetIssueByID(c.IssueID)
139+
if err != nil { // Silently dropping errors :unamused:
140+
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
141+
return ""
142+
}
143+
144+
if issue.IsPull {
145+
return ""
146+
}
147+
return issue.HTMLURL()
148+
}
149+
150+
// PRURL formats a URL-string to the pull-request
151+
func (c *Comment) PRURL() string {
152+
issue, err := GetIssueByID(c.IssueID)
153+
if err != nil { // Silently dropping errors :unamused:
154+
log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
155+
return ""
156+
}
157+
158+
if !issue.IsPull {
159+
return ""
160+
}
161+
return issue.HTMLURL()
162+
}
163+
126164
// APIFormat converts a Comment to the api.Comment format
127165
func (c *Comment) APIFormat() *api.Comment {
128166
return &api.Comment{
129-
ID: c.ID,
130-
Poster: c.Poster.APIFormat(),
131-
Body: c.Content,
132-
Created: c.Created,
133-
Updated: c.Updated,
167+
ID: c.ID,
168+
Poster: c.Poster.APIFormat(),
169+
HTMLURL: c.HTMLURL(),
170+
IssueURL: c.IssueURL(),
171+
PRURL: c.PRURL(),
172+
Body: c.Content,
173+
Created: c.Created,
174+
Updated: c.Updated,
134175
}
135176
}
136177

@@ -375,6 +416,15 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro
375416
return comments, sess.Find(&comments)
376417
}
377418

419+
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
420+
comments := make([]*Comment, 0, 10)
421+
sess := e.Where("issue.repo_id = ?", repoID).Join("INNER", "issue", "issue.id = comment.issue_id", repoID).Asc("created_unix")
422+
if since > 0 {
423+
sess.And("updated_unix >= ?", since)
424+
}
425+
return comments, sess.Find(&comments)
426+
}
427+
378428
func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
379429
return getCommentsByIssueIDSince(e, issueID, -1)
380430
}
@@ -389,6 +439,11 @@ func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
389439
return getCommentsByIssueIDSince(x, issueID, since)
390440
}
391441

442+
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
443+
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
444+
return getCommentsByRepoIDSince(x, repoID, since)
445+
}
446+
392447
// UpdateComment updates information of comment.
393448
func UpdateComment(c *Comment) error {
394449
_, err := x.Id(c.ID).AllCols().Update(c)

routers/api/v1/api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,17 @@ func RegisterRoutes(m *macaron.Macaron) {
280280
})
281281
m.Group("/issues", func() {
282282
m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
283+
m.Group("/comments", func() {
284+
m.Get("", repo.ListRepoIssueComments)
285+
m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
286+
})
283287
m.Group("/:index", func() {
284288
m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
285289

286290
m.Group("/comments", func() {
287291
m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
288-
m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
292+
m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
293+
Delete(repo.DeleteIssueComment)
289294
})
290295

291296
m.Group("/labels", func() {

routers/api/v1/repo/issue_comment.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ func ListIssueComments(ctx *context.APIContext) {
4040
ctx.JSON(200, &apiComments)
4141
}
4242

43+
// ListRepoIssueComments returns all issue-comments for an issue
44+
func ListRepoIssueComments(ctx *context.APIContext) {
45+
var since time.Time
46+
if len(ctx.Query("since")) > 0 {
47+
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
48+
}
49+
50+
comments, err := models.GetCommentsByRepoIDSince(ctx.Repo.Repository.ID, since.Unix())
51+
if err != nil {
52+
ctx.Error(500, "GetCommentsByRepoIDSince", err)
53+
return
54+
}
55+
56+
apiComments := make([]*api.Comment, len(comments))
57+
for i := range comments {
58+
apiComments[i] = comments[i].APIFormat()
59+
}
60+
ctx.JSON(200, &apiComments)
61+
}
62+
4363
// CreateIssueComment create a comment for an issue
4464
func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
4565
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
@@ -84,3 +104,30 @@ func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
84104
}
85105
ctx.JSON(200, comment.APIFormat())
86106
}
107+
108+
// DeleteIssueComment delete a comment from an issue
109+
func DeleteIssueComment(ctx *context.APIContext) {
110+
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
111+
if err != nil {
112+
if models.IsErrCommentNotExist(err) {
113+
ctx.Error(404, "GetCommentByID", err)
114+
} else {
115+
ctx.Error(500, "GetCommentByID", err)
116+
}
117+
return
118+
}
119+
120+
if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
121+
ctx.Status(403)
122+
return
123+
} else if comment.Type != models.CommentTypeComment {
124+
ctx.Status(204)
125+
return
126+
}
127+
128+
if err = models.DeleteCommentByID(comment.ID); err != nil {
129+
ctx.Error(500, "DeleteCommentByID", err)
130+
return
131+
}
132+
ctx.Status(204)
133+
}

0 commit comments

Comments
 (0)