Skip to content

Commit 408c929

Browse files
GiteaBotnorohindlunny
authored
Fix PR creation via api between branches of same repo with head field namespaced (#26986) (#29857)
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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent b9dd5dd commit 408c929

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

routers/api/v1/repo/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,8 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
969969
return nil, nil, nil, nil, "", ""
970970
}
971971
headBranch = headInfos[1]
972+
// The head repository can also point to the same repo
973+
isSameRepo = ctx.Repo.Owner.ID == headUser.ID
972974

973975
} else {
974976
ctx.NotFound()

tests/integration/api_pull_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ func TestAPICreatePullSuccess(t *testing.T) {
104104
MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
105105
}
106106

107+
func TestAPICreatePullSameRepoSuccess(t *testing.T) {
108+
defer tests.PrepareTestEnv(t)()
109+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
110+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
111+
112+
session := loginUser(t, owner.Name)
113+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
114+
115+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner.Name, repo.Name, token), &api.CreatePullRequestOption{
116+
Head: fmt.Sprintf("%s:pr-to-update", owner.Name),
117+
Base: "master",
118+
Title: "successfully create a PR between branches of the same repository",
119+
})
120+
MakeRequest(t, req, http.StatusCreated)
121+
MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
122+
}
123+
107124
func TestAPICreatePullWithFieldsSuccess(t *testing.T) {
108125
defer tests.PrepareTestEnv(t)()
109126
// repo10 have code, pulls units.

0 commit comments

Comments
 (0)