@@ -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+ }
0 commit comments