Skip to content

Commit dd8a726

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

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
@@ -1238,7 +1238,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
12381238
}
12391239
}
12401240

1241-
func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1241+
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
12421242
if opts.Page >= 0 && opts.PageSize > 0 {
12431243
var start int
12441244
if opts.Page == 0 {
@@ -1248,7 +1248,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
12481248
}
12491249
sess.Limit(opts.PageSize, start)
12501250
}
1251+
opts.setupSessionNoLimit(sess)
1252+
}
12511253

1254+
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
12521255
if len(opts.IssueIDs) > 0 {
12531256
sess.In("issue.id", opts.IssueIDs)
12541257
}
@@ -1414,7 +1417,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14141417

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

1417-
opts.setupSession(sess)
1420+
opts.setupSessionNoLimit(sess)
14181421

14191422
countsSlice := make([]*struct {
14201423
RepoID int64
@@ -1424,7 +1427,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14241427
Select("issue.repo_id AS repo_id, COUNT(*) AS count").
14251428
Table("issue").
14261429
Find(&countsSlice); err != nil {
1427-
return nil, err
1430+
return nil, fmt.Errorf("unable to CountIssuesByRepo: %w", err)
14281431
}
14291432

14301433
countMap := make(map[int64]int64, len(countsSlice))
@@ -1441,14 +1444,14 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14411444

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

1444-
opts.setupSession(sess)
1447+
opts.setupSessionNoLimit(sess)
14451448

14461449
accessCond := accessibleRepositoryCondition(user)
14471450
if err := sess.Where(accessCond).
14481451
Distinct("issue.repo_id").
14491452
Table("issue").
14501453
Find(&repoIDs); err != nil {
1451-
return nil, err
1454+
return nil, fmt.Errorf("unable to GetRepoIDsForIssuesOptions: %w", err)
14521455
}
14531456

14541457
return repoIDs, nil
@@ -1459,17 +1462,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
14591462
e := db.GetEngine(db.DefaultContext)
14601463

14611464
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1462-
opts.setupSession(sess)
1465+
opts.setupSessionWithLimit(sess)
14631466
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
14641467

14651468
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
14661469
if err := sess.Find(&issues); err != nil {
1467-
return nil, fmt.Errorf("Find: %v", err)
1470+
return nil, fmt.Errorf("unable to query Issues: %w", err)
14681471
}
1469-
sess.Close()
14701472

14711473
if err := IssueList(issues).LoadAttributes(); err != nil {
1472-
return nil, fmt.Errorf("LoadAttributes: %v", err)
1474+
return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err)
14731475
}
14741476

14751477
return issues, nil
@@ -1480,18 +1482,17 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
14801482
e := db.GetEngine(db.DefaultContext)
14811483

14821484
countsSlice := make([]*struct {
1483-
RepoID int64
1484-
Count int64
1485+
Count int64
14851486
}, 0, 1)
14861487

14871488
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
14881489
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1489-
opts.setupSession(sess)
1490+
opts.setupSessionNoLimit(sess)
14901491
if err := sess.Find(&countsSlice); err != nil {
1491-
return 0, fmt.Errorf("Find: %v", err)
1492+
return 0, fmt.Errorf("unable to CountIssues: %w", err)
14921493
}
1493-
if len(countsSlice) < 1 {
1494-
return 0, fmt.Errorf("there is less than one result sql record")
1494+
if len(countsSlice) != 1 {
1495+
return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice))
14951496
}
14961497
return countsSlice[0].Count, nil
14971498
}

0 commit comments

Comments
 (0)