Skip to content

Commit 83c50b6

Browse files
committed
fix: Ensure integration_id is treated as optional and nil
1 parent 061b2f5 commit 83c50b6

4 files changed

+88
-11
lines changed

github/resource_github_organization_ruleset.go

-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
250250
"integration_id": {
251251
Type: schema.TypeInt,
252252
Optional: true,
253-
Default: 0,
254253
Description: "The optional integration ID that this status check must originate from.",
255254
},
256255
},

github/resource_github_repository_ruleset.go

-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
238238
"integration_id": {
239239
Type: schema.TypeInt,
240240
Optional: true,
241-
Default: 0,
242241
Description: "The optional integration ID that this status check must originate from.",
243242
},
244243
},

github/resource_github_repository_ruleset_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,83 @@ func TestGithubRepositoryRulesets(t *testing.T) {
115115

116116
})
117117

118+
t.Run("Creates and updates repository rulesets including checks without errors", func(t *testing.T) {
119+
120+
config := fmt.Sprintf(`
121+
resource "github_repository" "test" {
122+
name = "tf-acc-test-checks-%s"
123+
auto_init = false
124+
vulnerability_alerts = true
125+
}
126+
127+
resource "github_repository_ruleset" "test" {
128+
name = "test"
129+
repository = github_repository.test.id
130+
target = "branch"
131+
enforcement = "active"
132+
133+
conditions {
134+
ref_name {
135+
include = ["refs/heads/main"]
136+
exclude = []
137+
}
138+
}
139+
140+
rules {
141+
deletion = true
142+
non_fast_forward = true
143+
144+
pull_request {
145+
required_approving_review_count = 1
146+
required_review_thread_resolution = false
147+
require_code_owner_review = false
148+
dismiss_stale_reviews_on_push = true
149+
require_last_push_approval = false
150+
}
151+
152+
required_status_checks {
153+
required_check {
154+
context = "ci"
155+
}
156+
strict_required_status_checks_policy = false
157+
}
158+
}
159+
}
160+
`, randomID)
161+
162+
check := resource.ComposeTestCheckFunc(
163+
resource.TestCheckNoResourceAttr(
164+
"github_repository_ruleset.test", "rules.0.required_status_checks.0.required_check.0.integration_id",
165+
),
166+
)
167+
168+
testCase := func(t *testing.T, mode string) {
169+
resource.Test(t, resource.TestCase{
170+
PreCheck: func() { skipUnlessMode(t, mode) },
171+
Providers: testAccProviders,
172+
Steps: []resource.TestStep{
173+
{
174+
Config: config,
175+
Check: check,
176+
},
177+
},
178+
})
179+
}
180+
181+
t.Run("with an anonymous account", func(t *testing.T) {
182+
t.Skip("anonymous account not supported for this operation")
183+
})
184+
185+
t.Run("with an individual account", func(t *testing.T) {
186+
testCase(t, individual)
187+
})
188+
189+
t.Run("with an organization account", func(t *testing.T) {
190+
testCase(t, organization)
191+
})
192+
193+
})
194+
118195
t.Run("Creates and updates repository rulesets with enterprise features without errors", func(t *testing.T) {
119196
if isEnterprise != "true" {
120197
t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false")

github/respository_rules_utils.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,16 @@ func expandRules(input []interface{}, org bool) []*github.RepositoryRule {
310310
requiredStatusChecksSet := requiredStatusChecksInput.(*schema.Set)
311311
for _, checkMap := range requiredStatusChecksSet.List() {
312312
check := checkMap.(map[string]interface{})
313-
integrationID := github.Int64(int64(check["integration_id"].(int)))
314313

315314
params := github.RuleRequiredStatusChecks{
316315
Context: check["context"].(string),
317316
}
318317

319-
if *integrationID != 0 {
320-
params.IntegrationID = integrationID
318+
if v, ok := check["integration_id"].(int); ok {
319+
integrationID := github.Int64(int64(v))
320+
if *integrationID != 0 {
321+
params.IntegrationID = integrationID
322+
}
321323
}
322324

323325
requiredStatusChecks = append(requiredStatusChecks, params)
@@ -490,14 +492,14 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {
490492

491493
requiredStatusChecksSlice := make([]map[string]interface{}, 0)
492494
for _, check := range params.RequiredStatusChecks {
493-
integrationID := int64(0)
495+
496+
checkMap := make(map[string]interface{})
497+
checkMap["context"] = check.Context
494498
if check.IntegrationID != nil {
495-
integrationID = *check.IntegrationID
499+
checkMap["integration_id"] = *check.IntegrationID
496500
}
497-
requiredStatusChecksSlice = append(requiredStatusChecksSlice, map[string]interface{}{
498-
"context": check.Context,
499-
"integration_id": integrationID,
500-
})
501+
502+
requiredStatusChecksSlice = append(requiredStatusChecksSlice, checkMap)
501503
}
502504

503505
rule := make(map[string]interface{})

0 commit comments

Comments
 (0)