Skip to content
Merged
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
42 changes: 42 additions & 0 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,48 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
Default: false,
Description: "All conversations on code must be resolved before a pull request can be merged. Defaults to `false`.",
},
"required_reviewers": {
Type: schema.TypeList,
Optional: true,
Description: "Require specific reviewers to approve pull requests targeting matching branches. Note: This feature is in beta and subject to change.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reviewer": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "The reviewer that must review matching files.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the reviewer that must review.",
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"Team"}, false), "type"),
Description: "The type of reviewer. Currently only `Team` is supported.",
},
},
},
},
"file_patterns": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "File patterns (fnmatch syntax) that this reviewer must approve.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"minimum_approvals": {
Type: schema.TypeInt,
Required: true,
Description: "Minimum number of approvals required from this reviewer. Set to 0 to make approval optional.",
},
},
},
},
},
},
},
Expand Down
159 changes: 159 additions & 0 deletions github/resource_github_organization_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,165 @@ resource "github_organization_ruleset" "test" {
})
})
})

t.Run("updates_required_reviewers", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName := fmt.Sprintf("%steam-req-rev-%s", testResourcePrefix, randomID)
rulesetName := fmt.Sprintf("%s-ruleset-req-rev-%s", testResourcePrefix, randomID)

config := `
resource "github_team" "test" {
name = "%s"
}

resource "github_organization_ruleset" "test" {
name = "%s"
target = "branch"
enforcement = "active"

conditions {
repository_name {
include = ["~ALL"]
exclude = []
}

ref_name {
include = ["~ALL"]
exclude = []
}
}

rules {
pull_request {
allowed_merge_methods = ["merge", "squash"]
required_approving_review_count = 1

required_reviewers {
reviewer {
id = github_team.test.id
type = "Team"
}
file_patterns = ["*.go", "src/**/*.ts"]
minimum_approvals = %d
}
}
}
}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(config, teamName, rulesetName, 1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", rulesetName),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "target", "branch"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "enforcement", "active"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.#", "1"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.minimum_approvals", "1"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.file_patterns.#", "2"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.file_patterns.0", "*.go"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.file_patterns.1", "src/**/*.ts"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.reviewer.0.type", "Team"),
),
},
{
Config: fmt.Sprintf(config, teamName, rulesetName, 2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.minimum_approvals", "2"),
),
},
{
ResourceName: "github_organization_ruleset.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag"},
},
},
})
})
t.Run("creates_rule_with_multiple_required_reviewers", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
teamName1 := fmt.Sprintf("%steam-req-rev-1-%s", testResourcePrefix, randomID)
teamName2 := fmt.Sprintf("%steam-req-rev-2-%s", testResourcePrefix, randomID)
rulesetName := fmt.Sprintf("%s-ruleset-multi-rev-%s", testResourcePrefix, randomID)

config := fmt.Sprintf(`
resource "github_team" "test1" {
name = "%s"
}

resource "github_team" "test2" {
name = "%s"
}

resource "github_organization_ruleset" "test" {
name = "%s"
target = "branch"
enforcement = "active"

conditions {
repository_name {
include = ["~ALL"]
exclude = []
}

ref_name {
include = ["~ALL"]
exclude = []
}
}

rules {
pull_request {
allowed_merge_methods = ["merge", "squash"]
required_approving_review_count = 1

required_reviewers {
reviewer {
id = github_team.test1.id
type = "Team"
}
file_patterns = ["*.go"]
minimum_approvals = 1
}

required_reviewers {
reviewer {
id = github_team.test2.id
type = "Team"
}
file_patterns = ["*.md", "docs/**/*"]
minimum_approvals = 1
}
}
}
}
`, teamName1, teamName2, rulesetName)

resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", rulesetName),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "target", "branch"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "enforcement", "active"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.#", "2"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.minimum_approvals", "1"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.file_patterns.#", "1"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.0.file_patterns.0", "*.go"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.1.minimum_approvals", "1"),
resource.TestCheckResourceAttr("github_organization_ruleset.test", "rules.0.pull_request.0.required_reviewers.1.file_patterns.#", "2"),
),
},
},
})
})
}

func TestOrganizationPushRulesetSupport(t *testing.T) {
Expand Down
42 changes: 42 additions & 0 deletions github/resource_github_repository_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,48 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
Default: false,
Description: "All conversations on code must be resolved before a pull request can be merged. Defaults to `false`.",
},
"required_reviewers": {
Type: schema.TypeList,
Optional: true,
Description: "Require specific reviewers to approve pull requests targeting matching branches. Note: This feature is in beta and subject to change.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reviewer": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Description: "The reviewer that must review matching files.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the reviewer that must review.",
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: toDiagFunc(validation.StringInSlice([]string{"Team"}, false), "type"),
Description: "The type of reviewer. Currently only `Team` is supported.",
},
},
},
},
"file_patterns": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "File patterns (fnmatch syntax) that this reviewer must approve.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"minimum_approvals": {
Type: schema.TypeInt,
Required: true,
Description: "Minimum number of approvals required from this reviewer. Set to 0 to make approval optional.",
},
},
},
},
},
},
},
Expand Down
Loading