Skip to content

Make backend code respond correct JSON when creating PR #25353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 16 additions & 41 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"code.gitea.io/gitea/modules/upload"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/routers/utils"
asymkey_service "code.gitea.io/gitea/services/asymkey"
"code.gitea.io/gitea/services/automerge"
Expand Down Expand Up @@ -1206,36 +1205,12 @@ func CompareAndPullRequestPost(ctx *context.Context) {
}

if ctx.HasError() {
middleware.AssignForm(form, ctx.Data)

// This stage is already stop creating new pull request, so it does not matter if it has
// something to compare or not.
PrepareCompareDiff(ctx, ci,
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
if ctx.Written() {
return
}

if len(form.Title) > 255 {
var trailer string
form.Title, trailer = util.SplitStringAtByteN(form.Title, 255)

form.Content = trailer + "\n\n" + form.Content
}
middleware.AssignForm(form, ctx.Data)

ctx.HTML(http.StatusOK, tplCompareDiff)
ctx.JSONError(ctx.GetErrMsg())
return
}

if util.IsEmptyString(form.Title) {
PrepareCompareDiff(ctx, ci,
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
if ctx.Written() {
return
}

ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplCompareDiff, form)
ctx.JSONError(ctx.Tr("repo.issues.new.title_empty"))
return
}

Expand Down Expand Up @@ -1278,28 +1253,28 @@ func CompareAndPullRequestPost(ctx *context.Context) {
pushrejErr := err.(*git.ErrPushRejected)
message := pushrejErr.Message
if len(message) == 0 {
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
} else {
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
"Message": ctx.Tr("repo.pulls.push_rejected"),
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
})
if err != nil {
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
return
}
ctx.Flash.Error(flashError)
ctx.JSONError(ctx.Tr("repo.pulls.push_rejected_no_message"))
return
}
ctx.Redirect(pullIssue.Link())
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
"Message": ctx.Tr("repo.pulls.push_rejected"),
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
})
if err != nil {
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
return
}
ctx.Flash.Error(flashError)
ctx.JSONRedirect(pullIssue.Link()) // FIXME: it's unfriendly, and will make the content lost
return
}
ctx.ServerError("NewPullRequest", err)
return
}

log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
ctx.Redirect(pullIssue.Link())
ctx.JSONRedirect(pullIssue.Link())
}

// CleanUpPullRequest responses for delete merged branch when PR has been merged
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/pull_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,8 +40,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch, titl
"_csrf": htmlDoc.GetCSRF(),
"title": title,
})
resp = session.MakeRequest(t, req, http.StatusSeeOther)

resp = session.MakeRequest(t, req, http.StatusOK)
return resp
}

Expand All @@ -52,7 +52,7 @@ func TestPullCreate(t *testing.T) {
resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title")

// check the redirected URL
url := resp.Header().Get("Location")
url := test.RedirectURL(resp)
assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)

// check .diff can be accessed and matches performed change
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestPullCreate_TitleEscape(t *testing.T) {
resp := testPullCreate(t, session, "user1", "repo1", "master", "<i>XSS PR</i>")

// check the redirected URL
url := resp.Header().Get("Location")
url := test.RedirectURL(resp)
assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)

// Edit title
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestPullBranchDelete(t *testing.T) {
resp := testPullCreate(t, session, "user1", "repo1", "master1", "This is a pull title")

// check the redirected URL
url := resp.Header().Get("Location")
url := test.RedirectURL(resp)
assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)
req := NewRequest(t, "GET", url)
session.MakeRequest(t, req, http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestCantMergeWorkInProgress(t *testing.T) {

resp := testPullCreate(t, session, "user1", "repo1", "master", "[wip] This is a pull title")

req := NewRequest(t, "GET", resp.Header().Get("Location"))
req := NewRequest(t, "GET", test.RedirectURL(resp))
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
text := strings.TrimSpace(htmlDoc.doc.Find(".merge-section > .item").Last().Text())
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/pull_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestPullCreate_CommitStatus(t *testing.T) {
"title": "pull request from status1",
},
)
session.MakeRequest(t, req, http.StatusSeeOther)
session.MakeRequest(t, req, http.StatusOK)

req = NewRequest(t, "GET", "/user1/repo1/pulls")
resp := session.MakeRequest(t, req, http.StatusOK)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
"title": "pull request from status1",
},
)
session.MakeRequest(t, req, http.StatusSeeOther)
session.MakeRequest(t, req, http.StatusOK)

req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
resp := session.MakeRequest(t, req, http.StatusOK)
Expand All @@ -150,7 +150,7 @@ func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
"title": "pull request from status1",
},
)
session.MakeRequest(t, req, http.StatusSeeOther)
session.MakeRequest(t, req, http.StatusOK)
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
Expand Down