Skip to content

Commit 6d97704

Browse files
authored
[bug]: Fix commit strategies when updating repos
2 parents c917477 + deb8c72 commit 6d97704

File tree

3 files changed

+127
-62
lines changed

3 files changed

+127
-62
lines changed

github/resource_github_repository.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func calculateSecurityAndAnalysis(d *schema.ResourceData) *github.SecurityAndAna
469469
}
470470

471471
func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
472-
return &github.Repository{
472+
repository := &github.Repository{
473473
Name: github.String(d.Get("name").(string)),
474474
Description: github.String(d.Get("description").(string)),
475475
Homepage: github.String(d.Get("homepage_url").(string)),
@@ -493,6 +493,26 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
493493
AllowUpdateBranch: github.Bool(d.Get("allow_update_branch").(bool)),
494494
SecurityAndAnalysis: calculateSecurityAndAnalysis(d),
495495
}
496+
497+
// only configure merge commit if we are in commit merge strategy
498+
allowMergeCommit, ok := d.Get("allow_merge_commit").(bool)
499+
if ok {
500+
if allowMergeCommit {
501+
repository.MergeCommitTitle = github.String(d.Get("merge_commit_title").(string))
502+
repository.MergeCommitMessage = github.String(d.Get("merge_commit_message").(string))
503+
}
504+
}
505+
506+
// only configure squash commit if we are in squash merge strategy
507+
allowSquashMerge, ok := d.Get("allow_squash_merge").(bool)
508+
if ok {
509+
if allowSquashMerge {
510+
repository.SquashMergeCommitTitle = github.String(d.Get("squash_merge_commit_title").(string))
511+
repository.SquashMergeCommitMessage = github.String(d.Get("squash_merge_commit_message").(string))
512+
}
513+
}
514+
515+
return repository
496516
}
497517

498518
func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) error {
@@ -524,24 +544,6 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
524544
}
525545
}
526546

527-
// only configure merge commit if we are in commit merge strategy
528-
allowMergeCommit, ok := d.Get("allow_merge_commit").(bool)
529-
if ok {
530-
if allowMergeCommit {
531-
repoReq.MergeCommitTitle = github.String(d.Get("merge_commit_title").(string))
532-
repoReq.MergeCommitMessage = github.String(d.Get("merge_commit_message").(string))
533-
}
534-
}
535-
536-
// only configure squash commit if we are in squash merge strategy
537-
allowSquashMerge, ok := d.Get("allow_squash_merge").(bool)
538-
if ok {
539-
if allowSquashMerge {
540-
repoReq.SquashMergeCommitTitle = github.String(d.Get("squash_merge_commit_title").(string))
541-
repoReq.SquashMergeCommitMessage = github.String(d.Get("squash_merge_commit_message").(string))
542-
}
543-
}
544-
545547
repoReq.Private = github.Bool(isPrivate)
546548

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

github/resource_github_repository_test.go

Lines changed: 102 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -771,36 +771,67 @@ func TestAccGithubRepositories(t *testing.T) {
771771

772772
})
773773

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)
774+
t.Run("create and modify merge commit strategy without error", func(t *testing.T) {
775+
mergeCommitTitle := "PR_TITLE"
776+
mergeCommitMessage := "BLANK"
777+
updatedMergeCommitTitle := "MERGE_MESSAGE"
778+
updatedMergeCommitMessage := "PR_TITLE"
779+
780+
configs := map[string]string{
781+
"before": fmt.Sprintf(`
782+
resource "github_repository" "test" {
783+
784+
name = "tf-acc-test-modify-co-str-%[1]s"
785+
allow_merge_commit = true
786+
merge_commit_title = "%s"
787+
merge_commit_message = "%s"
788+
}
789+
`, randomID, mergeCommitTitle, mergeCommitMessage),
790+
"after": fmt.Sprintf(`
791+
resource "github_repository" "test" {
792+
name = "tf-acc-test-modify-co-str-%[1]s"
793+
allow_merge_commit = true
794+
merge_commit_title = "%s"
795+
merge_commit_message = "%s"
796+
}
797+
`, randomID, updatedMergeCommitTitle, updatedMergeCommitMessage),
798+
}
784799

785-
check := resource.ComposeTestCheckFunc(
786-
resource.TestCheckResourceAttr(
787-
"github_repository.test", "merge_commit_title",
788-
"PR_TITLE",
800+
checks := map[string]resource.TestCheckFunc{
801+
"before": resource.ComposeTestCheckFunc(
802+
resource.TestCheckResourceAttr(
803+
"github_repository.test", "merge_commit_title",
804+
mergeCommitTitle,
805+
),
806+
resource.TestCheckResourceAttr(
807+
"github_repository.test", "merge_commit_message",
808+
mergeCommitMessage,
809+
),
789810
),
790-
resource.TestCheckResourceAttr(
791-
"github_repository.test", "merge_commit_message",
792-
"BLANK",
811+
"after": resource.ComposeTestCheckFunc(
812+
resource.TestCheckResourceAttr(
813+
"github_repository.test", "merge_commit_title",
814+
updatedMergeCommitTitle,
815+
),
816+
resource.TestCheckResourceAttr(
817+
"github_repository.test", "merge_commit_message",
818+
updatedMergeCommitMessage,
819+
),
793820
),
794-
)
821+
}
795822

796823
testCase := func(t *testing.T, mode string) {
797824
resource.Test(t, resource.TestCase{
798825
PreCheck: func() { skipUnlessMode(t, mode) },
799826
Providers: testAccProviders,
800827
Steps: []resource.TestStep{
801828
{
802-
Config: config,
803-
Check: check,
829+
Config: configs["before"],
830+
Check: checks["before"],
831+
},
832+
{
833+
Config: configs["after"],
834+
Check: checks["after"],
804835
},
805836
},
806837
})
@@ -819,35 +850,66 @@ func TestAccGithubRepositories(t *testing.T) {
819850
})
820851
})
821852

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)
853+
t.Run("create and modify squash merge commit strategy without error", func(t *testing.T) {
854+
squashMergeCommitTitle := "PR_TITLE"
855+
squashMergeCommitMessage := "PR_BODY"
856+
updatedSquashMergeCommitTitle := "COMMIT_OR_PR_TITLE"
857+
updatedSquashMergeCommitMessage := "COMMIT_MESSAGES"
858+
859+
configs := map[string]string{
860+
"before": fmt.Sprintf(`
861+
resource "github_repository" "test" {
862+
name = "tf-acc-test-modify-sq-str-%[1]s"
863+
allow_squash_merge = true
864+
squash_merge_commit_title = "%s"
865+
squash_merge_commit_message = "%s"
866+
}
867+
`, randomID, squashMergeCommitTitle, squashMergeCommitMessage),
868+
"after": fmt.Sprintf(`
869+
resource "github_repository" "test" {
870+
name = "tf-acc-test-modify-sq-str-%[1]s"
871+
allow_squash_merge = true
872+
squash_merge_commit_title = "%s"
873+
squash_merge_commit_message = "%s"
874+
}
875+
`, randomID, updatedSquashMergeCommitTitle, updatedSquashMergeCommitMessage),
876+
}
831877

832-
check := resource.ComposeTestCheckFunc(
833-
resource.TestCheckResourceAttr(
834-
"github_repository.test", "squash_merge_commit_title",
835-
"PR_TITLE",
878+
checks := map[string]resource.TestCheckFunc{
879+
"before": resource.ComposeTestCheckFunc(
880+
resource.TestCheckResourceAttr(
881+
"github_repository.test", "squash_merge_commit_title",
882+
squashMergeCommitTitle,
883+
),
884+
resource.TestCheckResourceAttr(
885+
"github_repository.test", "squash_merge_commit_message",
886+
squashMergeCommitMessage,
887+
),
836888
),
837-
resource.TestCheckResourceAttr(
838-
"github_repository.test", "squash_merge_commit_message",
839-
"BLANK",
889+
"after": resource.ComposeTestCheckFunc(
890+
resource.TestCheckResourceAttr(
891+
"github_repository.test", "squash_merge_commit_title",
892+
updatedSquashMergeCommitTitle,
893+
),
894+
resource.TestCheckResourceAttr(
895+
"github_repository.test", "squash_merge_commit_message",
896+
updatedSquashMergeCommitMessage,
897+
),
840898
),
841-
)
899+
}
842900

843901
testCase := func(t *testing.T, mode string) {
844902
resource.Test(t, resource.TestCase{
845903
PreCheck: func() { skipUnlessMode(t, mode) },
846904
Providers: testAccProviders,
847905
Steps: []resource.TestStep{
848906
{
849-
Config: config,
850-
Check: check,
907+
Config: configs["before"],
908+
Check: checks["before"],
909+
},
910+
{
911+
Config: configs["after"],
912+
Check: checks["after"],
851913
},
852914
},
853915
})
@@ -865,6 +927,7 @@ func TestAccGithubRepositories(t *testing.T) {
865927
testCase(t, organization)
866928
})
867929
})
930+
868931
t.Run("create a repository with go as primary_language", func(t *testing.T) {
869932
config := fmt.Sprintf(`
870933
resource "github_repository" "test" {

website/docs/r/repository.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ The following arguments are supported:
8484

8585
* `allow_auto_merge` - (Optional) Set to `true` to allow auto-merging pull requests on the repository.
8686

87-
* `squash_merge_commit_title` - (Optional) Can be `PR_TITLE` or `COMMIT_OR_PR_TITLE` for a default squash merge commit title.
87+
* `squash_merge_commit_title` - (Optional) Can be `PR_TITLE` or `COMMIT_OR_PR_TITLE` for a default squash merge commit title. Applicable only if `allow_squash_merge` is `true`.
8888

89-
* `squash_merge_commit_message` - (Optional) Can be `PR_BODY`, `COMMIT_MESSAGES`, or `BLANK` for a default squash merge commit message.
89+
* `squash_merge_commit_message` - (Optional) Can be `PR_BODY`, `COMMIT_MESSAGES`, or `BLANK` for a default squash merge commit message. Applicable only if `allow_squash_merge` is `true`.
9090

91-
* `merge_commit_title` - Can be `PR_TITLE` or `MERGE_MESSAGE` for a default merge commit title.
91+
* `merge_commit_title` - Can be `PR_TITLE` or `MERGE_MESSAGE` for a default merge commit title. Applicable only if `allow_merge_commit` is `true`.
9292

93-
* `merge_commit_message` - Can be `PR_BODY`, `PR_TITLE`, or `BLANK` for a default merge commit message.
93+
* `merge_commit_message` - Can be `PR_BODY`, `PR_TITLE`, or `BLANK` for a default merge commit message. Applicable only if `allow_merge_commit` is `true`.
9494

9595
* `delete_branch_on_merge` - (Optional) Automatically delete head branch after a pull request is merged. Defaults to `false`.
9696

0 commit comments

Comments
 (0)