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

feat : Support repository property for GitHub organization ruleset #2356

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
85 changes: 79 additions & 6 deletions github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Parameters for an organization ruleset condition. `ref_name` is required alongside one of `repository_name` or `repository_id`.",
Description: "Parameters for an organization ruleset condition. `ref_name` is required alongside one of `repository_name`, `repository_id`, or `repository_property`.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ref_name": {
Expand Down Expand Up @@ -112,12 +112,83 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
},
},
},
"repository_property": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: []string{"conditions.0.repository_id", "conditions.0.repository_name"},
AtLeastOneOf: []string{"conditions.0.repository_id", "conditions.0.repository_name"},
Description: "Conditions to target repositories by property ",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"include": {
Type: schema.TypeList,
Optional: true,
Description: "Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts `~ALL` to include all repositories.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the repository property to target",
},
"property_values": {
Type: schema.TypeList,
Required: true,
Description: "The values to match for the repository property.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Comment on lines +140 to +142
Copy link

@PaarthShah PaarthShah Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this handle properties with boolean values? From a json export which shows the use of string "false" and "true" I assume it does

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manualy tested the solution and can handle boelean custom_properties using strings

},
"source": {
Type: schema.TypeString,
Optional: true,
Description: "The source of the repository property. Defaults to 'custom' if not specified.Can be one of: custom, system",
Default: "custom",
},
},
},
},
"exclude": {
Type: schema.TypeList,
Optional: true,
Description: "Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the repository property to target",
},
"property_values": {
Type: schema.TypeList,
Required: true,
Description: "The values to match for the repository property.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"source": {
Type: schema.TypeString,
Optional: true,
Description: "The source of the repository property. Defaults to 'custom' if not specified.Can be one of: custom, system",
Default: "custom",
ValidateFunc: validation.StringInSlice([]string{"custom", "system"}, false),
},
},
},
},
},
},
},
"repository_name": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: []string{"conditions.0.repository_id"},
AtLeastOneOf: []string{"conditions.0.repository_id"},
ExactlyOneOf: []string{"conditions.0.repository_id", "conditions.0.repository_property"},
AtLeastOneOf: []string{"conditions.0.repository_id", "conditions.0.repository_property"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"include": {
Expand Down Expand Up @@ -146,9 +217,11 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
},
},
"repository_id": {
Type: schema.TypeList,
Optional: true,
Description: "The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.",
Type: schema.TypeList,
Optional: true,
ExactlyOneOf: []string{"conditions.0.repository_name", "conditions.0.repository_property"},
AtLeastOneOf: []string{"conditions.0.repository_name", "conditions.0.repository_property"},
Description: "The repository IDs that the ruleset applies to. One of these IDs must match for the condition to pass.",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Expand Down
68 changes: 65 additions & 3 deletions github/resource_github_organization_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGithubOrganizationRulesets(t *testing.T) {

t.Run("Creates and updates organization rulesets without errors", func(t *testing.T) {

config := fmt.Sprintf(`
rulesetRefName := fmt.Sprintf(`
resource "github_organization_ruleset" "test" {
name = "test-%s"
target = "branch"
Expand Down Expand Up @@ -81,6 +81,67 @@ func TestGithubOrganizationRulesets(t *testing.T) {
}
`, randomID)

rulesetRepositoryProperty := fmt.Sprintf(`
resource "github_organization_ruleset" "test" {
name = "test-%s"
target = "branch"
enforcement = "active"

conditions {
repository_property {
include = [ {
name: "team",
property_values: ["blue"],
}]
exclude = []
}
}

rules {
creation = true

update = true

deletion = true
required_linear_history = true

required_signatures = false

pull_request {
required_approving_review_count = 2
required_review_thread_resolution = true
require_code_owner_review = true
dismiss_stale_reviews_on_push = true
require_last_push_approval = true
}

required_status_checks {

required_check {
context = "ci"
}

strict_required_status_checks_policy = true
}

required_workflows {
required_workflow {
path = "path/to/workflow.yaml"
repository_id = 1234
}
}

branch_name_pattern {
name = "test"
negate = false
operator = "starts_with"
pattern = "test"
}

non_fast_forward = true
}
}
`, randomID)
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_ruleset.test", "name",
Expand All @@ -92,7 +153,7 @@ func TestGithubOrganizationRulesets(t *testing.T) {
),
)

testCase := func(t *testing.T, mode string) {
testCase := func(t *testing.T, mode string, config string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Expand All @@ -106,7 +167,8 @@ func TestGithubOrganizationRulesets(t *testing.T) {
}

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

})
Expand Down
Loading
Loading