Skip to content

Commit f9b822b

Browse files
norohindsilverwind
authored andcommitted
Fix PR creation via api between branches of same repo with head field namespaced (go-gitea#26986)
Fix go-gitea#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. --- Although GitHub implementation of API allows performing such action and since Gitea targeting compatibility with GitHub API I see it as an appropriate change. I'm proposing a fix to the described problem and test case which covers this logic. My use-case just in case: go-gitea#20175 (comment)
1 parent df5ab0c commit f9b822b

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
@@ -1068,6 +1068,8 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
10681068
return nil, nil, nil, nil, "", ""
10691069
}
10701070
headBranch = headInfos[1]
1071+
// The head repository can also point to the same repo
1072+
isSameRepo = ctx.Repo.Owner.ID == headUser.ID
10711073

10721074
} else {
10731075
ctx.NotFound()

tests/integration/api_pull_test.go

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

129+
func TestAPICreatePullSameRepoSuccess(t *testing.T) {
130+
defer tests.PrepareTestEnv(t)()
131+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
132+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
133+
134+
session := loginUser(t, owner.Name)
135+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
136+
137+
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner.Name, repo.Name), &api.CreatePullRequestOption{
138+
Head: fmt.Sprintf("%s:pr-to-update", owner.Name),
139+
Base: "master",
140+
Title: "successfully create a PR between branches of the same repository",
141+
}).AddTokenAuth(token)
142+
MakeRequest(t, req, http.StatusCreated)
143+
MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
144+
}
145+
129146
func TestAPICreatePullWithFieldsSuccess(t *testing.T) {
130147
defer tests.PrepareTestEnv(t)()
131148
// repo10 have code, pulls units.

0 commit comments

Comments
 (0)