Skip to content

Commit f1b1264

Browse files
committed
fix
1 parent 2db933d commit f1b1264

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

routers/private/hook_post_receive.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,11 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
303303
}
304304

305305
if pr == nil {
306-
if repo.IsFork {
307-
branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
308-
}
309306
results = append(results, private.HookPostReceiveBranchResult{
310307
Message: setting.Git.PullRequestPushMessage && baseRepo.AllowsPulls(ctx),
311308
Create: true,
312309
Branch: branch,
313-
URL: fmt.Sprintf("%s/compare/%s...%s", baseRepo.HTMLURL(), util.PathEscapeSegments(baseRepo.DefaultBranch), util.PathEscapeSegments(branch)),
310+
URL: fmt.Sprintf("%s/pulls/new/%s", repo.HTMLURL(), util.PathEscapeSegments(branch)),
314311
})
315312
} else {
316313
results = append(results, private.HookPostReceiveBranchResult{

routers/web/repo/pull.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,22 @@ func stopTimerIfAvailable(ctx *context.Context, user *user_model.User, issue *is
12691269
return nil
12701270
}
12711271

1272+
func PullsNewRedirect(ctx *context.Context) {
1273+
branch := ctx.PathParam("*")
1274+
redirectRepo := ctx.Repo.Repository
1275+
repo := ctx.Repo.Repository
1276+
if repo.IsFork {
1277+
if err := repo.GetBaseRepo(ctx); err != nil {
1278+
ctx.ServerError("GetBaseRepo", err)
1279+
}
1280+
if repo.BaseRepo != nil {
1281+
redirectRepo = repo.BaseRepo
1282+
branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
1283+
}
1284+
}
1285+
ctx.Redirect(fmt.Sprintf("%s/compare/%s...%s?expand=1", redirectRepo.Link(), util.PathEscapeSegments(redirectRepo.DefaultBranch), util.PathEscapeSegments(branch)))
1286+
}
1287+
12721288
// CompareAndPullRequestPost response for creating pull request
12731289
func CompareAndPullRequestPost(ctx *context.Context) {
12741290
form := web.GetForm(ctx).(*forms.CreateIssueForm)

routers/web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ func registerRoutes(m *web.Router) {
11851185
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
11861186
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
11871187
Post(reqSignIn, context.RepoMustNotBeArchived(), reqUnitPullsReader, repo.MustAllowPulls, web.Bind(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
1188+
m.Get("/pulls/new/*", repo.PullsNewRedirect)
11881189
}, optSignIn, context.RepoAssignment, reqUnitCodeReader)
11891190
// end "/{username}/{reponame}": repo code: find, compare, list
11901191

tests/integration/pull_compare_test.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,38 @@ import (
2424
func TestPullCompare(t *testing.T) {
2525
defer tests.PrepareTestEnv(t)()
2626

27-
session := loginUser(t, "user2")
28-
req := NewRequest(t, "GET", "/user2/repo1/pulls")
29-
resp := session.MakeRequest(t, req, http.StatusOK)
30-
htmlDoc := NewHTMLParser(t, resp.Body)
31-
link, exists := htmlDoc.doc.Find(".new-pr-button").Attr("href")
32-
assert.True(t, exists, "The template has changed")
33-
34-
req = NewRequest(t, "GET", link)
35-
resp = session.MakeRequest(t, req, http.StatusOK)
36-
assert.EqualValues(t, http.StatusOK, resp.Code)
37-
38-
// test the edit button in the PR diff view
39-
req = NewRequest(t, "GET", "/user2/repo1/pulls/3/files")
40-
resp = session.MakeRequest(t, req, http.StatusOK)
41-
doc := NewHTMLParser(t, resp.Body)
42-
editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
43-
assert.Positive(t, editButtonCount, "Expected to find a button to edit a file in the PR diff view but there were none")
27+
t.Run("PullsNewRedirect", func(t *testing.T) {
28+
req := NewRequest(t, "GET", "/user2/repo1/pulls/new/foo")
29+
resp := MakeRequest(t, req, http.StatusSeeOther)
30+
redirect := test.RedirectURL(resp)
31+
assert.Equal(t, "/user2/repo1/compare/master...foo?expand=1", redirect)
32+
33+
req = NewRequest(t, "GET", "/user13/repo11/pulls/new/foo")
34+
resp = MakeRequest(t, req, http.StatusSeeOther)
35+
redirect = test.RedirectURL(resp)
36+
assert.Equal(t, "/user12/repo10/compare/master...user13:foo?expand=1", redirect)
37+
})
38+
39+
t.Run("ButtonsExist", func(t *testing.T) {
40+
session := loginUser(t, "user2")
41+
42+
// test the "New PR" button
43+
req := NewRequest(t, "GET", "/user2/repo1/pulls")
44+
resp := session.MakeRequest(t, req, http.StatusOK)
45+
htmlDoc := NewHTMLParser(t, resp.Body)
46+
link, exists := htmlDoc.doc.Find(".new-pr-button").Attr("href")
47+
assert.True(t, exists, "The template has changed")
48+
req = NewRequest(t, "GET", link)
49+
resp = session.MakeRequest(t, req, http.StatusOK)
50+
assert.EqualValues(t, http.StatusOK, resp.Code)
51+
52+
// test the edit button in the PR diff view
53+
req = NewRequest(t, "GET", "/user2/repo1/pulls/3/files")
54+
resp = session.MakeRequest(t, req, http.StatusOK)
55+
doc := NewHTMLParser(t, resp.Body)
56+
editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
57+
assert.Positive(t, editButtonCount, "Expected to find a button to edit a file in the PR diff view but there were none")
58+
})
4459

4560
onGiteaRun(t, func(t *testing.T, u *url.URL) {
4661
defer tests.PrepareTestEnv(t)()
@@ -54,8 +69,8 @@ func TestPullCompare(t *testing.T) {
5469
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
5570
issueIndex := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueIndex{GroupID: repo1.ID}, unittest.OrderBy("group_id ASC"))
5671
prFilesURL := fmt.Sprintf("/user2/repo1/pulls/%d/files", issueIndex.MaxIndex)
57-
req = NewRequest(t, "GET", prFilesURL)
58-
resp = session.MakeRequest(t, req, http.StatusOK)
72+
req := NewRequest(t, "GET", prFilesURL)
73+
resp := session.MakeRequest(t, req, http.StatusOK)
5974
doc := NewHTMLParser(t, resp.Body)
6075
editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
6176
assert.Positive(t, editButtonCount, "Expected to find a button to edit a file in the PR diff view but there were none")

0 commit comments

Comments
 (0)