Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure integration_id is treated as optional and nil #2529

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 12 additions & 10 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)
if check.IntegrationID != nil {
integrationID = *check.IntegrationID

checkMap := make(map[string]interface{})
checkMap["context"] = check.Context
if check.IntegrationID != nil && *check.IntegrationID != 0 {
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
Loading