@@ -915,129 +915,142 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
915
915
func editOrAddPushRules (ctx context.Context , client * gitlab.Client , projectID string , d * schema.ResourceData ) error {
916
916
log .Printf ("[DEBUG] Editing push rules for project %q" , projectID )
917
917
918
- editOptions := expandEditProjectPushRuleOptions (d )
919
- _ , _ , err := client .Projects .EditProjectPushRule (projectID , editOptions , gitlab .WithContext (ctx ))
920
- if err == nil {
921
- return nil
922
- }
918
+ pushRules , _ , err := client .Projects .GetProjectPushRules (d .Id (), gitlab .WithContext (ctx ))
919
+ // NOTE: push rules id `0` indicates that there haven't been any push rules set.
920
+ if err != nil || is404 (err ) || pushRules .ID == 0 {
921
+ if addOptions , needToAdd := expandAddProjectPushRuleOptions (d ); needToAdd {
922
+ log .Printf ("[DEBUG] Creating new push rules for project %q" , projectID )
923
+ _ , _ , err = client .Projects .AddProjectPushRule (projectID , addOptions , gitlab .WithContext (ctx ))
924
+ if err != nil {
925
+ return err
926
+ }
927
+ } else {
928
+ log .Printf ("[DEBUG] Don't create new push rules for defaults for project %q" , projectID )
929
+ }
923
930
924
- var httpErr * gitlab.ErrorResponse
925
- if ! errors .As (err , & httpErr ) || httpErr .Response .StatusCode != http .StatusNotFound {
926
- return err
931
+ return nil
927
932
}
928
933
929
- // A 404 could mean that the push rules need to be re-created.
930
-
931
- log .Printf ("[DEBUG] Failed to edit push rules for project %q: %v" , projectID , err )
932
- log .Printf ("[DEBUG] Creating new push rules for project %q" , projectID )
933
-
934
- addOptions := expandAddProjectPushRuleOptions (d )
935
- _ , _ , err = client .Projects .AddProjectPushRule (projectID , addOptions , gitlab .WithContext (ctx ))
934
+ editOptions := expandEditProjectPushRuleOptions (d , pushRules )
935
+ log .Printf ("[DEBUG] Editing existing push rules for project %q" , projectID )
936
+ _ , _ , err = client .Projects .EditProjectPushRule (projectID , editOptions , gitlab .WithContext (ctx ))
936
937
if err != nil {
937
938
return err
938
939
}
939
940
940
941
return nil
941
942
}
942
943
943
- func expandEditProjectPushRuleOptions (d * schema.ResourceData ) * gitlab.EditProjectPushRuleOptions {
944
+ func expandEditProjectPushRuleOptions (d * schema.ResourceData , currentPushRules * gitlab. ProjectPushRules ) * gitlab.EditProjectPushRuleOptions {
944
945
options := & gitlab.EditProjectPushRuleOptions {}
945
946
946
- if d .HasChange ("push_rules.0.author_email_regex" ) {
947
+ if d .HasChange ("push_rules.0.author_email_regex" ) || d . Get ( "push_rules.0.author_email_regex" ) != currentPushRules . AuthorEmailRegex {
947
948
options .AuthorEmailRegex = gitlab .String (d .Get ("push_rules.0.author_email_regex" ).(string ))
948
949
}
949
950
950
- if d .HasChange ("push_rules.0.branch_name_regex" ) {
951
+ if d .HasChange ("push_rules.0.branch_name_regex" ) || d . Get ( "push_rules.0.branch_name_regex" ) != currentPushRules . BranchNameRegex {
951
952
options .BranchNameRegex = gitlab .String (d .Get ("push_rules.0.branch_name_regex" ).(string ))
952
953
}
953
954
954
- if d .HasChange ("push_rules.0.commit_message_regex" ) {
955
+ if d .HasChange ("push_rules.0.commit_message_regex" ) || d . Get ( "push_rules.0.commit_message_regex" ) != currentPushRules . CommitMessageRegex {
955
956
options .CommitMessageRegex = gitlab .String (d .Get ("push_rules.0.commit_message_regex" ).(string ))
956
957
}
957
958
958
- if d .HasChange ("push_rules.0.commit_message_negative_regex" ) {
959
+ if d .HasChange ("push_rules.0.commit_message_negative_regex" ) || d . Get ( "push_rules.0.commit_message_negative_regex" ) != currentPushRules . CommitMessageNegativeRegex {
959
960
options .CommitMessageNegativeRegex = gitlab .String (d .Get ("push_rules.0.commit_message_negative_regex" ).(string ))
960
961
}
961
962
962
- if d .HasChange ("push_rules.0.file_name_regex" ) {
963
+ if d .HasChange ("push_rules.0.file_name_regex" ) || d . Get ( "push_rules.0.file_name_regex" ) != currentPushRules . FileNameRegex {
963
964
options .FileNameRegex = gitlab .String (d .Get ("push_rules.0.file_name_regex" ).(string ))
964
965
}
965
966
966
- if d .HasChange ("push_rules.0.commit_committer_check" ) {
967
+ if d .HasChange ("push_rules.0.commit_committer_check" ) || d . Get ( "push_rules.0.commit_committer_check" ) != currentPushRules . CommitCommitterCheck {
967
968
options .CommitCommitterCheck = gitlab .Bool (d .Get ("push_rules.0.commit_committer_check" ).(bool ))
968
969
}
969
970
970
- if d .HasChange ("push_rules.0.deny_delete_tag" ) {
971
+ if d .HasChange ("push_rules.0.deny_delete_tag" ) || d . Get ( "push_rules.0.deny_delete_tag" ) != currentPushRules . DenyDeleteTag {
971
972
options .DenyDeleteTag = gitlab .Bool (d .Get ("push_rules.0.deny_delete_tag" ).(bool ))
972
973
}
973
974
974
- if d .HasChange ("push_rules.0.member_check" ) {
975
+ if d .HasChange ("push_rules.0.member_check" ) || d . Get ( "push_rules.0.member_check" ) != currentPushRules . MemberCheck {
975
976
options .MemberCheck = gitlab .Bool (d .Get ("push_rules.0.member_check" ).(bool ))
976
977
}
977
978
978
- if d .HasChange ("push_rules.0.prevent_secrets" ) {
979
+ if d .HasChange ("push_rules.0.prevent_secrets" ) || d . Get ( "push_rules.0.prevent_secrets" ) != currentPushRules . PreventSecrets {
979
980
options .PreventSecrets = gitlab .Bool (d .Get ("push_rules.0.prevent_secrets" ).(bool ))
980
981
}
981
982
982
- if d .HasChange ("push_rules.0.reject_unsigned_commits" ) {
983
+ if d .HasChange ("push_rules.0.reject_unsigned_commits" ) || d . Get ( "push_rules.0.reject_unsigned_commits" ) != currentPushRules . RejectUnsignedCommits {
983
984
options .RejectUnsignedCommits = gitlab .Bool (d .Get ("push_rules.0.reject_unsigned_commits" ).(bool ))
984
985
}
985
986
986
- if d .HasChange ("push_rules.0.max_file_size" ) {
987
+ if d .HasChange ("push_rules.0.max_file_size" ) || d . Get ( "push_rules.0.max_file_size" ) != currentPushRules . MaxFileSize {
987
988
options .MaxFileSize = gitlab .Int (d .Get ("push_rules.0.max_file_size" ).(int ))
988
989
}
989
990
990
991
return options
991
992
}
992
993
993
- func expandAddProjectPushRuleOptions (d * schema.ResourceData ) * gitlab.AddProjectPushRuleOptions {
994
+ func expandAddProjectPushRuleOptions (d * schema.ResourceData ) ( * gitlab.AddProjectPushRuleOptions , bool ) {
994
995
options := & gitlab.AddProjectPushRuleOptions {}
996
+ needToAdd := false
995
997
996
998
if v , ok := d .GetOk ("push_rules.0.author_email_regex" ); ok {
997
999
options .AuthorEmailRegex = gitlab .String (v .(string ))
1000
+ needToAdd = true
998
1001
}
999
1002
1000
1003
if v , ok := d .GetOk ("push_rules.0.branch_name_regex" ); ok {
1001
1004
options .BranchNameRegex = gitlab .String (v .(string ))
1005
+ needToAdd = true
1002
1006
}
1003
1007
1004
1008
if v , ok := d .GetOk ("push_rules.0.commit_message_regex" ); ok {
1005
1009
options .CommitMessageRegex = gitlab .String (v .(string ))
1010
+ needToAdd = true
1006
1011
}
1007
1012
1008
1013
if v , ok := d .GetOk ("push_rules.0.commit_message_negative_regex" ); ok {
1009
1014
options .CommitMessageNegativeRegex = gitlab .String (v .(string ))
1015
+ needToAdd = true
1010
1016
}
1011
1017
1012
1018
if v , ok := d .GetOk ("push_rules.0.file_name_regex" ); ok {
1013
1019
options .FileNameRegex = gitlab .String (v .(string ))
1020
+ needToAdd = true
1014
1021
}
1015
1022
1016
1023
if v , ok := d .GetOk ("push_rules.0.commit_committer_check" ); ok {
1017
1024
options .CommitCommitterCheck = gitlab .Bool (v .(bool ))
1025
+ needToAdd = true
1018
1026
}
1019
1027
1020
1028
if v , ok := d .GetOk ("push_rules.0.deny_delete_tag" ); ok {
1021
1029
options .DenyDeleteTag = gitlab .Bool (v .(bool ))
1030
+ needToAdd = true
1022
1031
}
1023
1032
1024
1033
if v , ok := d .GetOk ("push_rules.0.member_check" ); ok {
1025
1034
options .MemberCheck = gitlab .Bool (v .(bool ))
1035
+ needToAdd = true
1026
1036
}
1027
1037
1028
1038
if v , ok := d .GetOk ("push_rules.0.prevent_secrets" ); ok {
1029
1039
options .PreventSecrets = gitlab .Bool (v .(bool ))
1040
+ needToAdd = true
1030
1041
}
1031
1042
1032
1043
if v , ok := d .GetOk ("push_rules.0.reject_unsigned_commits" ); ok {
1033
1044
options .RejectUnsignedCommits = gitlab .Bool (v .(bool ))
1045
+ needToAdd = true
1034
1046
}
1035
1047
1036
1048
if v , ok := d .GetOk ("push_rules.0.max_file_size" ); ok {
1037
1049
options .MaxFileSize = gitlab .Int (v .(int ))
1050
+ needToAdd = true
1038
1051
}
1039
1052
1040
- return options
1053
+ return options , needToAdd
1041
1054
}
1042
1055
1043
1056
func flattenProjectPushRules (pushRules * gitlab.ProjectPushRules ) (values []map [string ]interface {}) {
0 commit comments