Skip to content

Commit

Permalink
[Bug]: Repository name updates break dependent repository secrets (#1754
Browse files Browse the repository at this point in the history
)

* fix rename issue & add tests

* fix rename issue & add tests

* fix rename issue & add tests

* remove main.tf

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
KenSpur and kfcampbell authored Jun 28, 2023
1 parent c7a1a70 commit 9e09aee
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 3 deletions.
2 changes: 1 addition & 1 deletion github/resource_github_actions_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func resourceGithubActionsSecret() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsSecretCreateOrUpdate,
Read: resourceGithubActionsSecretRead,
Update: resourceGithubActionsSecretCreateOrUpdate,
Delete: resourceGithubActionsSecretDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubActionsSecretImport,
Expand All @@ -27,6 +26,7 @@ func resourceGithubActionsSecret() *schema.Resource {
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the repository.",
},
"secret_name": {
Expand Down
98 changes: 98 additions & 0 deletions github/resource_github_actions_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,104 @@ func TestAccGithubActionsSecret(t *testing.T) {
})
})

t.Run("creates and updates repository name without error", func(t *testing.T) {
repoName := fmt.Sprintf("tf-acc-test-%s", randomID)
updatedRepoName := fmt.Sprintf("tf-acc-test-%s-updated", randomID)
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}
resource "github_actions_secret" "plaintext_secret" {
repository = github_repository.test.name
secret_name = "test_plaintext_secret"
plaintext_value = "%s"
}
resource "github_actions_secret" "encrypted_secret" {
repository = github_repository.test.name
secret_name = "test_encrypted_secret"
encrypted_value = "%s"
}
`, repoName, secretValue, secretValue)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_actions_secret.plaintext_secret", "repository",
repoName,
),
resource.TestCheckResourceAttr(
"github_actions_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_actions_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_actions_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_actions_secret.plaintext_secret", "updated_at",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_actions_secret.plaintext_secret", "repository",
updatedRepoName,
),
resource.TestCheckResourceAttr(
"github_actions_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_actions_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_actions_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_actions_secret.plaintext_secret", "updated_at",
),
),
}

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: checks["before"],
},
{
Config: strings.Replace(config,
repoName,
updatedRepoName, 2),
Check: checks["after"],
},
},
})
}

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("deletes secrets without error", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_codespaces_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func resourceGithubCodespacesSecret() *schema.Resource {
return &schema.Resource{
Create: resourceGithubCodespacesSecretCreateOrUpdate,
Read: resourceGithubCodespacesSecretRead,
Update: resourceGithubCodespacesSecretCreateOrUpdate,
Delete: resourceGithubCodespacesSecretDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubCodespacesSecretImport,
Expand All @@ -26,6 +25,7 @@ func resourceGithubCodespacesSecret() *schema.Resource {
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the repository.",
},
"secret_name": {
Expand Down
96 changes: 96 additions & 0 deletions github/resource_github_codespaces_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,102 @@ func TestAccGithubCodespacesSecret(t *testing.T) {
})
})

t.Run("creates and updates repository name without error", func(t *testing.T) {
repoName := fmt.Sprintf("tf-acc-test-%s", randomID)
updatedRepoName := fmt.Sprintf("tf-acc-test-%s-updated", randomID)
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}
resource "github_codespaces_secret" "plaintext_secret" {
repository = github_repository.test.name
secret_name = "test_plaintext_secret"
plaintext_value = "%s"
}
resource "github_codespaces_secret" "encrypted_secret" {
repository = github_repository.test.name
secret_name = "test_encrypted_secret"
encrypted_value = "%s"
}
`, repoName, secretValue, secretValue)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_codespaces_secret.plaintext_secret", "repository",
repoName,
),
resource.TestCheckResourceAttr(
"github_codespaces_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_codespaces_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_codespaces_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_codespaces_secret.plaintext_secret", "updated_at",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_codespaces_secret.plaintext_secret", "repository",
updatedRepoName,
),
resource.TestCheckResourceAttr(
"github_codespaces_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_codespaces_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_codespaces_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_codespaces_secret.plaintext_secret", "updated_at",
),
),
}

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: checks["before"],
},
{
Config: strings.Replace(config,
repoName,
updatedRepoName, 2),
Check: checks["after"],
},
},
})
}

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("deletes secrets without error", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_dependabot_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func resourceGithubDependabotSecret() *schema.Resource {
return &schema.Resource{
Create: resourceGithubDependabotSecretCreateOrUpdate,
Read: resourceGithubDependabotSecretRead,
Update: resourceGithubDependabotSecretCreateOrUpdate,
Delete: resourceGithubDependabotSecretDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubDependabotSecretImport,
Expand All @@ -27,6 +26,7 @@ func resourceGithubDependabotSecret() *schema.Resource {
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the repository.",
},
"secret_name": {
Expand Down
98 changes: 98 additions & 0 deletions github/resource_github_dependabot_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,104 @@ func TestAccGithubDependabotSecret(t *testing.T) {
})
})

t.Run("creates and updates repository name without error", func(t *testing.T) {
repoName := fmt.Sprintf("tf-acc-test-%s", randomID)
updatedRepoName := fmt.Sprintf("tf-acc-test-%s-updated", randomID)
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}
resource "github_dependabot_secret" "plaintext_secret" {
repository = github_repository.test.name
secret_name = "test_plaintext_secret"
plaintext_value = "%s"
}
resource "github_dependabot_secret" "encrypted_secret" {
repository = github_repository.test.name
secret_name = "test_encrypted_secret"
encrypted_value = "%s"
}
`, repoName, secretValue, secretValue)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_dependabot_secret.plaintext_secret", "repository",
repoName,
),
resource.TestCheckResourceAttr(
"github_dependabot_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_dependabot_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_dependabot_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_dependabot_secret.plaintext_secret", "updated_at",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_dependabot_secret.plaintext_secret", "repository",
updatedRepoName,
),
resource.TestCheckResourceAttr(
"github_dependabot_secret.plaintext_secret", "plaintext_value",
secretValue,
),
resource.TestCheckResourceAttr(
"github_dependabot_secret.encrypted_secret", "encrypted_value",
secretValue,
),
resource.TestCheckResourceAttrSet(
"github_dependabot_secret.plaintext_secret", "created_at",
),
resource.TestCheckResourceAttrSet(
"github_dependabot_secret.plaintext_secret", "updated_at",
),
),
}

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: checks["before"],
},
{
Config: strings.Replace(config,
repoName,
updatedRepoName, 2),
Check: checks["after"],
},
},
})
}

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("deletes secrets without error", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
Expand Down

0 comments on commit 9e09aee

Please sign in to comment.