-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathresource_github_repository_custom_property.go
More file actions
164 lines (137 loc) · 4.96 KB
/
resource_github_repository_custom_property.go
File metadata and controls
164 lines (137 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package github
import (
"context"
"fmt"
"github.com/google/go-github/v85/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubRepositoryCustomProperty() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryCustomPropertyCreate,
Read: resourceGithubRepositoryCustomPropertyRead,
Delete: resourceGithubRepositoryCustomPropertyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository which the custom properties should be on.",
ForceNew: true,
},
"property_type": {
Type: schema.TypeString,
Required: true,
Description: "Type of the custom property",
ForceNew: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{string(github.PropertyValueTypeString), string(github.PropertyValueTypeSingleSelect), string(github.PropertyValueTypeMultiSelect), string(github.PropertyValueTypeTrueFalse), string(github.PropertyValueTypeURL)}, false)),
},
"property_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the custom property.",
ForceNew: true,
},
"property_value": {
Type: schema.TypeSet,
MinItems: 1,
Required: true,
Description: "Value of the custom property.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
ForceNew: true,
},
},
}
}
func resourceGithubRepositoryCustomPropertyCreate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
propertyName := d.Get("property_name").(string)
propertyType := github.PropertyValueType(d.Get("property_type").(string))
propertyValue := expandStringList(d.Get("property_value").(*schema.Set).List())
customProperty := github.CustomPropertyValue{
PropertyName: propertyName,
}
// The propertyValue can either be a list of strings or a string
switch propertyType {
case github.PropertyValueTypeString, github.PropertyValueTypeSingleSelect, github.PropertyValueTypeURL, github.PropertyValueTypeTrueFalse:
if len(propertyValue) == 0 {
return fmt.Errorf("property_value must contain at least one element for property_type %q", propertyType)
}
customProperty.Value = propertyValue[0]
case github.PropertyValueTypeMultiSelect:
customProperty.Value = propertyValue
default:
return fmt.Errorf("custom property type is not valid: %v", propertyType)
}
_, err := client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, []*github.CustomPropertyValue{&customProperty})
if err != nil {
return err
}
d.SetId(buildThreePartID(owner, repoName, propertyName))
return resourceGithubRepositoryCustomPropertyRead(d, meta)
}
func resourceGithubRepositoryCustomPropertyRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner, repoName, propertyName, err := parseThreePartID(d.Id(), "owner", "repoName", "propertyName")
if err != nil {
return err
}
wantedCustomPropertyValue, err := readRepositoryCustomPropertyValue(ctx, client, owner, repoName, propertyName)
if err != nil {
return err
}
if wantedCustomPropertyValue == nil {
d.SetId("")
return nil
}
d.SetId(buildThreePartID(owner, repoName, propertyName))
_ = d.Set("repository", repoName)
_ = d.Set("property_name", propertyName)
_ = d.Set("property_value", wantedCustomPropertyValue)
return nil
}
func resourceGithubRepositoryCustomPropertyDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner, repoName, propertyName, err := parseThreePartID(d.Id(), "owner", "repoName", "propertyName")
if err != nil {
return err
}
customProperty := github.CustomPropertyValue{
PropertyName: propertyName,
Value: nil,
}
_, err = client.Repositories.CreateOrUpdateCustomProperties(ctx, owner, repoName, []*github.CustomPropertyValue{&customProperty})
if err != nil {
return err
}
return nil
}
func readRepositoryCustomPropertyValue(ctx context.Context, client *github.Client, owner, repoName, propertyName string) ([]string, error) {
allCustomProperties, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, owner, repoName)
if err != nil {
return nil, err
}
var wantedCustomProperty *github.CustomPropertyValue
for _, customProperty := range allCustomProperties {
if customProperty.PropertyName == propertyName {
wantedCustomProperty = customProperty
}
}
if wantedCustomProperty == nil {
return nil, nil
}
wantedPropertyValue, err := parseRepositoryCustomPropertyValueToStringSlice(wantedCustomProperty)
if err != nil {
return nil, err
}
return wantedPropertyValue, nil
}