Skip to content

Commit 16b6e5e

Browse files
tdelaunekfcampbell
andauthored
Do not change squash_merge/merge_commit if it is not allowed in conf (#1834)
Co-authored-by: Keegan Campbell <[email protected]>
1 parent 77cfacb commit 16b6e5e

File tree

2 files changed

+136
-26
lines changed

2 files changed

+136
-26
lines changed

github/resource_github_repository.go

+40-26
Original file line numberDiff line numberDiff line change
@@ -465,32 +465,28 @@ func calculateSecurityAndAnalysis(d *schema.ResourceData) *github.SecurityAndAna
465465

466466
func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
467467
return &github.Repository{
468-
Name: github.String(d.Get("name").(string)),
469-
Description: github.String(d.Get("description").(string)),
470-
Homepage: github.String(d.Get("homepage_url").(string)),
471-
Visibility: github.String(calculateVisibility(d)),
472-
HasDownloads: github.Bool(d.Get("has_downloads").(bool)),
473-
HasIssues: github.Bool(d.Get("has_issues").(bool)),
474-
HasDiscussions: github.Bool(d.Get("has_discussions").(bool)),
475-
HasProjects: github.Bool(d.Get("has_projects").(bool)),
476-
HasWiki: github.Bool(d.Get("has_wiki").(bool)),
477-
IsTemplate: github.Bool(d.Get("is_template").(bool)),
478-
AllowMergeCommit: github.Bool(d.Get("allow_merge_commit").(bool)),
479-
AllowSquashMerge: github.Bool(d.Get("allow_squash_merge").(bool)),
480-
AllowRebaseMerge: github.Bool(d.Get("allow_rebase_merge").(bool)),
481-
AllowAutoMerge: github.Bool(d.Get("allow_auto_merge").(bool)),
482-
SquashMergeCommitTitle: github.String(d.Get("squash_merge_commit_title").(string)),
483-
SquashMergeCommitMessage: github.String(d.Get("squash_merge_commit_message").(string)),
484-
MergeCommitTitle: github.String(d.Get("merge_commit_title").(string)),
485-
MergeCommitMessage: github.String(d.Get("merge_commit_message").(string)),
486-
DeleteBranchOnMerge: github.Bool(d.Get("delete_branch_on_merge").(bool)),
487-
AutoInit: github.Bool(d.Get("auto_init").(bool)),
488-
LicenseTemplate: github.String(d.Get("license_template").(string)),
489-
GitignoreTemplate: github.String(d.Get("gitignore_template").(string)),
490-
Archived: github.Bool(d.Get("archived").(bool)),
491-
Topics: expandStringList(d.Get("topics").(*schema.Set).List()),
492-
AllowUpdateBranch: github.Bool(d.Get("allow_update_branch").(bool)),
493-
SecurityAndAnalysis: calculateSecurityAndAnalysis(d),
468+
Name: github.String(d.Get("name").(string)),
469+
Description: github.String(d.Get("description").(string)),
470+
Homepage: github.String(d.Get("homepage_url").(string)),
471+
Visibility: github.String(calculateVisibility(d)),
472+
HasDownloads: github.Bool(d.Get("has_downloads").(bool)),
473+
HasIssues: github.Bool(d.Get("has_issues").(bool)),
474+
HasDiscussions: github.Bool(d.Get("has_discussions").(bool)),
475+
HasProjects: github.Bool(d.Get("has_projects").(bool)),
476+
HasWiki: github.Bool(d.Get("has_wiki").(bool)),
477+
IsTemplate: github.Bool(d.Get("is_template").(bool)),
478+
AllowMergeCommit: github.Bool(d.Get("allow_merge_commit").(bool)),
479+
AllowSquashMerge: github.Bool(d.Get("allow_squash_merge").(bool)),
480+
AllowRebaseMerge: github.Bool(d.Get("allow_rebase_merge").(bool)),
481+
AllowAutoMerge: github.Bool(d.Get("allow_auto_merge").(bool)),
482+
DeleteBranchOnMerge: github.Bool(d.Get("delete_branch_on_merge").(bool)),
483+
AutoInit: github.Bool(d.Get("auto_init").(bool)),
484+
LicenseTemplate: github.String(d.Get("license_template").(string)),
485+
GitignoreTemplate: github.String(d.Get("gitignore_template").(string)),
486+
Archived: github.Bool(d.Get("archived").(bool)),
487+
Topics: expandStringList(d.Get("topics").(*schema.Set).List()),
488+
AllowUpdateBranch: github.Bool(d.Get("allow_update_branch").(bool)),
489+
SecurityAndAnalysis: calculateSecurityAndAnalysis(d),
494490
}
495491
}
496492

@@ -523,6 +519,24 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
523519
}
524520
}
525521

522+
// only configure merge commit if we are in commit merge strategy
523+
allowMergeCommit, ok := d.Get("allow_merge_commit").(bool)
524+
if ok {
525+
if allowMergeCommit {
526+
repoReq.MergeCommitTitle = github.String(d.Get("merge_commit_title").(string))
527+
repoReq.MergeCommitMessage = github.String(d.Get("merge_commit_message").(string))
528+
}
529+
}
530+
531+
// only configure squash commit if we are in squash merge strategy
532+
allowSquashMerge, ok := d.Get("allow_squash_merge").(bool)
533+
if ok {
534+
if allowSquashMerge {
535+
repoReq.SquashMergeCommitTitle = github.String(d.Get("squash_merge_commit_title").(string))
536+
repoReq.SquashMergeCommitMessage = github.String(d.Get("squash_merge_commit_message").(string))
537+
}
538+
}
539+
526540
repoReq.Private = github.Bool(isPrivate)
527541

528542
if template, ok := d.GetOk("template"); ok {

github/resource_github_repository_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,102 @@ func TestAccGithubRepositories(t *testing.T) {
770770
})
771771

772772
})
773+
774+
t.Run("modify merge commit strategy without error", func(t *testing.T) {
775+
config := fmt.Sprintf(`
776+
resource "github_repository" "test" {
777+
778+
name = "tf-acc-test-modify-co-str-%[1]s"
779+
allow_merge_commit = true
780+
merge_commit_title = "PR_TITLE"
781+
merge_commit_message = "BLANK"
782+
}
783+
`, randomID)
784+
785+
check := resource.ComposeTestCheckFunc(
786+
resource.TestCheckResourceAttr(
787+
"github_repository.test", "merge_commit_title",
788+
"PR_TITLE",
789+
),
790+
resource.TestCheckResourceAttr(
791+
"github_repository.test", "merge_commit_message",
792+
"BLANK",
793+
),
794+
)
795+
796+
testCase := func(t *testing.T, mode string) {
797+
resource.Test(t, resource.TestCase{
798+
PreCheck: func() { skipUnlessMode(t, mode) },
799+
Providers: testAccProviders,
800+
Steps: []resource.TestStep{
801+
{
802+
Config: config,
803+
Check: check,
804+
},
805+
},
806+
})
807+
}
808+
809+
t.Run("with an anonymous account", func(t *testing.T) {
810+
t.Skip("anonymous account not supported for this operation")
811+
})
812+
813+
t.Run("with an individual account", func(t *testing.T) {
814+
testCase(t, individual)
815+
})
816+
817+
t.Run("with an organization account", func(t *testing.T) {
818+
testCase(t, organization)
819+
})
820+
})
821+
822+
t.Run("modify squash merge strategy without error", func(t *testing.T) {
823+
config := fmt.Sprintf(`
824+
resource "github_repository" "test" {
825+
name = "tf-acc-test-modify-sq-str-%[1]s"
826+
allow_squash_merge = true
827+
squash_merge_commit_title = "PR_TITLE"
828+
squash_merge_commit_message = "BLANK"
829+
}
830+
`, randomID)
831+
832+
check := resource.ComposeTestCheckFunc(
833+
resource.TestCheckResourceAttr(
834+
"github_repository.test", "squash_merge_commit_title",
835+
"PR_TITLE",
836+
),
837+
resource.TestCheckResourceAttr(
838+
"github_repository.test", "squash_merge_commit_message",
839+
"BLANK",
840+
),
841+
)
842+
843+
testCase := func(t *testing.T, mode string) {
844+
resource.Test(t, resource.TestCase{
845+
PreCheck: func() { skipUnlessMode(t, mode) },
846+
Providers: testAccProviders,
847+
Steps: []resource.TestStep{
848+
{
849+
Config: config,
850+
Check: check,
851+
},
852+
},
853+
})
854+
}
855+
856+
t.Run("with an anonymous account", func(t *testing.T) {
857+
t.Skip("anonymous account not supported for this operation")
858+
})
859+
860+
t.Run("with an individual account", func(t *testing.T) {
861+
testCase(t, individual)
862+
})
863+
864+
t.Run("with an organization account", func(t *testing.T) {
865+
testCase(t, organization)
866+
})
867+
})
868+
773869
}
774870
func TestAccGithubRepositoryPages(t *testing.T) {
775871

0 commit comments

Comments
 (0)