Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add state migration
Browse files Browse the repository at this point in the history
georgekaz committed Jan 6, 2024
1 parent fa97907 commit 545e983
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/dev.tfrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
provider_installation {
dev_overrides {
"integrations/github" = "~/go/bin/"
"integrations/github" = "/home/george/go/bin/"
}

direct {}
37 changes: 37 additions & 0 deletions github/migrate_github_branch_protection.go
Original file line number Diff line number Diff line change
@@ -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
@@ -13,7 +13,7 @@ import (

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

Schema: map[string]*schema.Schema{
// Input
@@ -183,6 +183,11 @@ func resourceGithubBranchProtection() *schema.Resource {
Upgrade: resourceGithubBranchProtectionUpgradeV0,
Version: 0,
},
{
Type: resourceGithubBranchProtectionV1().CoreConfigSchema().ImpliedType(),
Upgrade: resourceGithubBranchProtectionUpgradeV1,
Version: 1,
},
},
}
}
27 changes: 27 additions & 0 deletions github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package github

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

@@ -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 545e983

Please sign in to comment.