Skip to content

Commit 02ecc66

Browse files
committed
Support repository_property in push rulesets
1 parent 7245e32 commit 02ecc66

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

github/resource_github_organization_ruleset.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/go-github/v82/github"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1718
)
@@ -30,7 +31,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
3031

3132
SchemaVersion: 1,
3233

33-
CustomizeDiff: resourceGithubOrganizationRulesetDiff,
34+
CustomizeDiff: resourceGithubOrganizationRulesetDiff,
3435

3536
Schema: map[string]*schema.Schema{
3637
"name": {

github/resource_github_repository_ruleset.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/go-github/v82/github"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1718
)
@@ -104,7 +105,7 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
104105
Schema: map[string]*schema.Schema{
105106
"ref_name": {
106107
Type: schema.TypeList,
107-
Required: true,
108+
Optional: true,
108109
MaxItems: 1,
109110
Elem: &schema.Resource{
110111
Schema: map[string]*schema.Schema{

github/util_ruleset_validation.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,26 @@ func validateConditionsFieldForBranchAndTagTargets(ctx context.Context, target g
171171
return fmt.Errorf("ref_name must be set for %s target", target)
172172
}
173173

174-
// Repository rulesets don't have repository_name or repository_id, only org rulesets do.
174+
// Repository rulesets don't have repository_name, repository_id, or repository_property - only org rulesets do.
175175
if isOrg {
176-
if (conditions["repository_name"] == nil || len(conditions["repository_name"].([]any)) == 0) && (conditions["repository_id"] == nil || len(conditions["repository_id"].([]any)) == 0) {
177-
tflog.Debug(ctx, fmt.Sprintf("Missing repository_name or repository_id for %s target", target), map[string]any{"target": target})
178-
return fmt.Errorf("either repository_name or repository_id must be set for %s target", target)
176+
hasRepoName := conditions["repository_name"] != nil && len(conditions["repository_name"].([]any)) > 0
177+
hasRepoId := conditions["repository_id"] != nil && len(conditions["repository_id"].([]any)) > 0
178+
hasRepoProp := conditions["repository_property"] != nil && len(conditions["repository_property"].([]any)) > 0
179+
180+
repoTargetingCount := 0
181+
if hasRepoName {
182+
repoTargetingCount++
183+
}
184+
if hasRepoId {
185+
repoTargetingCount++
186+
}
187+
if hasRepoProp {
188+
repoTargetingCount++
189+
}
190+
191+
if repoTargetingCount != 1 {
192+
tflog.Debug(ctx, fmt.Sprintf("Invalid repository targeting for %s target", target), map[string]any{"target": target})
193+
return fmt.Errorf("exactly one of repository_name, repository_id, or repository_property must be set for %s target", target)
179194
}
180195
}
181196
tflog.Debug(ctx, fmt.Sprintf("Conditions validation passed for %s target", target))

0 commit comments

Comments
 (0)