diff --git a/github/resource_github_repository_environment.go b/github/resource_github_repository_environment.go index 4c8e7ec1a8..aa016b71c9 100644 --- a/github/resource_github_repository_environment.go +++ b/github/resource_github_repository_environment.go @@ -128,18 +128,26 @@ func resourceGithubRepositoryEnvironmentDiff(_ context.Context, d *schema.Resour } if v, ok := d.GetOk("reviewers"); ok { - count := 0 - o := v.([]any)[0] - if t, ok := o.(map[string]any)["teams"]; ok { - count += t.(*schema.Set).Len() - } + if c, ok := v.([]any); ok && len(c) > 0 { + if o, ok := c[0].(map[string]any); ok { + count := 0 - if t, ok := o.(map[string]any)["users"]; ok { - count += t.(*schema.Set).Len() - } + if t, ok := o["teams"]; ok { + if s, ok := t.(*schema.Set); ok { + count += s.Len() + } + } - if count > 6 { - return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment") + if u, ok := o["users"]; ok { + if s, ok := u.(*schema.Set); ok { + count += s.Len() + } + } + + if count > 6 { + return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment") + } + } } } diff --git a/github/resource_github_repository_environment_migration.go b/github/resource_github_repository_environment_migration.go index 45ac27dc05..03cea669ae 100644 --- a/github/resource_github_repository_environment_migration.go +++ b/github/resource_github_repository_environment_migration.go @@ -45,7 +45,7 @@ func resourceGithubRepositoryEnvironmentV0() *schema.Resource { "reviewers": { Type: schema.TypeList, Optional: true, - MaxItems: 1, + MaxItems: 6, Description: "The environment reviewers configuration.", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -53,14 +53,12 @@ func resourceGithubRepositoryEnvironmentV0() *schema.Resource { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeInt}, Optional: true, - MaxItems: 6, Description: "Up to 6 IDs for teams who may review jobs that reference the environment. Reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.", }, "users": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeInt}, Optional: true, - MaxItems: 6, Description: "Up to 6 IDs for users who may review jobs that reference the environment. Reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.", }, }, diff --git a/github/resource_github_repository_environment_test.go b/github/resource_github_repository_environment_test.go index 2166cc6c91..c6bc12a461 100644 --- a/github/resource_github_repository_environment_test.go +++ b/github/resource_github_repository_environment_test.go @@ -153,6 +153,7 @@ resource "github_repository_environment" "test" { Config: config, ConfigStateChecks: []statecheck.StateCheck{ statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()), + statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(1)), }, }, { @@ -165,6 +166,73 @@ resource "github_repository_environment" "test" { }) }) + t.Run("update_to_add_reviewers", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID) + envName := "test" + + preConfig := fmt.Sprintf(` +resource "github_team" "test" { + name = "%[1]s" + description = "test" + privacy = "closed" +} + +resource "github_repository" "test" { + name = "%[1]s" + visibility = "public" +} + +resource "github_team_repository" "test" { + team_id = github_team.test.id + repository = github_repository.test.name + permission = "pull" +} +`, repoName) + + config := fmt.Sprintf(` +%s + +resource "github_repository_environment" "test" { + repository = github_repository.test.name + environment = "%s" +} +`, preConfig, envName) + + configUpdated := fmt.Sprintf(` +%s + +resource "github_repository_environment" "test" { + repository = github_repository.test.name + environment = "%s" + + reviewers { + teams = [github_team_repository.test.team_id] + } +} +`, preConfig, envName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()), + statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(0)), + }, + }, + { + Config: configUpdated, + ConfigStateChecks: []statecheck.StateCheck{ + statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(1)), + }, + }, + }, + }) + }) + t.Run("import", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)