Skip to content
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
13 changes: 10 additions & 3 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Optional: true,
Default: false,
},
"require_code_owner_reviews": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"dismissal_users": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -268,9 +273,10 @@ func flattenRequiredPullRequestReviews(d *schema.ResourceData, protection *githu

if err := d.Set("required_pull_request_reviews", []interface{}{
map[string]interface{}{
"dismiss_stale_reviews": rprr.DismissStaleReviews,
"dismissal_users": schema.NewSet(schema.HashString, users),
"dismissal_teams": schema.NewSet(schema.HashString, teams),
"dismiss_stale_reviews": rprr.DismissStaleReviews,
"require_code_owner_reviews": rprr.RequireCodeOwnerReviews,
"dismissal_users": schema.NewSet(schema.HashString, users),
"dismissal_teams": schema.NewSet(schema.HashString, teams),
},
}); err != nil {
return err
Expand Down Expand Up @@ -363,6 +369,7 @@ func expandRequiredPullRequestReviews(d *schema.ResourceData) (*github.PullReque

rprr.DismissalRestrictionsRequest = drr
rprr.DismissStaleReviews = m["dismiss_stale_reviews"].(bool)
rprr.RequireCodeOwnerReviews = m["require_code_owner_reviews"].(bool)
}

return rprr, nil
Expand Down
11 changes: 8 additions & 3 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccGithubBranchProtection_basic(t *testing.T) {
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, true, []string{"github/foo"}),
testAccCheckGithubBranchProtectionRestrictions(&protection, []string{testUser}, []string{}),
testAccCheckGithubBranchProtectionPullRequestReviews(&protection, true, []string{testUser}, []string{}),
testAccCheckGithubBranchProtectionPullRequestReviews(&protection, true, true, []string{testUser}, []string{}),
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
resource.TestCheckResourceAttr("github_branch_protection.master", "branch", "master"),
resource.TestCheckResourceAttr("github_branch_protection.master", "enforce_admins", "true"),
Expand Down Expand Up @@ -180,7 +180,7 @@ func testAccCheckGithubBranchProtectionRestrictions(protection *github.Protectio
}
}

func testAccCheckGithubBranchProtectionPullRequestReviews(protection *github.Protection, expectedStale bool, expectedUsers, expectedTeams []string) resource.TestCheckFunc {
func testAccCheckGithubBranchProtectionPullRequestReviews(protection *github.Protection, expectedStale, expectedCodeOwner bool, expectedUsers, expectedTeams []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
reviews := protection.RequiredPullRequestReviews
if reviews == nil {
Expand All @@ -191,6 +191,10 @@ func testAccCheckGithubBranchProtectionPullRequestReviews(protection *github.Pro
return fmt.Errorf("Expected `dismiss_state_reviews` to be %t, got %t", expectedStale, reviews.DismissStaleReviews)
}

if reviews.RequireCodeOwnerReviews != expectedCodeOwner {
return fmt.Errorf("Expected `require_code_owner_reviews` to be %t, got %t", expectedCodeOwner, reviews.RequireCodeOwnerReviews)
}

users := []string{}
for _, u := range reviews.DismissalRestrictions.Users {
users = append(users, *u.Login)
Expand Down Expand Up @@ -276,7 +280,8 @@ resource "github_branch_protection" "master" {
}

required_pull_request_reviews {
dismiss_stale_reviews = true
dismiss_stale_reviews = true
require_code_owner_reviews = true
dismissal_users = ["%s"]
}

Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/branch_protection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The following arguments are supported:
`required_pull_request_reviews` supports the following arguments:

* `dismiss_stale_reviews`: (Optional) Dismiss approved reviews automatically when a new commit is pushed. Defaults to `false`.
* `require_code_owner_reviews`: (Optional) Require a review from a code owner. Defaults to `false`.
* `dismissal_users`: (Optional) The list of user logins with dismissal access
* `dismissal_teams`: (Optional) The list of team slugs with dismissal access

Expand All @@ -82,4 +83,4 @@ Github Branch Protection can be imported using an id made up of `repository:bran

```
$ terraform import github_branch_protection.terraform terraform:master
```
```