Skip to content
Open
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
9 changes: 8 additions & 1 deletion github/resource_github_repository_custom_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryCustomPropertyCreate,
Read: resourceGithubRepositoryCustomPropertyRead,
Update: resourceGithubRepositoryCustomPropertyUpdate,
Delete: resourceGithubRepositoryCustomPropertyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -46,7 +47,6 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ForceNew: true,
},
},
}
Expand Down Expand Up @@ -86,6 +86,13 @@ func resourceGithubRepositoryCustomPropertyCreate(d *schema.ResourceData, meta a
return resourceGithubRepositoryCustomPropertyRead(d, meta)
}

func resourceGithubRepositoryCustomPropertyUpdate(d *schema.ResourceData, meta any) error {
if err := resourceGithubRepositoryCustomPropertyCreate(d, meta); err != nil {
return err
}
return resourceGithubRepositoryCustomPropertyRead(d, meta)
}

func resourceGithubRepositoryCustomPropertyRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
Expand Down
74 changes: 74 additions & 0 deletions github/resource_github_repository_custom_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccGithubRepositoryCustomProperty(t *testing.T) {
Expand Down Expand Up @@ -135,6 +136,79 @@ func TestAccGithubRepositoryCustomProperty(t *testing.T) {
})
})

t.Run("updates custom property value in place without replacement", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
repoName := fmt.Sprintf("%srepo-custom-prop-%s", testResourcePrefix, randomID)
propertyName := fmt.Sprintf("tf-acc-test-property-%s", randomID)

configTemplate := `
resource "github_organization_custom_properties" "test" {
allowed_values = ["alpha", "beta"]
description = "Test Description"
property_name = "%s"
value_type = "single_select"
}
resource "github_repository" "test" {
name = "%s"
auto_init = true
}
resource "github_repository_custom_property" "test" {
repository = github_repository.test.name
property_name = github_organization_custom_properties.test.property_name
property_type = github_organization_custom_properties.test.value_type
property_value = ["%s"]
}
`

configAlpha := fmt.Sprintf(configTemplate, propertyName, repoName, "alpha")
configBeta := fmt.Sprintf(configTemplate, propertyName, repoName, "beta")

var firstID string

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: configAlpha,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.#", "1"),
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.0", "alpha"),
func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["github_repository_custom_property.test"]
if !ok {
return fmt.Errorf("resource not found in state")
}
firstID = rs.Primary.ID
return nil
},
),
},
{
Config: configBeta,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.#", "1"),
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.0", "beta"),
func(s *terraform.State) error {
rs, ok := s.RootModule().Resources["github_repository_custom_property.test"]
if !ok {
return fmt.Errorf("resource not found in state")
}
if rs.Primary.ID != firstID {
return fmt.Errorf("resource ID changed across update: %q -> %q (expected in-place update)", firstID, rs.Primary.ID)
}
return nil
},
),
},
{
Config: configBeta,
PlanOnly: true,
},
},
})
})

t.Run("creates custom property of type string without error", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
repoName := fmt.Sprintf("%srepo-custom-prop-%s", testResourcePrefix, randomID)
Expand Down