Skip to content

Commit 7769358

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 7769358

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

github/resource_github_repository.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
775775
d.SetId(repo.GetName())
776776
}
777777

778+
if v, ok := d.GetOk("custom_properties"); ok {
779+
customProps := v.(map[string]any)
780+
if err := setRepositoryCustomProperties(ctx, client, owner, repoName, customProps); err != nil {
781+
return diag.FromErr(err)
782+
}
783+
}
784+
778785
topics := repoReq.Topics
779786
if len(topics) > 0 {
780787
_, _, err := client.Repositories.ReplaceAllTopics(ctx, owner, repoName, topics)
@@ -924,8 +931,15 @@ func resourceGithubRepositoryRead(ctx context.Context, d *schema.ResourceData, m
924931
}
925932
}
926933

927-
if len(repo.CustomProperties) > 0 {
928-
if err := d.Set("custom_properties", repo.CustomProperties); err != nil {
934+
customPropertyValues, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, owner, repoName)
935+
if err == nil {
936+
customProps := make(map[string]string, len(customPropertyValues))
937+
for _, v := range customPropertyValues {
938+
if strVal, ok := v.Value.(string); ok {
939+
customProps[v.PropertyName] = strVal
940+
}
941+
}
942+
if err := d.Set("custom_properties", customProps); err != nil {
929943
return diag.Errorf("error setting custom_properties: %s", err.Error())
930944
}
931945
}
@@ -978,6 +992,30 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
978992
}
979993
d.SetId(repo.GetName()) // It's possible that `repo.GetName()` is different from `repoName` if the repository is renamed
980994

995+
if d.HasChange("custom_properties") {
996+
newProps := d.Get("custom_properties").(map[string]interface{})
997+
if len(newProps) > 0 {
998+
if err := setRepositoryCustomProperties(ctx, client, owner, repoName, newProps); err != nil {
999+
return diag.FromErr(err)
1000+
}
1001+
} else {
1002+
// custom_properties was removed from config — clear any previously set values
1003+
oldRaw, _ := d.GetChange("custom_properties")
1004+
if oldProps, ok := oldRaw.(map[string]interface{}); ok && len(oldProps) > 0 {
1005+
clearValues := make([]*github.CustomPropertyValue, 0, len(oldProps))
1006+
for k := range oldProps {
1007+
clearValues = append(clearValues, &github.CustomPropertyValue{
1008+
PropertyName: k,
1009+
Value: nil,
1010+
})
1011+
}
1012+
if _, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, clearValues); err != nil {
1013+
return diag.FromErr(err)
1014+
}
1015+
}
1016+
}
1017+
}
1018+
9811019
if d.HasChange("pages") && !d.IsNewResource() {
9821020
opts := expandPagesUpdate(d.Get("pages").([]any))
9831021
if opts != nil {
@@ -1138,6 +1176,18 @@ func expandPagesUpdate(input []any) *github.PagesUpdate {
11381176
return update
11391177
}
11401178

1179+
func setRepositoryCustomProperties(ctx context.Context, client *github.Client, owner, repoName string, props map[string]any) error {
1180+
customPropertyValues := make([]*github.CustomPropertyValue, 0, len(props))
1181+
for k, v := range props {
1182+
customPropertyValues = append(customPropertyValues, &github.CustomPropertyValue{
1183+
PropertyName: k,
1184+
Value: v,
1185+
})
1186+
}
1187+
_, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, customPropertyValues)
1188+
return err
1189+
}
1190+
11411191
func flattenPages(pages *github.Pages) []any {
11421192
if pages == nil {
11431193
return []any{}

0 commit comments

Comments
 (0)