Skip to content

Commit 6932754

Browse files
authored
Handle too long PR titles correctly (#16517) (#16549)
Backport #16517 The CompareAndPullRequestPost handler for POST to /compare incorrectly handles returning errors to the user. For a start it does not set the necessary markers to switch SimpleMDE but it also does not immediately return to the form. This PR fixes this by setting the appropriate values, fixing the templates and preventing the suggestion of a too long title. Fix #16507 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 9152743 commit 6932754

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

modules/util/truncate.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import "unicode/utf8"
8+
9+
// SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)
10+
func SplitStringAtByteN(input string, n int) (left, right string) {
11+
if len(input) <= n {
12+
left = input
13+
return
14+
}
15+
16+
if !utf8.ValidString(input) {
17+
left = input[:n-3] + "..."
18+
right = "..." + input[n-3:]
19+
return
20+
}
21+
22+
// in UTF8 "…" is 3 bytes so doesn't really gain us anything...
23+
end := 0
24+
for end <= n-3 {
25+
_, size := utf8.DecodeRuneInString(input[end:])
26+
if end+size > n-3 {
27+
break
28+
}
29+
end += size
30+
}
31+
32+
left = input[:end] + "…"
33+
right = "…" + input[end:]
34+
return
35+
}

routers/web/repo/compare.go

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/log"
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/upload"
27+
"code.gitea.io/gitea/modules/util"
2728
"code.gitea.io/gitea/services/gitdiff"
2829
)
2930

@@ -567,6 +568,18 @@ func PrepareCompareDiff(
567568
} else {
568569
title = headBranch
569570
}
571+
if len(title) > 255 {
572+
var trailer string
573+
title, trailer = util.SplitStringAtByteN(title, 255)
574+
if len(trailer) > 0 {
575+
if ctx.Data["content"] != nil {
576+
ctx.Data["content"] = fmt.Sprintf("%s\n\n%s", trailer, ctx.Data["content"])
577+
} else {
578+
ctx.Data["content"] = trailer + "\n"
579+
}
580+
}
581+
}
582+
570583
ctx.Data["title"] = title
571584
ctx.Data["Username"] = headUser.Name
572585
ctx.Data["Reponame"] = headRepo.Name

routers/web/repo/pull.go

+12
Original file line numberDiff line numberDiff line change
@@ -1001,10 +1001,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10011001
ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
10021002
ctx.Data["PageIsComparePull"] = true
10031003
ctx.Data["IsDiffCompare"] = true
1004+
ctx.Data["IsRepoToolbarCommits"] = true
1005+
ctx.Data["RequireTribute"] = true
1006+
ctx.Data["RequireSimpleMDE"] = true
10041007
ctx.Data["RequireHighlightJS"] = true
10051008
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
10061009
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
10071010
upload.AddUploadContext(ctx, "comment")
1011+
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(models.UnitTypePullRequests)
10081012

10091013
var (
10101014
repo = ctx.Repo.Repository
@@ -1037,6 +1041,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10371041
return
10381042
}
10391043

1044+
if len(form.Title) > 255 {
1045+
var trailer string
1046+
form.Title, trailer = util.SplitStringAtByteN(form.Title, 255)
1047+
1048+
form.Content = trailer + "\n\n" + form.Content
1049+
}
1050+
middleware.AssignForm(form, ctx.Data)
1051+
10401052
ctx.HTML(http.StatusOK, tplCompareDiff)
10411053
return
10421054
}

templates/repo/diff/compare.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@
176176
{{if .IsNothingToCompare}}
177177
{{if and $.IsSigned $.AllowEmptyPr (not .Repository.IsArchived) }}
178178
<div class="ui segment">{{.i18n.Tr "repo.pulls.nothing_to_compare_and_allow_empty_pr"}}</div>
179-
<div class="ui info message show-form-container">
179+
<div class="ui info message show-form-container" {{if .Flash}}style="display: none"{{end}}>
180180
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
181181
</div>
182-
<div class="pullrequest-form" style="display: none">
182+
<div class="pullrequest-form" {{if not .Flash}}style="display: none"{{end}}>
183183
{{template "repo/issue/new_form" .}}
184184
</div>
185185
{{else}}
@@ -192,7 +192,7 @@
192192
</div>
193193
{{else}}
194194
{{if and $.IsSigned (not .Repository.IsArchived)}}
195-
<div class="ui info message show-form-container">
195+
<div class="ui info message show-form-container" {{if .Flash}}style="display: none"{{end}}>
196196
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
197197
</div>
198198
{{else if .Repository.IsArchived}}
@@ -201,7 +201,7 @@
201201
</div>
202202
{{end}}
203203
{{if $.IsSigned}}
204-
<div class="pullrequest-form" style="display: none">
204+
<div class="pullrequest-form" {{if not .Flash}}style="display: none"{{end}}>
205205
{{template "repo/issue/new_form" .}}
206206
</div>
207207
{{end}}

0 commit comments

Comments
 (0)