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

Revert Config Changes To github_branch_protection #570

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
24 changes: 20 additions & 4 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ func resourceGithubBranchProtection() *schema.Resource {

Schema: map[string]*schema.Schema{
// Input
REPOSITORY: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
REPOSITORY_ID: {
Type: schema.TypeString,
Computed: true,
Description: "GraphQL `node_id` of a repository",
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

},
PROTECTION_BRANCH: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "",
Description: "`pattern` for a `BranchProtectionRule`",
Deprecated: "use `pattern` after v4.0.0 instead",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
PROTECTION_PATTERN: {
Type: schema.TypeString,
Required: true,
Description: "",
Computed: true,
Description: "`pattern` for a `BranchProtectionRule`",
},
PROTECTION_IS_ADMIN_ENFORCED: {
Type: schema.TypeBool,
Expand Down Expand Up @@ -180,6 +190,12 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_PATTERN, protection.Repository.Name, protection.Pattern, d.Id())
}

// FIXME: Remove in favour of PROTECTION_PATTERN on next major release
err = d.Set(PROTECTION_BRANCH, protection.Pattern)
if err != nil {
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_BRANCH, protection.Repository.Name, protection.Pattern, d.Id())
}

err = d.Set(PROTECTION_IS_ADMIN_ENFORCED, protection.IsAdminEnforced)
if err != nil {
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_IS_ADMIN_ENFORCED, protection.Repository.Name, protection.Pattern, d.Id())
Expand Down
37 changes: 16 additions & 21 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ func TestAccGithubBranchProtection(t *testing.T) {
t.Run("configures default settings when empty", func(t *testing.T) {

config := fmt.Sprintf(`

resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}

resource "github_branch_protection" "test" {

repository_id = github_repository.test.node_id
pattern = "main"

repository = github_repository.test.name
branch = "main"
}

`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_protection.test", "branch", "main",
),
Comment on lines +30 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

resource.TestCheckResourceAttr(
"github_branch_protection.test", "pattern", "main",
),
Expand Down Expand Up @@ -78,24 +77,22 @@ func TestAccGithubBranchProtection(t *testing.T) {
t.Run("configures required status checks", func(t *testing.T) {

config := fmt.Sprintf(`

resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}

resource "github_branch_protection" "test" {

repository_id = github_repository.test.node_id
pattern = "main"
repository = github_repository.test.name
branch = "main"

required_status_checks {
required_status_checks {
strict = true
contexts = ["github/foo"]
}

}

`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -134,24 +131,22 @@ func TestAccGithubBranchProtection(t *testing.T) {
t.Run("configures required pull request reviews", func(t *testing.T) {

config := fmt.Sprintf(`

resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}

resource "github_branch_protection" "test" {

repository_id = github_repository.test.node_id
pattern = "main"
repository = github_repository.test.name
branch = "main"

required_pull_request_reviews {
dismiss_stale_reviews = true
require_code_owner_reviews = true
}
required_pull_request_reviews {
dismiss_stale_reviews = true
require_code_owner_reviews = true
}

}

`, randomID)

check := resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -210,8 +205,8 @@ func TestAccGithubBranchProtection(t *testing.T) {

resource "github_branch_protection" "test" {

repository_id = github_repository.test.node_id
pattern = "main"
repository = github_repository.test.name
branch = "main"

push_restrictions = [
data.github_user.test.node_id,
Expand Down
12 changes: 9 additions & 3 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/shurcooL/githubv4"
)
Expand Down Expand Up @@ -67,11 +68,16 @@ type BranchProtectionResourceData struct {
func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (BranchProtectionResourceData, error) {
data := BranchProtectionResourceData{}

if v, ok := d.GetOk(REPOSITORY_ID); ok {
data.RepositoryID = v.(string)
if v, ok := d.GetOk(REPOSITORY); ok {
repoID, err := getRepositoryID(v.(string), meta)
if err != nil {
return data, err
}
data.RepositoryID = repoID.(string)
}

if v, ok := d.GetOk(PROTECTION_PATTERN); ok {
// FIXME: Re-instate to PROTECTION_PATTERN on next major release
if v, ok := d.GetOk(PROTECTION_BRANCH); ok {
data.Pattern = v.(string)
}

Expand Down
2 changes: 2 additions & 0 deletions github/util_v4_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
PROTECTION_DISMISSES_STALE_REVIEWS = "dismiss_stale_reviews"
PROTECTION_IS_ADMIN_ENFORCED = "enforce_admins"
PROTECTION_PATTERN = "pattern"
PROTECTION_BRANCH = "branch"
PROTECTION_REQUIRED_APPROVING_REVIEW_COUNT = "required_approving_review_count"
PROTECTION_REQUIRED_STATUS_CHECK_CONTEXTS = "contexts"
PROTECTION_REQUIRES_APPROVING_REVIEWS = "required_pull_request_reviews"
Expand All @@ -14,5 +15,6 @@ const (
PROTECTION_RESTRICTS_PUSHES = "push_restrictions"
PROTECTION_RESTRICTS_REVIEW_DISMISSALS = "dismissal_restrictions"

REPOSITORY = "repository"
REPOSITORY_ID = "repository_id"
)
8 changes: 4 additions & 4 deletions website/docs/r/branch_protection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ This resource allows you to configure branch protection for repositories in your
# the "ci/travis" context to be passing and only allow the engineers team merge
# to the branch.
resource "github_branch_protection" "example" {
repository_id = github_repository.example.node_id
pattern = "main"
repository = github_repository.example.name
branch = "main"
enforce_admins = true

required_status_checks {
Expand Down Expand Up @@ -60,8 +60,8 @@ resource "github_team_repository" "example" {

The following arguments are supported:

* `repository_id` - (Required) The repository associated with this branch protection rule.
* `pattern` - (Required) Identifies the protection rule pattern.
* `repository` - (Required) The repository associated with this branch protection rule.
* `branch` - (Deprecated) Identifies the protection rule pattern. In v4.0.0, will become `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_status_checks` - (Optional) Enforce restrictions for required status checks. See [Required Status Checks](#required-status-checks) below for details.
Expand Down