From f7a02c7de12524c16252b35bb8fd2f92c5fefacf Mon Sep 17 00:00:00 2001 From: Kevin Zhao Date: Thu, 27 Jan 2022 12:00:46 -0800 Subject: [PATCH 1/2] Add ability to set checks field in required_status_checks of resource_github_branch_protection_v3 --- .../resource_github_branch_protection_v3.go | 16 +++++ ...source_github_branch_protection_v3_test.go | 65 ++++++++++++++++++- .../docs/r/branch_protection_v3.html.markdown | 8 +++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/github/resource_github_branch_protection_v3.go b/github/resource_github_branch_protection_v3.go index 8cde59ae95..465463b887 100644 --- a/github/resource_github_branch_protection_v3.go +++ b/github/resource_github_branch_protection_v3.go @@ -59,6 +59,22 @@ func resourceGithubBranchProtectionV3() *schema.Resource { Type: schema.TypeString, }, }, + "checks": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "context": { + Type: schema.TypeString, + }, + "app_id": { + Type: schema.TypeInt, + Optional: true, + Default: nil, + }, + }, + }, + }, }, }, }, diff --git a/github/resource_github_branch_protection_v3_test.go b/github/resource_github_branch_protection_v3_test.go index 421cf81881..0bffd95cd3 100644 --- a/github/resource_github_branch_protection_v3_test.go +++ b/github/resource_github_branch_protection_v3_test.go @@ -151,10 +151,10 @@ func TestAccGithubBranchProtectionV3_conversation_resolution(t *testing.T) { }) } -func TestAccGithubBranchProtectionV3_required_status_checks(t *testing.T) { +func TestAccGithubBranchProtectionV3_required_status_checks_contexts(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("configures required status checks", func(t *testing.T) { + t.Run("configures required status checks (via contexts)", func(t *testing.T) { config := fmt.Sprintf(` @@ -210,6 +210,67 @@ func TestAccGithubBranchProtectionV3_required_status_checks(t *testing.T) { }) } +func TestAccGithubBranchProtectionV3_required_status_checks_checks(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + t.Run("configures required status checks (via checks)", func(t *testing.T) { + + config := fmt.Sprintf(` + + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_branch_protection_v3" "test" { + + repository = github_repository.test.name + branch = "main" + + required_status_checks { + strict = true + checks = [{ + context = "github/foo" + app_id = -1 + }] + } + } + + `, randomID) + + check := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "github_branch_protection_v3.test", "required_status_checks.#", "1", + ), + ) + + 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) { + t.Skip("individual account not supported for this operation") + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) +} func TestAccGithubBranchProtectionV3_required_pull_request_reviews(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) diff --git a/website/docs/r/branch_protection_v3.html.markdown b/website/docs/r/branch_protection_v3.html.markdown index 12a7c25efd..42558d5a25 100644 --- a/website/docs/r/branch_protection_v3.html.markdown +++ b/website/docs/r/branch_protection_v3.html.markdown @@ -89,6 +89,14 @@ The following arguments are supported: * `strict`: (Optional) Require branches to be up to date before merging. Defaults to `false`. * `contexts`: (Optional) The list of status checks to require in order to merge into this branch. No status checks are required by default. +* `checks` - (Required) The source branch and directory for the rendered Pages site. See [checks configuration](#checks-configuration) below for details. + +#### Checks Configuration #### + +The `checks` block supports the following: + +* `context`: The name of the required check. +* `app_id`: (Optional) The ID of the GitHub App that must provide this check. Omit this field to automatically select the GitHub App that has recently provided this check, or any app if it was not set by a GitHub App. Pass -1 to explicitly allow any app to set the status. ### Required Pull Request Reviews From 4c8d794fb80d2da2187d9811dcdf6ccf9d9c9cbd Mon Sep 17 00:00:00 2001 From: Kevin Zhao Date: Mon, 31 Jan 2022 12:37:57 -0800 Subject: [PATCH 2/2] attempt at flatten + expand --- github/resource_github_branch_protection_v3.go | 3 ++- ...source_github_branch_protection_v3_utils.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/github/resource_github_branch_protection_v3.go b/github/resource_github_branch_protection_v3.go index 465463b887..195a078a12 100644 --- a/github/resource_github_branch_protection_v3.go +++ b/github/resource_github_branch_protection_v3.go @@ -65,7 +65,8 @@ func resourceGithubBranchProtectionV3() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "context": { - Type: schema.TypeString, + Type: schema.TypeString, + Required: true, }, "app_id": { Type: schema.TypeInt, diff --git a/github/resource_github_branch_protection_v3_utils.go b/github/resource_github_branch_protection_v3_utils.go index ac82f141cd..8eb04244e9 100644 --- a/github/resource_github_branch_protection_v3_utils.go +++ b/github/resource_github_branch_protection_v3_utils.go @@ -46,10 +46,19 @@ func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *githu contexts = append(contexts, c) } + checks := make([]interface{}, 0, len(rsc.Checks)) + for _, c := range rsc.Checks { + checkMap := make(map[string]interface{}) + checkMap["context"] = c.Context + checkMap["app_id"] = c.AppID + checks = append(checks, checkMap) + } + return d.Set("required_status_checks", []interface{}{ map[string]interface{}{ "strict": rsc.Strict, "contexts": schema.NewSet(schema.HashString, contexts), + "checks": checks, }, }) } @@ -193,6 +202,15 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC contexts := expandNestedSet(m, "contexts") rsc.Contexts = contexts + + checks := make([]RequiredStatusCheck, 0) + for _, c := range m["checks"].([]map[string]interface{}) { + check := new(github.RequiredStatusCheck) + check.Context = c["context"] + check.AppID = c["app_id"] + checks = append(checks, check) + } + rsc.Checks = checks } return rsc, nil }