Skip to content

Commit 8fcda04

Browse files
ethantkoenigbkcsoft
authored andcommitted
Fix search by issue type (#1914)
* Fix search by issue type
1 parent bf48c8e commit 8fcda04

File tree

5 files changed

+121
-59
lines changed

5 files changed

+121
-59
lines changed

integrations/issue_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@ package integrations
66

77
import (
88
"net/http"
9+
"strconv"
10+
"strings"
911
"testing"
1012

13+
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/modules/setting"
15+
16+
"github.com/PuerkitoBio/goquery"
1117
"github.com/stretchr/testify/assert"
1218
)
1319

20+
func getIssuesSelection(htmlDoc *HtmlDoc) *goquery.Selection {
21+
return htmlDoc.doc.Find(".issue.list").Find("li").Find(".title")
22+
}
23+
24+
func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
25+
href, exists := issueSelection.Attr("href")
26+
assert.True(t, exists)
27+
indexStr := href[strings.LastIndexByte(href, '/')+1:]
28+
index, err := strconv.Atoi(indexStr)
29+
assert.NoError(t, err, "Invalid issue href: %s", href)
30+
return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
31+
}
32+
1433
func TestNoLoginViewIssues(t *testing.T) {
1534
prepareTestEnv(t)
1635

@@ -19,6 +38,37 @@ func TestNoLoginViewIssues(t *testing.T) {
1938
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
2039
}
2140

41+
func TestNoLoginViewIssuesSortByType(t *testing.T) {
42+
prepareTestEnv(t)
43+
44+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
45+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
46+
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
47+
48+
session := loginUser(t, user.Name, "password")
49+
req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
50+
resp := session.MakeRequest(t, req)
51+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
52+
53+
htmlDoc, err := NewHtmlParser(resp.Body)
54+
assert.NoError(t, err)
55+
issuesSelection := getIssuesSelection(htmlDoc)
56+
expectedNumIssues := models.GetCount(t,
57+
&models.Issue{RepoID: repo.ID, PosterID: user.ID},
58+
models.Cond("is_closed=?", false),
59+
models.Cond("is_pull=?", false),
60+
)
61+
if expectedNumIssues > setting.UI.IssuePagingNum {
62+
expectedNumIssues = setting.UI.IssuePagingNum
63+
}
64+
assert.EqualValues(t, expectedNumIssues, issuesSelection.Length())
65+
66+
issuesSelection.Each(func(_ int, selection *goquery.Selection) {
67+
issue := getIssue(t, repo.ID, selection)
68+
assert.EqualValues(t, user.ID, issue.PosterID)
69+
})
70+
}
71+
2272
func TestNoLoginViewIssue(t *testing.T) {
2373
prepareTestEnv(t)
2474

models/issue.go

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,6 @@ func parseCountResult(results []map[string][]byte) int64 {
12231223

12241224
// IssueStatsOptions contains parameters accepted by GetIssueStats.
12251225
type IssueStatsOptions struct {
1226-
FilterMode int
12271226
RepoID int64
12281227
Labels string
12291228
MilestoneID int64
@@ -1241,7 +1240,7 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
12411240
countSession := func(opts *IssueStatsOptions) *xorm.Session {
12421241
sess := x.
12431242
Where("issue.repo_id = ?", opts.RepoID).
1244-
And("is_pull = ?", opts.IsPull)
1243+
And("issue.is_pull = ?", opts.IsPull)
12451244

12461245
if len(opts.IssueIDs) > 0 {
12471246
sess.In("issue.id", opts.IssueIDs)
@@ -1252,8 +1251,8 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
12521251
if err != nil {
12531252
log.Warn("Malformed Labels argument: %s", opts.Labels)
12541253
} else if len(labelIDs) > 0 {
1255-
sess.Join("INNER", "issue_label", "issue.id = issue_id").
1256-
In("label_id", labelIDs)
1254+
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
1255+
In("issue_label.label_id", labelIDs)
12571256
}
12581257
}
12591258

@@ -1262,11 +1261,11 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
12621261
}
12631262

12641263
if opts.AssigneeID > 0 {
1265-
sess.And("assignee_id = ?", opts.AssigneeID)
1264+
sess.And("issue.assignee_id = ?", opts.AssigneeID)
12661265
}
12671266

12681267
if opts.PosterID > 0 {
1269-
sess.And("poster_id = ?", opts.PosterID)
1268+
sess.And("issue.poster_id = ?", opts.PosterID)
12701269
}
12711270

12721271
if opts.MentionedID > 0 {
@@ -1279,40 +1278,15 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
12791278
}
12801279

12811280
var err error
1282-
switch opts.FilterMode {
1283-
case FilterModeAll, FilterModeAssign:
1284-
stats.OpenCount, err = countSession(opts).
1285-
And("is_closed = ?", false).
1286-
Count(new(Issue))
1287-
1288-
stats.ClosedCount, err = countSession(opts).
1289-
And("is_closed = ?", true).
1290-
Count(new(Issue))
1291-
case FilterModeCreate:
1292-
stats.OpenCount, err = countSession(opts).
1293-
And("poster_id = ?", opts.PosterID).
1294-
And("is_closed = ?", false).
1295-
Count(new(Issue))
1296-
1297-
stats.ClosedCount, err = countSession(opts).
1298-
And("poster_id = ?", opts.PosterID).
1299-
And("is_closed = ?", true).
1300-
Count(new(Issue))
1301-
case FilterModeMention:
1302-
stats.OpenCount, err = countSession(opts).
1303-
Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
1304-
And("issue_user.uid = ?", opts.PosterID).
1305-
And("issue_user.is_mentioned = ?", true).
1306-
And("issue.is_closed = ?", false).
1307-
Count(new(Issue))
1308-
1309-
stats.ClosedCount, err = countSession(opts).
1310-
Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
1311-
And("issue_user.uid = ?", opts.PosterID).
1312-
And("issue_user.is_mentioned = ?", true).
1313-
And("issue.is_closed = ?", true).
1314-
Count(new(Issue))
1281+
stats.OpenCount, err = countSession(opts).
1282+
And("issue.is_closed = ?", false).
1283+
Count(new(Issue))
1284+
if err != nil {
1285+
return stats, err
13151286
}
1287+
stats.ClosedCount, err = countSession(opts).
1288+
And("issue.is_closed = ?", true).
1289+
Count(new(Issue))
13161290
return stats, err
13171291
}
13181292

models/unit_tests.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,31 @@ func PrepareTestDatabase() error {
3737
return LoadFixtures()
3838
}
3939

40+
type testCond struct {
41+
query interface{}
42+
args []interface{}
43+
}
44+
45+
// Cond create a condition with arguments for a test
46+
func Cond(query interface{}, args ...interface{}) interface{} {
47+
return &testCond{query: query, args: args}
48+
}
49+
50+
func whereConditions(sess *xorm.Session, conditions []interface{}) {
51+
for _, condition := range conditions {
52+
switch cond := condition.(type) {
53+
case *testCond:
54+
sess.Where(cond.query, cond.args...)
55+
default:
56+
sess.Where(cond)
57+
}
58+
}
59+
}
60+
4061
func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
4162
sess := x.NewSession()
4263
defer sess.Close()
43-
44-
for _, cond := range conditions {
45-
sess = sess.Where(cond)
46-
}
64+
whereConditions(sess, conditions)
4765
return sess.Get(bean)
4866
}
4967

@@ -65,6 +83,16 @@ func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...inter
6583
return bean
6684
}
6785

86+
// GetCount get the count of a bean
87+
func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
88+
sess := x.NewSession()
89+
defer sess.Close()
90+
whereConditions(sess, conditions)
91+
count, err := sess.Count(bean)
92+
assert.NoError(t, err)
93+
return int(count)
94+
}
95+
6896
// AssertNotExistsBean assert that a bean does not exist in the test database
6997
func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
7098
exists, err := loadBeanIfExists(bean, conditions...)
@@ -80,7 +108,5 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
80108

81109
// AssertCount assert the count of a bean
82110
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
83-
actual, err := x.Count(bean)
84-
assert.NoError(t, err)
85-
assert.EqualValues(t, expected, actual)
111+
assert.EqualValues(t, expected, GetCount(t, bean))
86112
}

routers/repo/issue.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ func Issues(ctx *context.Context) {
120120
forceEmpty bool
121121
)
122122

123+
if ctx.IsSigned {
124+
switch viewType {
125+
case "created_by":
126+
posterID = ctx.User.ID
127+
case "mentioned":
128+
mentionedID = ctx.User.ID
129+
}
130+
}
131+
123132
repo := ctx.Repo.Repository
124133
selectLabels := ctx.Query("labels")
125134
milestoneID := ctx.QueryInt64("milestone")
@@ -150,11 +159,12 @@ func Issues(ctx *context.Context) {
150159
MilestoneID: milestoneID,
151160
AssigneeID: assigneeID,
152161
MentionedID: mentionedID,
162+
PosterID: posterID,
153163
IsPull: isPullList,
154164
IssueIDs: issueIDs,
155165
})
156166
if err != nil {
157-
ctx.Error(500, "GetSearchIssueStats")
167+
ctx.Handle(500, "GetIssueStats", err)
158168
return
159169
}
160170
}

templates/repo/issue/list.tmpl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,21 @@
6868
</div>
6969
</div>
7070

71-
<!-- Type -->
72-
<div class="ui dropdown type jump item">
73-
<span class="text">
74-
{{.i18n.Tr "repo.issues.filter_type"}}
75-
<i class="dropdown icon"></i>
76-
</span>
77-
<div class="menu">
78-
<a class="{{if eq .ViewType "all"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=all&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.all_issues"}}</a>
79-
<a class="{{if eq .ViewType "assigned"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=assigned&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.assigned_to_you"}}</a>
80-
<a class="{{if eq .ViewType "created_by"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=created_by&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.created_by_you"}}</a>
81-
<a class="{{if eq .ViewType "mentioned"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=mentioned&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.mentioning_you"}}</a>
71+
{{if .IsSigned}}
72+
<!-- Type -->
73+
<div class="ui dropdown type jump item">
74+
<span class="text">
75+
{{.i18n.Tr "repo.issues.filter_type"}}
76+
<i class="dropdown icon"></i>
77+
</span>
78+
<div class="menu">
79+
<a class="{{if eq .ViewType "all"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=all&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.all_issues"}}</a>
80+
<a class="{{if eq .ViewType "assigned"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=assigned&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{.SignedUser.ID}}">{{.i18n.Tr "repo.issues.filter_type.assigned_to_you"}}</a>
81+
<a class="{{if eq .ViewType "created_by"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=created_by&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.created_by_you"}}</a>
82+
<a class="{{if eq .ViewType "mentioned"}}active{{end}} item" href="{{$.Link}}?q={{$.Keyword}}&type=mentioned&sort={{$.SortType}}&state={{$.State}}&labels={{.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}">{{.i18n.Tr "repo.issues.filter_type.mentioning_you"}}</a>
83+
</div>
8284
</div>
83-
</div>
85+
{{end}}
8486

8587
<!-- Sort -->
8688
<div class="ui dropdown type jump item">

0 commit comments

Comments
 (0)