From 385718dd78bf0b398091565494828aef2ec85f44 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 17 Aug 2024 01:04:54 +0800 Subject: [PATCH] Avoid returning without written ctx when posting PR (#31843) Fix #31625. If `pull_service.NewPullRequest` return an error which misses each `if` check, `CompareAndPullRequestPost` will return immediately, since it doesn't write the HTTP response, a 200 response with empty body will be sent to clients. ```go if err := pull_service.NewPullRequest(ctx, repo, pullIssue, labelIDs, attachments, pullRequest, assigneeIDs); err != nil { if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error()) } else if git.IsErrPushRejected(err) { // ... ctx.JSONError(flashError) } else if errors.Is(err, user_model.ErrBlockedUser) { // ... ctx.JSONError(flashError) } else if errors.Is(err, issues_model.ErrMustCollaborator) { // ... ctx.JSONError(flashError) } return } ``` Not sure what kind of error can cause it to happen, so this PR just expose it. And we can fix it when users report that creating PRs failed with error responses. It's all my guess since I cannot reproduce the problem, but even if it's not related, the code here needs to be improved. (cherry picked from commit acd7053e9d4968e8b9812ab379be9027ac8e7771) Conflicts: routers/web/repo/pull.go trivial context conflict --- routers/web/repo/pull.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 56a46f7f06..b372ef140a 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1524,13 +1524,12 @@ func CompareAndPullRequestPost(ctx *context.Context) { // instead of 500. if err := pull_service.NewPullRequest(ctx, repo, pullIssue, labelIDs, attachments, pullRequest, assigneeIDs); err != nil { - if errors.Is(err, user_model.ErrBlockedByUser) { + switch { + case errors.Is(err, user_model.ErrBlockedByUser): ctx.JSONError(ctx.Tr("repo.pulls.blocked_by_user")) - return - } else if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) { + case repo_model.IsErrUserDoesNotHaveAccessToRepo(err): ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error()) - return - } else if git.IsErrPushRejected(err) { + case git.IsErrPushRejected(err): pushrejErr := err.(*git.ErrPushRejected) message := pushrejErr.Message if len(message) == 0 { @@ -1547,7 +1546,11 @@ func CompareAndPullRequestPost(ctx *context.Context) { return } ctx.JSONError(flashError) - return + default: + // It's an unexpected error. + // If it happens, we should add another case to handle it. + log.Error("Unexpected error of NewPullRequest: %T %s", err, err) + ctx.ServerError("CompareAndPullRequest", err) } ctx.ServerError("NewPullRequest", err) return