Skip to content

Commit

Permalink
add state migration
Browse files Browse the repository at this point in the history
  • Loading branch information
georgekaz committed Jan 6, 2024
1 parent fa97907 commit 7f24a54
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
37 changes: 37 additions & 0 deletions github/migrate_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,40 @@ func resourceGithubBranchProtectionUpgradeV0(rawState map[string]interface{}, me

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(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"] = map[string]interface{}{
"blocks_creations": blocksCreations,
"push_restrictions": v,
}
}

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

return rawState, nil
}
7 changes: 6 additions & 1 deletion 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 Down Expand Up @@ -183,6 +183,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Upgrade: resourceGithubBranchProtectionUpgradeV0,
Version: 0,
},
{
Type: resourceGithubBranchProtectionV1().CoreConfigSchema().ImpliedType(),
Upgrade: resourceGithubBranchProtectionUpgradeV1,
Version: 1,
},
},
}
}
Expand Down
27 changes: 27 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"fmt"
"reflect"
"regexp"
"testing"

Expand Down Expand Up @@ -899,3 +900,29 @@ func importBranchProtectionByRepoID(repoLogicalName, pattern string) resource.Im
return fmt.Sprintf("%s:%s", repoID, pattern), nil
}
}

func testGithubBranchProtectionStateDataV1() map[string]interface{} {
return map[string]interface{}{
"blocks_creations": true,
"push_restrictions": [...]string{"/example-user"},
}
}

func testGithubBranchProtectionStateDataV2() map[string]interface{} {
v1 := testGithubBranchProtectionStateDataV1()
return map[string]interface{}{
"restrict_pushes": v1,
}
}

func TestAccGithubBranchProtectionV4StateUpgradeV1(t *testing.T) {
expected := testGithubBranchProtectionStateDataV2()
actual, err := resourceGithubBranchProtectionUpgradeV1(testGithubBranchProtectionStateDataV1(), nil)
if err != nil {
t.Fatalf("error migrating state: %s", err)
}

if !reflect.DeepEqual(expected, actual) {
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
}
}

0 comments on commit 7f24a54

Please sign in to comment.