Skip to content

Commit b4e4b7a

Browse files
authored
Make backend code respond correct JSON when creating PR (#25353)
Fix #25351
1 parent c09d0b4 commit b4e4b7a

File tree

4 files changed

+25
-50
lines changed

4 files changed

+25
-50
lines changed

routers/web/repo/pull.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"code.gitea.io/gitea/modules/upload"
3838
"code.gitea.io/gitea/modules/util"
3939
"code.gitea.io/gitea/modules/web"
40-
"code.gitea.io/gitea/modules/web/middleware"
4140
"code.gitea.io/gitea/routers/utils"
4241
asymkey_service "code.gitea.io/gitea/services/asymkey"
4342
"code.gitea.io/gitea/services/automerge"
@@ -1206,36 +1205,12 @@ func CompareAndPullRequestPost(ctx *context.Context) {
12061205
}
12071206

12081207
if ctx.HasError() {
1209-
middleware.AssignForm(form, ctx.Data)
1210-
1211-
// This stage is already stop creating new pull request, so it does not matter if it has
1212-
// something to compare or not.
1213-
PrepareCompareDiff(ctx, ci,
1214-
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
1215-
if ctx.Written() {
1216-
return
1217-
}
1218-
1219-
if len(form.Title) > 255 {
1220-
var trailer string
1221-
form.Title, trailer = util.SplitStringAtByteN(form.Title, 255)
1222-
1223-
form.Content = trailer + "\n\n" + form.Content
1224-
}
1225-
middleware.AssignForm(form, ctx.Data)
1226-
1227-
ctx.HTML(http.StatusOK, tplCompareDiff)
1208+
ctx.JSONError(ctx.GetErrMsg())
12281209
return
12291210
}
12301211

12311212
if util.IsEmptyString(form.Title) {
1232-
PrepareCompareDiff(ctx, ci,
1233-
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
1234-
if ctx.Written() {
1235-
return
1236-
}
1237-
1238-
ctx.RenderWithErr(ctx.Tr("repo.issues.new.title_empty"), tplCompareDiff, form)
1213+
ctx.JSONError(ctx.Tr("repo.issues.new.title_empty"))
12391214
return
12401215
}
12411216

@@ -1278,28 +1253,28 @@ func CompareAndPullRequestPost(ctx *context.Context) {
12781253
pushrejErr := err.(*git.ErrPushRejected)
12791254
message := pushrejErr.Message
12801255
if len(message) == 0 {
1281-
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
1282-
} else {
1283-
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
1284-
"Message": ctx.Tr("repo.pulls.push_rejected"),
1285-
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
1286-
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
1287-
})
1288-
if err != nil {
1289-
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
1290-
return
1291-
}
1292-
ctx.Flash.Error(flashError)
1256+
ctx.JSONError(ctx.Tr("repo.pulls.push_rejected_no_message"))
1257+
return
12931258
}
1294-
ctx.Redirect(pullIssue.Link())
1259+
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
1260+
"Message": ctx.Tr("repo.pulls.push_rejected"),
1261+
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
1262+
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
1263+
})
1264+
if err != nil {
1265+
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
1266+
return
1267+
}
1268+
ctx.Flash.Error(flashError)
1269+
ctx.JSONRedirect(pullIssue.Link()) // FIXME: it's unfriendly, and will make the content lost
12951270
return
12961271
}
12971272
ctx.ServerError("NewPullRequest", err)
12981273
return
12991274
}
13001275

13011276
log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
1302-
ctx.Redirect(pullIssue.Link())
1277+
ctx.JSONRedirect(pullIssue.Link())
13031278
}
13041279

13051280
// CleanUpPullRequest responses for delete merged branch when PR has been merged

tests/integration/pull_create_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"code.gitea.io/gitea/modules/test"
1415
"code.gitea.io/gitea/tests"
1516

1617
"github.com/stretchr/testify/assert"
@@ -39,8 +40,7 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch, titl
3940
"_csrf": htmlDoc.GetCSRF(),
4041
"title": title,
4142
})
42-
resp = session.MakeRequest(t, req, http.StatusSeeOther)
43-
43+
resp = session.MakeRequest(t, req, http.StatusOK)
4444
return resp
4545
}
4646

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

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

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

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

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

147147
// check the redirected URL
148-
url := resp.Header().Get("Location")
148+
url := test.RedirectURL(resp)
149149
assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)
150150
req := NewRequest(t, "GET", url)
151151
session.MakeRequest(t, req, http.StatusOK)

tests/integration/pull_merge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestCantMergeWorkInProgress(t *testing.T) {
199199

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

202-
req := NewRequest(t, "GET", resp.Header().Get("Location"))
202+
req := NewRequest(t, "GET", test.RedirectURL(resp))
203203
resp = session.MakeRequest(t, req, http.StatusOK)
204204
htmlDoc := NewHTMLParser(t, resp.Body)
205205
text := strings.TrimSpace(htmlDoc.doc.Find(".merge-section > .item").Last().Text())

tests/integration/pull_status_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestPullCreate_CommitStatus(t *testing.T) {
3030
"title": "pull request from status1",
3131
},
3232
)
33-
session.MakeRequest(t, req, http.StatusSeeOther)
33+
session.MakeRequest(t, req, http.StatusOK)
3434

3535
req = NewRequest(t, "GET", "/user1/repo1/pulls")
3636
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -127,7 +127,7 @@ func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
127127
"title": "pull request from status1",
128128
},
129129
)
130-
session.MakeRequest(t, req, http.StatusSeeOther)
130+
session.MakeRequest(t, req, http.StatusOK)
131131

132132
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
133133
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -150,7 +150,7 @@ func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
150150
"title": "pull request from status1",
151151
},
152152
)
153-
session.MakeRequest(t, req, http.StatusSeeOther)
153+
session.MakeRequest(t, req, http.StatusOK)
154154
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
155155
resp := session.MakeRequest(t, req, http.StatusOK)
156156
doc := NewHTMLParser(t, resp.Body)

0 commit comments

Comments
 (0)