diff --git a/integrations/api_pull_test.go b/integrations/api_pull_test.go index 9e22be5755..61daf917ff 100644 --- a/integrations/api_pull_test.go +++ b/integrations/api_pull_test.go @@ -58,7 +58,7 @@ func TestAPIMergePullWIP(t *testing.T) { session.MakeRequest(t, req, http.StatusMethodNotAllowed) } -func TestAPICreatePullSuccess1(t *testing.T) { +func TestAPICreatePullSuccess(t *testing.T) { defer prepareTestEnv(t)() repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) // repo10 have code, pulls units. @@ -78,7 +78,7 @@ func TestAPICreatePullSuccess1(t *testing.T) { session.MakeRequest(t, req, 201) } -func TestAPICreatePullSuccess2(t *testing.T) { +func TestAPIEditPull(t *testing.T) { defer prepareTestEnv(t)() repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) @@ -90,6 +90,21 @@ func TestAPICreatePullSuccess2(t *testing.T) { Base: "master", 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) } diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 8dea51f2c6..653091b2f4 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -83,6 +83,7 @@ type CreatePullRequestOption struct { type EditPullRequestOption struct { Title string `json:"title"` Body string `json:"body"` + Base string `json:"base"` Assignee string `json:"assignee"` Assignees []string `json:"assignees"` Milestone int64 `json:"milestone"` diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 284c231628..921f61a612 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -464,6 +464,8 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) { // "$ref": "#/responses/PullRequest" // "403": // "$ref": "#/responses/forbidden" + // "409": + // "$ref": "#/responses/error" // "412": // "$ref": "#/responses/error" // "422": @@ -590,6 +592,30 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) { 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 pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index) if err != nil { diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index e91fad693d..cc802efc95 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -6676,6 +6676,9 @@ "403": { "$ref": "#/responses/forbidden" }, + "409": { + "$ref": "#/responses/error" + }, "412": { "$ref": "#/responses/error" }, @@ -12187,6 +12190,10 @@ }, "x-go-name": "Assignees" }, + "base": { + "type": "string", + "x-go-name": "Base" + }, "body": { "type": "string", "x-go-name": "Body"