diff --git a/github/github-accessors.go b/github/github-accessors.go index 01f36e3418d..4ac72f5319b 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -7102,14 +7102,6 @@ func (c *CustomPatternBackfillScan) GetPatternSlug() string { return *c.PatternSlug } -// GetDefaultValue returns the DefaultValue field if it's non-nil, zero value otherwise. -func (c *CustomProperty) GetDefaultValue() string { - if c == nil || c.DefaultValue == nil { - return "" - } - return *c.DefaultValue -} - // GetDescription returns the Description field if it's non-nil, zero value otherwise. func (c *CustomProperty) GetDescription() string { if c == nil || c.Description == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 0b10ee00c8a..c79b58d99e7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -9290,17 +9290,6 @@ func TestCustomPatternBackfillScan_GetPatternSlug(tt *testing.T) { c.GetPatternSlug() } -func TestCustomProperty_GetDefaultValue(tt *testing.T) { - tt.Parallel() - var zeroValue string - c := &CustomProperty{DefaultValue: &zeroValue} - c.GetDefaultValue() - c = &CustomProperty{} - c.GetDefaultValue() - c = nil - c.GetDefaultValue() -} - func TestCustomProperty_GetDescription(tt *testing.T) { tt.Parallel() var zeroValue string diff --git a/github/orgs_properties.go b/github/orgs_properties.go index 502713b8bac..d104390581b 100644 --- a/github/orgs_properties.go +++ b/github/orgs_properties.go @@ -10,15 +10,19 @@ import ( "encoding/json" "errors" "fmt" + "strconv" ) +// PropertyValueType represents the type of a custom property value. +type PropertyValueType string + // Valid values for CustomProperty.ValueType. const ( - PropertyValueTypeString = "string" - PropertyValueTypeSingleSelect = "single_select" - PropertyValueTypeMultiSelect = "multi_select" - PropertyValueTypeTrueFalse = "true_false" - PropertyValueTypeURL = "url" + PropertyValueTypeString PropertyValueType = "string" + PropertyValueTypeSingleSelect PropertyValueType = "single_select" + PropertyValueTypeMultiSelect PropertyValueType = "multi_select" + PropertyValueTypeTrueFalse PropertyValueType = "true_false" + PropertyValueTypeURL PropertyValueType = "url" ) // CustomProperty represents an organization custom property object. @@ -31,11 +35,11 @@ type CustomProperty struct { // SourceType is the source type of the property where it has been created. Can be one of: organization, enterprise. SourceType *string `json:"source_type,omitempty"` // The type of the value for the property. Can be one of: string, single_select, multi_select, true_false, url. - ValueType string `json:"value_type"` + ValueType PropertyValueType `json:"value_type"` // Whether the property is required. Required *bool `json:"required,omitempty"` // Default value of the property. - DefaultValue *string `json:"default_value,omitempty"` + DefaultValue any `json:"default_value,omitempty"` // Short description of the property. Description *string `json:"description,omitempty"` // An ordered list of the allowed values of the property. The property can have up to 200 @@ -45,6 +49,42 @@ type CustomProperty struct { ValuesEditableBy *string `json:"values_editable_by,omitempty"` } +// DefaultValueString returns the DefaultValue as a string if the ValueType is string or single_select or url. +func (cp CustomProperty) DefaultValueString() (string, bool) { + switch cp.ValueType { + case PropertyValueTypeString, PropertyValueTypeSingleSelect, PropertyValueTypeURL: + s, ok := cp.DefaultValue.(string) + return s, ok + default: + return "", false + } +} + +// DefaultValueStrings returns the DefaultValue as a slice of string if the ValueType is string or multi_select. +func (cp CustomProperty) DefaultValueStrings() ([]string, bool) { + switch cp.ValueType { + case PropertyValueTypeMultiSelect: + s, ok := cp.DefaultValue.([]string) + return s, ok + default: + return nil, false + } +} + +// DefaultValueBool returns the DefaultValue as a string if the ValueType is string or true_false. +func (cp CustomProperty) DefaultValueBool() (bool, bool) { + switch cp.ValueType { + case PropertyValueTypeTrueFalse: + if s, ok := cp.DefaultValue.(string); ok { + b, err := strconv.ParseBool(s) + return b, err == nil + } + return false, false + default: + return false, false + } +} + // RepoCustomPropertyValue represents a repository custom property value. type RepoCustomPropertyValue struct { RepositoryID int64 `json:"repository_id"`