@@ -552,6 +552,15 @@ func CreateBranchProtection(ctx *context.APIContext) {
552
552
ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
553
553
return
554
554
}
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
+ }
555
564
mergeWhitelistUsers , err := user_model .GetUserIDsByNames (ctx , form .MergeWhitelistUsernames , false )
556
565
if err != nil {
557
566
if user_model .IsErrUserNotExist (err ) {
@@ -570,7 +579,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
570
579
ctx .Error (http .StatusInternalServerError , "GetUserIDsByNames" , err )
571
580
return
572
581
}
573
- var whitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
582
+ var whitelistTeams , forcePushWhitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
574
583
if repo .Owner .IsOrganization () {
575
584
whitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .PushWhitelistTeams , false )
576
585
if err != nil {
@@ -581,6 +590,15 @@ func CreateBranchProtection(ctx *context.APIContext) {
581
590
ctx .Error (http .StatusInternalServerError , "GetTeamIDsByNames" , err )
582
591
return
583
592
}
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
+ }
584
602
mergeWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .MergeWhitelistTeams , false )
585
603
if err != nil {
586
604
if organization .IsErrTeamNotExist (err ) {
@@ -606,8 +624,11 @@ func CreateBranchProtection(ctx *context.APIContext) {
606
624
RuleName : ruleName ,
607
625
CanPush : form .EnablePush ,
608
626
EnableWhitelist : form .EnablePush && form .EnablePushWhitelist ,
609
- EnableMergeWhitelist : form .EnableMergeWhitelist ,
610
627
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 ,
611
632
EnableStatusCheck : form .EnableStatusCheck ,
612
633
StatusCheckContexts : form .StatusCheckContexts ,
613
634
EnableApprovalsWhitelist : form .EnableApprovalsWhitelist ,
@@ -624,6 +645,8 @@ func CreateBranchProtection(ctx *context.APIContext) {
624
645
err = git_model .UpdateProtectBranch (ctx , ctx .Repo .Repository , protectBranch , git_model.WhitelistOptions {
625
646
UserIDs : whitelistUsers ,
626
647
TeamIDs : whitelistTeams ,
648
+ ForcePushUserIDs : forcePushWhitelistUsers ,
649
+ ForcePushTeamIDs : forcePushWhitelistTeams ,
627
650
MergeUserIDs : mergeWhitelistUsers ,
628
651
MergeTeamIDs : mergeWhitelistTeams ,
629
652
ApprovalsUserIDs : approvalsWhitelistUsers ,
@@ -754,6 +777,27 @@ func EditBranchProtection(ctx *context.APIContext) {
754
777
}
755
778
}
756
779
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
+
757
801
if form .EnableMergeWhitelist != nil {
758
802
protectBranch .EnableMergeWhitelist = * form .EnableMergeWhitelist
759
803
}
@@ -802,7 +846,7 @@ func EditBranchProtection(ctx *context.APIContext) {
802
846
protectBranch .BlockOnOutdatedBranch = * form .BlockOnOutdatedBranch
803
847
}
804
848
805
- var whitelistUsers []int64
849
+ var whitelistUsers , forcePushWhitelistUsers , mergeWhitelistUsers , approvalsWhitelistUsers []int64
806
850
if form .PushWhitelistUsernames != nil {
807
851
whitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .PushWhitelistUsernames , false )
808
852
if err != nil {
@@ -816,7 +860,19 @@ func EditBranchProtection(ctx *context.APIContext) {
816
860
} else {
817
861
whitelistUsers = protectBranch .WhitelistUserIDs
818
862
}
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
+ }
820
876
if form .MergeWhitelistUsernames != nil {
821
877
mergeWhitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .MergeWhitelistUsernames , false )
822
878
if err != nil {
@@ -830,7 +886,6 @@ func EditBranchProtection(ctx *context.APIContext) {
830
886
} else {
831
887
mergeWhitelistUsers = protectBranch .MergeWhitelistUserIDs
832
888
}
833
- var approvalsWhitelistUsers []int64
834
889
if form .ApprovalsWhitelistUsernames != nil {
835
890
approvalsWhitelistUsers , err = user_model .GetUserIDsByNames (ctx , form .ApprovalsWhitelistUsernames , false )
836
891
if err != nil {
@@ -845,7 +900,7 @@ func EditBranchProtection(ctx *context.APIContext) {
845
900
approvalsWhitelistUsers = protectBranch .ApprovalsWhitelistUserIDs
846
901
}
847
902
848
- var whitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
903
+ var whitelistTeams , forcePushWhitelistTeams , mergeWhitelistTeams , approvalsWhitelistTeams []int64
849
904
if repo .Owner .IsOrganization () {
850
905
if form .PushWhitelistTeams != nil {
851
906
whitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .PushWhitelistTeams , false )
@@ -860,6 +915,19 @@ func EditBranchProtection(ctx *context.APIContext) {
860
915
} else {
861
916
whitelistTeams = protectBranch .WhitelistTeamIDs
862
917
}
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
+ }
863
931
if form .MergeWhitelistTeams != nil {
864
932
mergeWhitelistTeams , err = organization .GetTeamIDsByNames (ctx , repo .OwnerID , form .MergeWhitelistTeams , false )
865
933
if err != nil {
@@ -891,6 +959,8 @@ func EditBranchProtection(ctx *context.APIContext) {
891
959
err = git_model .UpdateProtectBranch (ctx , ctx .Repo .Repository , protectBranch , git_model.WhitelistOptions {
892
960
UserIDs : whitelistUsers ,
893
961
TeamIDs : whitelistTeams ,
962
+ ForcePushUserIDs : forcePushWhitelistUsers ,
963
+ ForcePushTeamIDs : forcePushWhitelistTeams ,
894
964
MergeUserIDs : mergeWhitelistUsers ,
895
965
MergeTeamIDs : mergeWhitelistTeams ,
896
966
ApprovalsUserIDs : approvalsWhitelistUsers ,
0 commit comments