Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions github/resource_github_repository_environment.go
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Could we use more descriptive variable names for this? The code is highly nested and it's really hard to parse what each single char variable is supposed to be.
I don't care how idiomatic for golang this is supposed to be, it's poorly readable code

Original file line number Diff line number Diff line change
Expand Up @@ -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

Comment on lines 130 to 134
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")
}
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions github/resource_github_repository_environment_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ 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{
"teams": {
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.",
},
},
Expand Down
68 changes: 68 additions & 0 deletions github/resource_github_repository_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
},
},
{
Expand All @@ -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)
Expand Down