Skip to content

Commit

Permalink
fix: Ensure integration_id is treated as optional and nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Jan 6, 2025
1 parent 061b2f5 commit 83c50b6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
1 change: 0 additions & 1 deletion github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
},
Expand Down
1 change: 0 additions & 1 deletion github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
},
Expand Down
77 changes: 77 additions & 0 deletions github/resource_github_repository_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 11 additions & 9 deletions github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{})
Expand Down

0 comments on commit 83c50b6

Please sign in to comment.