@@ -552,6 +552,15 @@ func CreateBranchProtection(ctx *context.APIContext) {
552552 ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
553553 return
554554 }
555+ forcePushWhitelistUsers , err := user_model .GetUserIDsByNames (ctx , form .ForcePushWhitelistUsernames , false )
556+ if err != nil {
557+ if user_model .IsErrUserNotExist (err ) {
558+ ctx .Error (http .StatusUnprocessableEntity , "User does not exist" , err )
559+ return
560+ }
561+ ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
562+ return
563+ }
555564 mergeWhitelistUsers , err := user_model .GetUserIDsByNames (ctx , form .MergeWhitelistUsernames , false )
556565 if err != nil {
557566 if user_model .IsErrUserNotExist (err ) {
@@ -570,7 +579,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
570579 ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
571580 return
572581 }
573- var whitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
582+ var whitelistTeams , forcePushWhitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
574583 if repo .Owner .IsOrganization () {
575584 whitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .PushWhitelistTeams , false )
576585 if err != nil {
@@ -581,6 +590,15 @@ func CreateBranchProtection(ctx *context.APIContext) {
581590 ctx .Error (http .StatusInternalServerError , "GetTeamIDsByNames" , err )
582591 return
583592 }
593+ forcePushWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .ForcePushWhitelistTeams , false )
594+ if err != nil {
595+ if organization .IsErrTeamNotExist (err ) {
596+ ctx .Error (http .StatusUnprocessableEntity , "Team does not exist" , err )
597+ return
598+ }
599+ ctx .Error (http .StatusInternalServerError , "GetTeamIDsByNames" , err )
600+ return
601+ }
584602 mergeWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .MergeWhitelistTeams , false )
585603 if err != nil {
586604 if organization .IsErrTeamNotExist (err ) {
@@ -606,8 +624,11 @@ func CreateBranchProtection(ctx *context.APIContext) {
606624 RuleName : ruleName ,
607625 CanPush : form .EnablePush ,
608626 EnableWhitelist : form .EnablePush && form .EnablePushWhitelist ,
609- EnableMergeWhitelist : form .EnableMergeWhitelist ,
610627 WhitelistDeployKeys : form .EnablePush && form .EnablePushWhitelist && form .PushWhitelistDeployKeys ,
628+ CanForcePush : form .EnablePush && form .EnableForcePush ,
629+ EnableForcePushWhitelist : form .EnablePush && form .EnableForcePush && form .EnableForcePushWhitelist ,
630+ ForcePushWhitelistDeployKeys : form .EnablePush && form .EnableForcePush && form .EnableForcePushWhitelist && form .ForcePushWhitelistDeployKeys ,
631+ EnableMergeWhitelist : form .EnableMergeWhitelist ,
611632 EnableStatusCheck : form .EnableStatusCheck ,
612633 StatusCheckContexts : form .StatusCheckContexts ,
613634 EnableApprovalsWhitelist : form .EnableApprovalsWhitelist ,
@@ -624,6 +645,8 @@ func CreateBranchProtection(ctx *context.APIContext) {
624645 err = git_model .UpdateProtectBranch (ctx , ctx .Repo .Repository , protectBranch , git_model.WhitelistOptions {
625646 UserIDs : whitelistUsers ,
626647 TeamIDs : whitelistTeams ,
648+ ForcePushUserIDs : forcePushWhitelistUsers ,
649+ ForcePushTeamIDs : forcePushWhitelistTeams ,
627650 MergeUserIDs : mergeWhitelistUsers ,
628651 MergeTeamIDs : mergeWhitelistTeams ,
629652 ApprovalsUserIDs : approvalsWhitelistUsers ,
@@ -754,6 +777,27 @@ func EditBranchProtection(ctx *context.APIContext) {
754777 }
755778 }
756779
780+ if form .EnableForcePush != nil {
781+ if ! * form .EnableForcePush {
782+ protectBranch .CanForcePush = false
783+ protectBranch .EnableForcePushWhitelist = false
784+ protectBranch .ForcePushWhitelistDeployKeys = false
785+ } else {
786+ protectBranch .CanForcePush = true
787+ if form .EnableForcePushWhitelist != nil {
788+ if ! * form .EnableForcePushWhitelist {
789+ protectBranch .EnableForcePushWhitelist = false
790+ protectBranch .ForcePushWhitelistDeployKeys = false
791+ } else {
792+ protectBranch .EnableForcePushWhitelist = true
793+ if form .ForcePushWhitelistDeployKeys != nil {
794+ protectBranch .ForcePushWhitelistDeployKeys = * form .ForcePushWhitelistDeployKeys
795+ }
796+ }
797+ }
798+ }
799+ }
800+
757801 if form .EnableMergeWhitelist != nil {
758802 protectBranch .EnableMergeWhitelist = * form .EnableMergeWhitelist
759803 }
@@ -802,7 +846,7 @@ func EditBranchProtection(ctx *context.APIContext) {
802846 protectBranch .BlockOnOutdatedBranch = * form .BlockOnOutdatedBranch
803847 }
804848
805- var whitelistUsers []int64
849+ var whitelistUsers , forcePushWhitelistUsers , mergeWhitelistUsers , approvalsWhitelistUsers []int64
806850 if form .PushWhitelistUsernames != nil {
807851 whitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .PushWhitelistUsernames , false )
808852 if err != nil {
@@ -816,7 +860,19 @@ func EditBranchProtection(ctx *context.APIContext) {
816860 } else {
817861 whitelistUsers = protectBranch .WhitelistUserIDs
818862 }
819- var mergeWhitelistUsers []int64
863+ if form .ForcePushWhitelistUsernames != nil {
864+ forcePushWhitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .ForcePushWhitelistUsernames , false )
865+ if err != nil {
866+ if user_model .IsErrUserNotExist (err ) {
867+ ctx .Error (http .StatusUnprocessableEntity , "User does not exist" , err )
868+ return
869+ }
870+ ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
871+ return
872+ }
873+ } else {
874+ forcePushWhitelistUsers = protectBranch .ForcePushWhitelistUserIDs
875+ }
820876 if form .MergeWhitelistUsernames != nil {
821877 mergeWhitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .MergeWhitelistUsernames , false )
822878 if err != nil {
@@ -830,7 +886,6 @@ func EditBranchProtection(ctx *context.APIContext) {
830886 } else {
831887 mergeWhitelistUsers = protectBranch .MergeWhitelistUserIDs
832888 }
833- var approvalsWhitelistUsers []int64
834889 if form .ApprovalsWhitelistUsernames != nil {
835890 approvalsWhitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .ApprovalsWhitelistUsernames , false )
836891 if err != nil {
@@ -845,7 +900,7 @@ func EditBranchProtection(ctx *context.APIContext) {
845900 approvalsWhitelistUsers = protectBranch .ApprovalsWhitelistUserIDs
846901 }
847902
848- var whitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
903+ var whitelistTeams , forcePushWhitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
849904 if repo .Owner .IsOrganization () {
850905 if form .PushWhitelistTeams != nil {
851906 whitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .PushWhitelistTeams , false )
@@ -860,6 +915,19 @@ func EditBranchProtection(ctx *context.APIContext) {
860915 } else {
861916 whitelistTeams = protectBranch .WhitelistTeamIDs
862917 }
918+ if form .ForcePushWhitelistTeams != nil {
919+ forcePushWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .ForcePushWhitelistTeams , false )
920+ if err != nil {
921+ if organization .IsErrTeamNotExist (err ) {
922+ ctx .Error (http .StatusUnprocessableEntity , "Team does not exist" , err )
923+ return
924+ }
925+ ctx .Error (http .StatusInternalServerError , "GetTeamIDsByNames" , err )
926+ return
927+ }
928+ } else {
929+ forcePushWhitelistTeams = protectBranch .ForcePushWhitelistTeamIDs
930+ }
863931 if form .MergeWhitelistTeams != nil {
864932 mergeWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .MergeWhitelistTeams , false )
865933 if err != nil {
@@ -891,6 +959,8 @@ func EditBranchProtection(ctx *context.APIContext) {
891959 err = git_model .UpdateProtectBranch (ctx , ctx .Repo .Repository , protectBranch , git_model.WhitelistOptions {
892960 UserIDs : whitelistUsers ,
893961 TeamIDs : whitelistTeams ,
962+ ForcePushUserIDs : forcePushWhitelistUsers ,
963+ ForcePushTeamIDs : forcePushWhitelistTeams ,
894964 MergeUserIDs : mergeWhitelistUsers ,
895965 MergeTeamIDs : mergeWhitelistTeams ,
896966 ApprovalsUserIDs : approvalsWhitelistUsers ,
0 commit comments