Skip to content

Commit 8c6c1b3

Browse files
committed
ListAllInRepo & Delete Issue-Comments
1 parent 83ed234 commit 8c6c1b3

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

models/issue_comment.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro
375375
return comments, sess.Find(&comments)
376376
}
377377

378+
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
379+
comments := make([]*Comment, 0, 10)
380+
sess := e.Where("issue.repo_id = ?", repoID).Join("INNER", "issue", "issue.id = comment.issue_id", repoID).Asc("created_unix")
381+
if since > 0 {
382+
sess.And("updated_unix >= ?", since)
383+
}
384+
return comments, sess.Find(&comments)
385+
}
386+
378387
func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) {
379388
return getCommentsByIssueIDSince(e, issueID, -1)
380389
}
@@ -389,6 +398,11 @@ func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
389398
return getCommentsByIssueIDSince(x, issueID, since)
390399
}
391400

401+
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
402+
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
403+
return getCommentsByRepoIDSince(x, repoID, since)
404+
}
405+
392406
// UpdateComment updates information of comment.
393407
func UpdateComment(c *Comment) error {
394408
_, 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: 46 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,29 @@ func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
84104
}
85105
ctx.JSON(200, comment.APIFormat())
86106
}
107+
108+
func DeleteIssueComment(ctx *context.APIContext) {
109+
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
110+
if err != nil {
111+
if models.IsErrCommentNotExist(err) {
112+
ctx.Error(404, "GetCommentByID", err)
113+
} else {
114+
ctx.Error(500, "GetCommentByID", err)
115+
}
116+
return
117+
}
118+
119+
if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
120+
ctx.Status(403)
121+
return
122+
} else if comment.Type != models.COMMENT_TYPE_COMMENT {
123+
ctx.Status(204)
124+
return
125+
}
126+
127+
if err = models.DeleteCommentByID(comment.ID); err != nil {
128+
ctx.Error(500, "DeleteCommentByID", err)
129+
return
130+
}
131+
ctx.Status(204)
132+
}

0 commit comments

Comments
 (0)