Skip to content

Commit

Permalink
Add require_linear_history on github_branch_protection resource (i…
Browse files Browse the repository at this point in the history
…ntegrations#887)

* Add `require_linear_history` on `github_branch_protection` resource

* Run gofmt

* Move documentation to correct file
  • Loading branch information
charlie-collard authored Sep 22, 2021
1 parent e87e8b0 commit 9a46a6e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Optional: true,
Default: false,
},
PROTECTION_REQUIRES_LINEAR_HISTORY: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
PROTECTION_REQUIRES_APPROVING_REVIEWS: {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -217,6 +222,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_COMMIT_SIGNATURES, protection.Repository.Name, protection.Pattern, d.Id())
}

err = d.Set(PROTECTION_REQUIRES_LINEAR_HISTORY, protection.RequiresLinearHistory)
if err != nil {
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_REQUIRES_LINEAR_HISTORY, protection.Repository.Name, protection.Pattern, d.Id())
}

approvingReviews := setApprovingReviews(protection)
err = d.Set(PROTECTION_REQUIRES_APPROVING_REVIEWS, approvingReviews)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type BranchProtectionRule struct {
RequiresApprovingReviews githubv4.Boolean
RequiresCodeOwnerReviews githubv4.Boolean
RequiresCommitSignatures githubv4.Boolean
RequiresLinearHistory githubv4.Boolean
RequiresStatusChecks githubv4.Boolean
RequiresStrictStatusChecks githubv4.Boolean
RestrictsPushes githubv4.Boolean
Expand All @@ -70,6 +71,7 @@ type BranchProtectionResourceData struct {
RequiresApprovingReviews bool
RequiresCodeOwnerReviews bool
RequiresCommitSignatures bool
RequiresLinearHistory bool
RequiresStatusChecks bool
RequiresStrictStatusChecks bool
RestrictsPushes bool
Expand Down Expand Up @@ -108,6 +110,10 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
data.RequiresCommitSignatures = v.(bool)
}

if v, ok := d.GetOk(PROTECTION_REQUIRES_LINEAR_HISTORY); ok {
data.RequiresLinearHistory = v.(bool)
}

if v, ok := d.GetOk(PROTECTION_REQUIRES_APPROVING_REVIEWS); ok {
vL := v.([]interface{})
if len(vL) > 1 {
Expand Down
1 change: 1 addition & 0 deletions github/util_v4_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
PROTECTION_REQUIRES_APPROVING_REVIEWS = "required_pull_request_reviews"
PROTECTION_REQUIRES_CODE_OWNER_REVIEWS = "require_code_owner_reviews"
PROTECTION_REQUIRES_COMMIT_SIGNATURES = "require_signed_commits"
PROTECTION_REQUIRES_LINEAR_HISTORY = "required_linear_history"
PROTECTION_REQUIRES_STATUS_CHECKS = "required_status_checks"
PROTECTION_REQUIRES_STRICT_STATUS_CHECKS = "strict"
PROTECTION_RESTRICTS_PUSHES = "push_restrictions"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/branch_protection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The following arguments are supported:
* `pattern` - (Required) Identifies the protection rule pattern.
* `enforce_admins` - (Optional) Boolean, setting this to `true` enforces status checks for repository administrators.
* `require_signed_commits` - (Optional) Boolean, setting this to `true` requires all commits to be signed with GPG.
* `required_linear_history` - (Optional) Boolean, setting this to `true` enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch
* `required_status_checks` - (Optional) Enforce restrictions for required status checks. See [Required Status Checks](#required-status-checks) below for details.
* `required_pull_request_reviews` - (Optional) Enforce restrictions for pull request reviews. See [Required Pull Request Reviews](#required-pull-request-reviews) below for details.
* `push_restrictions` - (Optional) The list of actor IDs that may push to the branch.
Expand Down

0 comments on commit 9a46a6e

Please sign in to comment.