From 089ccb0c80050efc8c3afef2402ea4f4a5ed250e Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 22 Feb 2020 13:08:48 +0000 Subject: Handle push rejection message in Merge & Web Editor (#10373) * Handle push rejection message in Merge * placate golangci-lint * Fix sanitize, adjust message handling * oops * Oops * Handle push-rejection in webeditor CRUD too * Apply suggestions from code review Co-authored-by: Lauris BH --- routers/api/v1/repo/pull.go | 8 ++++++++ routers/repo/editor.go | 21 ++++++++++++++++++--- routers/repo/pull.go | 25 ++++++++++++++----------- routers/utils/utils.go | 12 ++++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) (limited to 'routers') diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 1abc2806f8..41e17f8c23 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -684,6 +684,14 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { } else if models.IsErrMergePushOutOfDate(err) { ctx.Error(http.StatusConflict, "Merge", "merge push out of date") return + } else if models.IsErrPushRejected(err) { + errPushRej := err.(models.ErrPushRejected) + if len(errPushRej.Message) == 0 { + ctx.Error(http.StatusConflict, "Merge", "PushRejected without remote error message") + return + } + ctx.Error(http.StatusConflict, "Merge", "PushRejected with remote message: "+errPushRej.Message) + return } ctx.Error(http.StatusInternalServerError, "Merge", err) return diff --git a/routers/repo/editor.go b/routers/repo/editor.go index 8d4f1f8827..97a3fea24c 100644 --- a/routers/repo/editor.go +++ b/routers/repo/editor.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/routers/utils" ) const ( @@ -264,10 +265,17 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo } else { ctx.Error(500, err.Error()) } - } else if models.IsErrCommitIDDoesNotMatch(err) { + } else if models.IsErrCommitIDDoesNotMatch(err) || models.IsErrMergePushOutOfDate(err) { ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+form.LastCommit+"..."+ctx.Repo.CommitID), tplEditFile, &form) + } else if models.IsErrPushRejected(err) { + errPushRej := err.(models.ErrPushRejected) + if len(errPushRej.Message) == 0 { + ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplEditFile, &form) + } else { + ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected", utils.SanitizeFlashErrorString(errPushRej.Message)), tplEditFile, &form) + } } else { - ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, err), tplEditFile, &form) + ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, utils.SanitizeFlashErrorString(err.Error())), tplEditFile, &form) } } @@ -428,8 +436,15 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) { } else { ctx.Error(500, err.Error()) } - } else if models.IsErrCommitIDDoesNotMatch(err) { + } else if models.IsErrCommitIDDoesNotMatch(err) || models.IsErrMergePushOutOfDate(err) { ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_deleting", ctx.Repo.RepoLink+"/compare/"+form.LastCommit+"..."+ctx.Repo.CommitID), tplDeleteFile, &form) + } else if models.IsErrPushRejected(err) { + errPushRej := err.(models.ErrPushRejected) + if len(errPushRej.Message) == 0 { + ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected_no_message"), tplDeleteFile, &form) + } else { + ctx.RenderWithErr(ctx.Tr("repo.editor.push_rejected", utils.SanitizeFlashErrorString(errPushRej.Message)), tplDeleteFile, &form) + } } else { ctx.ServerError("DeleteRepoFile", err) } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index b4760fd527..11c376be7e 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/modules/repofiles" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/gitdiff" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" @@ -774,27 +775,18 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) { } if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { - sanitize := func(x string) string { - runes := []rune(x) - - if len(runes) > 512 { - x = "..." + string(runes[len(runes)-512:]) - } - - return strings.Replace(html.EscapeString(x), "\n", "
", -1) - } if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) return } else if models.IsErrMergeConflicts(err) { conflictError := err.(models.ErrMergeConflicts) - ctx.Flash.Error(ctx.Tr("repo.pulls.merge_conflict", sanitize(conflictError.StdErr), sanitize(conflictError.StdOut))) + ctx.Flash.Error(ctx.Tr("repo.pulls.merge_conflict", utils.SanitizeFlashErrorString(conflictError.StdErr), utils.SanitizeFlashErrorString(conflictError.StdOut))) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) return } else if models.IsErrRebaseConflicts(err) { conflictError := err.(models.ErrRebaseConflicts) - ctx.Flash.Error(ctx.Tr("repo.pulls.rebase_conflict", sanitize(conflictError.CommitSHA), sanitize(conflictError.StdErr), sanitize(conflictError.StdOut))) + ctx.Flash.Error(ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA), utils.SanitizeFlashErrorString(conflictError.StdErr), utils.SanitizeFlashErrorString(conflictError.StdOut))) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) return } else if models.IsErrMergeUnrelatedHistories(err) { @@ -807,6 +799,17 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) { ctx.Flash.Error(ctx.Tr("repo.pulls.merge_out_of_date")) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) return + } else if models.IsErrPushRejected(err) { + log.Debug("MergePushRejected error: %v", err) + pushrejErr := err.(models.ErrPushRejected) + message := pushrejErr.Message + if len(message) == 0 { + ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message")) + } else { + ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected", utils.SanitizeFlashErrorString(pushrejErr.Message))) + } + ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index)) + return } ctx.ServerError("Merge", err) return diff --git a/routers/utils/utils.go b/routers/utils/utils.go index 7c90fd7048..64b132ff3e 100644 --- a/routers/utils/utils.go +++ b/routers/utils/utils.go @@ -5,6 +5,7 @@ package utils import ( + "html" "strings" ) @@ -34,3 +35,14 @@ func IsValidSlackChannel(channelName string) bool { return true } + +// SanitizeFlashErrorString will sanitize a flash error string +func SanitizeFlashErrorString(x string) string { + runes := []rune(x) + + if len(runes) > 512 { + x = "..." + string(runes[len(runes)-512:]) + } + + return strings.Replace(html.EscapeString(x), "\n", "
", -1) +} -- cgit v1.2.3