From 9a46a6e58f422ae1eb3257b805d6a9968eedec86 Mon Sep 17 00:00:00 2001 From: Charlie Collard Date: Wed, 22 Sep 2021 22:48:02 +0100 Subject: [PATCH] Add `require_linear_history` on `github_branch_protection` resource (#887) * Add `require_linear_history` on `github_branch_protection` resource * Run gofmt * Move documentation to correct file --- github/resource_github_branch_protection.go | 10 ++++++++++ github/util_v4_branch_protection.go | 6 ++++++ github/util_v4_consts.go | 1 + website/docs/r/branch_protection.html.markdown | 1 + 4 files changed, 18 insertions(+) diff --git a/github/resource_github_branch_protection.go b/github/resource_github_branch_protection.go index 74f16abb55..1f99a3db5a 100644 --- a/github/resource_github_branch_protection.go +++ b/github/resource_github_branch_protection.go @@ -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, @@ -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 { diff --git a/github/util_v4_branch_protection.go b/github/util_v4_branch_protection.go index 73bda828fd..e3f43aa61c 100644 --- a/github/util_v4_branch_protection.go +++ b/github/util_v4_branch_protection.go @@ -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 @@ -70,6 +71,7 @@ type BranchProtectionResourceData struct { RequiresApprovingReviews bool RequiresCodeOwnerReviews bool RequiresCommitSignatures bool + RequiresLinearHistory bool RequiresStatusChecks bool RequiresStrictStatusChecks bool RestrictsPushes bool @@ -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 { diff --git a/github/util_v4_consts.go b/github/util_v4_consts.go index fc4a88d8bc..b4a4bc6d67 100644 --- a/github/util_v4_consts.go +++ b/github/util_v4_consts.go @@ -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" diff --git a/website/docs/r/branch_protection.html.markdown b/website/docs/r/branch_protection.html.markdown index 013425b385..7ff6ded8bd 100644 --- a/website/docs/r/branch_protection.html.markdown +++ b/website/docs/r/branch_protection.html.markdown @@ -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.