Skip to content

Commit 255adc4

Browse files
ethantkoeniglunny
authored andcommitted
Don't show non-comments in comments API (#2001)
1 parent 4df1a24 commit 255adc4

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

integrations/api_comment_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
api "code.gitea.io/sdk/gitea"
14+
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestAPIListComments(t *testing.T) {
19+
prepareTestEnv(t)
20+
21+
comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
22+
models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
23+
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
24+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
25+
repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
26+
27+
session := loginUser(t, repoOwner.Name)
28+
requestUrl := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments",
29+
repoOwner.Name, repo.Name, issue.Index)
30+
req := NewRequest(t, "GET", requestUrl)
31+
resp := session.MakeRequest(t, req)
32+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
33+
34+
var comments []*api.Comment
35+
DecodeJSON(t, resp, &comments)
36+
expectedCount := models.GetCount(t, &models.Comment{IssueID: issue.ID},
37+
models.Cond("type = ?", models.CommentTypeComment))
38+
assert.EqualValues(t, expectedCount, len(comments))
39+
}

integrations/api_team_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package integrations
66

77
import (
8-
"bytes"
9-
"encoding/json"
108
"fmt"
119
"net/http"
1210
"testing"
@@ -30,8 +28,7 @@ func TestAPITeam(t *testing.T) {
3028
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
3129

3230
var apiTeam api.Team
33-
decoder := json.NewDecoder(bytes.NewBuffer(resp.Body))
34-
assert.NoError(t, decoder.Decode(&apiTeam))
31+
DecodeJSON(t, resp, &apiTeam)
3532
assert.EqualValues(t, team.ID, apiTeam.ID)
3633
assert.Equal(t, team.Name, apiTeam.Name)
3734
}

integrations/integration_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,8 @@ func MakeRequest(req *http.Request) *TestResponse {
252252
Headers: respWriter.Headers,
253253
}
254254
}
255+
256+
func DecodeJSON(t testing.TB, resp *TestResponse, v interface{}) {
257+
decoder := json.NewDecoder(bytes.NewBuffer(resp.Body))
258+
assert.NoError(t, decoder.Decode(v))
259+
}

integrations/version_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package integrations
66

77
import (
8-
"bytes"
9-
"encoding/json"
108
"net/http"
119
"testing"
1210

@@ -22,11 +20,9 @@ func TestVersion(t *testing.T) {
2220
setting.AppVer = "1.1.0+dev"
2321
req := NewRequest(t, "GET", "/api/v1/version")
2422
resp := MakeRequest(req)
23+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
2524

2625
var version gitea.ServerVersion
27-
decoder := json.NewDecoder(bytes.NewBuffer(resp.Body))
28-
assert.NoError(t, decoder.Decode(&version))
29-
30-
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
26+
DecodeJSON(t, resp, &version)
3127
assert.Equal(t, setting.AppVer, string(version.Version))
3228
}

models/issue_comment.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro
572572
comments := make([]*Comment, 0, 10)
573573
sess := e.
574574
Where("issue_id = ?", issueID).
575+
Where("type = ?", CommentTypeComment).
575576
Asc("created_unix")
576577
if since > 0 {
577578
sess.And("updated_unix >= ?", since)
@@ -582,6 +583,7 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro
582583
func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) {
583584
comments := make([]*Comment, 0, 10)
584585
sess := e.Where("issue.repo_id = ?", repoID).
586+
Where("comment.type = ?", CommentTypeComment).
585587
Join("INNER", "issue", "issue.id = comment.issue_id").
586588
Asc("comment.created_unix")
587589
if since > 0 {

0 commit comments

Comments
 (0)