Skip to content

Commit

Permalink
fix: Prevent loading of allowed actions if not configured (#2186)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
Danielku15 and kfcampbell authored Mar 8, 2024
1 parent a69c6f0 commit 480825c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
7 changes: 6 additions & 1 deletion github/resource_github_actions_organization_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,14 @@ func resourceGithubActionsOrganizationPermissionsRead(d *schema.ResourceData, me
// only load and fill allowed_actions_config if allowed_actions_config is also set
// in the TF code. (see #2105)
// on initial import there might not be any value in the state, then we have to import the data
// -> but we can only load an existing state if the current config is set to "selected" (see #2182)
allowedActions := d.Get("allowed_actions").(string)
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {

serverHasAllowedActionsConfig := actionsPermissions.GetAllowedActions() == "selected"
userWantsAllowedActionsConfig := (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == ""

if serverHasAllowedActionsConfig && userWantsAllowedActionsConfig {
actionsAllowed, _, err := client.Actions.GetActionsAllowed(ctx, d.Id())
if err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion github/resource_github_actions_repository_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,14 @@ func resourceGithubActionsRepositoryPermissionsRead(d *schema.ResourceData, meta
// only load and fill allowed_actions_config if allowed_actions_config is also set
// in the TF code. (see #2105)
// on initial import there might not be any value in the state, then we have to import the data
// -> but we can only load an existing state if the current config is set to "selected" (see #2182)
allowedActions := d.Get("allowed_actions").(string)
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {

serverHasAllowedActionsConfig := actionsPermissions.GetAllowedActions() == "selected" && actionsPermissions.GetEnabled()
userWantsAllowedActionsConfig := (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == ""

if serverHasAllowedActionsConfig && userWantsAllowedActionsConfig {
actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, owner, repoName)
if err != nil {
return err
Expand Down
57 changes: 57 additions & 0 deletions github/resource_github_actions_repository_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,61 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) {
})

})

// https://github.com/integrations/terraform-provider-github/issues/2182
t.Run("test load with disabled actions", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
locals {
actions_enabled = false
}
resource "github_repository" "test" {
name = "tf-acc-test-actions-permissions-%[1]s"
description = "Terraform acceptance tests %[1]s"
topics = ["terraform", "testing"]
}
resource "github_actions_repository_permissions" "test" {
repository = github_repository.test.name
enabled = local.actions_enabled
allowed_actions = local.actions_enabled ? "all" : null
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_actions_repository_permissions.test", "enabled", "false",
),
resource.TestCheckResourceAttr(
"github_actions_repository_permissions.test", "allowed_actions.#", "0",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})
}

0 comments on commit 480825c

Please sign in to comment.