Skip to content

Allow to set default unit per repo #32363

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

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func prepareMigrationTasks() []*migration {
newMigration(304, "Add index for release sha1", v1_23.AddIndexForReleaseSha1),
newMigration(305, "Add Repository Licenses", v1_23.AddRepositoryLicenses),
newMigration(306, "Add BlockAdminMergeOverride to ProtectedBranch", v1_23.AddBlockAdminMergeOverrideBranchProtection),
newMigration(307, "Add DefaultUnit to Repository", v1_23.AddDefaultUnitToRepository),
}
return preparedMigrations
}
Expand Down
13 changes: 13 additions & 0 deletions models/migrations/v1_23/v307.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_23 //nolint

import "xorm.io/xorm"

func AddDefaultUnitToRepository(x *xorm.Engine) error {
type Repository struct {
DefaultUnit int `xorm:"NOT NULL DEFAULT 1"`
}
return x.Sync(new(Repository))
}
1 change: 1 addition & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type Repository struct {
RenderingMetas map[string]string `xorm:"-"`
DocumentRenderingMetas map[string]string `xorm:"-"`
Units []*RepoUnit `xorm:"-"`
DefaultUnit unit.Type `xorm:"NOT NULL DEFAULT 1"`
PrimaryLanguage *LanguageStat `xorm:"-"`

IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
Expand Down
2 changes: 1 addition & 1 deletion models/unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ var (
UnitCode = Unit{
TypeCode,
"repo.code",
"/",
"/code",
"repo.code.desc",
0,
perm.AccessModeOwner,
Expand Down
14 changes: 12 additions & 2 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ func checkHomeCodeViewable(ctx *context.Context) {
}
}

// TODO: ctx.Repo.Repository.DefaultUnit has to be respected here ... but we need to let code unit have it's own subpath ...
var firstUnit *unit_model.Unit
for _, repoUnitType := range ctx.Repo.Permission.ReadableUnitTypes() {
if repoUnitType == unit_model.TypeCode {
Expand Down Expand Up @@ -772,8 +773,8 @@ func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) {
}
}

// Home render repository home page
func Home(ctx *context.Context) {
// HomeWithFeedCheck render repository home page or return feed
func HomeWithFeedCheck(ctx *context.Context) {
if setting.Other.EnableFeed {
isFeed, _, showFeedType := feed.GetFeedType(ctx.PathParam(":reponame"), ctx.Req)
if isFeed {
Expand All @@ -789,6 +790,15 @@ func Home(ctx *context.Context) {
}
}

defaultURI := ctx.Repo.Repository.MustGetUnit(ctx, ctx.Repo.Repository.DefaultUnit).Unit().URI
if defaultURI == "/" { // support legacy code units
defaultURI = "/code"
}
ctx.Redirect(ctx.Repo.RepoLink + defaultURI)
}

// Home render repository home page
func Home(ctx *context.Context) {
checkHomeCodeViewable(ctx)
if ctx.Written() {
return
Expand Down
3 changes: 2 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,8 @@ func registerRoutes(m *web.Router) {
// end "/{username}/{reponame}/settings"

// user/org home, including rss feeds
m.Get("/{username}/{reponame}", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.Home)
m.Get("/{username}/{reponame}", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.HomeWithFeedCheck)
m.Get("/{username}/{reponame}/code", ignSignIn, context.RepoAssignment, context.RepoRef(), repo.SetEditorconfigIfExists, repo.Home)

// TODO: maybe it should relax the permission to allow "any access"
m.Post("/{username}/{reponame}/markup", ignSignIn, context.RepoAssignment, context.RequireRepoReaderOr(unit.TypeCode, unit.TypeIssues, unit.TypePullRequests, unit.TypeReleases, unit.TypeWiki), web.Bind(structs.MarkupOption{}), misc.Markup)
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
{{if not (or .Repository.IsBeingCreated .Repository.IsBroken)}}
<div class="overflow-menu-items">
{{if .Permission.CanRead ctx.Consts.RepoUnitTypeCode}}
<a class="{{if .PageIsViewCode}}active {{end}}item" href="{{.RepoLink}}{{if and (ne .BranchName .Repository.DefaultBranch) (not $.PageIsWiki)}}/src/{{.BranchNameSubURL}}{{end}}">
<a class="{{if .PageIsViewCode}}active {{end}}item" href="{{.RepoLink}}{{if and (ne .BranchName .Repository.DefaultBranch) (not $.PageIsWiki)}}/src/{{.BranchNameSubURL}}{{else}}/code{{end}}">
{{svg "octicon-code"}} {{ctx.Locale.Tr "repo.code"}}
</a>
{{end}}
Expand Down
Loading