Skip to content

Commit 306b839

Browse files
committed
fixup! fix: Refactor repository custom property
1 parent e54f259 commit 306b839

4 files changed

Lines changed: 139 additions & 111 deletions

github/acc_helpers_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/google/go-github/v88/github"
10+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
)
12+
13+
const testRandomIDLength = 5
14+
15+
func mustCreateTestOrganizationRepositoryCustomProperty(t *testing.T, valType string, allowed []string) *github.CustomProperty {
16+
t.Helper()
17+
18+
meta, err := getTestMeta()
19+
if err != nil {
20+
t.Fatalf("failed to get test meta: %v", err)
21+
}
22+
23+
randomID := acctest.RandString(testRandomIDLength)
24+
name := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
25+
26+
req := &github.CustomProperty{
27+
PropertyName: &name,
28+
ValueType: github.PropertyValueType(valType),
29+
AllowedValues: allowed,
30+
}
31+
32+
prop, _, err := meta.v3client.Organizations.CreateOrUpdateCustomProperty(t.Context(), meta.name, name, req)
33+
if err != nil {
34+
t.Fatalf("failed to create test organization repository custom property: %v", err)
35+
}
36+
37+
t.Cleanup(func() {
38+
if _, err := meta.v3client.Organizations.RemoveCustomProperty(context.Background(), meta.name, name); err != nil {
39+
if err, ok := errors.AsType[*github.ErrorResponse](err); ok && err.Response.StatusCode == 404 {
40+
return
41+
}
42+
t.Logf("failed to delete test organization repository custom property %s: %v", name, err)
43+
}
44+
})
45+
46+
return prop
47+
}
48+
49+
func mustCreateTestRepository(t *testing.T) *github.Repository {
50+
t.Helper()
51+
52+
meta, err := getTestMeta()
53+
if err != nil {
54+
t.Fatalf("failed to get test meta: %v", err)
55+
}
56+
57+
randomID := acctest.RandString(testRandomIDLength)
58+
name := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
59+
60+
req := &github.Repository{
61+
Name: &name,
62+
AutoInit: new(true),
63+
}
64+
65+
repo, _, err := meta.v3client.Repositories.Create(t.Context(), meta.name, req)
66+
if err != nil {
67+
t.Fatalf("failed to create test repository: %v", err)
68+
}
69+
70+
t.Cleanup(func() {
71+
if _, err := meta.v3client.Repositories.Delete(context.Background(), meta.name, name); err != nil {
72+
if err, ok := errors.AsType[*github.ErrorResponse](err); ok && err.Response.StatusCode == 404 {
73+
return
74+
}
75+
t.Logf("failed to delete test repository %s: %v", name, err)
76+
}
77+
})
78+
79+
return repo
80+
}

github/resource_github_repository_custom_property.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,6 @@ func resourceGithubRepositoryCustomPropertyRead(ctx context.Context, d *schema.R
186186
return diag.FromErr(err)
187187
}
188188

189-
id, err := buildID(owner, repoName, propertyName)
190-
if err != nil {
191-
return diag.FromErr(err)
192-
}
193-
d.SetId(id)
194-
195189
if err := d.Set("property_value", propertyValue); err != nil {
196190
return diag.FromErr(err)
197191
}

github/resource_github_repository_custom_property_migration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package github
33
import (
44
"context"
55
"fmt"
6-
"log"
76

87
"github.com/google/go-github/v88/github"
8+
"github.com/hashicorp/terraform-plugin-log/tflog"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1111
)
@@ -53,7 +53,7 @@ func resourceGithubRepositoryCustomPropertyStateUpgradeV0(ctx context.Context, r
5353
client := meta.v3client
5454
owner := meta.name
5555

56-
log.Printf("[DEBUG] GitHub Repository Custom Property Attributes before migration: %#v", rawState)
56+
tflog.Debug(ctx, "GitHub Repository Custom Property Attributes before migration: %#v", rawState)
5757

5858
repoName, ok := rawState["repository"].(string)
5959
if !ok {
@@ -68,7 +68,7 @@ func resourceGithubRepositoryCustomPropertyStateUpgradeV0(ctx context.Context, r
6868
repoID := int(repo.GetID())
6969
rawState["repository_id"] = repoID
7070

71-
log.Printf("[DEBUG] GitHub Repository Custom Property Attributes after migration: %#v", rawState)
71+
tflog.Debug(ctx, "GitHub Repository Custom Property Attributes after migration: %#v", rawState)
7272

7373
return rawState, nil
7474
}

github/resource_github_repository_custom_property_test.go

Lines changed: 56 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"regexp"
66
"testing"
77

8-
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
98
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
109
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
1110
"github.com/hashicorp/terraform-plugin-testing/plancheck"
@@ -19,39 +18,30 @@ func TestAccGithubRepositoryCustomProperty(t *testing.T) {
1918
t.Run("single_select", func(t *testing.T) {
2019
t.Parallel()
2120

22-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
23-
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
24-
propertyName := fmt.Sprintf("%sproperty-%s", testResourcePrefix, randomID)
25-
option1 := "option1"
26-
option2 := "option2"
21+
prop := mustCreateTestOrganizationRepositoryCustomProperty(t, "single_select", []string{"option1", "option2"})
22+
repo := mustCreateTestRepository(t)
23+
allowed := prop.GetAllowedValues()
2724

2825
config := fmt.Sprintf(`
29-
resource "github_organization_custom_properties" "test" {
30-
allowed_values = ["%s", "%s"]
31-
description = "Test Description"
32-
property_name = "%s"
33-
value_type = "single_select"
34-
}
35-
36-
resource "github_repository" "test" {
37-
name = "%s"
38-
auto_init = true
39-
}
40-
4126
resource "github_repository_custom_property" "test" {
42-
repository = github_repository.test.name
43-
property_name = github_organization_custom_properties.test.property_name
44-
property_type = github_organization_custom_properties.test.value_type
27+
repository = "%s"
28+
property_name = "%s"
29+
property_type = "%s"
4530
property_value = %%s
4631
}
47-
`, option1, option2, propertyName, repoName)
32+
`, repo.GetName(), prop.GetPropertyName(), prop.GetValueType())
4833

4934
resource.Test(t, resource.TestCase{
5035
PreCheck: func() { skipUnlessHasOrgs(t) },
5136
ProviderFactories: providerFactories,
5237
Steps: []resource.TestStep{
5338
{
54-
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, option1)),
39+
Config: fmt.Sprintf(config, `[]`),
40+
ExpectError: regexp.MustCompile(`Not enough list items`),
41+
PlanOnly: true,
42+
},
43+
{
44+
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, allowed[0])),
5545
ConfigStateChecks: []statecheck.StateCheck{
5646
statecheck.ExpectKnownValue("github_repository_custom_property.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
5747
},
@@ -61,7 +51,7 @@ resource "github_repository_custom_property" "test" {
6151
ExpectError: regexp.MustCompile(`is not allowed for property`),
6252
},
6353
{
64-
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, option2)),
54+
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, allowed[1])),
6555
ConfigPlanChecks: resource.ConfigPlanChecks{
6656
PreApply: []plancheck.PlanCheck{
6757
plancheck.ExpectResourceAction("github_repository_custom_property.test", plancheck.ResourceActionUpdate),
@@ -80,40 +70,30 @@ resource "github_repository_custom_property" "test" {
8070
t.Run("multi_select", func(t *testing.T) {
8171
t.Parallel()
8272

83-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
84-
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
85-
propertyName := fmt.Sprintf("%sproperty-%s", testResourcePrefix, randomID)
86-
option1 := "option1"
87-
option2 := "option2"
88-
option3 := "option3"
73+
prop := mustCreateTestOrganizationRepositoryCustomProperty(t, "multi_select", []string{"option1", "option2", "option3"})
74+
repo := mustCreateTestRepository(t)
75+
allowed := prop.GetAllowedValues()
8976

9077
config := fmt.Sprintf(`
91-
resource "github_organization_custom_properties" "test" {
92-
allowed_values = ["%s", "%s", "%s"]
93-
description = "Test Description"
94-
property_name = "%s"
95-
value_type = "multi_select"
96-
}
97-
98-
resource "github_repository" "test" {
99-
name = "%s"
100-
auto_init = true
101-
}
102-
10378
resource "github_repository_custom_property" "test" {
104-
repository = github_repository.test.name
105-
property_name = github_organization_custom_properties.test.property_name
106-
property_type = github_organization_custom_properties.test.value_type
79+
repository = "%s"
80+
property_name = "%s"
81+
property_type = "%s"
10782
property_value = %%s
10883
}
109-
`, option1, option2, option3, propertyName, repoName)
84+
`, repo.GetName(), prop.GetPropertyName(), prop.GetValueType())
11085

11186
resource.Test(t, resource.TestCase{
11287
PreCheck: func() { skipUnlessHasOrgs(t) },
11388
ProviderFactories: providerFactories,
11489
Steps: []resource.TestStep{
11590
{
116-
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s", "%s"]`, option1, option2)),
91+
Config: fmt.Sprintf(config, `[]`),
92+
ExpectError: regexp.MustCompile(`Not enough list items`),
93+
PlanOnly: true,
94+
},
95+
{
96+
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s", "%s"]`, allowed[0], allowed[1])),
11797
ConfigStateChecks: []statecheck.StateCheck{
11898
statecheck.ExpectKnownValue("github_repository_custom_property.test", tfjsonpath.New("repository_id"), knownvalue.NotNull()),
11999
},
@@ -123,7 +103,7 @@ resource "github_repository_custom_property" "test" {
123103
ExpectError: regexp.MustCompile(`is not allowed for property`),
124104
},
125105
{
126-
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, option3)),
106+
Config: fmt.Sprintf(config, fmt.Sprintf(`["%s"]`, allowed[2])),
127107
ConfigPlanChecks: resource.ConfigPlanChecks{
128108
PreApply: []plancheck.PlanCheck{
129109
plancheck.ExpectResourceAction("github_repository_custom_property.test", plancheck.ResourceActionUpdate),
@@ -142,34 +122,27 @@ resource "github_repository_custom_property" "test" {
142122
t.Run("true_false", func(t *testing.T) {
143123
t.Parallel()
144124

145-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
146-
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
147-
propertyName := fmt.Sprintf("%sproperty-%s", testResourcePrefix, randomID)
125+
prop := mustCreateTestOrganizationRepositoryCustomProperty(t, "true_false", nil)
126+
repo := mustCreateTestRepository(t)
148127

149128
config := fmt.Sprintf(`
150-
resource "github_organization_custom_properties" "test" {
151-
description = "Test Description"
152-
property_name = "%s"
153-
value_type = "true_false"
154-
}
155-
156-
resource "github_repository" "test" {
157-
name = "%s"
158-
auto_init = true
159-
}
160-
161129
resource "github_repository_custom_property" "test" {
162-
repository = github_repository.test.name
163-
property_name = github_organization_custom_properties.test.property_name
164-
property_type = github_organization_custom_properties.test.value_type
130+
repository = "%s"
131+
property_name = "%s"
132+
property_type = "%s"
165133
property_value = %%s
166134
}
167-
`, propertyName, repoName)
135+
`, repo.GetName(), prop.GetPropertyName(), prop.GetValueType())
168136

169137
resource.Test(t, resource.TestCase{
170138
PreCheck: func() { skipUnlessHasOrgs(t) },
171139
ProviderFactories: providerFactories,
172140
Steps: []resource.TestStep{
141+
{
142+
Config: fmt.Sprintf(config, `[]`),
143+
ExpectError: regexp.MustCompile(`Not enough list items`),
144+
PlanOnly: true,
145+
},
173146
{
174147
Config: fmt.Sprintf(config, `["true"]`),
175148
ConfigStateChecks: []statecheck.StateCheck{
@@ -200,34 +173,27 @@ resource "github_repository_custom_property" "test" {
200173
t.Run("url", func(t *testing.T) {
201174
t.Parallel()
202175

203-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
204-
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
205-
propertyName := fmt.Sprintf("%sproperty-%s", testResourcePrefix, randomID)
176+
prop := mustCreateTestOrganizationRepositoryCustomProperty(t, "url", nil)
177+
repo := mustCreateTestRepository(t)
206178

207179
config := fmt.Sprintf(`
208-
resource "github_organization_custom_properties" "test" {
209-
description = "Test Description"
210-
property_name = "%s"
211-
value_type = "url"
212-
}
213-
214-
resource "github_repository" "test" {
215-
name = "%s"
216-
auto_init = true
217-
}
218-
219180
resource "github_repository_custom_property" "test" {
220-
repository = github_repository.test.name
221-
property_name = github_organization_custom_properties.test.property_name
222-
property_type = github_organization_custom_properties.test.value_type
181+
repository = "%s"
182+
property_name = "%s"
183+
property_type = "%s"
223184
property_value = %%s
224185
}
225-
`, propertyName, repoName)
186+
`, repo.GetName(), prop.GetPropertyName(), prop.GetValueType())
226187

227188
resource.Test(t, resource.TestCase{
228189
PreCheck: func() { skipUnlessHasOrgs(t) },
229190
ProviderFactories: providerFactories,
230191
Steps: []resource.TestStep{
192+
{
193+
Config: fmt.Sprintf(config, `[]`),
194+
ExpectError: regexp.MustCompile(`Not enough list items`),
195+
PlanOnly: true,
196+
},
231197
{
232198
Config: fmt.Sprintf(config, `["https://example.com"]`),
233199
ConfigStateChecks: []statecheck.StateCheck{
@@ -258,29 +224,17 @@ resource "github_repository_custom_property" "test" {
258224
t.Run("string", func(t *testing.T) {
259225
t.Parallel()
260226

261-
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
262-
repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID)
263-
propertyName := fmt.Sprintf("%sproperty-%s", testResourcePrefix, randomID)
227+
prop := mustCreateTestOrganizationRepositoryCustomProperty(t, "string", nil)
228+
repo := mustCreateTestRepository(t)
264229

265230
config := fmt.Sprintf(`
266-
resource "github_organization_custom_properties" "test" {
267-
description = "Test Description"
268-
property_name = "%s"
269-
value_type = "string"
270-
}
271-
272-
resource "github_repository" "test" {
273-
name = "%s"
274-
auto_init = true
275-
}
276-
277231
resource "github_repository_custom_property" "test" {
278-
repository = github_repository.test.name
279-
property_name = github_organization_custom_properties.test.property_name
280-
property_type = github_organization_custom_properties.test.value_type
232+
repository = "%s"
233+
property_name = "%s"
234+
property_type = "%s"
281235
property_value = %%s
282236
}
283-
`, propertyName, repoName)
237+
`, repo.GetName(), prop.GetPropertyName(), prop.GetValueType())
284238

285239
resource.Test(t, resource.TestCase{
286240
PreCheck: func() { skipUnlessHasOrgs(t) },

0 commit comments

Comments
 (0)