Skip to content

Commit 18acd20

Browse files
committed
resource/gitlab_project: correctly handle push rules add and edit
This is a possible approach to fix #836. Even though, I am not sure if that is really that elegant of a solution ...
1 parent 0852693 commit 18acd20

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -915,75 +915,72 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
915915
func editOrAddPushRules(ctx context.Context, client *gitlab.Client, projectID string, d *schema.ResourceData) error {
916916
log.Printf("[DEBUG] Editing push rules for project %q", projectID)
917917

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+
if is404(err) || pushRules.ID == 0 {
920+
log.Printf("[DEBUG] Creating new push rules for project %q", projectID)
921+
addOptions := expandAddProjectPushRuleOptions(d)
922+
_, _, err = client.Projects.AddProjectPushRule(projectID, addOptions, gitlab.WithContext(ctx))
923+
if err != nil {
924+
return err
925+
}
923926

924-
var httpErr *gitlab.ErrorResponse
925-
if !errors.As(err, &httpErr) || httpErr.Response.StatusCode != http.StatusNotFound {
926-
return err
927+
return nil
927928
}
928929

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
930+
editOptions := expandEditProjectPushRuleOptions(d, pushRules)
931+
log.Printf("[DEBUG] Editing existing push rules for project %q", projectID)
932+
_, _, err = client.Projects.EditProjectPushRule(projectID, editOptions, gitlab.WithContext(ctx))
933+
if err == nil {
934+
return nil
938935
}
939936

940937
return nil
941938
}
942939

943-
func expandEditProjectPushRuleOptions(d *schema.ResourceData) *gitlab.EditProjectPushRuleOptions {
940+
func expandEditProjectPushRuleOptions(d *schema.ResourceData, currentPushRules *gitlab.ProjectPushRules) *gitlab.EditProjectPushRuleOptions {
944941
options := &gitlab.EditProjectPushRuleOptions{}
945942

946-
if d.HasChange("push_rules.0.author_email_regex") {
943+
if d.HasChange("push_rules.0.author_email_regex") || d.Get("push_rules.0.author_email_regex") != currentPushRules.AuthorEmailRegex {
947944
options.AuthorEmailRegex = gitlab.String(d.Get("push_rules.0.author_email_regex").(string))
948945
}
949946

950-
if d.HasChange("push_rules.0.branch_name_regex") {
947+
if d.HasChange("push_rules.0.branch_name_regex") || d.Get("push_rules.0.branch_name_regex") != currentPushRules.BranchNameRegex {
951948
options.BranchNameRegex = gitlab.String(d.Get("push_rules.0.branch_name_regex").(string))
952949
}
953950

954-
if d.HasChange("push_rules.0.commit_message_regex") {
951+
if d.HasChange("push_rules.0.commit_message_regex") || d.Get("push_rules.0.commit_message_regex") != currentPushRules.CommitMessageRegex {
955952
options.CommitMessageRegex = gitlab.String(d.Get("push_rules.0.commit_message_regex").(string))
956953
}
957954

958-
if d.HasChange("push_rules.0.commit_message_negative_regex") {
955+
if d.HasChange("push_rules.0.commit_message_negative_regex") || d.Get("push_rules.0.commit_message_negative_regex") != currentPushRules.CommitMessageNegativeRegex {
959956
options.CommitMessageNegativeRegex = gitlab.String(d.Get("push_rules.0.commit_message_negative_regex").(string))
960957
}
961958

962-
if d.HasChange("push_rules.0.file_name_regex") {
959+
if d.HasChange("push_rules.0.file_name_regex") || d.Get("push_rules.0.file_name_regex") != currentPushRules.FileNameRegex {
963960
options.FileNameRegex = gitlab.String(d.Get("push_rules.0.file_name_regex").(string))
964961
}
965962

966-
if d.HasChange("push_rules.0.commit_committer_check") {
963+
if d.HasChange("push_rules.0.commit_committer_check") || d.Get("push_rules.0.commit_committer_check") != currentPushRules.CommitCommitterCheck {
967964
options.CommitCommitterCheck = gitlab.Bool(d.Get("push_rules.0.commit_committer_check").(bool))
968965
}
969966

970-
if d.HasChange("push_rules.0.deny_delete_tag") {
967+
if d.HasChange("push_rules.0.deny_delete_tag") || d.Get("push_rules.0.deny_delete_tag") != currentPushRules.DenyDeleteTag {
971968
options.DenyDeleteTag = gitlab.Bool(d.Get("push_rules.0.deny_delete_tag").(bool))
972969
}
973970

974-
if d.HasChange("push_rules.0.member_check") {
971+
if d.HasChange("push_rules.0.member_check") || d.Get("push_rules.0.member_check") != currentPushRules.MemberCheck {
975972
options.MemberCheck = gitlab.Bool(d.Get("push_rules.0.member_check").(bool))
976973
}
977974

978-
if d.HasChange("push_rules.0.prevent_secrets") {
975+
if d.HasChange("push_rules.0.prevent_secrets") || d.Get("push_rules.0.prevent_secrets") != currentPushRules.PreventSecrets {
979976
options.PreventSecrets = gitlab.Bool(d.Get("push_rules.0.prevent_secrets").(bool))
980977
}
981978

982-
if d.HasChange("push_rules.0.reject_unsigned_commits") {
979+
if d.HasChange("push_rules.0.reject_unsigned_commits") || d.Get("push_rules.0.reject_unsigned_commits") != currentPushRules.RejectUnsignedCommits {
983980
options.RejectUnsignedCommits = gitlab.Bool(d.Get("push_rules.0.reject_unsigned_commits").(bool))
984981
}
985982

986-
if d.HasChange("push_rules.0.max_file_size") {
983+
if d.HasChange("push_rules.0.max_file_size") || d.Get("push_rules.0.max_file_size") != currentPushRules.MaxFileSize {
987984
options.MaxFileSize = gitlab.Int(d.Get("push_rules.0.max_file_size").(int))
988985
}
989986

gitlab/resource_gitlab_project_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,27 @@ func TestAccGitlabProject_archiveOnDestroy(t *testing.T) {
398398
})
399399
}
400400

401+
func TestAccGitlabProject_setSinglePushRuleToDefault(t *testing.T) {
402+
rInt := acctest.RandInt()
403+
404+
resource.Test(t, resource.TestCase{
405+
PreCheck: func() { testAccPreCheck(t) },
406+
Providers: testAccProviders,
407+
CheckDestroy: testAccCheckGitlabProjectDestroy,
408+
Steps: []resource.TestStep{
409+
{
410+
SkipFunc: isRunningInCE,
411+
Config: testAccGitlabProjectConfigPushRules(rInt, `
412+
member_check = false
413+
`),
414+
Check: testAccCheckGitlabProjectPushRules("gitlab_project.foo", &gitlab.ProjectPushRules{
415+
MemberCheck: false,
416+
}),
417+
},
418+
},
419+
})
420+
}
421+
401422
func TestAccGitlabProject_IssueMergeRequestTemplates(t *testing.T) {
402423
var project gitlab.Project
403424
rInt := acctest.RandInt()

0 commit comments

Comments
 (0)