Skip to content

Commit 4767af9

Browse files
authored
fix: Use correct type for custom property default value (#3928)
1 parent cacc10b commit 4767af9

2 files changed

Lines changed: 103 additions & 36 deletions

File tree

github/orgs_properties.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,22 @@ func (cp CustomProperty) DefaultValueString() (string, bool) {
6464
func (cp CustomProperty) DefaultValueStrings() ([]string, bool) {
6565
switch cp.ValueType {
6666
case PropertyValueTypeMultiSelect:
67-
s, ok := cp.DefaultValue.([]string)
68-
return s, ok
67+
switch v := cp.DefaultValue.(type) {
68+
case []string:
69+
return v, true
70+
case []any:
71+
vals := make([]string, len(v))
72+
for i, item := range v {
73+
s, ok := item.(string)
74+
if !ok {
75+
return nil, false
76+
}
77+
vals[i] = s
78+
}
79+
return vals, true
80+
default:
81+
return nil, false
82+
}
6983
default:
7084
return nil, false
7185
}

github/orgs_properties_test.go

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,51 @@ func TestOrganizationsService_GetAllCustomProperties(t *testing.T) {
2121
mux.HandleFunc("/orgs/o/properties/schema", func(w http.ResponseWriter, r *http.Request) {
2222
testMethod(t, r, "GET")
2323
fmt.Fprint(w, `[
24-
{
25-
"property_name": "name",
26-
"value_type": "single_select",
27-
"required": true,
28-
"default_value": "production",
29-
"description": "Prod or dev environment",
30-
"allowed_values":[
31-
"production",
32-
"development"
33-
],
34-
"values_editable_by": "org_actors"
35-
},
36-
{
37-
"property_name": "service",
38-
"value_type": "string"
39-
},
40-
{
41-
"property_name": "team",
42-
"value_type": "string",
43-
"description": "Team owning the repository"
44-
},
45-
{
46-
"property_name": "documentation",
47-
"value_type": "url",
48-
"required": true,
49-
"description": "Link to the documentation",
50-
"default_value": "https://example.com/docs"
51-
}
52-
]`)
24+
{
25+
"property_name": "name",
26+
"value_type": "single_select",
27+
"required": true,
28+
"default_value": "production",
29+
"description": "Prod or dev environment",
30+
"allowed_values":[
31+
"production",
32+
"development"
33+
],
34+
"values_editable_by": "org_actors"
35+
},
36+
{
37+
"property_name": "test",
38+
"value_type": "multi_select",
39+
"required": true,
40+
"default_value": [
41+
"foo",
42+
"baz"
43+
],
44+
"description": "Prod or dev environment",
45+
"allowed_values":[
46+
"foo",
47+
"bar",
48+
"baz"
49+
],
50+
"values_editable_by": "org_actors"
51+
},
52+
{
53+
"property_name": "service",
54+
"value_type": "string"
55+
},
56+
{
57+
"property_name": "team",
58+
"value_type": "string",
59+
"description": "Team owning the repository"
60+
},
61+
{
62+
"property_name": "documentation",
63+
"value_type": "url",
64+
"required": true,
65+
"description": "Link to the documentation",
66+
"default_value": "https://example.com/docs"
67+
}
68+
]`)
5369
})
5470

5571
ctx := t.Context()
@@ -68,6 +84,15 @@ func TestOrganizationsService_GetAllCustomProperties(t *testing.T) {
6884
AllowedValues: []string{"production", "development"},
6985
ValuesEditableBy: Ptr("org_actors"),
7086
},
87+
{
88+
PropertyName: Ptr("test"),
89+
ValueType: PropertyValueTypeMultiSelect,
90+
Required: Ptr(true),
91+
DefaultValue: []any{"foo", "baz"},
92+
Description: Ptr("Prod or dev environment"),
93+
AllowedValues: []string{"foo", "bar", "baz"},
94+
ValuesEditableBy: Ptr("org_actors"),
95+
},
7196
{
7297
PropertyName: Ptr("service"),
7398
ValueType: PropertyValueTypeString,
@@ -85,12 +110,13 @@ func TestOrganizationsService_GetAllCustomProperties(t *testing.T) {
85110
DefaultValue: "https://example.com/docs",
86111
},
87112
}
88-
if !cmp.Equal(properties, want) {
89-
t.Errorf("Organizations.GetAllCustomProperties returned %+v, want %+v", properties, want)
90-
}
91113

92114
const methodName = "GetAllCustomProperties"
93115

116+
if diff := cmp.Diff(want, properties); diff != "" {
117+
t.Errorf("Organizations.%v diff mismatch (-want +got):\n%v", methodName, diff)
118+
}
119+
94120
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
95121
got, resp, err := client.Organizations.GetAllCustomProperties(ctx, "o")
96122
if got != nil {
@@ -617,6 +643,15 @@ func TestCustomPropertyDefaultValueStrings(t *testing.T) {
617643
ok: false,
618644
want: []string{},
619645
},
646+
{
647+
testName: "invalid_slice",
648+
property: &CustomProperty{
649+
ValueType: PropertyValueTypeString,
650+
DefaultValue: []any{1, 2, 3},
651+
},
652+
ok: false,
653+
want: []string{},
654+
},
620655
{
621656
testName: "multi_select_invalid_value",
622657
property: &CustomProperty{
@@ -636,7 +671,16 @@ func TestCustomPropertyDefaultValueStrings(t *testing.T) {
636671
want: []string{},
637672
},
638673
{
639-
testName: "multi_select_single_value",
674+
testName: "multi_select_any_slice_single_value",
675+
property: &CustomProperty{
676+
ValueType: PropertyValueTypeMultiSelect,
677+
DefaultValue: []any{"a"},
678+
},
679+
ok: true,
680+
want: []string{"a"},
681+
},
682+
{
683+
testName: "multi_select_string_slice_single_value",
640684
property: &CustomProperty{
641685
ValueType: PropertyValueTypeMultiSelect,
642686
DefaultValue: []string{"a"},
@@ -645,7 +689,16 @@ func TestCustomPropertyDefaultValueStrings(t *testing.T) {
645689
want: []string{"a"},
646690
},
647691
{
648-
testName: "multi_select_multi_value",
692+
testName: "multi_select_any_slice_multi_value",
693+
property: &CustomProperty{
694+
ValueType: PropertyValueTypeMultiSelect,
695+
DefaultValue: []any{"a", "b"},
696+
},
697+
ok: true,
698+
want: []string{"a", "b"},
699+
},
700+
{
701+
testName: "multi_select_string_slice_multi_value",
649702
property: &CustomProperty{
650703
ValueType: PropertyValueTypeMultiSelect,
651704
DefaultValue: []string{"a", "b"},

0 commit comments

Comments
 (0)