Skip to content

Commit 6e6e49e

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 b702889 commit 6e6e49e

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
@@ -924,8 +924,15 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
924924
}
925925
}
926926

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

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

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

0 commit comments

Comments
 (0)