From 9653ffc20a9e626b45191c26533e5fbe02ef50c1 Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Wed, 3 Jun 2026 12:38:06 +0100 Subject: [PATCH 1/2] fix: Correct repository environment diff function logic Signed-off-by: Steve Hipwell --- .../resource_github_repository_environment.go | 28 +++++--- ...github_repository_environment_migration.go | 4 +- ...urce_github_repository_environment_test.go | 68 +++++++++++++++++++ 3 files changed, 87 insertions(+), 13 deletions(-) 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) From ffd761684d5a4710cfdca1b86665e828b656a6b4 Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Thu, 4 Jun 2026 09:23:52 +0100 Subject: [PATCH 2/2] fixup! fix: Correct repository environment diff function logic --- .../resource_github_repository_environment.go | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/github/resource_github_repository_environment.go b/github/resource_github_repository_environment.go index aa016b71c9..c1b1ba700a 100644 --- a/github/resource_github_repository_environment.go +++ b/github/resource_github_repository_environment.go @@ -127,30 +127,46 @@ func resourceGithubRepositoryEnvironmentDiff(_ context.Context, d *schema.Resour return nil } - if v, ok := d.GetOk("reviewers"); ok { - if c, ok := v.([]any); ok && len(c) > 0 { - if o, ok := c[0].(map[string]any); ok { - count := 0 + reviewersVal, ok := d.GetOk("reviewers") + if !ok { + return nil + } - if t, ok := o["teams"]; ok { - if s, ok := t.(*schema.Set); ok { - count += s.Len() - } - } + reviewersCol, ok := reviewersVal.([]any) + if !ok || len(reviewersCol) == 0 || reviewersCol[0] == nil { + return nil + } - if u, ok := o["users"]; ok { - if s, ok := u.(*schema.Set); ok { - count += s.Len() - } - } + reviewers, ok := reviewersCol[0].(map[string]any) + if !ok { + return nil + } - if count > 6 { - return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment") - } - } + teamsVal, teamsOk := reviewers["teams"] + usersVal, usersOk := reviewers["users"] + + if !teamsOk && !usersOk { + return nil + } + + reviewersCount := 0 + + if teamsOk { + if teamsCol, ok := teamsVal.(*schema.Set); ok { + reviewersCount += teamsCol.Len() } } + if usersOk { + if usersCol, ok := usersVal.(*schema.Set); ok { + reviewersCount += usersCol.Len() + } + } + + if reviewersCount > 6 { + return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment") + } + return nil }