Skip to content

Commit dee33df

Browse files
6543Yohann Delafollye
authored andcommitted
Add option to API to update PullRequest base branch (go-gitea#11666)
* EditPull: add option to change base Close go-gitea#11552
1 parent fdff2e8 commit dee33df

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

integrations/api_pull_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestAPIMergePullWIP(t *testing.T) {
5858
session.MakeRequest(t, req, http.StatusMethodNotAllowed)
5959
}
6060

61-
func TestAPICreatePullSuccess1(t *testing.T) {
61+
func TestAPICreatePullSuccess(t *testing.T) {
6262
defer prepareTestEnv(t)()
6363
repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
6464
// repo10 have code, pulls units.
@@ -78,7 +78,7 @@ func TestAPICreatePullSuccess1(t *testing.T) {
7878
session.MakeRequest(t, req, 201)
7979
}
8080

81-
func TestAPICreatePullSuccess2(t *testing.T) {
81+
func TestAPIEditPull(t *testing.T) {
8282
defer prepareTestEnv(t)()
8383
repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
8484
owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User)
@@ -90,6 +90,21 @@ func TestAPICreatePullSuccess2(t *testing.T) {
9090
Base: "master",
9191
Title: "create a success pr",
9292
})
93+
pull := new(api.PullRequest)
94+
resp := session.MakeRequest(t, req, 201)
95+
DecodeJSON(t, resp, pull)
96+
assert.EqualValues(t, "master", pull.Base.Name)
97+
98+
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{
99+
Base: "feature/1",
100+
Title: "edit a this pr",
101+
})
102+
resp = session.MakeRequest(t, req, 201)
103+
DecodeJSON(t, resp, pull)
104+
assert.EqualValues(t, "feature/1", pull.Base.Name)
93105

94-
session.MakeRequest(t, req, 201)
106+
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{
107+
Base: "not-exist",
108+
})
109+
session.MakeRequest(t, req, 404)
95110
}

modules/structs/pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type CreatePullRequestOption struct {
8383
type EditPullRequestOption struct {
8484
Title string `json:"title"`
8585
Body string `json:"body"`
86+
Base string `json:"base"`
8687
Assignee string `json:"assignee"`
8788
Assignees []string `json:"assignees"`
8889
Milestone int64 `json:"milestone"`

routers/api/v1/repo/pull.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
464464
// "$ref": "#/responses/PullRequest"
465465
// "403":
466466
// "$ref": "#/responses/forbidden"
467+
// "409":
468+
// "$ref": "#/responses/error"
467469
// "412":
468470
// "$ref": "#/responses/error"
469471
// "422":
@@ -590,6 +592,30 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
590592
notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
591593
}
592594

595+
// change pull target branch
596+
if len(form.Base) != 0 && form.Base != pr.BaseBranch {
597+
if !ctx.Repo.GitRepo.IsBranchExist(form.Base) {
598+
ctx.Error(http.StatusNotFound, "NewBaseBranchNotExist", fmt.Errorf("new base '%s' not exist", form.Base))
599+
return
600+
}
601+
if err := pull_service.ChangeTargetBranch(pr, ctx.User, form.Base); err != nil {
602+
if models.IsErrPullRequestAlreadyExists(err) {
603+
ctx.Error(http.StatusConflict, "IsErrPullRequestAlreadyExists", err)
604+
return
605+
} else if models.IsErrIssueIsClosed(err) {
606+
ctx.Error(http.StatusUnprocessableEntity, "IsErrIssueIsClosed", err)
607+
return
608+
} else if models.IsErrPullRequestHasMerged(err) {
609+
ctx.Error(http.StatusConflict, "IsErrPullRequestHasMerged", err)
610+
return
611+
} else {
612+
ctx.InternalServerError(err)
613+
}
614+
return
615+
}
616+
notification.NotifyPullRequestChangeTargetBranch(ctx.User, pr, form.Base)
617+
}
618+
593619
// Refetch from database
594620
pr, err = models.GetPullRequestByIndex(ctx.Repo.Repository.ID, pr.Index)
595621
if err != nil {

templates/swagger/v1_json.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6676,6 +6676,9 @@
66766676
"403": {
66776677
"$ref": "#/responses/forbidden"
66786678
},
6679+
"409": {
6680+
"$ref": "#/responses/error"
6681+
},
66796682
"412": {
66806683
"$ref": "#/responses/error"
66816684
},
@@ -12187,6 +12190,10 @@
1218712190
},
1218812191
"x-go-name": "Assignees"
1218912192
},
12193+
"base": {
12194+
"type": "string",
12195+
"x-go-name": "Base"
12196+
},
1219012197
"body": {
1219112198
"type": "string",
1219212199
"x-go-name": "Body"

0 commit comments

Comments
 (0)