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

Handle too long PR titles correctly #16517

Merged
merged 8 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions modules/util/truncate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package util

import "unicode/utf8"

// SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)
func SplitStringAtByteN(input string, n int) (left, right string) {
if len(input) <= n {
left = input
return
}

if !utf8.ValidString(input) {
left = input[:n-3] + "..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidString don't mean it's totally ascii. Maybe we should correct it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test here is to simply abort trying to be nice if the string is not a valid utf8 string. (Which could be ASCII, Big5, UTF-16...)

If you don't have a utf-8 string in your commit header - getting it truncated nicely is going to be the least of your worries. We really don't handle other encodings in git commit headers.

right = "..." + input[n-3:]
return
}

// in UTF8 "…" is 3 bytes so doesn't really gain us anything...
end := 0
for end <= n-3 {
_, size := utf8.DecodeRuneInString(input[end:])
if end+size > n-3 {
break
}
end += size
}

left = input[:end] + "…"
right = "…" + input[end:]
return
}
13 changes: 13 additions & 0 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/upload"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/gitdiff"
)

Expand Down Expand Up @@ -567,6 +568,18 @@ func PrepareCompareDiff(
} else {
title = headBranch
}
if len(title) > 255 {
var trailer string
title, trailer = util.SplitStringAtByteN(title, 255)
if len(trailer) > 0 {
if ctx.Data["content"] != nil {
ctx.Data["content"] = fmt.Sprintf("%s\n\n%s", trailer, ctx.Data["content"])
} else {
ctx.Data["content"] = trailer + "\n"
}
}
}

ctx.Data["title"] = title
ctx.Data["Username"] = headUser.Name
ctx.Data["Reponame"] = headRepo.Name
Expand Down
12 changes: 12 additions & 0 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
ctx.Data["PageIsComparePull"] = true
ctx.Data["IsDiffCompare"] = true
ctx.Data["IsRepoToolbarCommits"] = true
ctx.Data["RequireTribute"] = true
ctx.Data["RequireSimpleMDE"] = true
ctx.Data["RequireHighlightJS"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
upload.AddUploadContext(ctx, "comment")
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(models.UnitTypePullRequests)

var (
repo = ctx.Repo.Repository
Expand Down Expand Up @@ -1037,6 +1041,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
return
}

if len(form.Title) > 255 {
var trailer string
form.Title, trailer = util.SplitStringAtByteN(form.Title, 255)

form.Content = trailer + "\n\n" + form.Content
}
middleware.AssignForm(form, ctx.Data)

ctx.HTML(http.StatusOK, tplCompareDiff)
return
}
Expand Down
8 changes: 4 additions & 4 deletions templates/repo/diff/compare.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@
{{if .IsNothingToCompare}}
{{if and $.IsSigned $.AllowEmptyPr (not .Repository.IsArchived) }}
<div class="ui segment">{{.i18n.Tr "repo.pulls.nothing_to_compare_and_allow_empty_pr"}}</div>
<div class="ui info message show-form-container">
<div class="ui info message show-form-container" {{if .Flash}}style="display: none"{{end}}>
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
</div>
<div class="pullrequest-form" style="display: none">
<div class="pullrequest-form" {{if not .Flash}}style="display: none"{{end}}>
{{template "repo/issue/new_form" .}}
</div>
{{else}}
Expand All @@ -192,7 +192,7 @@
</div>
{{else}}
{{if and $.IsSigned (not .Repository.IsArchived)}}
<div class="ui info message show-form-container">
<div class="ui info message show-form-container" {{if .Flash}}style="display: none"{{end}}>
<button class="ui button green show-form">{{.i18n.Tr "repo.pulls.new"}}</button>
</div>
{{else if .Repository.IsArchived}}
Expand All @@ -201,7 +201,7 @@
</div>
{{end}}
{{if $.IsSigned}}
<div class="pullrequest-form" style="display: none">
<div class="pullrequest-form" {{if not .Flash}}style="display: none"{{end}}>
{{template "repo/issue/new_form" .}}
</div>
{{end}}
Expand Down