Skip to content

Commit bb1290a

Browse files
committed
Refactor to use only CustomizeDiff for validation
`RequiredWith` doesn't work with `Default` values Signed-off-by: Timo Sand <[email protected]>
1 parent c59191a commit bb1290a

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

github/resource_github_repository.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -255,32 +255,28 @@ func resourceGithubRepository() *schema.Resource {
255255
Description: "Set to 'true' to allow private forking on the repository; this is only relevant if the repository is owned by an organization and is private or internal.",
256256
},
257257
"squash_merge_commit_title": {
258-
Type: schema.TypeString,
259-
Optional: true,
260-
Default: "COMMIT_OR_PR_TITLE",
261-
Description: "Can be 'PR_TITLE' or 'COMMIT_OR_PR_TITLE' for a default squash merge commit title.",
262-
RequiredWith: []string{"allow_squash_merge"},
258+
Type: schema.TypeString,
259+
Optional: true,
260+
Default: "COMMIT_OR_PR_TITLE",
261+
Description: "Can be 'PR_TITLE' or 'COMMIT_OR_PR_TITLE' for a default squash merge commit title.",
263262
},
264263
"squash_merge_commit_message": {
265-
Type: schema.TypeString,
266-
Optional: true,
267-
Default: "COMMIT_MESSAGES",
268-
Description: "Can be 'PR_BODY', 'COMMIT_MESSAGES', or 'BLANK' for a default squash merge commit message.",
269-
RequiredWith: []string{"allow_squash_merge", "squash_merge_commit_title"},
264+
Type: schema.TypeString,
265+
Optional: true,
266+
Default: "COMMIT_MESSAGES",
267+
Description: "Can be 'PR_BODY', 'COMMIT_MESSAGES', or 'BLANK' for a default squash merge commit message.",
270268
},
271269
"merge_commit_title": {
272-
Type: schema.TypeString,
273-
Optional: true,
274-
Default: "MERGE_MESSAGE",
275-
Description: "Can be 'PR_TITLE' or 'MERGE_MESSAGE' for a default merge commit title.",
276-
RequiredWith: []string{"allow_merge_commit"},
270+
Type: schema.TypeString,
271+
Optional: true,
272+
Default: "MERGE_MESSAGE",
273+
Description: "Can be 'PR_TITLE' or 'MERGE_MESSAGE' for a default merge commit title.",
277274
},
278275
"merge_commit_message": {
279-
Type: schema.TypeString,
280-
Optional: true,
281-
Default: "PR_TITLE",
282-
Description: "Can be 'PR_BODY', 'PR_TITLE', or 'BLANK' for a default merge commit message.",
283-
RequiredWith: []string{"allow_merge_commit", "merge_commit_title"},
276+
Type: schema.TypeString,
277+
Optional: true,
278+
Default: "PR_TITLE",
279+
Description: "Can be 'PR_BODY', 'PR_TITLE', or 'BLANK' for a default merge commit message.",
284280
},
285281
"delete_branch_on_merge": {
286282
Type: schema.TypeBool,
@@ -513,27 +509,31 @@ func valueChangedButNotEmpty(ctx context.Context, oldVal, newVal, meta any) bool
513509
return oldValStr != "" && oldValStr != newValStr
514510
}
515511

516-
func customDiffFunction(_ context.Context, diff *schema.ResourceDiff, v any) error {
512+
func customDiffFunction(ctx context.Context, diff *schema.ResourceDiff, v any) error {
517513
if diff.HasChange("name") {
518514
if err := diff.SetNewComputed("full_name"); err != nil {
519515
return err
520516
}
521517
}
522-
_, titleOk := diff.GetOk("squash_merge_commit_title")
523-
_, messageOk := diff.GetOk("squash_merge_commit_message")
524-
if messageOk && titleOk {
518+
519+
// We need to check the `allow_squash_merge` flag by checking if the `squash_merge_commit_title` and `squash_merge_commit_message` are set in the configuration.
520+
isSquashMergeCommitTitleSet := !diff.GetRawConfig().GetAttr("squash_merge_commit_title").IsNull()
521+
isSquashMergeCommitMessageSet := !diff.GetRawConfig().GetAttr("squash_merge_commit_message").IsNull()
522+
if isSquashMergeCommitMessageSet && isSquashMergeCommitTitleSet {
525523
if !diff.Get("allow_squash_merge").(bool) {
526524
return fmt.Errorf("allow_squash_merge is required when squash_merge_commit_title and squash_merge_commit_message is set")
527525
}
528526
}
529527

530-
_, titleOk = diff.GetOk("merge_commit_title")
531-
_, messageOk = diff.GetOk("merge_commit_message")
532-
if messageOk && titleOk {
528+
// We need to check the `allow_merge_commit` flag by checking if the `merge_commit_title` and `merge_commit_message` are set in the configuration.
529+
isMergeCommitTitleSet := !diff.GetRawConfig().GetAttr("merge_commit_title").IsNull()
530+
isMergeCommitMessageSet := !diff.GetRawConfig().GetAttr("merge_commit_message").IsNull()
531+
if isMergeCommitMessageSet && isMergeCommitTitleSet {
533532
if !diff.Get("allow_merge_commit").(bool) {
534533
return fmt.Errorf("allow_merge_commit is required when merge_commit_title and merge_commit_message is set")
535534
}
536535
}
536+
537537
return nil
538538
}
539539

github/resource_github_repository_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,10 @@ resource "github_repository" "test" {
753753
ExpectError: regexp.MustCompile("allow_squash_merge is required.*"),
754754
},
755755
{
756-
Config: fmt.Sprintf(config, testRepoName, testAccConf.testRepositoryVisibility, ""),
757-
ExpectError: regexp.MustCompile(`all of[\s\S]*?allow_squash_merge[\s\S]*?must be specified`),
756+
Config: fmt.Sprintf(config, testRepoName, testAccConf.testRepositoryVisibility, ""),
757+
ConfigStateChecks: []statecheck.StateCheck{
758+
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("allow_squash_merge"), knownvalue.Bool(true)),
759+
},
758760
},
759761
},
760762
},
@@ -783,8 +785,10 @@ resource "github_repository" "test" {
783785
ExpectError: regexp.MustCompile("allow_merge_commit is required.*"),
784786
},
785787
{
786-
Config: fmt.Sprintf(config, testRepoName, testAccConf.testRepositoryVisibility, ""),
787-
ExpectError: regexp.MustCompile(`all of[\s\S]*?allow_merge_commit[\s\S]*?must[\s\S]be[\s\S]specified`),
788+
Config: fmt.Sprintf(config, testRepoName, testAccConf.testRepositoryVisibility, ""),
789+
ConfigStateChecks: []statecheck.StateCheck{
790+
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("allow_merge_commit"), knownvalue.Bool(true)),
791+
},
788792
},
789793
},
790794
},

0 commit comments

Comments
 (0)