Skip to content

Commit 99e133b

Browse files
6543wxiaoguang
andcommitted
API: Search Issues, dont show 500 if filter result in empty list (go-gitea#19244)
* remove error who is none * use setupSessionNoLimit instead of setupSessionWithLimit when no pagination Co-authored-by: wxiaoguang <[email protected]>
1 parent 5471311 commit 99e133b

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

models/issue.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
12471247
}
12481248
}
12491249

1250-
func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1250+
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
12511251
if opts.Page >= 0 && opts.PageSize > 0 {
12521252
var start int
12531253
if opts.Page == 0 {
@@ -1257,7 +1257,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
12571257
}
12581258
sess.Limit(opts.PageSize, start)
12591259
}
1260+
opts.setupSessionNoLimit(sess)
1261+
}
12601262

1263+
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
12611264
if len(opts.IssueIDs) > 0 {
12621265
sess.In("issue.id", opts.IssueIDs)
12631266
}
@@ -1423,7 +1426,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14231426

14241427
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
14251428

1426-
opts.setupSession(sess)
1429+
opts.setupSessionNoLimit(sess)
14271430

14281431
countsSlice := make([]*struct {
14291432
RepoID int64
@@ -1433,7 +1436,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14331436
Select("issue.repo_id AS repo_id, COUNT(*) AS count").
14341437
Table("issue").
14351438
Find(&countsSlice); err != nil {
1436-
return nil, err
1439+
return nil, fmt.Errorf("unable to CountIssuesByRepo: %w", err)
14371440
}
14381441

14391442
countMap := make(map[int64]int64, len(countsSlice))
@@ -1450,14 +1453,14 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14501453

14511454
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
14521455

1453-
opts.setupSession(sess)
1456+
opts.setupSessionNoLimit(sess)
14541457

14551458
accessCond := accessibleRepositoryCondition(user)
14561459
if err := sess.Where(accessCond).
14571460
Distinct("issue.repo_id").
14581461
Table("issue").
14591462
Find(&repoIDs); err != nil {
1460-
return nil, err
1463+
return nil, fmt.Errorf("unable to GetRepoIDsForIssuesOptions: %w", err)
14611464
}
14621465

14631466
return repoIDs, nil
@@ -1468,17 +1471,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
14681471
e := db.GetEngine(db.DefaultContext)
14691472

14701473
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1471-
opts.setupSession(sess)
1474+
opts.setupSessionWithLimit(sess)
14721475
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
14731476

14741477
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
14751478
if err := sess.Find(&issues); err != nil {
1476-
return nil, fmt.Errorf("Find: %v", err)
1479+
return nil, fmt.Errorf("unable to query Issues: %w", err)
14771480
}
1478-
sess.Close()
14791481

14801482
if err := IssueList(issues).LoadAttributes(); err != nil {
1481-
return nil, fmt.Errorf("LoadAttributes: %v", err)
1483+
return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err)
14821484
}
14831485

14841486
return issues, nil
@@ -1489,18 +1491,17 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
14891491
e := db.GetEngine(db.DefaultContext)
14901492

14911493
countsSlice := make([]*struct {
1492-
RepoID int64
1493-
Count int64
1494+
Count int64
14941495
}, 0, 1)
14951496

14961497
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
14971498
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1498-
opts.setupSession(sess)
1499+
opts.setupSessionNoLimit(sess)
14991500
if err := sess.Find(&countsSlice); err != nil {
1500-
return 0, fmt.Errorf("Find: %v", err)
1501+
return 0, fmt.Errorf("unable to CountIssues: %w", err)
15011502
}
1502-
if len(countsSlice) < 1 {
1503-
return 0, fmt.Errorf("there is less than one result sql record")
1503+
if len(countsSlice) != 1 {
1504+
return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice))
15041505
}
15051506
return countsSlice[0].Count, nil
15061507
}

0 commit comments

Comments
 (0)