Skip to content

Commit

Permalink
Added allowsDeletionsand allowsForcePushessettings (#623)
Browse files Browse the repository at this point in the history
* Added `allowsDeletions`and `allowsForcePushes`settings https://developer.github.com/v4/changelog/2020-11-13-schema-changes/ (#1)

* complete documentation

* update module github.com/shurcooL/githubv4 with `go get github.com/shurcooL/githubv4`

* vendor latest githubv4

* add test for deletions and force pushes

Co-authored-by: Jeremy Udit <[email protected]>
  • Loading branch information
frbayart and Jeremy Udit authored Dec 18, 2020
1 parent 81cbb1a commit 8927341
Show file tree
Hide file tree
Showing 12 changed files with 837 additions and 283 deletions.
24 changes: 24 additions & 0 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func resourceGithubBranchProtection() *schema.Resource {
Required: true,
Description: "",
},
PROTECTION_ALLOWS_DELETIONS: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
PROTECTION_ALLOWS_FORCE_PUSHES: {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
PROTECTION_IS_ADMIN_ENFORCED: {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -122,6 +132,8 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
return err
}
input := githubv4.CreateBranchProtectionRuleInput{
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),
Pattern: githubv4.String(data.Pattern),
Expand Down Expand Up @@ -181,6 +193,16 @@ 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())
}

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

err = d.Set(PROTECTION_ALLOWS_FORCE_PUSHES, protection.AllowsForcePushes)
if err != nil {
log.Printf("[WARN] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_ALLOWS_FORCE_PUSHES, 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 Expand Up @@ -226,6 +248,8 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
}
input := githubv4.UpdateBranchProtectionRuleInput{
BranchProtectionRuleID: d.Id(),
AllowsDeletions: githubv4.NewBoolean(githubv4.Boolean(data.AllowsDeletions)),
AllowsForcePushes: githubv4.NewBoolean(githubv4.Boolean(data.AllowsForcePushes)),
DismissesStaleReviews: githubv4.NewBoolean(githubv4.Boolean(data.DismissesStaleReviews)),
IsAdminEnforced: githubv4.NewBoolean(githubv4.Boolean(data.IsAdminEnforced)),
Pattern: githubv4.NewString(githubv4.String(data.Pattern)),
Expand Down
59 changes: 59 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,65 @@ func TestAccGithubBranchProtection(t *testing.T) {
})

})

t.Run("configures force pushes and deletions", 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"
allows_deletions = true
allows_force_pushes = true
}
`, randomID, testOwnerFunc())

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_protection.test", "allows_deletions", "true",
),
resource.TestCheckResourceAttr(
"github_branch_protection.test", "allows_force_pushes", "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)
})

})

}

func branchProtectionImportStateIdFunc(repo, pattern string) resource.ImportStateIdFunc {
Expand Down
12 changes: 12 additions & 0 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type BranchProtectionRule struct {
ReviewDismissalAllowances struct {
Nodes []DismissalActorTypes
} `graphql:"reviewDismissalAllowances(first: 100)"`
AllowsDeletions githubv4.Boolean
AllowsForcePushes githubv4.Boolean
DismissesStaleReviews githubv4.Boolean
ID githubv4.ID
IsAdminEnforced githubv4.Boolean
Expand All @@ -55,6 +57,8 @@ type BranchProtectionRule struct {
}

type BranchProtectionResourceData struct {
AllowsDeletions bool
AllowsForcePushes bool
BranchProtectionRuleID string
DismissesStaleReviews bool
IsAdminEnforced bool
Expand Down Expand Up @@ -88,6 +92,14 @@ func branchProtectionResourceData(d *schema.ResourceData, meta interface{}) (Bra
data.Pattern = v.(string)
}

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

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

if v, ok := d.GetOk(PROTECTION_IS_ADMIN_ENFORCED); ok {
data.IsAdminEnforced = v.(bool)
}
Expand Down
2 changes: 2 additions & 0 deletions github/util_v4_consts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package github

const (
PROTECTION_ALLOWS_DELETIONS = "allows_deletions"
PROTECTION_ALLOWS_FORCE_PUSHES = "allows_force_pushes"
PROTECTION_DISMISSES_STALE_REVIEWS = "dismiss_stale_reviews"
PROTECTION_IS_ADMIN_ENFORCED = "enforce_admins"
PROTECTION_PATTERN = "pattern"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.13
require (
github.com/client9/misspell v0.3.4
github.com/golangci/golangci-lint v1.25.1
github.com/google/go-github/v31 v31.0.0
github.com/google/go-github/v31 v31.0.0 // indirect
github.com/google/go-github/v32 v32.1.0
github.com/hashicorp/terraform v0.12.24
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lz
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0 h1:T9uus1QvcPgeLShS30YOnnzk3r9Vvygp45muhlrufgY=
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa h1:jozR3igKlnYCj9IVHOVump59bp07oIRoLQ/CcjMYIUA=
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa/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
21 changes: 13 additions & 8 deletions vendor/github.com/shurcooL/githubv4/README.md

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

5 changes: 1 addition & 4 deletions vendor/github.com/shurcooL/githubv4/doc.go

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

Loading

0 comments on commit 8927341

Please sign in to comment.