Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions github/resource_github_organization_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,74 @@ func TestGithubOrganizationRulesets(t *testing.T) {

})

t.Run("Updates a ruleset required workflow without error", func(t *testing.T) {
oldWorkflowName := fmt.Sprintf(`workflow-%[1]s`, randomID)
newWorkflowName := fmt.Sprintf(`%[1]s-renamed`, randomID)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test"
auto_init = true
}

resource "github_organization_ruleset" "test" {
name = "test"
enforcement = "active"

rules {
required_workflows {
required_workflow {
path = ".github/workflows/%s.yml"
ref = "main"
repository_id = github_repository.test.node_id
}
}
}
}
`, oldWorkflowName)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_ruleset.test.rules[0].required_workflows[0].required_workflow", "path",
oldWorkflowName,
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_ruleset.test.rules[0].required_workflows[0].required_workflow", "path",
newWorkflowName,
),
),
}

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: checks["before"],
},
{
// Rename the ruleset to something else
Config: strings.Replace(
config,
oldWorkflowName,
newWorkflowName, 1),
Check: checks["after"],
},
},
})
}

t.Run("with an enterprise account", func(t *testing.T) {
testCase(t, enterprise)
})

})

t.Run("Imports rulesets without error", func(t *testing.T) {

config := fmt.Sprintf(`
Expand Down
23 changes: 23 additions & 0 deletions github/respository_rules_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,29 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {
rule["do_not_enforce_on_create"] = params.DoNotEnforceOnCreate
rulesMap[v.Type] = []map[string]interface{}{rule}

case "workflows":
var params github.RequiredWorkflowsRuleParameters

err := json.Unmarshal(*v.Parameters, &params)
if err != nil {
log.Printf("[INFO] Unexpected error unmarshalling rule %s with parameters: %v",
v.Type, v.Parameters)
}

requiredWorkflowsSlice := make([]map[string]interface{}, 0)
for _, workflow := range params.RequiredWorkflows {
requiredWorkflowsSlice = append(requiredWorkflowsSlice, map[string]interface{}{
"repository_id": workflow.RepositoryID,
"path": workflow.Path,
"ref": workflow.Ref,
})
}

rule := make(map[string]interface{})
rule["workflows"] = requiredWorkflowsSlice
rule["do_not_enforce_on_create"] = params.DoNotEnforceOnCreate
rulesMap[v.Type] = []map[string]interface{}{rule}

case "merge_queue":
var params github.MergeQueueRuleParameters

Expand Down