[GITEA] Allow changing the repo Wiki branch to main

Previously, the repo wiki was hardcoded to use `master` as its branch,
this change makes it possible to use `main` (or something else, governed
by `[repository].DEFAULT_BRANCH`, a setting that already exists and
defaults to `main`).

The way it is done is that a new column is added to the `repository`
table: `wiki_branch`. The migration will make existing repositories
default to `master`, for compatibility's sake, even if they don't have a
Wiki (because it's easier to do that). Newly created repositories will
default to `[repository].DEFAULT_BRANCH` instead.

The Wiki service was updated to use the branch name stored in the
database, and fall back to the default if it is empty.

Old repositories with Wikis using the older `master` branch will have
the option to do a one-time transition to `main`, available via the
repository settings in the "Danger Zone". This option will only be
available for repositories that have the internal wiki enabled, it is
not empty, and the wiki branch is not `[repository].DEFAULT_BRANCH`.

When migrating a repository with a Wiki, Forgejo will use the same
branch name for the wiki as the source repository did. If that's not the
same as the default, the option to normalize it will be available after
the migration's done.

Additionally, the `/api/v1/{owner}/{repo}` endpoint was updated: it will
now include the wiki branch name in `GET` requests, and allow changing
the wiki branch via `PATCH`.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit d87c526d2a)
This commit is contained in:
Gergely Nagy 2024-01-30 12:18:53 +01:00 committed by Earl Warren
parent 361617eea0
commit 2ca4862f8b
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
19 changed files with 364 additions and 24 deletions

View file

@ -48,6 +48,8 @@ var migrations = []*Migration{
NewMigration("Add default_permissions to repo_unit", forgejo_v1_22.AddDefaultPermissionsToRepoUnit),
// v4 -> v5
NewMigration("create the forgejo_repo_flag table", forgejo_v1_22.CreateRepoFlagTable),
// v5 -> v6
NewMigration("Add wiki_branch to repository", forgejo_v1_22.AddWikiBranchToRepository),
}
// GetCurrentDBVersion returns the current Forgejo database version.

View file

@ -0,0 +1,24 @@
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package v1_22 //nolint
import (
"xorm.io/xorm"
)
func AddWikiBranchToRepository(x *xorm.Engine) error {
type Repository struct {
ID int64
WikiBranch string
}
if err := x.Sync(&Repository{}); err != nil {
return err
}
// Update existing repositories to use `master` as the wiki branch, for
// compatilibty's sake.
_, err := x.Exec("UPDATE repository SET wiki_branch = 'master' WHERE wiki_branch = '' OR wiki_branch IS NULL")
return err
}

View file

@ -135,6 +135,7 @@ type Repository struct {
OriginalServiceType api.GitServiceType `xorm:"index"`
OriginalURL string `xorm:"VARCHAR(2048)"`
DefaultBranch string
WikiBranch string
NumWatches int
NumStars int
@ -204,6 +205,13 @@ func (repo *Repository) GetOwnerName() string {
return repo.OwnerName
}
func (repo *Repository) GetWikiBranchName() string {
if repo.WikiBranch == "" {
return setting.Repository.DefaultBranch
}
return repo.WikiBranch
}
// SanitizedOriginalURL returns a sanitized OriginalURL
func (repo *Repository) SanitizedOriginalURL() string {
if repo.OriginalURL == "" {

View file

@ -400,6 +400,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
ctx.Data["PushMirrors"] = pushMirrors
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty
ctx.Data["DefaultWikiBranchName"] = setting.Repository.DefaultBranch
}
// RepoIDAssignment returns a handler which assigns the repo to the context.

View file

@ -1,4 +1,5 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
@ -99,7 +100,6 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
Branch: "master",
SkipTLSVerify: setting.Migrations.SkipTLSVerify,
}); err != nil {
log.Warn("Clone wiki: %v", err)
@ -107,6 +107,30 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
}
} else {
// Figure out the branch of the wiki we just cloned. We assume
// that the default branch is to be used, and we'll use the same
// name as the source.
gitRepo, err := git.OpenRepository(ctx, wikiPath)
if err != nil {
log.Warn("Failed to open wiki repository during migration: %v", err)
if err := util.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
}
return repo, err
}
defer gitRepo.Close()
branch, err := gitRepo.GetDefaultBranch()
if err != nil {
log.Warn("Failed to get the default branch of a migrated wiki repo: %v", err)
if err := util.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
}
return repo, err
}
repo.WikiBranch = branch
if err := git.WriteCommitGraph(ctx, wikiPath); err != nil {
return repo, err
}

View file

@ -88,6 +88,7 @@ type Repository struct {
ExternalTracker *ExternalTracker `json:"external_tracker,omitempty"`
HasWiki bool `json:"has_wiki"`
ExternalWiki *ExternalWiki `json:"external_wiki,omitempty"`
WikiBranch string `json:"wiki_branch,omitempty"`
HasPullRequests bool `json:"has_pull_requests"`
HasProjects bool `json:"has_projects"`
HasReleases bool `json:"has_releases"`
@ -175,6 +176,8 @@ type EditRepoOption struct {
ExternalWiki *ExternalWiki `json:"external_wiki,omitempty"`
// sets the default branch for this repository.
DefaultBranch *string `json:"default_branch,omitempty"`
// sets the branch used for this repository's wiki.
WikiBranch *string `json:"wiki_branch,omitempty"`
// either `true` to allow pull requests, or `false` to prevent pull request.
HasPullRequests *bool `json:"has_pull_requests,omitempty"`
// either `true` to enable project unit, or `false` to disable them.

View file

@ -2142,6 +2142,13 @@ settings.trust_model.committer.desc = Valid signatures will only be marked "trus
settings.trust_model.collaboratorcommitter = Collaborator+Committer
settings.trust_model.collaboratorcommitter.long = Collaborator+Committer: Trust signatures by collaborators which match the committer
settings.trust_model.collaboratorcommitter.desc = Valid signatures by collaborators of this repository will be marked "trusted" if they match the committer. Otherwise, valid signatures will be marked "untrusted" if the signature matches the committer and "unmatched" otherwise. This will force Forgejo to be marked as the committer on signed commits with the actual committer marked as Co-Authored-By: and Co-Committed-By: trailer in the commit. The default Forgejo key must match a User in the database.
settings.wiki_rename_branch_main = Normalize the Wiki branch name
settings.wiki_rename_branch_main_desc = Rename the branch used internally by the Wiki to "%s". This is a permanent and cannot be undone.
settings.wiki_rename_branch_main_notices_1 = This operation <strong>CANNOT</strong> be undone.
settings.wiki_rename_branch_main_notices_2 = This will premanently rename the the internal branch of %s's repository wiki. Existing checkouts will need to be updated.
settings.wiki_branch_rename_success = The repository wiki's branch name has been successfully normalized.
settings.wiki_branch_rename_failure = Failed to normalize the repository wiki's branch name.
settings.confirm_wiki_branch_rename = Rename the wiki branch
settings.wiki_delete = Delete Wiki Data
settings.wiki_delete_desc = Deleting repository wiki data is permanent and cannot be undone.
settings.wiki_delete_notices_1 = - This will permanently delete and disable the repository wiki for %s.

View file

@ -34,6 +34,7 @@ import (
"code.gitea.io/gitea/services/convert"
"code.gitea.io/gitea/services/issue"
repo_service "code.gitea.io/gitea/services/repository"
wiki_service "code.gitea.io/gitea/services/wiki"
)
// Search repositories via options
@ -740,6 +741,18 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
repo.DefaultBranch = *opts.DefaultBranch
}
// Wiki branch is updated if changed
if opts.WikiBranch != nil && repo.WikiBranch != *opts.WikiBranch {
if err := wiki_service.NormalizeWikiBranch(ctx, repo, *opts.WikiBranch); err != nil {
ctx.Error(http.StatusInternalServerError, "NormalizeWikiBranch", err)
return err
}
// While NormalizeWikiBranch updates the db, we need to update *this*
// instance of `repo`, so that the `UpdateRepository` below will not
// reset the branch back.
repo.WikiBranch = *opts.WikiBranch
}
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
return err

View file

@ -872,6 +872,27 @@ func SettingsPost(ctx *context.Context) {
ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
case "rename-wiki-branch":
if !ctx.Repo.IsOwner() {
ctx.Error(http.StatusNotFound)
return
}
if repo.FullName() != form.RepoName {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
return
}
if err := wiki_service.NormalizeWikiBranch(ctx, repo, setting.Repository.DefaultBranch); err != nil {
log.Error("Normalize Wiki branch: %v", err.Error())
ctx.Flash.Error(ctx.Tr("repo.settings.wiki_branch_rename_failure"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
return
}
log.Trace("Repository wiki normalized: %s#%s", repo.FullName(), setting.Repository.DefaultBranch)
ctx.Flash.Success(ctx.Tr("repo.settings.wiki_branch_rename_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
case "archive":
if !ctx.Repo.IsOwner() {
ctx.Error(http.StatusForbidden)

View file

@ -99,7 +99,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
return nil, nil, err
}
commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
commit, err := wikiRepo.GetBranchCommit(ctx.Repo.Repository.GetWikiBranchName())
if err != nil {
return wikiRepo, nil, err
}
@ -316,7 +316,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
}
// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.GetWikiBranchName(), pageFilename)
ctx.Data["CommitCount"] = commitsCount
return wikiRepo, entry
@ -368,7 +368,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["footerContent"] = ""
// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.GetWikiBranchName(), pageFilename)
ctx.Data["CommitCount"] = commitsCount
// get page
@ -380,7 +380,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: wiki_service.DefaultBranch,
Revision: ctx.Repo.Repository.GetWikiBranchName(),
File: pageFilename,
Page: page,
})

View file

@ -208,6 +208,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
ExternalTracker: externalTracker,
InternalTracker: internalTracker,
HasWiki: hasWiki,
WikiBranch: repo.WikiBranch,
HasProjects: hasProjects,
HasReleases: hasReleases,
HasPackages: hasPackages,

View file

@ -173,6 +173,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
}
repo.DefaultBranch = setting.Repository.DefaultBranch
repo.WikiBranch = setting.Repository.DefaultBranch
if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
@ -240,6 +241,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
TrustModel: opts.TrustModel,
IsMirror: opts.IsMirror,
DefaultBranch: opts.DefaultBranch,
WikiBranch: setting.Repository.DefaultBranch,
ObjectFormatName: opts.ObjectFormatName,
}

View file

@ -1,5 +1,6 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package wiki
@ -26,7 +27,6 @@ var wikiWorkingPool = sync.NewExclusivePool()
const (
DefaultRemote = "origin"
DefaultBranch = "master"
)
// InitWiki initializes a wiki for repository,
@ -36,26 +36,74 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
return nil
}
branch := repo.GetWikiBranchName()
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
return fmt.Errorf("InitRepository: %w", err)
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
return fmt.Errorf("createDelegateHooks: %w", err)
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+DefaultBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
return fmt.Errorf("unable to set default wiki branch to master: %w", err)
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix + branch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
return fmt.Errorf("unable to set default wiki branch to %s: %w", branch, err)
}
return nil
}
// NormalizeWikiBranch renames a repository wiki's branch to `setting.Repository.DefaultBranch`
func NormalizeWikiBranch(ctx context.Context, repo *repo_model.Repository, to string) error {
from := repo.GetWikiBranchName()
if err := repo.MustNotBeArchived(); err != nil {
return err
}
updateDB := func() error {
repo.WikiBranch = to
return repo_model.UpdateRepositoryCols(ctx, repo, "wiki_branch")
}
if !repo.HasWiki() {
return updateDB()
}
if from == to {
return nil
}
gitRepo, err := git.OpenRepository(ctx, repo.WikiPath())
if err != nil {
return err
}
defer gitRepo.Close()
if gitRepo.IsBranchExist(to) {
return nil
}
if !gitRepo.IsBranchExist(from) {
return nil
}
if err := gitRepo.RenameBranch(from, to); err != nil {
return err
}
if err := gitRepo.SetDefaultBranch(to); err != nil {
return err
}
return updateDB()
}
// prepareGitPath try to find a suitable file path with file name by the given raw wiki name.
// return: existence, prepared file path with name, error
func prepareGitPath(gitRepo *git.Repository, wikiPath WebPath) (bool, string, error) {
func prepareGitPath(gitRepo *git.Repository, branch string, wikiPath WebPath) (bool, string, error) {
unescaped := string(wikiPath) + ".md"
gitPath := WebPathToGitPath(wikiPath)
// Look for both files
filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, gitPath)
filesInIndex, err := gitRepo.LsTree(branch, unescaped, gitPath)
if err != nil {
if strings.Contains(err.Error(), "Not a valid object name master") {
if strings.Contains(err.Error(), "Not a valid object name "+branch) {
return false, gitPath, nil
}
log.Error("%v", err)
@ -94,7 +142,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("InitWiki: %w", err)
}
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), DefaultBranch)
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.GetWikiBranchName())
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
if err != nil {
@ -112,7 +160,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
}
if hasMasterBranch {
cloneOpts.Branch = DefaultBranch
cloneOpts.Branch = repo.GetWikiBranchName()
}
if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil {
@ -134,7 +182,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
}
}
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, newWikiName)
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), newWikiName)
if err != nil {
return err
}
@ -150,7 +198,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
isOldWikiExist := true
oldWikiPath := newWikiPath
if oldWikiName != newWikiName {
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, oldWikiName)
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.GetWikiBranchName(), oldWikiName)
if err != nil {
return err
}
@ -211,7 +259,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
Env: repo_module.FullPushingEnvironment(
doer,
doer,
@ -268,7 +316,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
Bare: true,
Shared: true,
Branch: DefaultBranch,
Branch: repo.GetWikiBranchName(),
}); err != nil {
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err)
@ -286,7 +334,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err)
}
found, wikiPath, err := prepareGitPath(gitRepo, wikiName)
found, wikiPath, err := prepareGitPath(gitRepo, repo.GetWikiBranchName(), wikiName)
if err != nil {
return err
}
@ -330,7 +378,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.GetWikiBranchName()),
Env: repo_module.FullPushingEnvironment(
doer,
doer,

View file

@ -170,7 +170,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
return
}
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath(webPath)
entry, err := masterTree.GetTreeEntryByPath(gitPath)
@ -215,7 +215,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
// Now need to show that the page has been added:
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
assert.NoError(t, err)
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath(webPath)
entry, err := masterTree.GetTreeEntryByPath(gitPath)
@ -242,7 +242,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
return
}
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree(DefaultBranch)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
gitPath := WebPathToGitPath("Home")
_, err = masterTree.GetTreeEntryByPath(gitPath)
@ -280,7 +280,7 @@ func TestPrepareWikiFileName(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
webPath := UserTitleToWebPath("", tt.arg)
existence, newWikiPath, err := prepareGitPath(gitRepo, webPath)
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", webPath)
if (err != nil) != tt.wantErr {
assert.NoError(t, err)
return
@ -312,7 +312,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
}
defer gitRepo.Close()
existence, newWikiPath, err := prepareGitPath(gitRepo, "Home")
existence, newWikiPath, err := prepareGitPath(gitRepo, "master", "Home")
assert.False(t, existence)
assert.NoError(t, err)
assert.EqualValues(t, "Home.md", newWikiPath)

View file

@ -780,6 +780,17 @@
</div>
</div>
{{if .Permission.CanRead $.UnitTypeWiki}}
{{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}}
<div class="flex-item">
<div class="flex-item-main">
<div class="flex-item-title">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}</div>
<div class="flex-item-body">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_desc" .DefaultWikiBranchName}}</div>
</div>
<div class="flex-item-trailing">
<button class="ui basic red show-modal button" data-modal="#rename-wiki-branch-modal">{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}</button>
</div>
</div>
{{end}}
<div class="flex-item">
<div class="flex-item-main">
<div class="flex-item-title">{{ctx.Locale.Tr "repo.settings.wiki_delete"}}</div>
@ -991,6 +1002,40 @@
</form>
</div>
</div>
{{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}}
<div class="ui small modal" id="rename-wiki-branch-modal">
<div class="header">
{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}
</div>
<div class="content">
<div class="ui warning message">
<ul>
<li>{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_notices_1" | Safe}}</li>
<li>{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_notices_2" .Repository.Name}}</li>
</ul>
</div>
<form class="ui form" action="{{.Link}}" method="post">
{{.CsrfTokenHtml}}
<input type="hidden" name="action" value="rename-wiki-branch">
<div class="field">
<label>
{{ctx.Locale.Tr "repo.settings.transfer_form_title"}}
<span class="text red">{{.Repository.FullName}}</span>
</label>
</div>
<div class="required field">
<label for="repo_name">{{ctx.Locale.Tr "repo.repo_name"}}</label>
<input id="repo_name" name="repo_name" required>
</div>
<div class="text right actions">
<button class="ui cancel button">{{ctx.Locale.Tr "settings.cancel"}}</button>
<button class="ui red button">{{ctx.Locale.Tr "repo.settings.confirm_wiki_branch_rename"}}</button>
</div>
</form>
</div>
</div>
{{end}}
{{end}}
{{if not .Repository.IsMirror}}

View file

@ -19839,6 +19839,11 @@
"description": "a URL with more information about the repository.",
"type": "string",
"x-go-name": "Website"
},
"wiki_branch": {
"description": "sets the branch used for this repository's wiki.",
"type": "string",
"x-go-name": "WikiBranch"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
@ -22796,6 +22801,10 @@
"website": {
"type": "string",
"x-go-name": "Website"
},
"wiki_branch": {
"type": "string",
"x-go-name": "WikiBranch"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"

View file

@ -1,4 +1,5 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
@ -21,6 +22,30 @@ import (
"github.com/stretchr/testify/assert"
)
func TestAPIRenameWikiBranch(t *testing.T) {
defer tests.PrepareTestEnv(t)()
username := "user2"
session := loginUser(t, username)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
repoURLStr := fmt.Sprintf("/api/v1/repos/%s/%s", username, "repo1")
wikiBranch := "wiki"
req := NewRequestWithJSON(t, "PATCH", repoURLStr, &api.EditRepoOption{
WikiBranch: &wikiBranch,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusOK)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "wiki", repo.WikiBranch)
req = NewRequest(t, "GET", repoURLStr)
resp := MakeRequest(t, req, http.StatusOK)
var repoData *api.Repository
DecodeJSON(t, resp, &repoData)
assert.Equal(t, "wiki", repoData.WikiBranch)
}
func TestAPIGetWikiPage(t *testing.T) {
defer tests.PrepareTestEnv(t)()

View file

@ -89,6 +89,39 @@ func TestDangerZoneConfirmation(t *testing.T) {
})
})
t.Run("Rename wiki branch", func(t *testing.T) {
session := loginUser(t, "user2")
// NOTE: No need to rename the wiki branch here to make the form appear.
// We can submit it anyway, even if it doesn't appear on the web.
t.Run("Fail", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings", map[string]string{
"_csrf": GetCSRF(t, session, "/user2/repo1/settings"),
"action": "rename-wiki-branch",
"repo_name": "repo1",
})
resp := session.MakeRequest(t, req, http.StatusOK)
mustInvalidRepoName(resp)
})
t.Run("Pass", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings", map[string]string{
"_csrf": GetCSRF(t, session, "/user2/repo1/settings"),
"action": "rename-wiki-branch",
"repo_name": "user2/repo1",
})
session.MakeRequest(t, req, http.StatusSeeOther)
flashCookie := session.GetCookie(gitea_context.CookieNameFlash)
assert.NotNil(t, flashCookie)
assert.EqualValues(t, "success%3DThe%2Brepository%2Bwiki%2527s%2Bbranch%2Bname%2Bhas%2Bbeen%2Bsuccessfully%2Bnormalized.", flashCookie.Value)
})
})
t.Run("Delete wiki", func(t *testing.T) {
session := loginUser(t, "user2")

View file

@ -0,0 +1,74 @@
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"fmt"
"net/http"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
)
func TestWikiBranchNormalize(t *testing.T) {
defer tests.PrepareTestEnv(t)()
username := "user2"
session := loginUser(t, username)
settingsURLStr := "/user2/repo1/settings"
assertNormalizeButton := func(present bool) string {
req := NewRequest(t, "GET", settingsURLStr) //.AddTokenAuth(token)
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, "button[data-modal='#rename-wiki-branch-modal']", present)
return htmlDoc.GetCSRF()
}
// By default the repo wiki branch is empty
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Empty(t, repo.WikiBranch)
// This means we default to setting.Repository.DefaultBranch
assert.Equal(t, setting.Repository.DefaultBranch, repo.GetWikiBranchName())
// Which further means that the "Normalize wiki branch" parts do not appear on settings
assertNormalizeButton(false)
// Lets rename the branch!
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
repoURLStr := fmt.Sprintf("/api/v1/repos/%s/%s", username, repo.Name)
wikiBranch := "wiki"
req := NewRequestWithJSON(t, "PATCH", repoURLStr, &api.EditRepoOption{
WikiBranch: &wikiBranch,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusOK)
// The wiki branch should now be changed
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, wikiBranch, repo.GetWikiBranchName())
// And as such, the button appears!
csrf := assertNormalizeButton(true)
// Invoking the normalization renames the wiki branch back to the default
req = NewRequestWithValues(t, "POST", settingsURLStr, map[string]string{
"_csrf": csrf,
"action": "rename-wiki-branch",
"repo_name": repo.FullName(),
})
session.MakeRequest(t, req, http.StatusSeeOther)
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, setting.Repository.DefaultBranch, repo.GetWikiBranchName())
assertNormalizeButton(false)
}