diff options
author | zeripath <art27@cantab.net> | 2020-06-08 19:07:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 19:07:41 +0100 |
commit | 09f7d84f4cbbfdf81caec922638d8fec4cb7dfca (patch) | |
tree | bb253c991c0d34519d7fbcb5cf0e887110c810c2 /routers | |
parent | 5814079bf5fd2369f8720a94128533bad21d1b17 (diff) | |
download | gitea-09f7d84f4cbbfdf81caec922638d8fec4cb7dfca.tar.gz gitea-09f7d84f4cbbfdf81caec922638d8fec4cb7dfca.zip |
Ensure rejected push to refs/pull/index/head fails nicely (#11724)
A pre-receive hook that rejects pushes to refs/pull/index/head
will cause a broken PR which causes an internal server error
whenever it is viewed. This PR handles prevents the internal server
error by handling non-existent pr heads and sends a flash error
informing the creator there was a problem.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/pull.go | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 30913e4766..42846fb93d 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -430,6 +430,20 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare sha, err := baseGitRepo.GetRefCommitID(pull.GetGitRefName()) if err != nil { + if git.IsErrNotExist(err) { + ctx.Data["IsPullRequestBroken"] = true + if pull.IsSameRepo() { + ctx.Data["HeadTarget"] = pull.HeadBranch + } else if pull.HeadRepo == nil { + ctx.Data["HeadTarget"] = "<deleted>:" + pull.HeadBranch + } else { + ctx.Data["HeadTarget"] = pull.HeadRepo.OwnerName + ":" + pull.HeadBranch + } + ctx.Data["BaseTarget"] = pull.BaseBranch + ctx.Data["NumCommits"] = 0 + ctx.Data["NumFiles"] = 0 + return nil + } ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err) return nil } @@ -464,12 +478,10 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare ctx.Data["IsPullRequestBroken"] = true if pull.IsSameRepo() { ctx.Data["HeadTarget"] = pull.HeadBranch + } else if pull.HeadRepo == nil { + ctx.Data["HeadTarget"] = "<deleted>:" + pull.HeadBranch } else { - if pull.HeadRepo == nil { - ctx.Data["HeadTarget"] = "<deleted>:" + pull.HeadBranch - } else { - ctx.Data["HeadTarget"] = pull.HeadRepo.OwnerName + ":" + pull.HeadBranch - } + ctx.Data["HeadTarget"] = pull.HeadRepo.OwnerName + ":" + pull.HeadBranch } } @@ -952,6 +964,16 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error()) return + } else if git.IsErrPushRejected(err) { + pushrejErr := err.(*git.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(pullIssue.Index)) + return } ctx.ServerError("NewPullRequest", err) return |