Skip to content

Commit 16ff64c

Browse files
author
milan-dewilde_liantis
committed
fix: support in-place updates for repository custom property values
The github_repository_custom_property resource declared property_value as ForceNew with no Update function, causing every value change to plan as a destroy+create. The destroy step calls the upsert API with a nil value, which unsets the property on GitHub before the create step rewrites it. Wire an Update function that calls the same upsert as Create, and remove ForceNew from property_value. Identity fields (repository, property_name, property_type) remain ForceNew.
1 parent 7ddbf35 commit 16ff64c

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

github/resource_github_repository_custom_property.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
1313
return &schema.Resource{
1414
Create: resourceGithubRepositoryCustomPropertyCreate,
1515
Read: resourceGithubRepositoryCustomPropertyRead,
16+
Update: resourceGithubRepositoryCustomPropertyUpdate,
1617
Delete: resourceGithubRepositoryCustomPropertyDelete,
1718
Importer: &schema.ResourceImporter{
1819
StateContext: schema.ImportStatePassthroughContext,
@@ -46,7 +47,6 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
4647
Elem: &schema.Schema{
4748
Type: schema.TypeString,
4849
},
49-
ForceNew: true,
5050
},
5151
},
5252
}
@@ -86,6 +86,13 @@ func resourceGithubRepositoryCustomPropertyCreate(d *schema.ResourceData, meta a
8686
return resourceGithubRepositoryCustomPropertyRead(d, meta)
8787
}
8888

89+
func resourceGithubRepositoryCustomPropertyUpdate(d *schema.ResourceData, meta any) error {
90+
if err := resourceGithubRepositoryCustomPropertyCreate(d, meta); err != nil {
91+
return err
92+
}
93+
return resourceGithubRepositoryCustomPropertyRead(d, meta)
94+
}
95+
8996
func resourceGithubRepositoryCustomPropertyRead(d *schema.ResourceData, meta any) error {
9097
client := meta.(*Owner).v3client
9198
ctx := context.Background()

github/resource_github_repository_custom_property_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
910
)
1011

1112
func TestAccGithubRepositoryCustomProperty(t *testing.T) {
@@ -135,6 +136,79 @@ func TestAccGithubRepositoryCustomProperty(t *testing.T) {
135136
})
136137
})
137138

139+
t.Run("updates custom property value in place without replacement", func(t *testing.T) {
140+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
141+
repoName := fmt.Sprintf("%srepo-custom-prop-%s", testResourcePrefix, randomID)
142+
propertyName := fmt.Sprintf("tf-acc-test-property-%s", randomID)
143+
144+
configTemplate := `
145+
resource "github_organization_custom_properties" "test" {
146+
allowed_values = ["alpha", "beta"]
147+
description = "Test Description"
148+
property_name = "%s"
149+
value_type = "single_select"
150+
}
151+
resource "github_repository" "test" {
152+
name = "%s"
153+
auto_init = true
154+
}
155+
resource "github_repository_custom_property" "test" {
156+
repository = github_repository.test.name
157+
property_name = github_organization_custom_properties.test.property_name
158+
property_type = github_organization_custom_properties.test.value_type
159+
property_value = ["%s"]
160+
}
161+
`
162+
163+
configAlpha := fmt.Sprintf(configTemplate, propertyName, repoName, "alpha")
164+
configBeta := fmt.Sprintf(configTemplate, propertyName, repoName, "beta")
165+
166+
var firstID string
167+
168+
resource.Test(t, resource.TestCase{
169+
PreCheck: func() { skipUnlessHasOrgs(t) },
170+
ProviderFactories: providerFactories,
171+
Steps: []resource.TestStep{
172+
{
173+
Config: configAlpha,
174+
Check: resource.ComposeTestCheckFunc(
175+
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.#", "1"),
176+
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.0", "alpha"),
177+
func(s *terraform.State) error {
178+
rs, ok := s.RootModule().Resources["github_repository_custom_property.test"]
179+
if !ok {
180+
return fmt.Errorf("resource not found in state")
181+
}
182+
firstID = rs.Primary.ID
183+
return nil
184+
},
185+
),
186+
},
187+
{
188+
Config: configBeta,
189+
Check: resource.ComposeTestCheckFunc(
190+
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.#", "1"),
191+
resource.TestCheckResourceAttr("github_repository_custom_property.test", "property_value.0", "beta"),
192+
func(s *terraform.State) error {
193+
rs, ok := s.RootModule().Resources["github_repository_custom_property.test"]
194+
if !ok {
195+
return fmt.Errorf("resource not found in state")
196+
}
197+
if rs.Primary.ID != firstID {
198+
return fmt.Errorf("resource ID changed across update: %q -> %q (expected in-place update)", firstID, rs.Primary.ID)
199+
}
200+
return nil
201+
},
202+
),
203+
},
204+
{
205+
Config: configBeta,
206+
PlanOnly: true,
207+
},
208+
},
209+
})
210+
})
211+
138212
t.Run("creates custom property of type string without error", func(t *testing.T) {
139213
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
140214
repoName := fmt.Sprintf("%srepo-custom-prop-%s", testResourcePrefix, randomID)

0 commit comments

Comments
 (0)