@@ -915,83 +915,88 @@ 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 || pushRules .ID == 0 {
921
+ if addOptions := expandAddProjectPushRuleOptions (d ); (gitlab.AddProjectPushRuleOptions {}) != addOptions {
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 ))
936
- if err != nil {
937
- return err
934
+ editOptions := expandEditProjectPushRuleOptions ( d , pushRules )
935
+ if (gitlab. EditProjectPushRuleOptions {}) != editOptions {
936
+ log .Printf ("[DEBUG] Editing existing push rules for project %q" , projectID )
937
+ _ , _ , err = client . Projects . EditProjectPushRule ( projectID , & editOptions , gitlab . WithContext ( ctx ) )
938
+ if err != nil {
939
+ return err
940
+ }
941
+ } else {
942
+ log . Printf ( "[DEBUG] Don't edit existing push rules for defaults for project %q" , projectID )
938
943
}
939
944
940
945
return nil
941
946
}
942
947
943
- func expandEditProjectPushRuleOptions (d * schema.ResourceData ) * gitlab.EditProjectPushRuleOptions {
944
- options := & gitlab.EditProjectPushRuleOptions {}
948
+ func expandEditProjectPushRuleOptions (d * schema.ResourceData , currentPushRules * gitlab. ProjectPushRules ) gitlab.EditProjectPushRuleOptions {
949
+ options := gitlab.EditProjectPushRuleOptions {}
945
950
946
- if d .HasChange ("push_rules.0.author_email_regex" ) {
951
+ if d .Get ("push_rules.0.author_email_regex" ) != currentPushRules . AuthorEmailRegex {
947
952
options .AuthorEmailRegex = gitlab .String (d .Get ("push_rules.0.author_email_regex" ).(string ))
948
953
}
949
954
950
- if d .HasChange ("push_rules.0.branch_name_regex" ) {
955
+ if d .Get ("push_rules.0.branch_name_regex" ) != currentPushRules . BranchNameRegex {
951
956
options .BranchNameRegex = gitlab .String (d .Get ("push_rules.0.branch_name_regex" ).(string ))
952
957
}
953
958
954
- if d .HasChange ("push_rules.0.commit_message_regex" ) {
959
+ if d .Get ("push_rules.0.commit_message_regex" ) != currentPushRules . CommitMessageRegex {
955
960
options .CommitMessageRegex = gitlab .String (d .Get ("push_rules.0.commit_message_regex" ).(string ))
956
961
}
957
962
958
- if d .HasChange ("push_rules.0.commit_message_negative_regex" ) {
963
+ if d .Get ("push_rules.0.commit_message_negative_regex" ) != currentPushRules . CommitMessageNegativeRegex {
959
964
options .CommitMessageNegativeRegex = gitlab .String (d .Get ("push_rules.0.commit_message_negative_regex" ).(string ))
960
965
}
961
966
962
- if d .HasChange ("push_rules.0.file_name_regex" ) {
967
+ if d .Get ("push_rules.0.file_name_regex" ) != currentPushRules . FileNameRegex {
963
968
options .FileNameRegex = gitlab .String (d .Get ("push_rules.0.file_name_regex" ).(string ))
964
969
}
965
970
966
- if d .HasChange ("push_rules.0.commit_committer_check" ) {
971
+ if d .Get ("push_rules.0.commit_committer_check" ) != currentPushRules . CommitCommitterCheck {
967
972
options .CommitCommitterCheck = gitlab .Bool (d .Get ("push_rules.0.commit_committer_check" ).(bool ))
968
973
}
969
974
970
- if d .HasChange ("push_rules.0.deny_delete_tag" ) {
975
+ if d .Get ("push_rules.0.deny_delete_tag" ) != currentPushRules . DenyDeleteTag {
971
976
options .DenyDeleteTag = gitlab .Bool (d .Get ("push_rules.0.deny_delete_tag" ).(bool ))
972
977
}
973
978
974
- if d .HasChange ("push_rules.0.member_check" ) {
979
+ if d .Get ("push_rules.0.member_check" ) != currentPushRules . MemberCheck {
975
980
options .MemberCheck = gitlab .Bool (d .Get ("push_rules.0.member_check" ).(bool ))
976
981
}
977
982
978
- if d .HasChange ("push_rules.0.prevent_secrets" ) {
983
+ if d .Get ("push_rules.0.prevent_secrets" ) != currentPushRules . PreventSecrets {
979
984
options .PreventSecrets = gitlab .Bool (d .Get ("push_rules.0.prevent_secrets" ).(bool ))
980
985
}
981
986
982
- if d .HasChange ("push_rules.0.reject_unsigned_commits" ) {
987
+ if d .Get ("push_rules.0.reject_unsigned_commits" ) != currentPushRules . RejectUnsignedCommits {
983
988
options .RejectUnsignedCommits = gitlab .Bool (d .Get ("push_rules.0.reject_unsigned_commits" ).(bool ))
984
989
}
985
990
986
- if d .HasChange ("push_rules.0.max_file_size" ) {
991
+ if d .Get ("push_rules.0.max_file_size" ) != currentPushRules . MaxFileSize {
987
992
options .MaxFileSize = gitlab .Int (d .Get ("push_rules.0.max_file_size" ).(int ))
988
993
}
989
994
990
995
return options
991
996
}
992
997
993
- func expandAddProjectPushRuleOptions (d * schema.ResourceData ) * gitlab.AddProjectPushRuleOptions {
994
- options := & gitlab.AddProjectPushRuleOptions {}
998
+ func expandAddProjectPushRuleOptions (d * schema.ResourceData ) gitlab.AddProjectPushRuleOptions {
999
+ options := gitlab.AddProjectPushRuleOptions {}
995
1000
996
1001
if v , ok := d .GetOk ("push_rules.0.author_email_regex" ); ok {
997
1002
options .AuthorEmailRegex = gitlab .String (v .(string ))
0 commit comments