]> source.dussan.org Git - gitea.git/commitdiff
Fix PR creation via api between branches of same repo with head field namespaced...
authorGiteabot <teabot@gitea.io>
Wed, 20 Mar 2024 01:38:30 +0000 (09:38 +0800)
committerGitHub <noreply@github.com>
Wed, 20 Mar 2024 01:38:30 +0000 (09:38 +0800)
Backport #26986 by @norohind

Fix #20175

Current implementation of API does not allow creating pull requests
between branches of the same
repo when you specify *namespace* (owner of the repo) in `head` field in
http request body.

---------

Co-authored-by: norohind <60548839+norohind@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
routers/api/v1/repo/pull.go
tests/integration/api_pull_test.go

index 5b9ec4e1324afc525c9c50f6b002bd2249a4b2a9..f55ede3f730c7a3dd207734725ac35b504716047 100644 (file)
@@ -969,6 +969,8 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
                        return nil, nil, nil, nil, "", ""
                }
                headBranch = headInfos[1]
+               // The head repository can also point to the same repo
+               isSameRepo = ctx.Repo.Owner.ID == headUser.ID
 
        } else {
                ctx.NotFound()
index 9d590630e451017038798457a0d8aa21d929a671..693ee3f413dcc664624dd75fb3c88a7219b5d556 100644 (file)
@@ -104,6 +104,23 @@ func TestAPICreatePullSuccess(t *testing.T) {
        MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
 }
 
+func TestAPICreatePullSameRepoSuccess(t *testing.T) {
+       defer tests.PrepareTestEnv(t)()
+       repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
+       owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
+
+       session := loginUser(t, owner.Name)
+       token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
+
+       req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner.Name, repo.Name, token), &api.CreatePullRequestOption{
+               Head:  fmt.Sprintf("%s:pr-to-update", owner.Name),
+               Base:  "master",
+               Title: "successfully create a PR between branches of the same repository",
+       })
+       MakeRequest(t, req, http.StatusCreated)
+       MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
+}
+
 func TestAPICreatePullWithFieldsSuccess(t *testing.T) {
        defer tests.PrepareTestEnv(t)()
        // repo10 have code, pulls units.