Skip to content

Commit e1fcd6b

Browse files
adelowolafriks
authored andcommitted
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
1 parent 8a92544 commit e1fcd6b

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

cmd/cmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ package cmd
99
import (
1010
"errors"
1111
"fmt"
12-
"strings"
1312

1413
"code.gitea.io/gitea/models"
1514
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/util"
16+
1617
"github.com/urfave/cli"
1718
)
1819

@@ -24,7 +25,7 @@ func argsSet(c *cli.Context, args ...string) error {
2425
return errors.New(a + " is not set")
2526
}
2627

27-
if len(strings.TrimSpace(c.String(a))) == 0 {
28+
if util.IsEmptyString(a) {
2829
return errors.New(a + " is required")
2930
}
3031
}

modules/util/util.go

+5
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ func Min(a, b int) int {
9898
}
9999
return a
100100
}
101+
102+
// IsEmptyString checks if the provided string is empty
103+
func IsEmptyString(s string) bool {
104+
return len(strings.TrimSpace(s)) == 0
105+
}

modules/util/util_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,20 @@ func TestIsExternalURL(t *testing.T) {
7777
assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
7878
}
7979
}
80+
81+
func TestIsEmptyString(t *testing.T) {
82+
83+
cases := []struct {
84+
s string
85+
expected bool
86+
}{
87+
{"", true},
88+
{" ", true},
89+
{" ", true},
90+
{" a", false},
91+
}
92+
93+
for _, v := range cases {
94+
assert.Equal(t, v.expected, IsEmptyString(v.s))
95+
}
96+
}

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ ext_issues.desc = Link to an external issue tracker.
662662
663663
issues.desc = Organize bug reports, tasks and milestones.
664664
issues.new = New Issue
665+
issues.new.title_empty = Title cannot be empty
665666
issues.new.labels = Labels
666667
issues.new.no_label = No Label
667668
issues.new.clear_labels = Clear labels

routers/repo/issue.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles
355355
}
356356
}
357357

358-
// NewIssue render createing issue page
358+
// NewIssue render creating issue page
359359
func NewIssue(ctx *context.Context) {
360360
ctx.Data["Title"] = ctx.Tr("repo.issues.new")
361361
ctx.Data["PageIsIssueList"] = true
@@ -494,6 +494,11 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
494494
return
495495
}
496496

497+
if util.IsEmptyString(form.Title) {
498+
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplIssueNew, form)
499+
return
500+
}
501+
497502
issue := &models.Issue{
498503
RepoID: repo.ID,
499504
Title: form.Title,

routers/repo/pull.go

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/notification"
2424
"code.gitea.io/gitea/modules/setting"
25+
"code.gitea.io/gitea/modules/util"
2526

2627
"github.com/Unknwon/com"
2728
)
@@ -875,6 +876,16 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
875876
return
876877
}
877878

879+
if util.IsEmptyString(form.Title) {
880+
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
881+
if ctx.Written() {
882+
return
883+
}
884+
885+
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplComparePull, form)
886+
return
887+
}
888+
878889
patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
879890
if err != nil {
880891
ctx.ServerError("GetPatch", err)

routers/repo/wiki.go

+5
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ func NewWikiPost(ctx *context.Context, form auth.NewWikiForm) {
341341
return
342342
}
343343

344+
if util.IsEmptyString(form.Title) {
345+
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplWikiNew, form)
346+
return
347+
}
348+
344349
wikiName := models.NormalizeWikiName(form.Title)
345350
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, wikiName, form.Content, form.Message); err != nil {
346351
if models.IsErrWikiReservedName(err) {

0 commit comments

Comments
 (0)