Skip to content

Commit 288cd77

Browse files
committed
fix: correct custom_properties lifecycle in github_repository
- include custom_properties in create request body to satisfy org-required property enforcement at creation time - read custom_properties via dedicated GetAllCustomPropertyValues endpoint instead of repo.CustomProperties (which GET /repos never populates) - handle partial and full removal in update via CreateOrUpdateCustomProperties with null values; removing the block entirely leaves existing values intact (Optional+Computed behavior)
1 parent 5968b5e commit 288cd77

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

github/resource_github_repository.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,15 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
925925
}
926926
}
927927

928-
if len(repo.CustomProperties) > 0 {
929-
if err := d.Set("custom_properties", repo.CustomProperties); err != nil {
928+
customPropertyValues, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, owner, repoName)
929+
if err == nil {
930+
customProps := make(map[string]string, len(customPropertyValues))
931+
for _, v := range customPropertyValues {
932+
if strVal, ok := v.Value.(string); ok {
933+
customProps[v.PropertyName] = strVal
934+
}
935+
}
936+
if err := d.Set("custom_properties", customProps); err != nil {
930937
return diag.Errorf("error setting custom_properties: %s", err.Error())
931938
}
932939
}
@@ -979,6 +986,30 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
979986
}
980987
d.SetId(repo.GetName()) // It's possible that `repo.GetName()` is different from `repoName` if the repository is renamed
981988

989+
if d.HasChange("custom_properties") {
990+
newProps := d.Get("custom_properties").(map[string]interface{})
991+
if len(newProps) > 0 {
992+
if err := setRepositoryCustomProperties(ctx, client, owner, repoName, newProps); err != nil {
993+
return diag.FromErr(err)
994+
}
995+
} else {
996+
// custom_properties was removed from config — clear any previously set values
997+
oldRaw, _ := d.GetChange("custom_properties")
998+
if oldProps, ok := oldRaw.(map[string]interface{}); ok && len(oldProps) > 0 {
999+
clearValues := make([]*github.CustomPropertyValue, 0, len(oldProps))
1000+
for k := range oldProps {
1001+
clearValues = append(clearValues, &github.CustomPropertyValue{
1002+
PropertyName: k,
1003+
Value: nil,
1004+
})
1005+
}
1006+
if _, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, clearValues); err != nil {
1007+
return diag.FromErr(err)
1008+
}
1009+
}
1010+
}
1011+
}
1012+
9821013
if d.HasChange("pages") && !d.IsNewResource() {
9831014
opts := expandPagesUpdate(d.Get("pages").([]any))
9841015
if opts != nil {
@@ -1139,6 +1170,18 @@ func expandPagesUpdate(input []any) *github.PagesUpdate {
11391170
return update
11401171
}
11411172

1173+
func setRepositoryCustomProperties(ctx context.Context, client *github.Client, owner, repoName string, props map[string]any) error {
1174+
customPropertyValues := make([]*github.CustomPropertyValue, 0, len(props))
1175+
for k, v := range props {
1176+
customPropertyValues = append(customPropertyValues, &github.CustomPropertyValue{
1177+
PropertyName: k,
1178+
Value: v,
1179+
})
1180+
}
1181+
_, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, customPropertyValues)
1182+
return err
1183+
}
1184+
11421185
func flattenPages(pages *github.Pages) []any {
11431186
if pages == nil {
11441187
return []any{}

0 commit comments

Comments
 (0)