Skip to content

Commit f44d60f

Browse files
james-callahankfcampbell
authored andcommitted
feat(branch_protection): add blocks_creations attribute (integrations#1174)
* chore: run go get github.com/shurcooL/githubv4 to update dependency * feat(branch_protection): add blocks_creations attribute
1 parent 621ddfd commit f44d60f

File tree

12 files changed

+472
-14
lines changed

12 files changed

+472
-14
lines changed

github/resource_github_branch_protection.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func resourceGithubBranchProtection() *schema.Resource {
3838
Optional: true,
3939
Default: false,
4040
},
41+
PROTECTION_BLOCKS_CREATIONS: {
42+
Type: schema.TypeBool,
43+
Optional: true,
44+
Default: false,
45+
},
4146
PROTECTION_IS_ADMIN_ENFORCED: {
4247
Type: schema.TypeBool,
4348
Optional: true,
@@ -152,6 +157,7 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
152157
input := githubv4.CreateBranchProtectionRuleInput{
153158
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
154159
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
160+
BlocksCreations: githubv4.NewBoolean(githubv4.Boolean(data.BlocksCreations)),
155161
BypassPullRequestActorIDs: githubv4NewIDSlice(githubv4IDSliceEmpty(data.BypassPullRequestActorIDs)),
156162
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
157163
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),
@@ -224,6 +230,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
224230
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_ALLOWS_FORCE_PUSHES, protection.Repository.Name, protection.Pattern, d.Id())
225231
}
226232

233+
err = d.Set(PROTECTION_BLOCKS_CREATIONS, protection.BlocksCreations)
234+
if err != nil {
235+
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_BLOCKS_CREATIONS, protection.Repository.Name, protection.Pattern, d.Id())
236+
}
237+
227238
err = d.Set(PROTECTION_IS_ADMIN_ENFORCED, protection.IsAdminEnforced)
228239
if err != nil {
229240
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_IS_ADMIN_ENFORCED, protection.Repository.Name, protection.Pattern, d.Id())
@@ -281,6 +292,7 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
281292
BranchProtectionRuleID: d.Id(),
282293
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
283294
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
295+
BlocksCreations: githubv4.NewBoolean(githubv4.Boolean(data.BlocksCreations)),
284296
BypassPullRequestActorIDs: githubv4NewIDSlice(githubv4IDSliceEmpty(data.BypassPullRequestActorIDs)),
285297
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
286298
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),

github/resource_github_branch_protection_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,60 @@ func TestAccGithubBranchProtection(t *testing.T) {
438438

439439
})
440440

441+
t.Run("configures blocksCreations", func(t *testing.T) {
442+
443+
config := fmt.Sprintf(`
444+
resource "github_repository" "test" {
445+
name = "tf-acc-test-%s"
446+
auto_init = true
447+
}
448+
449+
data "github_user" "test" {
450+
username = "%s"
451+
}
452+
453+
resource "github_branch_protection" "test" {
454+
455+
repository_id = github_repository.test.name
456+
pattern = "main"
457+
blocks_creations = true
458+
459+
}
460+
`, randomID, testOwnerFunc())
461+
462+
check := resource.ComposeAggregateTestCheckFunc(
463+
resource.TestCheckResourceAttr(
464+
"github_branch_protection.test", "blocks_creations", "true",
465+
),
466+
)
467+
468+
testCase := func(t *testing.T, mode string) {
469+
resource.Test(t, resource.TestCase{
470+
PreCheck: func() { skipUnlessMode(t, mode) },
471+
Providers: testAccProviders,
472+
Steps: []resource.TestStep{
473+
{
474+
Config: config,
475+
Check: check,
476+
},
477+
},
478+
})
479+
}
480+
481+
t.Run("with an anonymous account", func(t *testing.T) {
482+
t.Skip("anonymous account not supported for this operation")
483+
})
484+
485+
t.Run("with an individual account", func(t *testing.T) {
486+
t.Skip("individual account not supported for this operation")
487+
})
488+
489+
t.Run("with an organization account", func(t *testing.T) {
490+
testCase(t, organization)
491+
})
492+
493+
})
494+
441495
t.Run("configures non-empty list of pull request bypassers", func(t *testing.T) {
442496

443497
config := fmt.Sprintf(`

github/util_v4_branch_protection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type BranchProtectionRule struct {
5151
} `graphql:"bypassPullRequestAllowances(first: 100)"`
5252
AllowsDeletions githubv4.Boolean
5353
AllowsForcePushes githubv4.Boolean
54+
BlocksCreations githubv4.Boolean
5455
DismissesStaleReviews githubv4.Boolean
5556
ID githubv4.ID
5657
IsAdminEnforced githubv4.Boolean
@@ -71,6 +72,7 @@ type BranchProtectionRule struct {
7172
type BranchProtectionResourceData struct {
7273
AllowsDeletions bool
7374
AllowsForcePushes bool
75+
BlocksCreations bool
7476
BranchProtectionRuleID string
7577
BypassPullRequestActorIDs []string
7678
DismissesStaleReviews bool
@@ -115,6 +117,10 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
115117
data.AllowsForcePushes = v.(bool)
116118
}
117119

120+
if v, ok := d.GetOk(PROTECTION_BLOCKS_CREATIONS); ok {
121+
data.BlocksCreations = v.(bool)
122+
}
123+
118124
if v, ok := d.GetOk(PROTECTION_IS_ADMIN_ENFORCED); ok {
119125
data.IsAdminEnforced = v.(bool)
120126
}

github/util_v4_consts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
const (
44
PROTECTION_ALLOWS_DELETIONS = "allows_deletions"
55
PROTECTION_ALLOWS_FORCE_PUSHES = "allows_force_pushes"
6+
PROTECTION_BLOCKS_CREATIONS = "blocks_creations"
67
PROTECTION_DISMISSES_STALE_REVIEWS = "dismiss_stale_reviews"
78
PROTECTION_IS_ADMIN_ENFORCED = "enforce_admins"
89
PROTECTION_PATTERN = "pattern"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
1111
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
1212
github.com/hashicorp/terraform-plugin-sdk v1.7.0
13-
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543
13+
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00
1414
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
1515
github.com/ulikunitz/xz v0.5.10 // indirect
1616
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
360360
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
361361
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
362362
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
363-
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543 h1:TLml5yQBxKTGrjQQUt+fMcJNNIUyNH0wDeCVGyaLF+s=
364-
github.com/shurcooL/githubv4 v0.0.0-20220106005112-0707a5a90543/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
363+
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00 h1:fiFvD4lT0aWjuuAb64LlZ/67v87m+Kc9Qsu5cMFNK0w=
364+
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
365365
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt3d2aYa0SiNms/hFqC9qJYolM=
366366
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
367367
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=

vendor/github.com/shurcooL/githubv4/README.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/shurcooL/githubv4/deprecated.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)