Skip to content

Fix issue overview for teams #19652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64
}
}

func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) error {
if opts.Page >= 0 && opts.PageSize > 0 {
var start int
if opts.Page == 0 {
Expand All @@ -1251,10 +1251,10 @@ func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) {
}
sess.Limit(opts.PageSize, start)
}
opts.setupSessionNoLimit(sess)
return opts.setupSessionNoLimit(sess)
}

func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) error {
if len(opts.IssueIDs) > 0 {
sess.In("issue.id", opts.IssueIDs)
}
Expand Down Expand Up @@ -1348,22 +1348,50 @@ func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) {
}

if opts.User != nil {
sess.And(
issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.IsTrue()),
)
repoCond, err := issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.IsTrue())
if err != nil {
return err
}
sess.And(repoCond)
}
return nil
}

// issuePullAccessibleRepoCond userID must not be zero, this condition require join repository table
func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organization.Organization, team *organization.Team, isPull bool) builder.Cond {
func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organization.Organization, team *organization.Team, isPull bool) (builder.Cond, error) {
cond := builder.NewCond()
unitType := unit.TypeIssues
if isPull {
unitType = unit.TypePullRequests
}
if org != nil {
if team != nil {
cond = cond.And(teamUnitsRepoCond(repoIDstr, userID, org.ID, team.ID, unitType)) // special team member repos
// If the current user is a admin, it doesn't necesarly mean
// it has joined the team, but we still should return all repo's
// of that team.
isAdmin, err := org.IsOwnedBy(userID)
if err != nil {
return nil, err
}
if isAdmin {
cond = cond.And(builder.In(repoIDstr,
builder.Select("repo_id").From("team_repo").Where(
builder.Eq{
"team_id": team.ID,
}.And(
builder.In(
"team_id", builder.Select("team_id").From("team_unit").Where(
builder.Eq{
"`team_unit`.org_id": org.ID,
"`team_unit`.type": unitType,
},
),
),
),
)))
} else {
cond = cond.And(teamUnitsRepoCond(repoIDstr, userID, org.ID, team.ID, unitType)) // special team member repos
}
} else {
cond = cond.And(
builder.Or(
Expand All @@ -1383,7 +1411,7 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati
),
)
}
return cond
return cond, nil
}

func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) *xorm.Session {
Expand Down Expand Up @@ -1416,7 +1444,9 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {

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

opts.setupSessionNoLimit(sess)
if err := opts.setupSessionNoLimit(sess); err != nil {
return nil, fmt.Errorf("setupSessionNoLimit: %v", err)
}

countsSlice := make([]*struct {
RepoID int64
Expand All @@ -1443,7 +1473,9 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i

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

opts.setupSessionNoLimit(sess)
if err := opts.setupSessionNoLimit(sess); err != nil {
return nil, fmt.Errorf("setupSessionNoLimit: %v", err)
}

accessCond := accessibleRepositoryCondition(user)
if err := sess.Where(accessCond).
Expand All @@ -1461,7 +1493,9 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
e := db.GetEngine(db.DefaultContext)

sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSessionWithLimit(sess)
if err := opts.setupSessionWithLimit(sess); err != nil {
return nil, fmt.Errorf("setupSessionWithLimit: %v", err)
}
sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID)

issues := make([]*Issue, 0, opts.ListOptions.PageSize)
Expand All @@ -1482,7 +1516,10 @@ func CountIssues(opts *IssuesOptions) (int64, error) {

sess := e.Select("COUNT(issue.id) AS count").Table("issue")
sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
opts.setupSessionNoLimit(sess)
if err := opts.setupSessionNoLimit(sess); err != nil {
return 0, fmt.Errorf("setupSessionNoLimit: %v", err)
}

return sess.Count()
}

Expand Down Expand Up @@ -1709,7 +1746,11 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
}

if opts.UserID > 0 {
cond = cond.And(issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull))
repoCond, err := issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull)
if err != nil {
return nil, err
}
cond = cond.And(repoCond)
}

sess := func(cond builder.Cond) *xorm.Session {
Expand Down
20 changes: 4 additions & 16 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,13 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
AllLimited: false,
}

if ctxUser.IsOrganization() && ctx.Org.Team != nil {
repoOpts.TeamID = ctx.Org.Team.ID
if team != nil {
repoOpts.TeamID = team.ID
}

switch filterMode {
case models.FilterModeAll:
case models.FilterModeYourRepositories:
case models.FilterModeAssign:
opts.AssigneeID = ctx.Doer.ID
case models.FilterModeCreate:
Expand All @@ -457,13 +458,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
opts.MentionedID = ctx.Doer.ID
case models.FilterModeReviewRequested:
opts.ReviewRequestedID = ctx.Doer.ID
case models.FilterModeYourRepositories:
if ctxUser.IsOrganization() && ctx.Org.Team != nil {
// Fixes a issue whereby the user's ID would be used
// to check if it's in the team(which possible isn't the case).
opts.User = nil
}
opts.RepoCond = models.SearchRepositoryCondition(repoOpts)
}

// keyword holds the search term entered into the search field.
Expand Down Expand Up @@ -595,13 +589,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
Org: org,
Team: team,
}
if filterMode == models.FilterModeYourRepositories {
statsOpts.RepoCond = models.SearchRepositoryCondition(repoOpts)
}
// Detect when we only should search by team.
if opts.User == nil {
statsOpts.UserID = 0
}

issueStats, err = models.GetUserIssueStats(statsOpts)
if err != nil {
ctx.ServerError("GetUserIssueStats Shown", err)
Expand Down