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

[work in progress] Add ability to set checks field in required_status_checks of github_branch_protection_v3 #1051

Closed
wants to merge 2 commits 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
17 changes: 17 additions & 0 deletions github/resource_github_branch_protection_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ 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,
Required: true,
},
"app_id": {
Type: schema.TypeInt,
Optional: true,
Default: nil,
},
},
},
},
},
},
},
Expand Down
65 changes: 63 additions & 2 deletions github/resource_github_branch_protection_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`

Expand Down Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions github/resource_github_branch_protection_v3_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/branch_protection_v3.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down