Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent loading of allowed actions if not configured #2186

Merged
merged 2 commits into from
Mar 8, 2024
Merged
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
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)
})
})
}
Loading