@ -13,13 +13,24 @@ import (
)
// Update updates pull request with base branch.
func Update ( pull * models . PullRequest , doer * models . User , message string ) error {
//use merge functions but switch repo's and branch's
pr := & models . PullRequest {
HeadRepoID : pull . BaseRepoID ,
BaseRepoID : pull . HeadRepoID ,
HeadBranch : pull . BaseBranch ,
BaseBranch : pull . HeadBranch ,
func Update ( pull * models . PullRequest , doer * models . User , message string , rebase bool ) error {
var (
pr * models . PullRequest
style models . MergeStyle
)
if rebase {
pr = pull
style = models . MergeStyleRebaseUpdate
} else {
//use merge functions but switch repo's and branch's
pr = & models . PullRequest {
HeadRepoID : pull . BaseRepoID ,
BaseRepoID : pull . HeadRepoID ,
HeadBranch : pull . BaseBranch ,
BaseBranch : pull . HeadBranch ,
}
style = models . MergeStyleMerge
}
if pull . Flow == models . PullRequestFlowAGit {
@ -42,9 +53,13 @@ func Update(pull *models.PullRequest, doer *models.User, message string) error {
return fmt . Errorf ( "HeadBranch of PR %d is up to date" , pull . Index )
}
_ , err = rawMerge ( pr , doer , models . MergeS tyleMerg e, message )
_ , err = rawMerge ( pr , doer , s tyle, message )
defer func ( ) {
if rebase {
go AddTestPullRequestTask ( doer , pr . BaseRepo . ID , pr . BaseBranch , false , "" , "" )
return
}
go AddTestPullRequestTask ( doer , pr . HeadRepo . ID , pr . HeadBranch , false , "" , "" )
} ( )
@ -52,17 +67,17 @@ func Update(pull *models.PullRequest, doer *models.User, message string) error {
}
// IsUserAllowedToUpdate check if user is allowed to update PR with given permissions and branch protections
func IsUserAllowedToUpdate ( pull * models . PullRequest , user * models . User ) ( bool , error ) {
func IsUserAllowedToUpdate ( pull * models . PullRequest , user * models . User ) ( mergeAllowed , rebaseAllowed bool , err error ) {
if pull . Flow == models . PullRequestFlowAGit {
return false , nil
return false , false , nil
}
if user == nil {
return false , nil
return false , false , nil
}
headRepoPerm , err := models . GetUserRepoPermission ( pull . HeadRepo , user )
if err != nil {
return false , err
return false , false , err
}
pr := & models . PullRequest {
@ -74,15 +89,25 @@ func IsUserAllowedToUpdate(pull *models.PullRequest, user *models.User) (bool, e
err = pr . LoadProtectedBranch ( )
if err != nil {
return false , err
return false , false , err
}
// can't do rebase on protected branch because need force push
if pr . ProtectedBranch == nil {
rebaseAllowed = true
}
// Update function need push permission
if pr . ProtectedBranch != nil && ! pr . ProtectedBranch . CanUserPush ( user . ID ) {
return false , nil
return false , false , nil
}
mergeAllowed , err = IsUserAllowedToMerge ( pr , headRepoPerm , user )
if err != nil {
return false , false , err
}
return IsUserAllowedToMerge ( pr , headRepoPerm , user )
return mergeAllowed , rebaseAllowed , nil
}
// GetDiverging determines how many commits a PR is ahead or behind the PR base branch