Skip to content

Commit 6956d82

Browse files
committed
fix: Correct repository environment diff function logic
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent f494cc6 commit 6956d82

3 files changed

Lines changed: 87 additions & 13 deletions

github/resource_github_repository_environment.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,26 @@ func resourceGithubRepositoryEnvironmentDiff(_ context.Context, d *schema.Resour
128128
}
129129

130130
if v, ok := d.GetOk("reviewers"); ok {
131-
count := 0
132-
o := v.([]any)[0]
133-
if t, ok := o.(map[string]any)["teams"]; ok {
134-
count += t.(*schema.Set).Len()
135-
}
131+
if c, ok := v.([]any); ok && len(c) > 0 {
132+
if o, ok := c[0].(map[string]any); ok {
133+
count := 0
136134

137-
if t, ok := o.(map[string]any)["users"]; ok {
138-
count += t.(*schema.Set).Len()
139-
}
135+
if t, ok := o["teams"]; ok {
136+
if s, ok := t.(*schema.Set); ok {
137+
count += s.Len()
138+
}
139+
}
140140

141-
if count > 6 {
142-
return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment")
141+
if u, ok := o["users"]; ok {
142+
if s, ok := u.(*schema.Set); ok {
143+
count += s.Len()
144+
}
145+
}
146+
147+
if count > 6 {
148+
return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment")
149+
}
150+
}
143151
}
144152
}
145153

github/resource_github_repository_environment_migration.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,20 @@ func resourceGithubRepositoryEnvironmentV0() *schema.Resource {
4545
"reviewers": {
4646
Type: schema.TypeList,
4747
Optional: true,
48-
MaxItems: 1,
48+
MaxItems: 6,
4949
Description: "The environment reviewers configuration.",
5050
Elem: &schema.Resource{
5151
Schema: map[string]*schema.Schema{
5252
"teams": {
5353
Type: schema.TypeSet,
5454
Elem: &schema.Schema{Type: schema.TypeInt},
5555
Optional: true,
56-
MaxItems: 6,
5756
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.",
5857
},
5958
"users": {
6059
Type: schema.TypeSet,
6160
Elem: &schema.Schema{Type: schema.TypeInt},
6261
Optional: true,
63-
MaxItems: 6,
6462
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.",
6563
},
6664
},

github/resource_github_repository_environment_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ resource "github_repository_environment" "test" {
153153
Config: config,
154154
ConfigStateChecks: []statecheck.StateCheck{
155155
statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
156+
statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(1)),
156157
},
157158
},
158159
{
@@ -165,6 +166,73 @@ resource "github_repository_environment" "test" {
165166
})
166167
})
167168

169+
t.Run("update_to_add_reviewers", func(t *testing.T) {
170+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
171+
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
172+
envName := "test"
173+
174+
preConfig := fmt.Sprintf(`
175+
resource "github_team" "test" {
176+
name = "%[1]s"
177+
description = "test"
178+
privacy = "closed"
179+
}
180+
181+
resource "github_repository" "test" {
182+
name = "%[1]s"
183+
visibility = "public"
184+
}
185+
186+
resource "github_team_repository" "test" {
187+
team_id = github_team.test.id
188+
repository = github_repository.test.name
189+
permission = "pull"
190+
}
191+
`, repoName)
192+
193+
config := fmt.Sprintf(`
194+
%s
195+
196+
resource "github_repository_environment" "test" {
197+
repository = github_repository.test.name
198+
environment = "%s"
199+
}
200+
`, preConfig, envName)
201+
202+
configUpdated := fmt.Sprintf(`
203+
%s
204+
205+
resource "github_repository_environment" "test" {
206+
repository = github_repository.test.name
207+
environment = "%s"
208+
209+
reviewers {
210+
teams = [github_team_repository.test.team_id]
211+
}
212+
}
213+
`, preConfig, envName)
214+
215+
resource.Test(t, resource.TestCase{
216+
PreCheck: func() { skipUnlessHasOrgs(t) },
217+
ProviderFactories: providerFactories,
218+
Steps: []resource.TestStep{
219+
{
220+
Config: config,
221+
ConfigStateChecks: []statecheck.StateCheck{
222+
statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
223+
statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(0)),
224+
},
225+
},
226+
{
227+
Config: configUpdated,
228+
ConfigStateChecks: []statecheck.StateCheck{
229+
statecheck.ExpectKnownValue("github_repository_environment.test", tfjsonpath.New("reviewers"), knownvalue.ListSizeExact(1)),
230+
},
231+
},
232+
},
233+
})
234+
})
235+
168236
t.Run("import", func(t *testing.T) {
169237
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
170238
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)

0 commit comments

Comments
 (0)