Skip to content

Commit a3ddd73

Browse files
author
Gusted
committed
Fix issue overview for teams (go-gitea#19652)
- Backport go-gitea#19652 - Don't use hacky solution to limit to the correct RepoID's, instead use current code to handle these limits. The existing code is more correct than the hacky solution. - Resolves go-gitea#19636
1 parent c8a83ac commit a3ddd73

File tree

2 files changed

+58
-30
lines changed

2 files changed

+58
-30
lines changed

models/issue.go

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
12391239
}
12401240
}
12411241

1242-
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
1242+
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) error {
12431243
if opts.Page >= 0 && opts.PageSize > 0 {
12441244
var start int
12451245
if opts.Page == 0 {
@@ -1249,10 +1249,10 @@ func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
12491249
}
12501250
sess.Limit(opts.PageSize, start)
12511251
}
1252-
opts.setupSessionNoLimit(sess)
1252+
return opts.setupSessionNoLimit(sess)
12531253
}
12541254

1255-
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
1255+
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) error {
12561256
if len(opts.IssueIDs) > 0 {
12571257
sess.In("issue.id", opts.IssueIDs)
12581258
}
@@ -1346,22 +1346,51 @@ func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
13461346
}
13471347

13481348
if opts.User != nil {
1349-
sess.And(
1350-
issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.IsTrue()),
1351-
)
1349+
repoCond, err := issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.IsTrue())
1350+
if err != nil {
1351+
return err
1352+
}
1353+
sess.And(repoCond)
1354+
13521355
}
1356+
return nil
13531357
}
13541358

13551359
// issuePullAccessibleRepoCond userID must not be zero, this condition require join repository table
1356-
func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *Organization, team *Team, isPull bool) builder.Cond {
1360+
func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *Organization, team *Team, isPull bool) (builder.Cond, error) {
13571361
cond := builder.NewCond()
13581362
unitType := unit.TypeIssues
13591363
if isPull {
13601364
unitType = unit.TypePullRequests
13611365
}
13621366
if org != nil {
13631367
if team != nil {
1364-
cond = cond.And(teamUnitsRepoCond(repoIDstr, userID, org.ID, team.ID, unitType)) // special team member repos
1368+
// If the current user is a admin, it doesn't necesarly mean
1369+
// it has joined the team, but we still should return all repo's
1370+
// of that team.
1371+
isAdmin, err := org.IsOwnedBy(userID)
1372+
if err != nil {
1373+
return nil, err
1374+
}
1375+
if isAdmin {
1376+
cond = cond.And(builder.In(repoIDstr,
1377+
builder.Select("repo_id").From("team_repo").Where(
1378+
builder.Eq{
1379+
"team_id": team.ID,
1380+
}.And(
1381+
builder.In(
1382+
"team_id", builder.Select("team_id").From("team_unit").Where(
1383+
builder.Eq{
1384+
"`team_unit`.org_id": org.ID,
1385+
"`team_unit`.type": unitType,
1386+
},
1387+
),
1388+
),
1389+
),
1390+
)))
1391+
} else {
1392+
cond = cond.And(teamUnitsRepoCond(repoIDstr, userID, org.ID, team.ID, unitType)) // special team member repos
1393+
}
13651394
} else {
13661395
cond = cond.And(
13671396
builder.Or(
@@ -1381,7 +1410,7 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *Organizati
13811410
),
13821411
)
13831412
}
1384-
return cond
1413+
return cond, nil
13851414
}
13861415

13871416
func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) *xorm.Session {
@@ -1414,7 +1443,9 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
14141443

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

1417-
opts.setupSessionNoLimit(sess)
1446+
if err := opts.setupSessionNoLimit(sess); err != nil {
1447+
return nil, fmt.Errorf("setupSessionNoLimit: %v", err)
1448+
}
14181449

14191450
countsSlice := make([]*struct {
14201451
RepoID int64
@@ -1441,7 +1472,9 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14411472

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

1444-
opts.setupSessionNoLimit(sess)
1475+
if err := opts.setupSessionWithLimit(sess); err != nil {
1476+
return nil, fmt.Errorf("setupSessionWithLimit: %v", err)
1477+
}
14451478

14461479
accessCond := accessibleRepositoryCondition(user)
14471480
if err := sess.Where(accessCond).
@@ -1459,7 +1492,9 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
14591492
e := db.GetEngine(db.DefaultContext)
14601493

14611494
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1462-
opts.setupSessionWithLimit(sess)
1495+
if err := opts.setupSessionNoLimit(sess); err != nil {
1496+
return nil, fmt.Errorf("setupSessionNoLimit: %v", err)
1497+
}
14631498
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)
14641499

14651500
issues := make([]*Issue, 0, opts.ListOptions.PageSize)
@@ -1484,7 +1519,9 @@ func CountIssues(opts *IssuesOptions) (int64, error) {
14841519

14851520
sess := e.Select("COUNT(issue.id) AS count").Table("issue")
14861521
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
1487-
opts.setupSessionNoLimit(sess)
1522+
if err := opts.setupSessionNoLimit(sess); err != nil {
1523+
return 0, fmt.Errorf("setupSessionNoLimit: %v", err)
1524+
}
14881525
if err := sess.Find(&countsSlice); err != nil {
14891526
return 0, fmt.Errorf("unable to CountIssues: %w", err)
14901527
}
@@ -1717,7 +1754,11 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
17171754
}
17181755

17191756
if opts.UserID > 0 {
1720-
cond = cond.And(issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull))
1757+
repoCond, err := issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull)
1758+
if err != nil {
1759+
return nil, err
1760+
}
1761+
cond = cond.And(repoCond)
17211762
}
17221763

17231764
sess := func(cond builder.Cond) *xorm.Session {

routers/web/user/home.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,13 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
442442
AllLimited: false,
443443
}
444444

445-
if ctxUser.IsOrganization() && ctx.Org.Team != nil {
446-
repoOpts.TeamID = ctx.Org.Team.ID
445+
if team != nil {
446+
repoOpts.TeamID = team.ID
447447
}
448448

449449
switch filterMode {
450450
case models.FilterModeAll:
451+
case models.FilterModeYourRepositories:
451452
case models.FilterModeAssign:
452453
opts.AssigneeID = ctx.User.ID
453454
case models.FilterModeCreate:
@@ -456,13 +457,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
456457
opts.MentionedID = ctx.User.ID
457458
case models.FilterModeReviewRequested:
458459
opts.ReviewRequestedID = ctx.User.ID
459-
case models.FilterModeYourRepositories:
460-
if ctxUser.IsOrganization() && ctx.Org.Team != nil {
461-
// Fixes a issue whereby the user's ID would be used
462-
// to check if it's in the team(which possible isn't the case).
463-
opts.User = nil
464-
}
465-
opts.RepoCond = models.SearchRepositoryCondition(repoOpts)
466460
}
467461

468462
// keyword holds the search term entered into the search field.
@@ -594,13 +588,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
594588
Org: org,
595589
Team: team,
596590
}
597-
if filterMode == models.FilterModeYourRepositories {
598-
statsOpts.RepoCond = models.SearchRepositoryCondition(repoOpts)
599-
}
600-
// Detect when we only should search by team.
601-
if opts.User == nil {
602-
statsOpts.UserID = 0
603-
}
604591
issueStats, err = models.GetUserIssueStats(statsOpts)
605592
if err != nil {
606593
ctx.ServerError("GetUserIssueStats Shown", err)

0 commit comments

Comments
 (0)