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

Fix restrict pushes on github_branch_protection. Fix branch protection tests #2045

Merged
merged 11 commits into from
Feb 2, 2024
6 changes: 3 additions & 3 deletions github/data_source_github_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ func dataSourceGithubUserRead(d *schema.ResourceData, meta interface{}) error {
if err = d.Set("following", user.GetFollowing()); err != nil {
return err
}
if err = d.Set("created_at", user.GetCreatedAt()); err != nil {
if err = d.Set("created_at", user.GetCreatedAt().String()); err != nil {
return err
}
if err = d.Set("updated_at", user.GetUpdatedAt()); err != nil {
if err = d.Set("updated_at", user.GetUpdatedAt().String()); err != nil {
return err
}
if err = d.Set("suspended_at", user.GetSuspendedAt()); err != nil {
if err = d.Set("suspended_at", user.GetSuspendedAt().String()); err != nil {
return err
}
if err = d.Set("node_id", user.GetNodeID()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion github/data_source_github_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccGithubUserDataSource(t *testing.T) {
`, testOwnerFunc())

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_user.test", "name"),
resource.TestCheckResourceAttrSet("data.github_user.test", "login"),
resource.TestCheckResourceAttrSet("data.github_user.test", "id"),
)

Expand Down
37 changes: 37 additions & 0 deletions github/migrate_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ func resourceGithubBranchProtectionUpgradeV0(_ context.Context, rawState map[str

return rawState, nil
}

func resourceGithubBranchProtectionV1() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"push_restrictions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"blocks_creations": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}

func resourceGithubBranchProtectionUpgradeV1(_ context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
var blocksCreations bool = false

if v, ok := rawState["blocks_creations"]; ok {
blocksCreations = v.(bool)
}

if v, ok := rawState["push_restrictions"]; ok {
rawState["restrict_pushes"] = []interface{}{map[string]interface{}{
"blocks_creations": blocksCreations,
"push_allowances": v,
}}
}

delete(rawState, "blocks_creations")
delete(rawState, "push_restrictions")

return rawState, nil
}
50 changes: 26 additions & 24 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func resourceGithubBranchProtection() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
SchemaVersion: 2,

Schema: map[string]*schema.Schema{
// Input
Expand All @@ -40,12 +40,6 @@ func resourceGithubBranchProtection() *schema.Resource {
Default: false,
Description: "Setting this to 'true' to allow force pushes on the branch.",
},
PROTECTION_BLOCKS_CREATIONS: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Setting this to 'true' to block creating the branch.",
},
PROTECTION_IS_ADMIN_ENFORCED: {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -104,7 +98,7 @@ func resourceGithubBranchProtection() *schema.Resource {
Optional: true,
Description: "Restrict pull request review dismissals.",
},
PROTECTION_RESTRICTS_REVIEW_DISMISSERS: {
PROTECTION_REVIEW_DISMISSAL_ALLOWANCES: {
Type: schema.TypeSet,
Optional: true,
Description: "The list of actor Names/IDs with dismissal access. If not empty, 'restrict_dismissals' is ignored. Actor names must either begin with a '/' for users or the organization name followed by a '/' for teams.",
Expand All @@ -116,7 +110,7 @@ func resourceGithubBranchProtection() *schema.Resource {
Description: "The list of actor Names/IDs that are allowed to bypass pull request requirements. Actor names must either begin with a '/' for users or the organization name followed by a '/' for teams.",
Elem: &schema.Schema{Type: schema.TypeString},
},
PROTECTION_REQUIRES_LAST_PUSH_APPROVAL: {
PROTECTION_REQUIRE_LAST_PUSH_APPROVAL: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Expand Down Expand Up @@ -146,10 +140,25 @@ func resourceGithubBranchProtection() *schema.Resource {
},
},
PROTECTION_RESTRICTS_PUSHES: {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Description: "The list of actor Names/IDs that may push to the branch. Actor names must either begin with a '/' for users or the organization name followed by a '/' for teams.",
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Restrict who can push to matching branches.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
PROTECTION_BLOCKS_CREATIONS: {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Restrict pushes that create matching branches.",
},
PROTECTION_PUSH_ALLOWANCES: {
Type: schema.TypeSet,
Optional: true,
Description: "The list of actor Names/IDs that may push to the branch. Actor names must either begin with a '/' for users or the organization name followed by a '/' for teams.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
PROTECTION_FORCE_PUSHES_BYPASSERS: {
Type: schema.TypeSet,
Expand All @@ -174,6 +183,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Upgrade: resourceGithubBranchProtectionUpgradeV0,
Version: 0,
},
{
Type: resourceGithubBranchProtectionV1().CoreConfigSchema().ImpliedType(),
Upgrade: resourceGithubBranchProtectionUpgradeV1,
Version: 1,
},
},
}
}
Expand Down Expand Up @@ -265,7 +279,6 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
variables := map[string]interface{}{
"id": d.Id(),
}

ctx := context.WithValue(context.Background(), ctxId, d.Id())
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
Expand All @@ -278,7 +291,6 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}

return err
}

protection := query.Node.Node

err = d.Set(PROTECTION_PATTERN, protection.Pattern)
Expand All @@ -296,11 +308,6 @@ 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 @@ -350,11 +357,6 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_FORCE_PUSHES_BYPASSERS, protection.Repository.Name, protection.Pattern, d.Id())
}

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

err = d.Set(PROTECTION_LOCK_BRANCH, protection.LockBranch)
if err != nil {
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_LOCK_BRANCH, protection.Repository.Name, protection.Pattern, d.Id())
Expand Down
Loading