diff options
author | Lanre Adelowo <adelowomailbox@gmail.com> | 2019-01-21 12:45:32 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-01-21 13:45:32 +0200 |
commit | e1fcd6b7427d1e7c43ac9dcb32462a8e6d77c905 (patch) | |
tree | 46b617eb487061dcb8bfd3da7175d7bd23f51e6d | |
parent | 8a92544a3e06e0c1146ee6d0c340d662e1b0633c (diff) | |
download | gitea-e1fcd6b7427d1e7c43ac9dcb32462a8e6d77c905.tar.gz gitea-e1fcd6b7427d1e7c43ac9dcb32462a8e6d77c905.zip |
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
-rw-r--r-- | cmd/cmd.go | 5 | ||||
-rw-r--r-- | modules/util/util.go | 5 | ||||
-rw-r--r-- | modules/util/util_test.go | 17 | ||||
-rw-r--r-- | options/locale/locale_en-US.ini | 1 | ||||
-rw-r--r-- | routers/repo/issue.go | 7 | ||||
-rw-r--r-- | routers/repo/pull.go | 11 | ||||
-rw-r--r-- | routers/repo/wiki.go | 5 |
7 files changed, 48 insertions, 3 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go index d246d2599d..5a55ac318c 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,10 +9,11 @@ package cmd import ( "errors" "fmt" - "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" + "github.com/urfave/cli" ) @@ -24,7 +25,7 @@ func argsSet(c *cli.Context, args ...string) error { 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") } } diff --git a/modules/util/util.go b/modules/util/util.go index 5dcbe448fc..b2067c8df6 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -98,3 +98,8 @@ func Min(a, b int) int { } return a } + +// IsEmptyString checks if the provided string is empty +func IsEmptyString(s string) bool { + return len(strings.TrimSpace(s)) == 0 +} diff --git a/modules/util/util_test.go b/modules/util/util_test.go index d9357ffa3d..3a2b4b71ff 100644 --- a/modules/util/util_test.go +++ b/modules/util/util_test.go @@ -77,3 +77,20 @@ func TestIsExternalURL(t *testing.T) { 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)) + } +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 0c32f3f5fd..85ce82084b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -662,6 +662,7 @@ ext_issues.desc = Link to an external issue tracker. issues.desc = Organize bug reports, tasks and milestones. issues.new = New Issue +issues.new.title_empty = Title cannot be empty issues.new.labels = Labels issues.new.no_label = No Label issues.new.clear_labels = Clear labels diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 6c6c2612bf..8e0b22eb62 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -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) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true @@ -494,6 +494,11 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) { return } + if util.IsEmptyString(form.Title) { + ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplIssueNew, form) + return + } + issue := &models.Issue{ RepoID: repo.ID, Title: form.Title, diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 688a033fdb..eecb4809a6 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" ) @@ -875,6 +876,16 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) 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) if err != nil { ctx.ServerError("GetPatch", err) diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index 7e52129773..4cea3c890a 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -341,6 +341,11 @@ func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) { return } + if util.IsEmptyString(form.Title) { + ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form) + return + } + wikiName := models.NormalizeWikiName(form.Title) if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil { if models.IsErrWikiReservedName(err) { |