Add option to API to update PullRequest base branch (#11666)

* EditPull: add option to change base

Close #11552
release/v1.15
6543 2020-06-07 21:13:40 +02:00 committed by GitHub
parent 880ae454db
commit 5814079bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 3 deletions

View File

@ -58,7 +58,7 @@ func TestAPIMergePullWIP(t *testing.T) {
session.MakeRequest(t, req, http.StatusMethodNotAllowed) session.MakeRequest(t, req, http.StatusMethodNotAllowed)
} }
func TestAPICreatePullSuccess1(t *testing.T) { func TestAPICreatePullSuccess(t *testing.T) {
defer prepareTestEnv(t)() defer prepareTestEnv(t)()
repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
// repo10 have code, pulls units. // repo10 have code, pulls units.
@ -78,7 +78,7 @@ func TestAPICreatePullSuccess1(t *testing.T) {
session.MakeRequest(t, req, 201) session.MakeRequest(t, req, 201)
} }
func TestAPICreatePullSuccess2(t *testing.T) { func TestAPIEditPull(t *testing.T) {
defer prepareTestEnv(t)() defer prepareTestEnv(t)()
repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User)
@ -90,6 +90,21 @@ func TestAPICreatePullSuccess2(t *testing.T) {
Base: "master", Base: "master",
Title: "create a success pr", Title: "create a success pr",
}) })
pull := new(api.PullRequest)
resp := session.MakeRequest(t, req, 201)
DecodeJSON(t, resp, pull)
assert.EqualValues(t, "master", pull.Base.Name)
session.MakeRequest(t, req, 201) req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
Base: "feature/1",
Title: "edit a this pr",
})
resp = session.MakeRequest(t, req, 201)
DecodeJSON(t, resp, pull)
assert.EqualValues(t, "feature/1", pull.Base.Name)
req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
Base: "not-exist",
})
session.MakeRequest(t, req, 404)
} }

View File

@ -83,6 +83,7 @@ type CreatePullRequestOption struct {
type EditPullRequestOption struct { type EditPullRequestOption struct {
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"body"`
Base string `json:"base"`
Assignee string `json:"assignee"` Assignee string `json:"assignee"`
Assignees []string `json:"assignees"` Assignees []string `json:"assignees"`
Milestone int64 `json:"milestone"` Milestone int64 `json:"milestone"`

View File

@ -464,6 +464,8 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
// "$ref": "#/responses/PullRequest" // "$ref": "#/responses/PullRequest"
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
// "409":
// "$ref": "#/responses/error"
// "412": // "412":
// "$ref": "#/responses/error" // "$ref": "#/responses/error"
// "422": // "422":
@ -590,6 +592,30 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed) notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
} }
// change pull target branch
if len(form.Base) != 0 && form.Base != pr.BaseBranch {
if !ctx.Repo.GitRepo.IsBranchExist(form.Base) {
ctx.Error(http.StatusNotFound, "NewBaseBranchNotExist", fmt.Errorf("new base '%s' not exist", form.Base))
return
}
if err := pull_service.ChangeTargetBranch(pr, ctx.User, form.Base); err != nil {
if models.IsErrPullRequestAlreadyExists(err) {
ctx.Error(http.StatusConflict, "IsErrPullRequestAlreadyExists", err)
return
} else if models.IsErrIssueIsClosed(err) {
ctx.Error(http.StatusUnprocessableEntity, "IsErrIssueIsClosed", err)
return
} else if models.IsErrPullRequestHasMerged(err) {
ctx.Error(http.StatusConflict, "IsErrPullRequestHasMerged", err)
return
} else {
ctx.InternalServerError(err)
}
return
}
notification.NotifyPullRequestChangeTargetBranch(ctx.User, pr, form.Base)
}
// Refetch from database // Refetch from database
pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index) pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index)
if err != nil { if err != nil {

View File

@ -6676,6 +6676,9 @@
"403": { "403": {
"$ref": "#/responses/forbidden" "$ref": "#/responses/forbidden"
}, },
"409": {
"$ref": "#/responses/error"
},
"412": { "412": {
"$ref": "#/responses/error" "$ref": "#/responses/error"
}, },
@ -12187,6 +12190,10 @@
}, },
"x-go-name": "Assignees" "x-go-name": "Assignees"
}, },
"base": {
"type": "string",
"x-go-name": "Base"
},
"body": { "body": {
"type": "string", "type": "string",
"x-go-name": "Body" "x-go-name": "Body"