From b559b3d7d393360dbe238bfe73e6c02c62c427df Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Thu, 22 Jul 2021 18:13:30 +0100 Subject: [PATCH 1/6] Handle too long PR titles correctly 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 --- routers/web/repo/compare.go | 9 +++++++++ routers/web/repo/pull.go | 10 ++++++++++ templates/repo/diff/compare.tmpl | 8 ++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index fddfc4a63a897..f00b35aaad86b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -567,6 +567,15 @@ func PrepareCompareDiff( } else { title = headBranch } + if len(title) > 255 { + if ctx.Data["content"] != nil { + ctx.Data["content"] = fmt.Sprintf("...%s\n\n%s", title[252:], ctx.Data["content"]) + } else { + ctx.Data["content"] = "..." + title[252:] + "\n" + } + title = title[:252] + "..." + } + ctx.Data["title"] = title ctx.Data["Username"] = headUser.Name ctx.Data["Reponame"] = headRepo.Name diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index a299799647777..6ef5a09309b75 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -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 @@ -1037,6 +1041,12 @@ func CompareAndPullRequestPost(ctx *context.Context) { return } + if len(form.Title) > 255 { + form.Content = "..." + form.Title[252:] + "\n\n" + form.Content + form.Title = form.Title[:252] + "..." + } + middleware.AssignForm(form, ctx.Data) + ctx.HTML(http.StatusOK, tplCompareDiff) return } diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index 7959ef7bb5173..2beca579601f8 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -176,10 +176,10 @@ {{if .IsNothingToCompare}} {{if and $.IsSigned $.AllowEmptyPr (not .Repository.IsArchived) }}
{{.i18n.Tr "repo.pulls.nothing_to_compare_and_allow_empty_pr"}}
-
+
- {{else}} {{if and $.IsSigned (not .Repository.IsArchived)}} -
+
{{else if .Repository.IsArchived}} @@ -201,7 +201,7 @@
{{end}} {{if $.IsSigned}} -