From 83c50b67e8614016622783c4f92b2a950f6928a7 Mon Sep 17 00:00:00 2001 From: Danielku15 Date: Mon, 6 Jan 2025 16:16:01 +0100 Subject: [PATCH] fix: Ensure integration_id is treated as optional and nil --- .../resource_github_organization_ruleset.go | 1 - github/resource_github_repository_ruleset.go | 1 - ...resource_github_repository_ruleset_test.go | 77 +++++++++++++++++++ github/respository_rules_utils.go | 20 ++--- 4 files changed, 88 insertions(+), 11 deletions(-) diff --git a/github/resource_github_organization_ruleset.go b/github/resource_github_organization_ruleset.go index 014b28ef65..885690988b 100644 --- a/github/resource_github_organization_ruleset.go +++ b/github/resource_github_organization_ruleset.go @@ -250,7 +250,6 @@ func resourceGithubOrganizationRuleset() *schema.Resource { "integration_id": { Type: schema.TypeInt, Optional: true, - Default: 0, Description: "The optional integration ID that this status check must originate from.", }, }, diff --git a/github/resource_github_repository_ruleset.go b/github/resource_github_repository_ruleset.go index e1bc9fc3ac..813576a86f 100644 --- a/github/resource_github_repository_ruleset.go +++ b/github/resource_github_repository_ruleset.go @@ -238,7 +238,6 @@ func resourceGithubRepositoryRuleset() *schema.Resource { "integration_id": { Type: schema.TypeInt, Optional: true, - Default: 0, Description: "The optional integration ID that this status check must originate from.", }, }, diff --git a/github/resource_github_repository_ruleset_test.go b/github/resource_github_repository_ruleset_test.go index a51682507f..aa08b7df35 100644 --- a/github/resource_github_repository_ruleset_test.go +++ b/github/resource_github_repository_ruleset_test.go @@ -115,6 +115,83 @@ func TestGithubRepositoryRulesets(t *testing.T) { }) + t.Run("Creates and updates repository rulesets including checks without errors", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-checks-%s" + auto_init = false + vulnerability_alerts = true + } + + resource "github_repository_ruleset" "test" { + name = "test" + repository = github_repository.test.id + target = "branch" + enforcement = "active" + + conditions { + ref_name { + include = ["refs/heads/main"] + exclude = [] + } + } + + rules { + deletion = true + non_fast_forward = true + + pull_request { + required_approving_review_count = 1 + required_review_thread_resolution = false + require_code_owner_review = false + dismiss_stale_reviews_on_push = true + require_last_push_approval = false + } + + required_status_checks { + required_check { + context = "ci" + } + strict_required_status_checks_policy = false + } + } + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr( + "github_repository_ruleset.test", "rules.0.required_status_checks.0.required_check.0.integration_id", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) + t.Run("Creates and updates repository rulesets with enterprise features without errors", func(t *testing.T) { if isEnterprise != "true" { t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") diff --git a/github/respository_rules_utils.go b/github/respository_rules_utils.go index cfb8ceb462..5208edd8fc 100644 --- a/github/respository_rules_utils.go +++ b/github/respository_rules_utils.go @@ -310,14 +310,16 @@ func expandRules(input []interface{}, org bool) []*github.RepositoryRule { requiredStatusChecksSet := requiredStatusChecksInput.(*schema.Set) for _, checkMap := range requiredStatusChecksSet.List() { check := checkMap.(map[string]interface{}) - integrationID := github.Int64(int64(check["integration_id"].(int))) params := github.RuleRequiredStatusChecks{ Context: check["context"].(string), } - if *integrationID != 0 { - params.IntegrationID = integrationID + if v, ok := check["integration_id"].(int); ok { + integrationID := github.Int64(int64(v)) + if *integrationID != 0 { + params.IntegrationID = integrationID + } } requiredStatusChecks = append(requiredStatusChecks, params) @@ -490,14 +492,14 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} { requiredStatusChecksSlice := make([]map[string]interface{}, 0) for _, check := range params.RequiredStatusChecks { - integrationID := int64(0) + + checkMap := make(map[string]interface{}) + checkMap["context"] = check.Context if check.IntegrationID != nil { - integrationID = *check.IntegrationID + checkMap["integration_id"] = *check.IntegrationID } - requiredStatusChecksSlice = append(requiredStatusChecksSlice, map[string]interface{}{ - "context": check.Context, - "integration_id": integrationID, - }) + + requiredStatusChecksSlice = append(requiredStatusChecksSlice, checkMap) } rule := make(map[string]interface{})