forgejo/tests/integration/repo_wiki_test.go
Gergely Nagy 2ca4862f8b
[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)
2024-02-05 16:57:47 +01:00

75 lines
2.4 KiB
Go

// 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)
}