Skip to content

Commit f1e87d1

Browse files
authored
Merge branch 'main' into refactor/migrate-resource-github-issue-label-to-tflog-and-context
2 parents 905fa62 + 4349001 commit f1e87d1

3 files changed

Lines changed: 103 additions & 13 deletions

github/resource_github_repository_environment.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,46 @@ func resourceGithubRepositoryEnvironmentDiff(_ context.Context, d *schema.Resour
127127
return nil
128128
}
129129

130-
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-
}
130+
reviewersVal, ok := d.GetOk("reviewers")
131+
if !ok {
132+
return nil
133+
}
134+
135+
reviewersCol, ok := reviewersVal.([]any)
136+
if !ok || len(reviewersCol) == 0 || reviewersCol[0] == nil {
137+
return nil
138+
}
139+
140+
reviewers, ok := reviewersCol[0].(map[string]any)
141+
if !ok {
142+
return nil
143+
}
144+
145+
teamsVal, teamsOk := reviewers["teams"]
146+
usersVal, usersOk := reviewers["users"]
147+
148+
if !teamsOk && !usersOk {
149+
return nil
150+
}
136151

137-
if t, ok := o.(map[string]any)["users"]; ok {
138-
count += t.(*schema.Set).Len()
152+
reviewersCount := 0
153+
154+
if teamsOk {
155+
if teamsCol, ok := teamsVal.(*schema.Set); ok {
156+
reviewersCount += teamsCol.Len()
139157
}
158+
}
140159

141-
if count > 6 {
142-
return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment")
160+
if usersOk {
161+
if usersCol, ok := usersVal.(*schema.Set); ok {
162+
reviewersCount += usersCol.Len()
143163
}
144164
}
145165

166+
if reviewersCount > 6 {
167+
return fmt.Errorf("a maximum of 6 reviewers (users and teams combined) can be set for an environment")
168+
}
169+
146170
return nil
147171
}
148172

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)