From 567856a2403b6cb72b50ae86dd7ffc1d7720e1a3 Mon Sep 17 00:00:00 2001 From: TheQueenIsDead <30706552+TheQueenIsDead@users.noreply.github.com> Date: Mon, 12 Dec 2022 10:00:57 +1300 Subject: [PATCH] feat: change branch protection v3 utils to flatten and expand contexts into checks --- .../resource_github_branch_protection_v3.go | 1 + ...ource_github_branch_protection_v3_utils.go | 44 ++++++++++++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/github/resource_github_branch_protection_v3.go b/github/resource_github_branch_protection_v3.go index e17a94cc3a..f48e937bf1 100644 --- a/github/resource_github_branch_protection_v3.go +++ b/github/resource_github_branch_protection_v3.go @@ -72,6 +72,7 @@ func resourceGithubBranchProtectionV3() *schema.Resource { "app_id": { Type: schema.TypeInt, Optional: true, + Default: -1, }, }, }, diff --git a/github/resource_github_branch_protection_v3_utils.go b/github/resource_github_branch_protection_v3_utils.go index 14e2e89acb..f58e9c7509 100644 --- a/github/resource_github_branch_protection_v3_utils.go +++ b/github/resource_github_branch_protection_v3_utils.go @@ -4,11 +4,10 @@ import ( "context" "errors" "fmt" - "log" - "strings" - "github.com/google/go-github/v48/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "log" + "strings" ) func buildProtectionRequest(d *schema.ResourceData) (*github.ProtectionRequest, error) { @@ -40,25 +39,35 @@ func buildProtectionRequest(d *schema.ResourceData) (*github.ProtectionRequest, func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *github.Protection) error { rsc := protection.GetRequiredStatusChecks() + if rsc != nil { - contexts := make([]interface{}, 0, len(rsc.Contexts)) + + var checks []interface{} + + // TODO: Remove once contexts is fully deprecated. + // Handle contexts as check objects while waiting for contexts to become deprecated + // Flatten contexts for _, c := range rsc.Contexts { - contexts = append(contexts, c) + chkMap := make(map[string]interface{}) + chkMap["context"] = c + chkMap["app_id"] = -1 + checks = append(checks, chkMap) } - var checks []interface{} + // Flatten checks for _, chk := range rsc.Checks { chkMap := make(map[string]interface{}) chkMap["context"] = chk.Context - chkMap["app_id"] = chk.GetAppID() + chkMap["app_id"] = chk.AppID checks = append(checks, chkMap) } return d.Set("required_status_checks", []interface{}{ map[string]interface{}{ - "strict": rsc.Strict, - "contexts": schema.NewSet(schema.HashString, contexts), - "check": checks, + "strict": rsc.Strict, + // TODO: As above, remove if unneeded + //"contexts": schema.NewSet(schema.HashString, contexts), + "check": checks, }, }) } @@ -200,12 +209,22 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC m := v.(map[string]interface{}) rsc.Strict = m["strict"].(bool) + var rscChecks []*github.RequiredStatusCheck + + // Iterate and parse contexts into checks objects as contexts is deprecated. + // TODO: Remove this code once contexts is fully deprecated + // Iterate and parse contexts contexts := expandNestedSet(m, "contexts") - rsc.Contexts = contexts + for _, c := range contexts { + appID := int64(-1) // Default + rscChecks = append(rscChecks, &github.RequiredStatusCheck{ + Context: c, + AppID: &appID, + }) + } // Iterate and parse checks checks := m["check"].([]interface{}) - var rscChecks []*github.RequiredStatusCheck for _, c := range checks { chk := c.(map[string]interface{}) @@ -224,6 +243,7 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC AppID: &rscAppId, }) } + // Assign after looping both checks and contexts rsc.Checks = rscChecks } return rsc, nil