diff --git a/models/forgejo_migrations/migrate.go b/models/forgejo_migrations/migrate.go index f0e22c046..da417cc08 100644 --- a/models/forgejo_migrations/migrate.go +++ b/models/forgejo_migrations/migrate.go @@ -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. diff --git a/models/forgejo_migrations/v1_22/v6.go b/models/forgejo_migrations/v1_22/v6.go new file mode 100644 index 000000000..70feef033 --- /dev/null +++ b/models/forgejo_migrations/v1_22/v6.go @@ -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 +} diff --git a/models/repo/repo.go b/models/repo/repo.go index 13493ba6e..a7bc4b3c7 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -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 == "" { diff --git a/modules/context/repo.go b/modules/context/repo.go index 75ebfec70..b48f6ded2 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -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. diff --git a/modules/repository/repo.go b/modules/repository/repo.go index fc3af0407..65b50b2e4 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -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 } diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 51e175fba..483723b57 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -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. diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 260bf862c..1c6a26819 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -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 CANNOT 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. diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 636d58b4a..b7ac5b454 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -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 diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 9ade318c8..e7a0628df 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -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) diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 5e7b971e6..2c445fe7f 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -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, }) diff --git a/services/convert/repository.go b/services/convert/repository.go index c16180c0a..b032d97d7 100644 --- a/services/convert/repository.go +++ b/services/convert/repository.go @@ -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, diff --git a/services/repository/create.go b/services/repository/create.go index a648c0d81..c3b50ae74 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -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, } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index f264ca876..81e0b84ea 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -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, diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 59c77060f..ef0c3a0a3 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -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) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index f822af0dd..8e2efea8d 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -780,6 +780,17 @@ {{if .Permission.CanRead $.UnitTypeWiki}} + {{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}} +
+
+
{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main"}}
+
{{ctx.Locale.Tr "repo.settings.wiki_rename_branch_main_desc" .DefaultWikiBranchName}}
+
+
+ +
+
+ {{end}}
{{ctx.Locale.Tr "repo.settings.wiki_delete"}}
@@ -991,6 +1002,40 @@
+ {{if ne $.Repository.GetWikiBranchName .DefaultWikiBranchName}} + + {{end}} {{end}} {{if not .Repository.IsMirror}} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 92ee62aff..09ad79827 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -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" diff --git a/tests/integration/api_wiki_test.go b/tests/integration/api_wiki_test.go index 19fd8c536..c61b4a061 100644 --- a/tests/integration/api_wiki_test.go +++ b/tests/integration/api_wiki_test.go @@ -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)() diff --git a/tests/integration/forgejo_confirmation_repo_test.go b/tests/integration/forgejo_confirmation_repo_test.go index 7e1629ebd..c63d0ae75 100644 --- a/tests/integration/forgejo_confirmation_repo_test.go +++ b/tests/integration/forgejo_confirmation_repo_test.go @@ -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") diff --git a/tests/integration/repo_wiki_test.go b/tests/integration/repo_wiki_test.go new file mode 100644 index 000000000..171191e16 --- /dev/null +++ b/tests/integration/repo_wiki_test.go @@ -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) +}