Skip to content

Commit 8a6790b

Browse files
authored
Add API to update pr headBranch (#12419)
* [API] Add update pr headBranch api Signed-off-by: a1012112796 <1012112796@qq.com>
1 parent e61c09e commit 8a6790b

5 files changed

Lines changed: 159 additions & 7 deletions

File tree

integrations/pull_update_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package integrations
66

77
import (
8-
"fmt"
8+
"net/http"
99
"net/url"
1010
"testing"
1111
"time"
@@ -19,7 +19,7 @@ import (
1919
"github.com/stretchr/testify/assert"
2020
)
2121

22-
func TestPullUpdate(t *testing.T) {
22+
func TestAPIPullUpdate(t *testing.T) {
2323
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
2424
//Create PR to test
2525
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
@@ -31,17 +31,19 @@ func TestPullUpdate(t *testing.T) {
3131
assert.NoError(t, err)
3232
assert.EqualValues(t, 1, diffCount.Behind)
3333
assert.EqualValues(t, 1, diffCount.Ahead)
34+
assert.NoError(t, pr.LoadBaseRepo())
35+
assert.NoError(t, pr.LoadIssue())
3436

35-
message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch)
36-
err = pull_service.Update(pr, user, message)
37-
assert.NoError(t, err)
37+
session := loginUser(t, "user2")
38+
token := getTokenForLoggedInUser(t, session)
39+
req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
40+
session.MakeRequest(t, req, http.StatusOK)
3841

3942
//Test GetDiverging after update
4043
diffCount, err = pull_service.GetDiverging(pr)
4144
assert.NoError(t, err)
4245
assert.EqualValues(t, 0, diffCount.Behind)
4346
assert.EqualValues(t, 2, diffCount.Ahead)
44-
4547
})
4648
}
4749

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ func RegisterRoutes(m *macaron.Macaron) {
806806
Patch(reqToken(), reqRepoWriter(models.UnitTypePullRequests), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
807807
m.Get(".diff", repo.DownloadPullDiff)
808808
m.Get(".patch", repo.DownloadPullPatch)
809+
m.Post("/update", reqToken(), repo.UpdatePullRequest)
809810
m.Combo("/merge").Get(repo.IsPullRequestMerged).
810811
Post(reqToken(), mustNotBeArchived, bind(auth.MergePullRequestForm{}), repo.MergePullRequest)
811812
m.Group("/reviews", func() {

routers/api/v1/repo/pull.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,99 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
968968

969969
return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
970970
}
971+
972+
// UpdatePullRequest merge PR's baseBranch into headBranch
973+
func UpdatePullRequest(ctx *context.APIContext) {
974+
// swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/update repository repoUpdatePullRequest
975+
// ---
976+
// summary: Merge PR's baseBranch into headBranch
977+
// produces:
978+
// - application/json
979+
// parameters:
980+
// - name: owner
981+
// in: path
982+
// description: owner of the repo
983+
// type: string
984+
// required: true
985+
// - name: repo
986+
// in: path
987+
// description: name of the repo
988+
// type: string
989+
// required: true
990+
// - name: index
991+
// in: path
992+
// description: index of the pull request to get
993+
// type: integer
994+
// format: int64
995+
// required: true
996+
// responses:
997+
// "200":
998+
// "$ref": "#/responses/empty"
999+
// "403":
1000+
// "$ref": "#/responses/forbidden"
1001+
// "404":
1002+
// "$ref": "#/responses/notFound"
1003+
// "409":
1004+
// "$ref": "#/responses/error"
1005+
// "422":
1006+
// "$ref": "#/responses/validationError"
1007+
1008+
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
1009+
if err != nil {
1010+
if models.IsErrPullRequestNotExist(err) {
1011+
ctx.NotFound()
1012+
} else {
1013+
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
1014+
}
1015+
return
1016+
}
1017+
1018+
if pr.HasMerged {
1019+
ctx.Error(http.StatusUnprocessableEntity, "UpdatePullRequest", err)
1020+
return
1021+
}
1022+
1023+
if err = pr.LoadIssue(); err != nil {
1024+
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
1025+
return
1026+
}
1027+
1028+
if pr.Issue.IsClosed {
1029+
ctx.Error(http.StatusUnprocessableEntity, "UpdatePullRequest", err)
1030+
return
1031+
}
1032+
1033+
if err = pr.LoadBaseRepo(); err != nil {
1034+
ctx.Error(http.StatusInternalServerError, "LoadBaseRepo", err)
1035+
return
1036+
}
1037+
if err = pr.LoadHeadRepo(); err != nil {
1038+
ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
1039+
return
1040+
}
1041+
1042+
allowedUpdate, err := pull_service.IsUserAllowedToUpdate(pr, ctx.User)
1043+
if err != nil {
1044+
ctx.Error(http.StatusInternalServerError, "IsUserAllowedToMerge", err)
1045+
return
1046+
}
1047+
1048+
if !allowedUpdate {
1049+
ctx.Status(http.StatusForbidden)
1050+
return
1051+
}
1052+
1053+
// default merge commit message
1054+
message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch)
1055+
1056+
if err = pull_service.Update(pr, ctx.User, message); err != nil {
1057+
if models.IsErrMergeConflicts(err) {
1058+
ctx.Error(http.StatusConflict, "Update", "merge failed because of conflict")
1059+
return
1060+
}
1061+
ctx.Error(http.StatusInternalServerError, "pull_service.Update", err)
1062+
return
1063+
}
1064+
1065+
ctx.Status(http.StatusOK)
1066+
}

routers/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func ViewPullFiles(ctx *context.Context) {
666666
ctx.HTML(200, tplPullFiles)
667667
}
668668

669-
// UpdatePullRequest merge master into PR
669+
// UpdatePullRequest merge PR's baseBranch into headBranch
670670
func UpdatePullRequest(ctx *context.Context) {
671671
issue := checkPullInfo(ctx)
672672
if ctx.Written() {

templates/swagger/v1_json.tmpl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7285,6 +7285,59 @@
72857285
}
72867286
}
72877287
},
7288+
"/repos/{owner}/{repo}/pulls/{index}/update": {
7289+
"post": {
7290+
"produces": [
7291+
"application/json"
7292+
],
7293+
"tags": [
7294+
"repository"
7295+
],
7296+
"summary": "Merge PR's baseBranch into headBranch",
7297+
"operationId": "repoUpdatePullRequest",
7298+
"parameters": [
7299+
{
7300+
"type": "string",
7301+
"description": "owner of the repo",
7302+
"name": "owner",
7303+
"in": "path",
7304+
"required": true
7305+
},
7306+
{
7307+
"type": "string",
7308+
"description": "name of the repo",
7309+
"name": "repo",
7310+
"in": "path",
7311+
"required": true
7312+
},
7313+
{
7314+
"type": "integer",
7315+
"format": "int64",
7316+
"description": "index of the pull request to get",
7317+
"name": "index",
7318+
"in": "path",
7319+
"required": true
7320+
}
7321+
],
7322+
"responses": {
7323+
"200": {
7324+
"$ref": "#/responses/empty"
7325+
},
7326+
"403": {
7327+
"$ref": "#/responses/forbidden"
7328+
},
7329+
"404": {
7330+
"$ref": "#/responses/notFound"
7331+
},
7332+
"409": {
7333+
"$ref": "#/responses/error"
7334+
},
7335+
"422": {
7336+
"$ref": "#/responses/validationError"
7337+
}
7338+
}
7339+
}
7340+
},
72887341
"/repos/{owner}/{repo}/raw/{filepath}": {
72897342
"get": {
72907343
"produces": [

0 commit comments

Comments
 (0)