Skip to content

Commit ef3792f

Browse files
committed
Fix PR, milestone and label functionality if issue unit is disabled or not assigned to user
1 parent f3833b7 commit ef3792f

File tree

6 files changed

+54
-60
lines changed

6 files changed

+54
-60
lines changed

routers/repo/issue.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,12 @@ func ViewIssue(ctx *context.Context) {
720720
func GetActionIssue(ctx *context.Context) *models.Issue {
721721
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
722722
if err != nil {
723-
if models.IsErrIssueNotExist(err) {
724-
ctx.Error(404, "GetIssueByIndex")
725-
} else {
726-
ctx.Handle(500, "GetIssueByIndex", err)
727-
}
723+
ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
724+
return nil
725+
}
726+
if issue.IsPull && !ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests) ||
727+
!issue.IsPull && !ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues) {
728+
ctx.Handle(404, "UnitEnabled", nil)
728729
return nil
729730
}
730731
return issue
@@ -749,6 +750,15 @@ func getActionIssues(ctx *context.Context) []*models.Issue {
749750
ctx.Handle(500, "GetIssuesByIDs", err)
750751
return nil
751752
}
753+
// Check access rights for all issues
754+
issueUnitEnabled := ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues)
755+
prUnitEnabled := ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests)
756+
for _, issue := range issues {
757+
if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
758+
ctx.Handle(404, "UnitEnabled", nil)
759+
return nil
760+
}
761+
}
752762
return issues
753763
}
754764

@@ -884,9 +894,8 @@ func UpdateIssueStatus(ctx *context.Context) {
884894

885895
// NewComment create a comment for issue
886896
func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
887-
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
888-
if err != nil {
889-
ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
897+
issue := GetActionIssue(ctx)
898+
if ctx.Written() {
890899
return
891900
}
892901

@@ -913,7 +922,7 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
913922

914923
if form.Status == "reopen" && issue.IsPull {
915924
pull := issue.PullRequest
916-
pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
925+
pr, err := models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
917926
if err != nil {
918927
if !models.IsErrPullRequestNotExist(err) {
919928
ctx.Handle(500, "GetUnmergedPullRequest", err)
@@ -935,7 +944,7 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
935944
if pr != nil {
936945
ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
937946
} else {
938-
if err = issue.ChangeStatus(ctx.User, ctx.Repo.Repository, form.Status == "close"); err != nil {
947+
if err := issue.ChangeStatus(ctx.User, ctx.Repo.Repository, form.Status == "close"); err != nil {
939948
log.Error(4, "ChangeStatus: %v", err)
940949
} else {
941950
log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
@@ -962,7 +971,7 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
962971
return
963972
}
964973

965-
comment, err = models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
974+
comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
966975
if err != nil {
967976
ctx.Handle(500, "CreateIssueComment", err)
968977
return
@@ -1032,10 +1041,6 @@ func DeleteComment(ctx *context.Context) {
10321041

10331042
// Milestones render milestones page
10341043
func Milestones(ctx *context.Context) {
1035-
MustEnableIssues(ctx)
1036-
if ctx.Written() {
1037-
return
1038-
}
10391044
ctx.Data["Title"] = ctx.Tr("repo.milestones")
10401045
ctx.Data["PageIsIssueList"] = true
10411046
ctx.Data["PageIsMilestones"] = true

routers/repo/issue_label.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const (
1818

1919
// Labels render issue's labels page
2020
func Labels(ctx *context.Context) {
21-
MustEnableIssues(ctx)
22-
if ctx.Written() {
23-
return
24-
}
2521
ctx.Data["Title"] = ctx.Tr("repo.labels")
2622
ctx.Data["PageIsIssueList"] = true
2723
ctx.Data["PageIsLabels"] = true

routers/repo/issue_stopwatch.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313

1414
// IssueStopwatch creates or stops a stopwatch for the given issue.
1515
func IssueStopwatch(c *context.Context) {
16-
issueIndex := c.ParamsInt64("index")
17-
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex)
18-
19-
if err != nil {
20-
c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err)
16+
issue := GetActionIssue(c)
17+
if c.Written() {
18+
return
19+
}
20+
if !c.Repo.CanUseTimetracker(issue, c.User) {
21+
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
2122
return
2223
}
2324

@@ -32,11 +33,12 @@ func IssueStopwatch(c *context.Context) {
3233

3334
// CancelStopwatch cancel the stopwatch
3435
func CancelStopwatch(c *context.Context) {
35-
issueIndex := c.ParamsInt64("index")
36-
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex)
37-
38-
if err != nil {
39-
c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err)
36+
issue := GetActionIssue(c)
37+
if c.Written() {
38+
return
39+
}
40+
if !c.Repo.CanUseTimetracker(issue, c.User) {
41+
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
4042
return
4143
}
4244

routers/repo/issue_timetrack.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ import (
1515

1616
// AddTimeManually tracks time manually
1717
func AddTimeManually(c *context.Context, form auth.AddTimeManuallyForm) {
18-
issueIndex := c.ParamsInt64("index")
19-
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex)
20-
if err != nil {
21-
if models.IsErrIssueNotExist(err) {
22-
c.Handle(http.StatusNotFound, "GetIssueByIndex", err)
23-
return
24-
}
25-
c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err)
18+
issue := GetActionIssue(c)
19+
if c.Written() {
20+
return
21+
}
22+
if !c.Repo.CanUseTimetracker(issue, c.User) {
23+
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
2624
return
2725
}
2826
url := issue.HTMLURL()

routers/repo/issue_watch.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ func IssueWatch(c *context.Context) {
2121
return
2222
}
2323

24-
issueIndex := c.ParamsInt64("index")
25-
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex)
26-
if err != nil {
27-
c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err)
24+
issue := GetActionIssue(c)
25+
if c.Written() {
2826
return
2927
}
3028

@@ -33,6 +31,6 @@ func IssueWatch(c *context.Context) {
3331
return
3432
}
3533

36-
url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issueIndex)
34+
url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issue.Index)
3735
c.Redirect(url, http.StatusSeeOther)
3836
}

routers/routes/routes.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,13 @@ func RegisterRoutes(m *macaron.Macaron) {
474474
m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
475475

476476
m.Group("/:username/:reponame", func() {
477-
// FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
478-
// So they can apply their own enable/disable logic on routers.
479477
m.Group("/issues", func() {
480478
m.Combo("/new").Get(context.RepoRef(), repo.NewIssue).
481479
Post(bindIgnErr(auth.CreateIssueForm{}), repo.NewIssuePost)
482-
480+
}, context.CheckUnit(models.UnitTypeIssues))
481+
// FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
482+
// So they can apply their own enable/disable logic on routers.
483+
m.Group("/issues", func() {
483484
m.Group("/:index", func() {
484485
m.Post("/title", repo.UpdateIssueTitle)
485486
m.Post("/content", repo.UpdateIssueContent)
@@ -491,20 +492,14 @@ func RegisterRoutes(m *macaron.Macaron) {
491492
m.Post("/toggle", repo.IssueStopwatch)
492493
m.Post("/cancel", repo.CancelStopwatch)
493494
})
494-
495-
}, func(ctx *context.Context) {
496-
if !ctx.Repo.CanUseTimetracker(repo.GetActionIssue(ctx), ctx.User) {
497-
ctx.Handle(404, ctx.Req.RequestURI, nil)
498-
return
499-
}
500495
})
501496
})
502497

503-
m.Post("/labels", repo.UpdateIssueLabel, reqRepoWriter)
504-
m.Post("/milestone", repo.UpdateIssueMilestone, reqRepoWriter)
505-
m.Post("/assignee", repo.UpdateIssueAssignee, reqRepoWriter)
506-
m.Post("/status", repo.UpdateIssueStatus, reqRepoWriter)
507-
}, context.CheckUnit(models.UnitTypeIssues))
498+
m.Post("/labels", reqRepoWriter, repo.UpdateIssueLabel)
499+
m.Post("/milestone", reqRepoWriter, repo.UpdateIssueMilestone)
500+
m.Post("/assignee", reqRepoWriter, repo.UpdateIssueAssignee)
501+
m.Post("/status", reqRepoWriter, repo.UpdateIssueStatus)
502+
})
508503
m.Group("/comments/:id", func() {
509504
m.Post("", repo.UpdateCommentContent)
510505
m.Post("/delete", repo.DeleteComment)
@@ -514,15 +509,15 @@ func RegisterRoutes(m *macaron.Macaron) {
514509
m.Post("/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
515510
m.Post("/delete", repo.DeleteLabel)
516511
m.Post("/initialize", bindIgnErr(auth.InitializeLabelsForm{}), repo.InitializeLabels)
517-
}, reqRepoWriter, context.RepoRef(), context.CheckUnit(models.UnitTypeIssues))
512+
}, reqRepoWriter, context.RepoRef(), context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests))
518513
m.Group("/milestones", func() {
519514
m.Combo("/new").Get(repo.NewMilestone).
520515
Post(bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
521516
m.Get("/:id/edit", repo.EditMilestone)
522517
m.Post("/:id/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.EditMilestonePost)
523518
m.Get("/:id/:action", repo.ChangeMilestonStatus)
524519
m.Post("/delete", repo.DeleteMilestone)
525-
}, reqRepoWriter, context.RepoRef(), context.CheckUnit(models.UnitTypeIssues))
520+
}, reqRepoWriter, context.RepoRef(), context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests))
526521

527522
m.Combo("/compare/*", repo.MustAllowPulls, repo.SetEditorconfigIfExists).
528523
Get(repo.CompareAndPullRequest).
@@ -593,8 +588,8 @@ func RegisterRoutes(m *macaron.Macaron) {
593588
m.Group("", func() {
594589
m.Get("/^:type(issues|pulls)$", repo.RetrieveLabels, repo.Issues)
595590
m.Get("/^:type(issues|pulls)$/:index", repo.ViewIssue)
596-
m.Get("/labels/", repo.RetrieveLabels, repo.Labels)
597-
m.Get("/milestones", repo.Milestones)
591+
m.Get("/labels/", context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests), repo.RetrieveLabels, repo.Labels)
592+
m.Get("/milestones", context.CheckAnyUnit(models.UnitTypeIssues, models.UnitTypePullRequests), repo.Milestones)
598593
}, context.RepoRef())
599594

600595
m.Group("/wiki", func() {

0 commit comments

Comments
 (0)