1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-22 17:34:03 +03:00

Disallow empty titles (#5785)

* add util method and tests

* make sure the title of an issue cannot be empty

* wiki title cannot be empty

* pull request title cannot be empty

* update to make use of the new util methof
This commit is contained in:
Lanre Adelowo 2019-01-21 12:45:32 +01:00 committed by Lauris BH
parent 8a92544a3e
commit e1fcd6b742
7 changed files with 48 additions and 3 deletions

View File

@ -9,10 +9,11 @@ package cmd
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -24,7 +25,7 @@ func argsSet(c *cli.Context, args ...string) error {
return errors.New(a + " is not set") return errors.New(a + " is not set")
} }
if len(strings.TrimSpace(c.String(a))) == 0 { if util.IsEmptyString(a) {
return errors.New(a + " is required") return errors.New(a + " is required")
} }
} }

View File

@ -98,3 +98,8 @@ func Min(a, b int) int {
} }
return a return a
} }
// IsEmptyString checks if the provided string is empty
func IsEmptyString(s string) bool {
return len(strings.TrimSpace(s)) == 0
}

View File

@ -77,3 +77,20 @@ func TestIsExternalURL(t *testing.T) {
assert.Equal(t, test.Expected, IsExternalURL(test.RawURL)) assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
} }
} }
func TestIsEmptyString(t *testing.T) {
cases := []struct {
s string
expected bool
}{
{"", true},
{" ", true},
{" ", true},
{" a", false},
}
for _, v := range cases {
assert.Equal(t, v.expected, IsEmptyString(v.s))
}
}

View File

@ -662,6 +662,7 @@ ext_issues.desc = Link to an external issue tracker.
issues.desc = Organize bug reports, tasks and milestones. issues.desc = Organize bug reports, tasks and milestones.
issues.new = New Issue issues.new = New Issue
issues.new.title_empty = Title cannot be empty
issues.new.labels = Labels issues.new.labels = Labels
issues.new.no_label = No Label issues.new.no_label = No Label
issues.new.clear_labels = Clear labels issues.new.clear_labels = Clear labels

View File

@ -355,7 +355,7 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles
} }
} }
// NewIssue render createing issue page // NewIssue render creating issue page
func NewIssue(ctx *context.Context) { func NewIssue(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["Title"] = ctx.Tr("repo.issues.new")
ctx.Data["PageIsIssueList"] = true ctx.Data["PageIsIssueList"] = true
@ -494,6 +494,11 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
return return
} }
if util.IsEmptyString(form.Title) {
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplIssueNew, form)
return
}
issue := &models.Issue{ issue := &models.Issue{
RepoID: repo.ID, RepoID: repo.ID,
Title: form.Title, Title: form.Title,

View File

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/Unknwon/com" "github.com/Unknwon/com"
) )
@ -875,6 +876,16 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
return return
} }
if util.IsEmptyString(form.Title) {
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
if ctx.Written() {
return
}
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplComparePull, form)
return
}
patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch) patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
if err != nil { if err != nil {
ctx.ServerError("GetPatch", err) ctx.ServerError("GetPatch", err)

View File

@ -341,6 +341,11 @@ func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
return return
} }
if util.IsEmptyString(form.Title) {
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
return
}
wikiName := models.NormalizeWikiName(form.Title) wikiName := models.NormalizeWikiName(form.Title)
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil { if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil {
if models.IsErrWikiReservedName(err) { if models.IsErrWikiReservedName(err) {