Skip to content

Commit 42229dc

Browse files
6543Gusted
and
Gusted
authored
Fix showing issues in your repositories (#18916) (#19191)
- Make a restriction on which issues can be shown based on if you the user or team has write permission to the repository. - Fixes a issue whereby you wouldn't see any associated issues with a specific team on a organization if you wasn't a member(fixed by zeroing the User{ID} in the options). - Resolves #18913 Co-authored-by: Gusted <[email protected]>
1 parent e3d8e92 commit 42229dc

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

models/issue.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,7 @@ const (
15511551
FilterModeCreate
15521552
FilterModeMention
15531553
FilterModeReviewRequested
1554+
FilterModeYourRepositories
15541555
)
15551556

15561557
func parseCountResult(results []map[string][]byte) int64 {
@@ -1695,6 +1696,7 @@ type UserIssueStatsOptions struct {
16951696
IssueIDs []int64
16961697
IsArchived util.OptionalBool
16971698
LabelIDs []int64
1699+
RepoCond builder.Cond
16981700
Org *Organization
16991701
Team *Team
17001702
}
@@ -1712,6 +1714,9 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
17121714
if len(opts.IssueIDs) > 0 {
17131715
cond = cond.And(builder.In("issue.id", opts.IssueIDs))
17141716
}
1717+
if opts.RepoCond != nil {
1718+
cond = cond.And(opts.RepoCond)
1719+
}
17151720

17161721
if opts.UserID > 0 {
17171722
cond = cond.And(issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull))
@@ -1733,7 +1738,7 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) {
17331738
}
17341739

17351740
switch opts.FilterMode {
1736-
case FilterModeAll:
1741+
case FilterModeAll, FilterModeYourRepositories:
17371742
stats.OpenCount, err = sess(cond).
17381743
And("issue.is_closed = ?", false).
17391744
Count(new(Issue))

routers/web/user/home.go

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func Milestones(ctx *context.Context) {
197197
if issueReposQueryPattern.MatchString(reposQuery) {
198198
// remove "[" and "]" from string
199199
reposQuery = reposQuery[1 : len(reposQuery)-1]
200-
//for each ID (delimiter ",") add to int to repoIDs
200+
// for each ID (delimiter ",") add to int to repoIDs
201201

202202
for _, rID := range strings.Split(reposQuery, ",") {
203203
// Ensure nonempty string entries
@@ -350,7 +350,6 @@ func Issues(ctx *context.Context) {
350350
var issueReposQueryPattern = regexp.MustCompile(`^\[\d+(,\d+)*,?\]$`)
351351

352352
func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
353-
354353
// ----------------------------------------------------
355354
// Determine user; can be either user or organization.
356355
// Return with NotFound or ServerError if unsuccessful.
@@ -364,7 +363,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
364363
var (
365364
viewType string
366365
sortType = ctx.FormString("sort")
367-
filterMode = models.FilterModeAll
366+
filterMode int
368367
)
369368

370369
// --------------------------------------------------------------------------------
@@ -390,8 +389,10 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
390389
filterMode = models.FilterModeMention
391390
case "review_requested":
392391
filterMode = models.FilterModeReviewRequested
393-
case "your_repositories": // filterMode already set to All
392+
case "your_repositories":
393+
fallthrough
394394
default:
395+
filterMode = models.FilterModeYourRepositories
395396
viewType = "your_repositories"
396397
}
397398

@@ -421,6 +422,30 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
421422
User: ctx.User,
422423
}
423424

425+
// Search all repositories which
426+
//
427+
// As user:
428+
// - Owns the repository.
429+
// - Have collaborator permissions in repository.
430+
//
431+
// As org:
432+
// - Owns the repository.
433+
//
434+
// As team:
435+
// - Team org's owns the repository.
436+
// - Team has read permission to repository.
437+
repoOpts := &models.SearchRepoOptions{
438+
Actor: ctx.User,
439+
OwnerID: ctx.User.ID,
440+
Private: true,
441+
AllPublic: false,
442+
AllLimited: false,
443+
}
444+
445+
if ctxUser.IsOrganization() && ctx.Org.Team != nil {
446+
repoOpts.TeamID = ctx.Org.Team.ID
447+
}
448+
424449
switch filterMode {
425450
case models.FilterModeAll:
426451
case models.FilterModeAssign:
@@ -431,6 +456,19 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
431456
opts.MentionedID = ctx.User.ID
432457
case models.FilterModeReviewRequested:
433458
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+
userRepoIDs, _, err := models.SearchRepositoryIDs(repoOpts)
466+
if err != nil {
467+
ctx.ServerError("models.SearchRepositoryIDs: %v", err)
468+
return
469+
}
470+
471+
opts.RepoIDs = userRepoIDs
434472
}
435473

436474
// keyword holds the search term entered into the search field.
@@ -562,8 +600,12 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
562600
Org: org,
563601
Team: team,
564602
}
565-
if len(repoIDs) > 0 {
566-
statsOpts.RepoIDs = repoIDs
603+
if filterMode == models.FilterModeYourRepositories {
604+
statsOpts.RepoCond = models.SearchRepositoryCondition(repoOpts)
605+
}
606+
// Detect when we only should search by team.
607+
if opts.User == nil {
608+
statsOpts.UserID = 0
567609
}
568610
issueStats, err = models.GetUserIssueStats(statsOpts)
569611
if err != nil {
@@ -586,8 +628,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
586628

587629
ctx.Data["IsShowClosed"] = isShowClosed
588630

589-
ctx.Data["IssueRefEndNames"], ctx.Data["IssueRefURLs"] =
590-
issue_service.GetRefEndNamesAndURLs(issues, ctx.FormString("RepoLink"))
631+
ctx.Data["IssueRefEndNames"], ctx.Data["IssueRefURLs"] = issue_service.GetRefEndNamesAndURLs(issues, ctx.FormString("RepoLink"))
591632

592633
ctx.Data["Issues"] = issues
593634

@@ -661,7 +702,7 @@ func getRepoIDs(reposQuery string) []int64 {
661702
var repoIDs []int64
662703
// remove "[" and "]" from string
663704
reposQuery = reposQuery[1 : len(reposQuery)-1]
664-
//for each ID (delimiter ",") add to int to repoIDs
705+
// for each ID (delimiter ",") add to int to repoIDs
665706
for _, rID := range strings.Split(reposQuery, ",") {
666707
// Ensure nonempty string entries
667708
if rID != "" && rID != "0" {
@@ -693,8 +734,8 @@ func issueIDsFromSearch(ctxUser *user_model.User, keyword string, opts *models.I
693734
}
694735

695736
func loadRepoByIDs(ctxUser *user_model.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*repo_model.Repository, error) {
696-
var totalRes = make(map[int64]*repo_model.Repository, len(issueCountByRepo))
697-
var repoIDs = make([]int64, 0, 500)
737+
totalRes := make(map[int64]*repo_model.Repository, len(issueCountByRepo))
738+
repoIDs := make([]int64, 0, 500)
698739
for id := range issueCountByRepo {
699740
if id <= 0 {
700741
continue
@@ -745,7 +786,7 @@ func ShowGPGKeys(ctx *context.Context, uid int64) {
745786
if err != nil {
746787
if asymkey_model.IsErrGPGKeyImportNotExist(err) {
747788
failedEntitiesID = append(failedEntitiesID, k.KeyID)
748-
continue //Skip previous import without backup of imported armored key
789+
continue // Skip previous import without backup of imported armored key
749790
}
750791
ctx.ServerError("ShowGPGKeys", err)
751792
return
@@ -755,12 +796,12 @@ func ShowGPGKeys(ctx *context.Context, uid int64) {
755796
var buf bytes.Buffer
756797

757798
headers := make(map[string]string)
758-
if len(failedEntitiesID) > 0 { //If some key need re-import to be exported
799+
if len(failedEntitiesID) > 0 { // If some key need re-import to be exported
759800
headers["Note"] = fmt.Sprintf("The keys with the following IDs couldn't be exported and need to be reuploaded %s", strings.Join(failedEntitiesID, ", "))
760801
}
761802
writer, _ := armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", headers)
762803
for _, e := range entities {
763-
err = e.Serialize(writer) //TODO find why key are exported with a different cipherTypeByte as original (should not be blocking but strange)
804+
err = e.Serialize(writer) // TODO find why key are exported with a different cipherTypeByte as original (should not be blocking but strange)
764805
if err != nil {
765806
ctx.ServerError("ShowGPGKeys", err)
766807
return

0 commit comments

Comments
 (0)