Skip to content

Commit

Permalink
Include required review count
Browse files Browse the repository at this point in the history
This commit includes the `required_approving_review_count` field on the
`github_branch_protection` resource, allowing to manage the miminum
amount of reviewers to allow a merge to happen.

This field must be between 1 - 6, according to the docs, and must be
valid if present.

Bumping the `go-github` to `v24` made it default to `0` when not
present, causing the followin error

<detail>
<summary>GitHub API error when count is 0</summary>

```
422 Invalid request.

No subschema in "anyOf" matched.
0 must be greater than or equal to 1.
Not all subschemas of "allOf" matched.
For 'anyOf/1', {"dismissal_restrictions"=>{"users"=>[], "teams"=>[]}, "dismiss_stale_reviews"=>false, "require_code_owner_reviews"=>true, "required_approving_review_count"=>0} is not a null. []

Payload:
{
 "required_status_checks": {
  "strict": true,
  "contexts": [
   "lint",
   "test"
  ]
 },
 "required_pull_request_reviews": {
  "dismissal_restrictions": {
   "users": [],
   "teams": []
  },
  "dismiss_stale_reviews": false,
  "require_code_owner_reviews": true,
  "required_approving_review_count": 0
 },
 "enforce_admins": true,
 "restrictions": null
}
```
</detail>

This PR is important when upgrading `go-github`.

Built on top of: https://github.com/terraform-providers/terraform-provider-github/pull/207
  • Loading branch information
bltavares authored and paultyng committed May 2, 2019
1 parent 78242e3 commit 0f6f13c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/go-github/v25/github"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceGithubBranchProtection() *schema.Resource {
Expand Down Expand Up @@ -96,6 +97,12 @@ func resourceGithubBranchProtection() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"required_approving_review_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 6),
},
},
},
},
Expand Down Expand Up @@ -338,10 +345,11 @@ func flattenAndSetRequiredPullRequestReviews(d *schema.ResourceData, protection

return 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),
"require_code_owner_reviews": rprr.RequireCodeOwnerReviews,
"dismiss_stale_reviews": rprr.DismissStaleReviews,
"dismissal_users": schema.NewSet(schema.HashString, users),
"dismissal_teams": schema.NewSet(schema.HashString, teams),
"require_code_owner_reviews": rprr.RequireCodeOwnerReviews,
"required_approving_review_count": rprr.RequiredApprovingReviewCount,
},
})
}
Expand Down Expand Up @@ -427,6 +435,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)
rprr.RequiredApprovingReviewCount = m["required_approving_review_count"].(int)
}

return rprr, nil
Expand Down

0 comments on commit 0f6f13c

Please sign in to comment.