Skip to content

Commit d21951d

Browse files
committed
fixup! fix: Refactor repository custom property
1 parent c327081 commit d21951d

2 files changed

Lines changed: 256 additions & 204 deletions

File tree

github/resource_github_repository_custom_property.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package github
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"strings"
78

89
"github.com/google/go-github/v88/github"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1013
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1114
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1215
)
@@ -21,7 +24,7 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
2124
StateContext: resourceGithubRepositoryCustomPropertyImport,
2225
},
2326

24-
CustomizeDiff: diffRepository,
27+
CustomizeDiff: customdiff.All(diffRepository, resourceGithubRepositoryCustomPropertyDiff),
2528

2629
SchemaVersion: 1,
2730
StateUpgraders: []schema.StateUpgrader{
@@ -72,6 +75,30 @@ func resourceGithubRepositoryCustomProperty() *schema.Resource {
7275
}
7376
}
7477

78+
func resourceGithubRepositoryCustomPropertyDiff(ctx context.Context, d *schema.ResourceDiff, _ any) error {
79+
tflog.Debug(ctx, "Diffing GitHub repository custom property")
80+
81+
propertyTypeVal, _ := d.Get("property_type").(string)
82+
propertyType := github.PropertyValueType(propertyTypeVal)
83+
propertyValueVal, _ := d.Get("property_value").(*schema.Set)
84+
propertyValue := expandStringList(propertyValueVal.List())
85+
propertyCount := len(propertyValue)
86+
87+
// The propertyValue can either be a list of strings or a string
88+
switch propertyType {
89+
case github.PropertyValueTypeMultiSelect:
90+
if propertyCount < 1 {
91+
return fmt.Errorf("custom property type %v requires at least one value", propertyType)
92+
}
93+
default:
94+
if propertyCount != 1 {
95+
return fmt.Errorf("custom property type %v requires exactly one value", propertyType)
96+
}
97+
}
98+
99+
return nil
100+
}
101+
75102
func resourceGithubRepositoryCustomPropertyCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
76103
meta, _ := m.(*Owner)
77104
client := meta.v3client
@@ -90,12 +117,10 @@ func resourceGithubRepositoryCustomPropertyCreate(ctx context.Context, d *schema
90117

91118
// The propertyValue can either be a list of strings or a string
92119
switch propertyType {
93-
case github.PropertyValueTypeString, github.PropertyValueTypeSingleSelect, github.PropertyValueTypeURL, github.PropertyValueTypeTrueFalse:
94-
customProperty.Value = propertyValue[0]
95120
case github.PropertyValueTypeMultiSelect:
96121
customProperty.Value = propertyValue
97122
default:
98-
return diag.Errorf("custom property type is not valid: %v", propertyType)
123+
customProperty.Value = propertyValue[0]
99124
}
100125

101126
_, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, []*github.CustomPropertyValue{&customProperty})
@@ -188,12 +213,10 @@ func resourceGithubRepositoryCustomPropertyUpdate(ctx context.Context, d *schema
188213

189214
// The propertyValue can either be a list of strings or a string
190215
switch propertyType {
191-
case github.PropertyValueTypeString, github.PropertyValueTypeSingleSelect, github.PropertyValueTypeURL, github.PropertyValueTypeTrueFalse:
192-
customProperty.Value = propertyValue[0]
193216
case github.PropertyValueTypeMultiSelect:
194217
customProperty.Value = propertyValue
195218
default:
196-
return diag.Errorf("custom property type is not valid: %v", propertyType)
219+
customProperty.Value = propertyValue[0]
197220
}
198221

199222
_, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, []*github.CustomPropertyValue{&customProperty})

0 commit comments

Comments
 (0)