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: Make allowed_actions_config optional #2114

Merged
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
26 changes: 18 additions & 8 deletions github/resource_github_actions_organization_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"errors"
"log"

"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -106,8 +107,7 @@ func resourceGithubActionsOrganizationAllowedObject(d *schema.ResourceData) (*gi

allowed.PatternsAllowed = patternsAllowed
} else {
return &github.ActionsAllowed{},
errors.New("the allowed_actions_config {} block must be specified if allowed_actions == 'selected'")
return nil, nil
}

return allowed, nil
Expand Down Expand Up @@ -162,11 +162,16 @@ func resourceGithubActionsOrganizationPermissionsCreateOrUpdate(d *schema.Resour
if err != nil {
return err
}
_, _, err = client.Actions.EditActionsAllowed(ctx,
orgName,
*actionsAllowedData)
if err != nil {
return err
if actionsAllowedData != nil {
log.Printf("[DEBUG] Allowed actions config is set")
_, _, err = client.Actions.EditActionsAllowed(ctx,
orgName,
*actionsAllowedData)
if err != nil {
return err
}
} else {
log.Printf("[DEBUG] Allowed actions config not set, skipping")
}
}

Expand Down Expand Up @@ -201,7 +206,12 @@ func resourceGithubActionsOrganizationPermissionsRead(d *schema.ResourceData, me
return err
}

if actionsPermissions.GetAllowedActions() == "selected" {
// 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
allowedActions := d.Get("allowed_actions").(string)
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {
actionsAllowed, _, err := client.Actions.GetActionsAllowed(ctx, d.Id())
if err != nil {
return err
Expand Down
43 changes: 43 additions & 0 deletions github/resource_github_actions_organization_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,49 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) {
})
Copy link
Member

Choose a reason for hiding this comment

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

The tests in this file are passing on the main branch but failing for me on this branch like so:

--- FAIL: TestAccGithubActionsOrganizationPermissions (51.85s)
    --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_basic_actions_organization_permissions (3.42s)
        --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_basic_actions_organization_permissions/with_an_organization_account (3.42s)
    --- FAIL: TestAccGithubActionsOrganizationPermissions/imports_entire_set_of_github_action_organization_permissions_without_error (16.76s)
        --- FAIL: TestAccGithubActionsOrganizationPermissions/imports_entire_set_of_github_action_organization_permissions_without_error/with_an_organization_account (16.76s)
    --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_organization_allowed_actions (4.79s)
        --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_organization_allowed_actions/with_an_organization_account (4.79s)
    --- PASS: TestAccGithubActionsOrganizationPermissions/test_not_setting_of_organization_allowed_actions_without_error (3.05s)
        --- PASS: TestAccGithubActionsOrganizationPermissions/test_not_setting_of_organization_allowed_actions_without_error/with_an_organization_account (3.05s)
    --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_organization_enabled_repositories (23.82s)
        --- PASS: TestAccGithubActionsOrganizationPermissions/test_setting_of_organization_enabled_repositories/with_an_organization_account (23.82s)
FAIL

Can you reproduce this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kfcampbell Yes, I can reproduce the problem. Seems I had some env-vars wrongly set in my setup. Will prepare a fix.

})

t.Run("test not setting of organization allowed actions without error", func(t *testing.T) {

allowedActions := "selected"
enabledRepositories := "all"

config := fmt.Sprintf(`

resource "github_actions_organization_permissions" "test" {
allowed_actions = "%s"
enabled_repositories = "%s"
}
`, allowedActions, enabledRepositories)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_actions_organization_permissions.test", "allowed_actions", allowedActions,
),
resource.TestCheckResourceAttr(
"github_actions_organization_permissions.test", "enabled_repositories", enabledRepositories,
),
resource.TestCheckResourceAttr(
"github_actions_organization_permissions.test", "allowed_actions_config.#", "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 organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("test setting of organization enabled repositories", func(t *testing.T) {

allowedActions := "all"
Expand Down
28 changes: 18 additions & 10 deletions github/resource_github_actions_repository_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package github

import (
"context"
"errors"
"log"

"github.com/google/go-github/v57/github"
Expand Down Expand Up @@ -97,8 +96,7 @@ func resourceGithubActionsRepositoryAllowedObject(d *schema.ResourceData) (*gith

allowed.PatternsAllowed = patternsAllowed
} else {
return &github.ActionsAllowed{},
errors.New("the allowed_actions_config {} block must be specified if allowed_actions == 'selected'")
return nil, nil
}

return allowed, nil
Expand Down Expand Up @@ -141,12 +139,17 @@ func resourceGithubActionsRepositoryPermissionsCreateOrUpdate(d *schema.Resource
if err != nil {
return err
}
_, _, err = client.Repositories.EditActionsAllowed(ctx,
owner,
repoName,
*actionsAllowedData)
if err != nil {
return err
if actionsAllowedData != nil {
log.Printf("[DEBUG] Allowed actions config is set")
_, _, err = client.Repositories.EditActionsAllowed(ctx,
owner,
repoName,
*actionsAllowedData)
if err != nil {
return err
}
} else {
log.Printf("[DEBUG] Allowed actions config not set, skipping")
}
}

Expand All @@ -166,7 +169,12 @@ func resourceGithubActionsRepositoryPermissionsRead(d *schema.ResourceData, meta
return err
}

if actionsPermissions.GetAllowedActions() == "selected" {
// 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
allowedActions := d.Get("allowed_actions").(string)
allowedActionsConfig := d.Get("allowed_actions_config").([]interface{})
if (allowedActions == "selected" && len(allowedActionsConfig) > 0) || allowedActions == "" {
actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, owner, repoName)
if err != nil {
return err
Expand Down
56 changes: 56 additions & 0 deletions github/resource_github_actions_repository_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,62 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) {

})

t.Run("test not setting of repository allowed actions without error", func(t *testing.T) {

allowedActions := "selected"
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-topic-%[1]s"
description = "Terraform acceptance tests %[1]s"
topics = ["terraform", "testing"]
}

resource "github_actions_repository_permissions" "test" {
allowed_actions = "%s"
repository = github_repository.test.name
}
`, randomID, allowedActions)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_actions_repository_permissions.test", "allowed_actions", allowedActions,
),
// Even if we do not set the allowed_actions_config,
// it will be set to the organization level settings
resource.TestCheckResourceAttr(
"github_actions_repository_permissions.test", "allowed_actions_config.#", "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)
})

})

t.Run("test disabling actions on a repository", func(t *testing.T) {

actionsEnabled := false
Expand Down
Loading