Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not able to update local created non-urlencoded wiki pages #16139

Merged
merged 9 commits into from
Jul 7, 2021
48 changes: 40 additions & 8 deletions services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func NormalizeWikiName(name string) string {
return strings.ReplaceAll(name, "-", " ")
}

// NameToUnescapedFilename converts a wiki name to its corresponding filename without url query escape.
func NameToUnescapedFilename(name string) string {
6543 marked this conversation as resolved.
Show resolved Hide resolved
name = strings.ReplaceAll(name, " ", "-")
BLumia marked this conversation as resolved.
Show resolved Hide resolved
return name + ".md"
}

// NameToFilename converts a wiki name to its corresponding filename.
func NameToFilename(name string) string {
name = strings.ReplaceAll(name, " ", "-")
Expand Down Expand Up @@ -81,6 +87,19 @@ func InitWiki(repo *models.Repository) error {
return nil
}

func checkFileExistence(gitRepo *git.Repository, filePath string) (bool, error) {
BLumia marked this conversation as resolved.
Show resolved Hide resolved
filesInIndex, err := gitRepo.LsFiles(filePath)
if err != nil {
log.Error("%v", err)
return false, err
}
if util.IsStringInSlice(filePath, filesInIndex) {
return true, nil
}

return false, nil
}

// updateWikiPage adds a new page to the repository wiki.
func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
if err = nameAllowed(newWikiName); err != nil {
Expand Down Expand Up @@ -133,27 +152,40 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new
}
}

newWikiPath := NameToFilename(newWikiName)
newWikiPath := NameToUnescapedFilename(newWikiName)
isWikiExist, err := checkFileExistence(gitRepo, newWikiPath)
if err != nil {
return err
}
if !isWikiExist {
newWikiPath = NameToFilename(newWikiName)
}

if isNew {
filesInIndex, err := gitRepo.LsFiles(newWikiPath)
isNewWikiExist, err := checkFileExistence(gitRepo, newWikiPath)
if err != nil {
log.Error("%v", err)
return err
}
if util.IsStringInSlice(newWikiPath, filesInIndex) {
if isNewWikiExist {
return models.ErrWikiAlreadyExist{
Title: newWikiPath,
}
}
} else {
oldWikiPath := NameToFilename(oldWikiName)
filesInIndex, err := gitRepo.LsFiles(oldWikiPath)
oldWikiPath := NameToUnescapedFilename(oldWikiName)
isOldWikiExist, err := checkFileExistence(gitRepo, oldWikiPath)
if err != nil {
log.Error("%v", err)
return err
}
if !isOldWikiExist {
oldWikiPath = NameToFilename(oldWikiName)
isOldWikiExist, err = checkFileExistence(gitRepo, oldWikiPath)
if err != nil {
return err
}
}

if util.IsStringInSlice(oldWikiPath, filesInIndex) {
if isOldWikiExist {
err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
if err != nil {
log.Error("%v", err)
Expand Down