]> source.dussan.org Git - gitea.git/commitdiff
Fix PR creation on forked repositories (#31863)
authorJob <LordChunk@users.noreply.github.com>
Fri, 4 Oct 2024 17:12:48 +0000 (19:12 +0200)
committerGitHub <noreply@github.com>
Fri, 4 Oct 2024 17:12:48 +0000 (13:12 -0400)
Resolves #20475

routers/api/v1/repo/pull.go
tests/integration/pull_create_test.go

index 7eb4a8b8a2d4dbc3e9f4b5e6b25353676cd53c0c..4e3de77032fb5bb3be80f4e002550945db508519 100644 (file)
@@ -1124,9 +1124,20 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
        // Check if current user has fork of repository or in the same repository.
        headRepo := repo_model.GetForkedRepo(ctx, headUser.ID, baseRepo.ID)
        if headRepo == nil && !isSameRepo {
-               log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
-               ctx.NotFound("GetForkedRepo")
-               return nil, nil, nil, "", ""
+               err := baseRepo.GetBaseRepo(ctx)
+               if err != nil {
+                       ctx.Error(http.StatusInternalServerError, "GetBaseRepo", err)
+                       return nil, nil, nil, "", ""
+               }
+
+               // Check if baseRepo's base repository is the same as headUser's repository.
+               if baseRepo.BaseRepo == nil || baseRepo.BaseRepo.OwnerID != headUser.ID {
+                       log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
+                       ctx.NotFound("GetBaseRepo")
+                       return nil, nil, nil, "", ""
+               }
+               // Assign headRepo so it can be used below.
+               headRepo = baseRepo.BaseRepo
        }
 
        var headGitRepo *git.Repository
index 5a06a7817f661916c6c2a2c6941cc5e8565acf0b..9812d2073d1e953d79e219e14929cc6e356127cb 100644 (file)
@@ -199,3 +199,30 @@ func TestPullBranchDelete(t *testing.T) {
                session.MakeRequest(t, req, http.StatusOK)
        })
 }
+
+/*
+Setup:
+The base repository is: user2/repo1
+Fork repository to: user1/repo1
+Push extra commit to: user2/repo1, which changes README.md
+Create a PR on user1/repo1
+
+Test checks:
+Check if pull request can be created from base to the fork repository.
+*/
+func TestPullCreatePrFromBaseToFork(t *testing.T) {
+       onGiteaRun(t, func(t *testing.T, u *url.URL) {
+               sessionFork := loginUser(t, "user1")
+               testRepoFork(t, sessionFork, "user2", "repo1", "user1", "repo1", "")
+
+               // Edit base repository
+               sessionBase := loginUser(t, "user2")
+               testEditFile(t, sessionBase, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
+
+               // Create a PR
+               resp := testPullCreateDirectly(t, sessionFork, "user1", "repo1", "master", "user2", "repo1", "master", "This is a pull title")
+               // check the redirected URL
+               url := test.RedirectURL(resp)
+               assert.Regexp(t, "^/user1/repo1/pulls/[0-9]*$", url)
+       })
+}