Refactor repo unit "disabled" check (#31389)
1. There are already global "unit consts", no need to use context data, which is fragile 2. Remove the "String()" method from "unit", it would only cause rendering problems in templates --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
d32648b204
commit
37a4b233a0
@ -362,7 +362,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
|
|||||||
if log.IsTrace() {
|
if log.IsTrace() {
|
||||||
unitTypeStrings := make([]string, len(repo.Units))
|
unitTypeStrings := make([]string, len(repo.Units))
|
||||||
for i, unit := range repo.Units {
|
for i, unit := range repo.Units {
|
||||||
unitTypeStrings[i] = unit.Type.String()
|
unitTypeStrings[i] = unit.Type.LogString()
|
||||||
}
|
}
|
||||||
log.Trace("repo.Units, ID=%d, Types: [%s]", repo.ID, strings.Join(unitTypeStrings, ", "))
|
log.Trace("repo.Units, ID=%d, Types: [%s]", repo.ID, strings.Join(unitTypeStrings, ", "))
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func IsErrUnitTypeNotExist(err error) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (err ErrUnitTypeNotExist) Error() string {
|
func (err ErrUnitTypeNotExist) Error() string {
|
||||||
return fmt.Sprintf("Unit type does not exist: %s", err.UT.String())
|
return fmt.Sprintf("Unit type does not exist: %s", err.UT.LogString())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err ErrUnitTypeNotExist) Unwrap() error {
|
func (err ErrUnitTypeNotExist) Unwrap() error {
|
||||||
|
@ -33,39 +33,18 @@ const (
|
|||||||
TypeActions // 10 Actions
|
TypeActions // 10 Actions
|
||||||
)
|
)
|
||||||
|
|
||||||
// Value returns integer value for unit type
|
// Value returns integer value for unit type (used by template)
|
||||||
func (u Type) Value() int {
|
func (u Type) Value() int {
|
||||||
return int(u)
|
return int(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u Type) String() string {
|
|
||||||
switch u {
|
|
||||||
case TypeCode:
|
|
||||||
return "TypeCode"
|
|
||||||
case TypeIssues:
|
|
||||||
return "TypeIssues"
|
|
||||||
case TypePullRequests:
|
|
||||||
return "TypePullRequests"
|
|
||||||
case TypeReleases:
|
|
||||||
return "TypeReleases"
|
|
||||||
case TypeWiki:
|
|
||||||
return "TypeWiki"
|
|
||||||
case TypeExternalWiki:
|
|
||||||
return "TypeExternalWiki"
|
|
||||||
case TypeExternalTracker:
|
|
||||||
return "TypeExternalTracker"
|
|
||||||
case TypeProjects:
|
|
||||||
return "TypeProjects"
|
|
||||||
case TypePackages:
|
|
||||||
return "TypePackages"
|
|
||||||
case TypeActions:
|
|
||||||
return "TypeActions"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("Unknown Type %d", u)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u Type) LogString() string {
|
func (u Type) LogString() string {
|
||||||
return fmt.Sprintf("<UnitType:%d:%s>", u, u.String())
|
unit, ok := Units[u]
|
||||||
|
unitName := "unknown"
|
||||||
|
if ok {
|
||||||
|
unitName = unit.NameKey
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("<UnitType:%d:%s>", u, unitName)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -133,7 +112,7 @@ func validateDefaultRepoUnits(defaultUnits, settingDefaultUnits []Type) []Type {
|
|||||||
units = make([]Type, 0, len(settingDefaultUnits))
|
units = make([]Type, 0, len(settingDefaultUnits))
|
||||||
for _, settingUnit := range settingDefaultUnits {
|
for _, settingUnit := range settingDefaultUnits {
|
||||||
if !settingUnit.CanBeDefault() {
|
if !settingUnit.CanBeDefault() {
|
||||||
log.Warn("Not allowed as default unit: %s", settingUnit.String())
|
log.Warn("Not allowed as default unit: %s", settingUnit.LogString())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
units = append(units, settingUnit)
|
units = append(units, settingUnit)
|
||||||
|
@ -384,18 +384,18 @@ func registerRoutes(m *web.Route) {
|
|||||||
return func(ctx *context.Context) {
|
return func(ctx *context.Context) {
|
||||||
// only check global disabled units when ignoreGlobal is false
|
// only check global disabled units when ignoreGlobal is false
|
||||||
if !ignoreGlobal && unitType.UnitGlobalDisabled() {
|
if !ignoreGlobal && unitType.UnitGlobalDisabled() {
|
||||||
ctx.NotFound(unitType.String(), nil)
|
ctx.NotFound("Repo unit is is disabled: "+unitType.LogString(), nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.ContextUser == nil {
|
if ctx.ContextUser == nil {
|
||||||
ctx.NotFound(unitType.String(), nil)
|
ctx.NotFound("ContextUser is nil", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.ContextUser.IsOrganization() {
|
if ctx.ContextUser.IsOrganization() {
|
||||||
if ctx.Org.Organization.UnitPermission(ctx, ctx.Doer, unitType) < accessMode {
|
if ctx.Org.Organization.UnitPermission(ctx, ctx.Doer, unitType) < accessMode {
|
||||||
ctx.NotFound(unitType.String(), nil)
|
ctx.NotFound("ContextUser is org but doer has no access to unit", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -487,7 +487,7 @@ func registerRoutes(m *web.Route) {
|
|||||||
m.Get("/organizations", explore.Organizations)
|
m.Get("/organizations", explore.Organizations)
|
||||||
m.Get("/code", func(ctx *context.Context) {
|
m.Get("/code", func(ctx *context.Context) {
|
||||||
if unit.TypeCode.UnitGlobalDisabled() {
|
if unit.TypeCode.UnitGlobalDisabled() {
|
||||||
ctx.NotFound(unit.TypeCode.String(), nil)
|
ctx.NotFound("Repo unit code is disabled", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}, explore.Code)
|
}, explore.Code)
|
||||||
|
@ -210,16 +210,9 @@ func Contexter() func(next http.Handler) http.Handler {
|
|||||||
// FIXME: do we really always need these setting? There should be someway to have to avoid having to always set these
|
// FIXME: do we really always need these setting? There should be someway to have to avoid having to always set these
|
||||||
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
|
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
|
||||||
ctx.Data["DisableStars"] = setting.Repository.DisableStars
|
ctx.Data["DisableStars"] = setting.Repository.DisableStars
|
||||||
ctx.Data["EnableActions"] = setting.Actions.Enabled
|
ctx.Data["EnableActions"] = setting.Actions.Enabled && !unit.TypeActions.UnitGlobalDisabled()
|
||||||
|
|
||||||
ctx.Data["ManifestData"] = setting.ManifestData
|
ctx.Data["ManifestData"] = setting.ManifestData
|
||||||
|
|
||||||
ctx.Data["UnitWikiGlobalDisabled"] = unit.TypeWiki.UnitGlobalDisabled()
|
|
||||||
ctx.Data["UnitIssuesGlobalDisabled"] = unit.TypeIssues.UnitGlobalDisabled()
|
|
||||||
ctx.Data["UnitPullsGlobalDisabled"] = unit.TypePullRequests.UnitGlobalDisabled()
|
|
||||||
ctx.Data["UnitProjectsGlobalDisabled"] = unit.TypeProjects.UnitGlobalDisabled()
|
|
||||||
ctx.Data["UnitActionsGlobalDisabled"] = unit.TypeActions.UnitGlobalDisabled()
|
|
||||||
|
|
||||||
ctx.Data["AllLangs"] = translation.AllLangs()
|
ctx.Data["AllLangs"] = translation.AllLangs()
|
||||||
|
|
||||||
next.ServeHTTP(ctx.Resp, ctx.Req)
|
next.ServeHTTP(ctx.Resp, ctx.Req)
|
||||||
|
@ -35,13 +35,13 @@
|
|||||||
{{if and .IsSigned .MustChangePassword}}
|
{{if and .IsSigned .MustChangePassword}}
|
||||||
{{/* No links */}}
|
{{/* No links */}}
|
||||||
{{else if .IsSigned}}
|
{{else if .IsSigned}}
|
||||||
{{if not .UnitIssuesGlobalDisabled}}
|
{{if not ctx.Consts.RepoUnitTypeIssues.UnitGlobalDisabled}}
|
||||||
<a class="item{{if .PageIsIssues}} active{{end}}" href="{{AppSubUrl}}/issues">{{ctx.Locale.Tr "issues"}}</a>
|
<a class="item{{if .PageIsIssues}} active{{end}}" href="{{AppSubUrl}}/issues">{{ctx.Locale.Tr "issues"}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if not .UnitPullsGlobalDisabled}}
|
{{if not ctx.Consts.RepoUnitTypePullRequests.UnitGlobalDisabled}}
|
||||||
<a class="item{{if .PageIsPulls}} active{{end}}" href="{{AppSubUrl}}/pulls">{{ctx.Locale.Tr "pull_requests"}}</a>
|
<a class="item{{if .PageIsPulls}} active{{end}}" href="{{AppSubUrl}}/pulls">{{ctx.Locale.Tr "pull_requests"}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if not (and .UnitIssuesGlobalDisabled .UnitPullsGlobalDisabled)}}
|
{{if not (and ctx.Consts.RepoUnitTypeIssues.UnitGlobalDisabled ctx.Consts.RepoUnitTypePullRequests.UnitGlobalDisabled)}}
|
||||||
{{if .ShowMilestonesDashboardPage}}
|
{{if .ShowMilestonesDashboardPage}}
|
||||||
<a class="item{{if .PageIsMilestonesDashboard}} active{{end}}" href="{{AppSubUrl}}/milestones">{{ctx.Locale.Tr "milestones"}}</a>
|
<a class="item{{if .PageIsMilestonesDashboard}} active{{end}}" href="{{AppSubUrl}}/milestones">{{ctx.Locale.Tr "milestones"}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -162,7 +162,7 @@
|
|||||||
</a>
|
</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if and .EnableActions (not .UnitActionsGlobalDisabled) (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}}
|
{{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}}
|
||||||
<a class="{{if .PageIsActions}}active {{end}}item" href="{{.RepoLink}}/actions">
|
<a class="{{if .PageIsActions}}active {{end}}item" href="{{.RepoLink}}/actions">
|
||||||
{{svg "octicon-play"}} {{ctx.Locale.Tr "actions.actions"}}
|
{{svg "octicon-play"}} {{ctx.Locale.Tr "actions.actions"}}
|
||||||
{{if .Repository.NumOpenActionRuns}}
|
{{if .Repository.NumOpenActionRuns}}
|
||||||
@ -178,7 +178,7 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{$projectsUnit := .Repository.MustGetUnit $.Context ctx.Consts.RepoUnitTypeProjects}}
|
{{$projectsUnit := .Repository.MustGetUnit $.Context ctx.Consts.RepoUnitTypeProjects}}
|
||||||
{{if and (not .UnitProjectsGlobalDisabled) (.Permission.CanRead ctx.Consts.RepoUnitTypeProjects) ($projectsUnit.ProjectsConfig.IsProjectsAllowed "repo")}}
|
{{if and (not ctx.Consts.RepoUnitTypeProjects.UnitGlobalDisabled) (.Permission.CanRead ctx.Consts.RepoUnitTypeProjects) ($projectsUnit.ProjectsConfig.IsProjectsAllowed "repo")}}
|
||||||
<a href="{{.RepoLink}}/projects" class="{{if .IsProjectsPage}}active {{end}}item">
|
<a href="{{.RepoLink}}/projects" class="{{if .IsProjectsPage}}active {{end}}item">
|
||||||
{{svg "octicon-project"}} {{ctx.Locale.Tr "repo.projects"}}
|
{{svg "octicon-project"}} {{ctx.Locale.Tr "repo.projects"}}
|
||||||
{{if .Repository.NumOpenProjects}}
|
{{if .Repository.NumOpenProjects}}
|
||||||
|
@ -574,7 +574,6 @@
|
|||||||
{{template "repo/commits_list_small" dict "comment" . "root" $}}
|
{{template "repo/commits_list_small" dict "comment" . "root" $}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{else if eq .Type 30}}
|
{{else if eq .Type 30}}
|
||||||
{{if not $.UnitProjectsGlobalDisabled}}
|
|
||||||
<div class="timeline-item event" id="{{.HashTag}}">
|
<div class="timeline-item event" id="{{.HashTag}}">
|
||||||
<span class="badge">{{svg "octicon-project"}}</span>
|
<span class="badge">{{svg "octicon-project"}}</span>
|
||||||
{{template "shared/user/avatarlink" dict "user" .Poster}}
|
{{template "shared/user/avatarlink" dict "user" .Poster}}
|
||||||
@ -599,7 +598,6 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
|
||||||
{{else if eq .Type 32}}
|
{{else if eq .Type 32}}
|
||||||
<div class="timeline-item-group">
|
<div class="timeline-item-group">
|
||||||
<div class="timeline-item event" id="{{.HashTag}}">
|
<div class="timeline-item event" id="{{.HashTag}}">
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
{{if not .ctxData.Repository.IsArchived}}
|
{{if not .ctxData.Repository.IsArchived}}
|
||||||
{{$needDivider = true}}
|
{{$needDivider = true}}
|
||||||
<div class="item context js-aria-clickable quote-reply {{if .diff}}quote-reply-diff{{end}}" data-target="{{.item.HashTag}}-raw">{{ctx.Locale.Tr "repo.issues.context.quote_reply"}}</div>
|
<div class="item context js-aria-clickable quote-reply {{if .diff}}quote-reply-diff{{end}}" data-target="{{.item.HashTag}}-raw">{{ctx.Locale.Tr "repo.issues.context.quote_reply"}}</div>
|
||||||
{{if not .ctxData.UnitIssuesGlobalDisabled}}
|
{{if not ctx.Consts.RepoUnitTypeIssues.UnitGlobalDisabled}}
|
||||||
<div class="item context js-aria-clickable reference-issue" data-target="{{.item.HashTag}}-raw" data-modal="#reference-issue-modal" data-poster="{{.item.Poster.GetDisplayName}}" data-poster-username="{{.item.Poster.Name}}" data-reference="{{$referenceUrl}}">{{ctx.Locale.Tr "repo.issues.context.reference_issue"}}</div>
|
<div class="item context js-aria-clickable reference-issue" data-target="{{.item.HashTag}}-raw" data-modal="#reference-issue-modal" data-poster="{{.item.Poster.GetDisplayName}}" data-poster-username="{{.item.Poster.Name}}" data-reference="{{$referenceUrl}}">{{ctx.Locale.Tr "repo.issues.context.reference_issue"}}</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if or .ctxData.Permission.IsAdmin .IsCommentPoster .ctxData.HasIssuesOrPullsWritePermission}}
|
{{if or .ctxData.Permission.IsAdmin .IsCommentPoster .ctxData.HasIssuesOrPullsWritePermission}}
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
</a>
|
</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if and .EnableActions (not .UnitActionsGlobalDisabled) (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}}
|
{{if and .EnableActions (.Permission.CanRead ctx.Consts.RepoUnitTypeActions)}}
|
||||||
<details class="item toggleable-item" {{if or .PageIsSharedSettingsRunners .PageIsSharedSettingsSecrets .PageIsSharedSettingsVariables}}open{{end}}>
|
<details class="item toggleable-item" {{if or .PageIsSharedSettingsRunners .PageIsSharedSettingsSecrets .PageIsSharedSettingsVariables}}open{{end}}>
|
||||||
<summary>{{ctx.Locale.Tr "actions.actions"}}</summary>
|
<summary>{{ctx.Locale.Tr "actions.actions"}}</summary>
|
||||||
<div class="menu">
|
<div class="menu">
|
||||||
|
@ -81,17 +81,17 @@
|
|||||||
<a class="{{if .PageIsNews}}active {{end}}item tw-ml-auto" href="{{.ContextUser.DashboardLink}}{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
<a class="{{if .PageIsNews}}active {{end}}item tw-ml-auto" href="{{.ContextUser.DashboardLink}}{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
||||||
{{svg "octicon-rss"}} {{ctx.Locale.Tr "activities"}}
|
{{svg "octicon-rss"}} {{ctx.Locale.Tr "activities"}}
|
||||||
</a>
|
</a>
|
||||||
{{if not .UnitIssuesGlobalDisabled}}
|
{{if not ctx.Consts.RepoUnitTypeIssues.UnitGlobalDisabled}}
|
||||||
<a class="{{if .PageIsIssues}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/issues{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
<a class="{{if .PageIsIssues}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/issues{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
||||||
{{svg "octicon-issue-opened"}} {{ctx.Locale.Tr "issues"}}
|
{{svg "octicon-issue-opened"}} {{ctx.Locale.Tr "issues"}}
|
||||||
</a>
|
</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if not .UnitPullsGlobalDisabled}}
|
{{if not ctx.Consts.RepoUnitTypePullRequests.UnitGlobalDisabled}}
|
||||||
<a class="{{if .PageIsPulls}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/pulls{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
<a class="{{if .PageIsPulls}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/pulls{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
||||||
{{svg "octicon-git-pull-request"}} {{ctx.Locale.Tr "pull_requests"}}
|
{{svg "octicon-git-pull-request"}} {{ctx.Locale.Tr "pull_requests"}}
|
||||||
</a>
|
</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if and .ShowMilestonesDashboardPage (not (and .UnitIssuesGlobalDisabled .UnitPullsGlobalDisabled))}}
|
{{if and .ShowMilestonesDashboardPage (not (and ctx.Consts.RepoUnitTypeIssues.UnitGlobalDisabled ctx.Consts.RepoUnitTypePullRequests.UnitGlobalDisabled))}}
|
||||||
<a class="{{if .PageIsMilestonesDashboard}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/milestones{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
<a class="{{if .PageIsMilestonesDashboard}}active {{end}}item" href="{{.ContextUser.OrganisationLink}}/milestones{{if .Team}}/{{PathEscape .Team.Name}}{{end}}">
|
||||||
{{svg "octicon-milestone"}} {{ctx.Locale.Tr "milestones"}}
|
{{svg "octicon-milestone"}} {{ctx.Locale.Tr "milestones"}}
|
||||||
</a>
|
</a>
|
||||||
|
Loading…
Reference in New Issue
Block a user