mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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 <lauris@nix.lv>
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -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", "<br>", -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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", "<br>", -1)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user