fix(api): keep wiki page name when editing with an empty title (#40153)

Match `PATCH /repos/{owner}/{repo}/wiki/page/{pageName}` document "leave
empty to keep unchanged"

Fixes #39179

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Gaurav Dubeyandsilverwind authored and GitHub committed 2026-11-03 16:20:39 +00:00
1 parent 5c68ba3e0a
commit f26a314636
3 files changed
+28 -12

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ type WikiPageMetaData struct {
// CreateWikiPageOptions form for creating wiki // CreateWikiPageOptions form for creating wiki
type CreateWikiPageOptions struct { type CreateWikiPageOptions struct {
// page title. leave empty to keep unchanged // page title. leave empty to keep unchanged
Title string `json:"title" binding:"TrimSpace;Required"` Title string `json:"title" binding:"TrimSpace"`
// content must be base64 encoded // content must be base64 encoded
ContentBase64 string `json:"content_base64"` ContentBase64 string `json:"content_base64"`
// optional commit message summarizing the change // optional commit message summarizing the change
+8 -4
View File
@@ -49,6 +49,11 @@ func NewWikiPage(ctx *context.APIContext) {
// "$ref": "#/responses/error" // "$ref": "#/responses/error"
form := web.GetForm[*api.CreateWikiPageOptions](ctx) form := web.GetForm[*api.CreateWikiPageOptions](ctx)
// EditWikiPage shares the form but allows an empty title
if form.Title == "" {
ctx.APIError(http.StatusUnprocessableEntity, "title is required")
return
}
wikiName := wiki_service.UserTitleToWebPath("", form.Title) wikiName := wiki_service.UserTitleToWebPath("", form.Title)
if len(form.Message) == 0 { if len(form.Message) == 0 {
@@ -117,10 +122,9 @@ func EditWikiPage(ctx *context.APIContext) {
form := web.GetForm[*api.CreateWikiPageOptions](ctx) form := web.GetForm[*api.CreateWikiPageOptions](ctx)
oldWikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName")) oldWikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName"))
newWikiName := wiki_service.UserTitleToWebPath("", form.Title) newWikiName := oldWikiName
if form.Title != "" {
if len(newWikiName) == 0 { newWikiName = wiki_service.UserTitleToWebPath("", form.Title)
newWikiName = oldWikiName
} }
if len(form.Message) == 0 { if len(form.Message) == 0 {
+19 -7
View File
@@ -172,6 +172,17 @@ func TestAPIGetWikiPage(t *testing.T) {
session := loginUser(t, username) session := loginUser(t, username)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
for _, title := range []string{"", " "} {
content := base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "Edited with title %q", title))
req := NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1/wiki/page/Home", &api.CreateWikiPageOptions{
Title: title,
ContentBase64: content,
}).AddTokenAuth(token)
page := DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.WikiPage{})
assert.Equal(t, "Home", page.Title)
assert.Equal(t, content, page.ContentBase64)
}
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/page/Page-With-Spaced-Name", username, "repo1") urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/wiki/page/Page-With-Spaced-Name", username, "repo1")
req := NewRequestWithJSON(t, "PATCH", urlStr, &api.CreateWikiPageOptions{ req := NewRequestWithJSON(t, "PATCH", urlStr, &api.CreateWikiPageOptions{
@@ -197,13 +208,14 @@ func testAPICreateWikiPage(t *testing.T, session *TestSession, userName, repoNam
} }
func TestAPINewWikiPage(t *testing.T) { func TestAPINewWikiPage(t *testing.T) {
for _, title := range []string{
"New page",
"&&&&",
} {
defer tests.PrepareTestEnv(t)() defer tests.PrepareTestEnv(t)()
username := "user2" session := loginUser(t, "user2")
session := loginUser(t, username) for title, status := range map[string]int{
testAPICreateWikiPage(t, session, username, "repo1", title, http.StatusCreated) "New page": http.StatusCreated,
"&&&&": http.StatusCreated,
"": http.StatusUnprocessableEntity,
" ": http.StatusUnprocessableEntity,
} {
testAPICreateWikiPage(t, session, "user2", "repo1", title, status)
} }
} }