Skip to content

Commit 9cb35ac

Browse files
authored
Merge pull request #2 from wxiaoguang/fix-500-on-issue-search-no-items
Use setupSessionNoLimit instead of setupSessionWithLimit when no pagination
2 parents c026632 + 18aeedf commit 9cb35ac

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

1274-
func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
1274+
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
12751275
if opts.Page >= 0 && opts.PageSize > 0 {
12761276
var start int
12771277
if opts.Page == 0 {
@@ -1281,7 +1281,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
12811281
}
12821282
sess.Limit(opts.PageSize, start)
12831283
}
1284+
opts.setupSessionNoLimit(sess)
1285+
}
12841286

1287+
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
12851288
if len(opts.IssueIDs) > 0 {
12861289
sess.In("issue.id", opts.IssueIDs)
12871290
}
@@ -1447,7 +1450,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14471450

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

1450-
opts.setupSession(sess)
1453+
opts.setupSessionNoLimit(sess)
14511454

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

14631466
countMap := make(map[int64]int64, len(countsSlice))
@@ -1474,14 +1477,14 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14741477

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

1477-
opts.setupSession(sess)
1480+
opts.setupSessionNoLimit(sess)
14781481

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

14871490
return repoIDs, nil
@@ -1492,17 +1495,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
14921495
e := db.GetEngine(db.DefaultContext)
14931496

14941497
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1495-
opts.setupSession(sess)
1498+
opts.setupSessionWithLimit(sess)
14961499
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
14971500

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

15041506
if err := IssueList(issues).LoadAttributes(); err != nil {
1505-
return nil, fmt.Errorf("LoadAttributes: %v", err)
1507+
return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err)
15061508
}
15071509

15081510
return issues, nil
@@ -1513,18 +1515,17 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
15131515
e := db.GetEngine(db.DefaultContext)
15141516

15151517
countsSlice := make([]*struct {
1516-
RepoID int64
1517-
Count int64
1518+
Count int64
15181519
}, 0, 1)
15191520

15201521
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
15211522
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1522-
opts.setupSession(sess)
1523+
opts.setupSessionNoLimit(sess)
15231524
if err := sess.Find(&countsSlice); err != nil {
1524-
return 0, fmt.Errorf("Find: %v", err)
1525+
return 0, fmt.Errorf("unable to CountIssues: %w", err)
15251526
}
1526-
if len(countsSlice) == 0 {
1527-
return 0, nil
1527+
if len(countsSlice) != 1 {
1528+
return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice))
15281529
}
15291530
return countsSlice[0].Count, nil
15301531
}

0 commit comments

Comments
 (0)