Skip to content

Commit

Permalink
feat(branch_protection): add blocks_creations attribute (integrations…
Browse files Browse the repository at this point in the history
…#1174)

* chore: run go get github.com/shurcooL/githubv4 to update dependency

* feat(branch_protection): add blocks_creations attribute
  • Loading branch information
james-callahan authored Jul 15, 2022
1 parent e011d62 commit f0f9f22
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 14 deletions.
12 changes: 12 additions & 0 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Optional: true,
Default: false,
},
PROTECTION_BLOCKS_CREATIONS: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
PROTECTION_IS_ADMIN_ENFORCED: {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -152,6 +157,7 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
input := githubv4.CreateBranchProtectionRuleInput{
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
BlocksCreations: githubv4.NewBoolean(githubv4.Boolean(data.BlocksCreations)),
BypassPullRequestActorIDs: githubv4NewIDSlice(githubv4IDSliceEmpty(data.BypassPullRequestActorIDs)),
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),
Expand Down Expand Up @@ -224,6 +230,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_ALLOWS_FORCE_PUSHES, protection.Repository.Name, protection.Pattern, d.Id())
}

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

err = d.Set(PROTECTION_IS_ADMIN_ENFORCED, protection.IsAdminEnforced)
if err != nil {
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_IS_ADMIN_ENFORCED, protection.Repository.Name, protection.Pattern, d.Id())
Expand Down Expand Up @@ -281,6 +292,7 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
BranchProtectionRuleID: d.Id(),
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
BlocksCreations: githubv4.NewBoolean(githubv4.Boolean(data.BlocksCreations)),
BypassPullRequestActorIDs: githubv4NewIDSlice(githubv4IDSliceEmpty(data.BypassPullRequestActorIDs)),
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),
Expand Down
54 changes: 54 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,60 @@ func TestAccGithubBranchProtection(t *testing.T) {

})

t.Run("configures blocksCreations", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}
data "github_user" "test" {
username = "%s"
}
resource "github_branch_protection" "test" {
repository_id = github_repository.test.name
pattern = "main"
blocks_creations = true
}
`, randomID, testOwnerFunc())

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_protection.test", "blocks_creations", "true",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
t.Skip("individual account not supported for this operation")
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

t.Run("configures non-empty list of pull request bypassers", func(t *testing.T) {

config := fmt.Sprintf(`
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 @@ -51,6 +51,7 @@ type BranchProtectionRule struct {
} `graphql:"bypassPullRequestAllowances(first: 100)"`
AllowsDeletions githubv4.Boolean
AllowsForcePushes githubv4.Boolean
BlocksCreations githubv4.Boolean
DismissesStaleReviews githubv4.Boolean
ID githubv4.ID
IsAdminEnforced githubv4.Boolean
Expand All @@ -71,6 +72,7 @@ type BranchProtectionRule struct {
type BranchProtectionResourceData struct {
AllowsDeletions bool
AllowsForcePushes bool
BlocksCreations bool
BranchProtectionRuleID string
BypassPullRequestActorIDs []string
DismissesStaleReviews bool
Expand Down Expand Up @@ -115,6 +117,10 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
data.AllowsForcePushes = v.(bool)
}

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

if v, ok := d.GetOk(PROTECTION_IS_ADMIN_ENFORCED); ok {
data.IsAdminEnforced = v.(bool)
}
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 @@ -3,6 +3,7 @@ package github
const (
PROTECTION_ALLOWS_DELETIONS = "allows_deletions"
PROTECTION_ALLOWS_FORCE_PUSHES = "allows_force_pushes"
PROTECTION_BLOCKS_CREATIONS = "blocks_creations"
PROTECTION_DISMISSES_STALE_REVIEWS = "dismiss_stale_reviews"
PROTECTION_IS_ADMIN_ENFORCED = "enforce_admins"
PROTECTION_PATTERN = "pattern"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543 h1:TLml5yQBxKTGrjQQUt+fMcJNNIUyNH0wDeCVGyaLF+s=
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00 h1:fiFvD4lT0aWjuuAb64LlZ/67v87m+Kc9Qsu5cMFNK0w=
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt3d2aYa0SiNms/hFqC9qJYolM=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/shurcooL/githubv4/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/shurcooL/githubv4/deprecated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f0f9f22

Please sign in to comment.