From c647e8639f30ef57fe1f6598208bc1aa72371389 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 24 Apr 2024 22:16:21 +0200 Subject: [PATCH 1/4] api: The repo wiki APIs should respect WikiBranch Back in #2264, we made it possible to change the branch wikis use from the hardcoded "master" branch to `[repository].DEFAULT_BRANCH`. However, the API endpoints were not updated, and the "master" branch remained hardcoded there. This change fixes that, the API endpoints will now respect the repository's `WikiBranch`. Fixes #3391. Signed-off-by: Gergely Nagy --- routers/api/v1/repo/wiki.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index f18ea087c4..b6f51cdadc 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -193,7 +193,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi } // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.GetWikiBranchName(), pageFilename) // Get last change information. lastCommit, err := wikiRepo.GetCommitByPath(pageFilename) @@ -432,7 +432,7 @@ func ListPageRevisions(ctx *context.APIContext) { } // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.GetWikiBranchName(), pageFilename) page := ctx.FormInt("page") if page <= 1 { @@ -442,7 +442,7 @@ func ListPageRevisions(ctx *context.APIContext) { // get Commit Count commitsHistory, err := wikiRepo.CommitsByFileAndRange( git.CommitsByFileAndRangeOptions{ - Revision: "master", + Revision: ctx.Repo.Repository.GetWikiBranchName(), File: pageFilename, Page: page, }) @@ -487,7 +487,7 @@ func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) return nil, nil } - commit, err := wikiRepo.GetBranchCommit("master") + commit, err := wikiRepo.GetBranchCommit(ctx.Repo.Repository.GetWikiBranchName()) if err != nil { if git.IsErrNotExist(err) { ctx.NotFound(err) From 6d2f6453637cc4335dca74f1afad5a80c6d34282 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 25 Apr 2024 00:08:53 +0200 Subject: [PATCH 2/4] tests: Let CreateDeclarativeRepoWithOptions create a Wiki too Add a new member to `DeclarativeRepoOptions`: `WikiBranch`. If specified, create a Wiki with the given branch, and a single "Home" page. This will be used by an upcoming test. Signed-off-by: Gergely Nagy --- tests/integration/integration_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 7a373c74ee..61a243e52f 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -46,6 +46,7 @@ import ( repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" user_service "code.gitea.io/gitea/services/user" + wiki_service "code.gitea.io/gitea/services/wiki" "code.gitea.io/gitea/tests" "github.com/PuerkitoBio/goquery" @@ -658,6 +659,7 @@ type DeclarativeRepoOptions struct { EnabledUnits optional.Option[[]unit_model.Type] DisabledUnits optional.Option[[]unit_model.Type] Files optional.Option[[]*files_service.ChangeRepoFile] + WikiBranch optional.Option[string] } func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) { @@ -734,6 +736,22 @@ func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts sha = resp.Commit.SHA } + // If there's a Wiki branch specified, create a wiki, and a default wiki page. + if opts.WikiBranch.Has() { + // Set the wiki branch in the database first + repo.WikiBranch = opts.WikiBranch.Value() + err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "wiki_branch") + assert.NoError(t, err) + + // Initialize the wiki + err = wiki_service.InitWiki(db.DefaultContext, repo) + assert.NoError(t, err) + + // Add a new wiki page + err = wiki_service.AddWikiPage(db.DefaultContext, owner, repo, "Home", "Welcome to the wiki!", "Add a Home page") + assert.NoError(t, err) + } + // Return the repo, the top commit, and a defer-able function to delete the // repo. return repo, sha, func() { From a1dfe07bfcbedd895602609ad1846356ab342d76 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 25 Apr 2024 00:17:53 +0200 Subject: [PATCH 3/4] tests: Test the Wiki APIs with a non-master branch Signed-off-by: Gergely Nagy --- tests/integration/api_wiki_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/integration/api_wiki_test.go b/tests/integration/api_wiki_test.go index 2b71616112..400cf068b4 100644 --- a/tests/integration/api_wiki_test.go +++ b/tests/integration/api_wiki_test.go @@ -16,6 +16,7 @@ import ( unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/optional" api "code.gitea.io/gitea/modules/structs" repo_service "code.gitea.io/gitea/services/repository" "code.gitea.io/gitea/tests" @@ -382,3 +383,28 @@ func TestAPIListPageRevisions(t *testing.T) { assert.Equal(t, dummyrevisions, revisions) } + +func TestAPIWikiNonMasterBranch(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + repo, _, f := CreateDeclarativeRepoWithOptions(t, user, DeclarativeRepoOptions{ + WikiBranch: optional.Some("main"), + }) + defer f() + + uris := []string{ + "revisions/Home", + "pages", + "page/Home", + } + baseURL := fmt.Sprintf("/api/v1/repos/%s/wiki", repo.FullName()) + for _, uri := range uris { + t.Run(uri, func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequestf(t, "GET", "%s/%s", baseURL, uri) + MakeRequest(t, req, http.StatusOK) + }) + } +} From 1d894dda241f9bba46c09bb7d83f60811b715665 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 25 Apr 2024 01:07:41 +0200 Subject: [PATCH 4/4] Add a note about the previous bugfix to RELEASE-NOTES Signed-off-by: Gergely Nagy --- RELEASE-NOTES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 2ad10ff564..f38182a6eb 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -12,6 +12,8 @@ In addition to the following notable bug fixes, you can browse the [full list of * **Bug fixes:** +- Fixed a bug where the `/api/v1/repos/{owner}/{repo}/wiki` API endpoints were using a hardcoded "master" branch for the wiki, rather than the branch they really use. ([#3430](https://codeberg.org/forgejo/forgejo/pulls/3430)) + ## 7.0.0 The [complete list of commits](https://codeberg.org/forgejo/forgejo/commits/branch/v7.0/forgejo) included in the `Forgejo v7.0.0` release can be reviewed from the command line with: