Skip to content

Commit 2b8b277

Browse files
lunnyLordChunk
andauthored
Fix PR creation on forked repositories (#31863) (#32591)
Resolves #20475 Backport #31863 Co-authored-by: Job <[email protected]>
1 parent 87ceecf commit 2b8b277

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

routers/api/v1/repo/pull.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1103,9 +1103,20 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
11031103
// Check if current user has fork of repository or in the same repository.
11041104
headRepo := repo_model.GetForkedRepo(ctx, headUser.ID, baseRepo.ID)
11051105
if headRepo == nil && !isSameRepo {
1106-
log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
1107-
ctx.NotFound("GetForkedRepo")
1108-
return nil, nil, nil, nil, "", ""
1106+
err := baseRepo.GetBaseRepo(ctx)
1107+
if err != nil {
1108+
ctx.Error(http.StatusInternalServerError, "GetBaseRepo", err)
1109+
return nil, nil, nil, nil, "", ""
1110+
}
1111+
1112+
// Check if baseRepo's base repository is the same as headUser's repository.
1113+
if baseRepo.BaseRepo == nil || baseRepo.BaseRepo.OwnerID != headUser.ID {
1114+
log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
1115+
ctx.NotFound("GetBaseRepo")
1116+
return nil, nil, nil, nil, "", ""
1117+
}
1118+
// Assign headRepo so it can be used below.
1119+
headRepo = baseRepo.BaseRepo
11091120
}
11101121

11111122
var headGitRepo *git.Repository

tests/integration/pull_create_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,30 @@ func TestPullBranchDelete(t *testing.T) {
199199
session.MakeRequest(t, req, http.StatusOK)
200200
})
201201
}
202+
203+
/*
204+
Setup:
205+
The base repository is: user2/repo1
206+
Fork repository to: user1/repo1
207+
Push extra commit to: user2/repo1, which changes README.md
208+
Create a PR on user1/repo1
209+
210+
Test checks:
211+
Check if pull request can be created from base to the fork repository.
212+
*/
213+
func TestPullCreatePrFromBaseToFork(t *testing.T) {
214+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
215+
sessionFork := loginUser(t, "user1")
216+
testRepoFork(t, sessionFork, "user2", "repo1", "user1", "repo1", "")
217+
218+
// Edit base repository
219+
sessionBase := loginUser(t, "user2")
220+
testEditFile(t, sessionBase, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
221+
222+
// Create a PR
223+
resp := testPullCreateDirectly(t, sessionFork, "user1", "repo1", "master", "user2", "repo1", "master", "This is a pull title")
224+
// check the redirected URL
225+
url := test.RedirectURL(resp)
226+
assert.Regexp(t, "^/user1/repo1/pulls/[0-9]*$", url)
227+
})
228+
}

0 commit comments

Comments
 (0)