Skip to content

Commit 75f8534

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

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
@@ -1270,7 +1270,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
12701270
}
12711271
}
12721272

1273-
func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1273+
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
12741274
if opts.Page >= 0 && opts.PageSize > 0 {
12751275
var start int
12761276
if opts.Page == 0 {
@@ -1280,7 +1280,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
12801280
}
12811281
sess.Limit(opts.PageSize, start)
12821282
}
1283+
opts.setupSessionNoLimit(sess)
1284+
}
12831285

1286+
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
12841287
if len(opts.IssueIDs) > 0 {
12851288
sess.In("issue.id", opts.IssueIDs)
12861289
}
@@ -1446,7 +1449,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14461449

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

1449-
opts.setupSession(sess)
1452+
opts.setupSessionNoLimit(sess)
14501453

14511454
countsSlice := make([]*struct {
14521455
RepoID int64
@@ -1456,7 +1459,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14561459
Select("issue.repo_id AS repo_id, COUNT(*) AS count").
14571460
Table("issue").
14581461
Find(&countsSlice); err != nil {
1459-
return nil, err
1462+
return nil, fmt.Errorf("unable to CountIssuesByRepo: %w", err)
14601463
}
14611464

14621465
countMap := make(map[int64]int64, len(countsSlice))
@@ -1473,14 +1476,14 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14731476

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

1476-
opts.setupSession(sess)
1479+
opts.setupSessionNoLimit(sess)
14771480

14781481
accessCond := accessibleRepositoryCondition(user)
14791482
if err := sess.Where(accessCond).
14801483
Distinct("issue.repo_id").
14811484
Table("issue").
14821485
Find(&repoIDs); err != nil {
1483-
return nil, err
1486+
return nil, fmt.Errorf("unable to GetRepoIDsForIssuesOptions: %w", err)
14841487
}
14851488

14861489
return repoIDs, nil
@@ -1491,17 +1494,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
14911494
e := db.GetEngine(db.DefaultContext)
14921495

14931496
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1494-
opts.setupSession(sess)
1497+
opts.setupSessionWithLimit(sess)
14951498
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
14961499

14971500
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
14981501
if err := sess.Find(&issues); err != nil {
1499-
return nil, fmt.Errorf("Find: %v", err)
1502+
return nil, fmt.Errorf("unable to query Issues: %w", err)
15001503
}
1501-
sess.Close()
15021504

15031505
if err := IssueList(issues).LoadAttributes(); err != nil {
1504-
return nil, fmt.Errorf("LoadAttributes: %v", err)
1506+
return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err)
15051507
}
15061508

15071509
return issues, nil
@@ -1512,18 +1514,17 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
15121514
e := db.GetEngine(db.DefaultContext)
15131515

15141516
countsSlice := make([]*struct {
1515-
RepoID int64
1516-
Count int64
1517+
Count int64
15171518
}, 0, 1)
15181519

15191520
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
15201521
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1521-
opts.setupSession(sess)
1522+
opts.setupSessionNoLimit(sess)
15221523
if err := sess.Find(&countsSlice); err != nil {
1523-
return 0, fmt.Errorf("Find: %v", err)
1524+
return 0, fmt.Errorf("unable to CountIssues: %w", err)
15241525
}
1525-
if len(countsSlice) < 1 {
1526-
return 0, fmt.Errorf("there is less than one result sql record")
1526+
if len(countsSlice) != 1 {
1527+
return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice))
15271528
}
15281529
return countsSlice[0].Count, nil
15291530
}

0 commit comments

Comments
 (0)