|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +// branchTagOnlyRules contains rules that are only valid for branch and tag targets. |
| 13 | +// |
| 14 | +// These rules apply to ref-based operations (branches and tags) and are not supported |
| 15 | +// for push rulesets which operate on file content. |
| 16 | +// |
| 17 | +// To verify/maintain this list: |
| 18 | +// 1. Check the GitHub API documentation for organization rulesets: |
| 19 | +// https://docs.github.com/en/rest/orgs/rules?apiVersion=2022-11-28#create-an-organization-repository-ruleset |
| 20 | +// 2. The API docs don't clearly separate push vs branch/tag rules. To verify, |
| 21 | +// attempt to create a push ruleset via API or UI with each rule type. |
| 22 | +// Push rulesets will reject branch/tag rules with "Invalid rule '<name>'" error. |
| 23 | +// 3. Generally, push rules deal with file content (paths, sizes, extensions), |
| 24 | +// while branch/tag rules deal with ref lifecycle and merge requirements. |
| 25 | +var branchTagOnlyRules = []string{ |
| 26 | + "creation", |
| 27 | + "update", |
| 28 | + "deletion", |
| 29 | + "required_linear_history", |
| 30 | + "required_signatures", |
| 31 | + "pull_request", |
| 32 | + "required_status_checks", |
| 33 | + "non_fast_forward", |
| 34 | + "commit_message_pattern", |
| 35 | + "commit_author_email_pattern", |
| 36 | + "committer_email_pattern", |
| 37 | + "branch_name_pattern", |
| 38 | + "tag_name_pattern", |
| 39 | + "required_workflows", |
| 40 | + "required_code_scanning", |
| 41 | + "required_deployments", |
| 42 | + "merge_queue", |
| 43 | +} |
| 44 | + |
| 45 | +// pushOnlyRules contains rules that are only valid for push targets. |
| 46 | +// |
| 47 | +// These rules apply to push operations and control what content can be pushed |
| 48 | +// to repositories. They are not supported for branch or tag rulesets. |
| 49 | +// |
| 50 | +// To verify/maintain this list: |
| 51 | +// 1. Check the GitHub API documentation for organization rulesets: |
| 52 | +// https://docs.github.com/en/rest/orgs/rules?apiVersion=2022-11-28#create-an-organization-repository-ruleset |
| 53 | +// 2. The API docs don't clearly separate push vs branch/tag rules. To verify, |
| 54 | +// attempt to create a branch ruleset via API or UI with each rule type. |
| 55 | +// Branch rulesets will reject push-only rules with an error. |
| 56 | +// 3. Push rules control file content: paths, sizes, extensions, path lengths. |
| 57 | +var pushOnlyRules = []string{ |
| 58 | + "file_path_restriction", |
| 59 | + "max_file_path_length", |
| 60 | + "file_extension_restriction", |
| 61 | + "max_file_size", |
| 62 | +} |
| 63 | + |
| 64 | +func validateRulesForTarget(ctx context.Context, d *schema.ResourceDiff) error { |
| 65 | + target := d.Get("target").(string) |
| 66 | + tflog.Debug(ctx, "Validating rules for target", map[string]any{"target": target}) |
| 67 | + |
| 68 | + switch target { |
| 69 | + case "push": |
| 70 | + return validateRulesForPushTarget(ctx, d) |
| 71 | + case "branch", "tag": |
| 72 | + return validateRulesForBranchTagTarget(ctx, d) |
| 73 | + } |
| 74 | + |
| 75 | + tflog.Debug(ctx, "Rules validation passed", map[string]any{"target": target}) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func validateRulesForPushTarget(ctx context.Context, d *schema.ResourceDiff) error { |
| 80 | + return validateRules(ctx, d, pushOnlyRules) |
| 81 | +} |
| 82 | + |
| 83 | +func validateRulesForBranchTagTarget(ctx context.Context, d *schema.ResourceDiff) error { |
| 84 | + return validateRules(ctx, d, branchTagOnlyRules) |
| 85 | +} |
| 86 | + |
| 87 | +func validateRules(ctx context.Context, d *schema.ResourceDiff, allowedRules []string) error { |
| 88 | + target := d.Get("target").(string) |
| 89 | + rules := d.Get("rules").([]any)[0].(map[string]any) |
| 90 | + for ruleName := range rules { |
| 91 | + ruleValue, exists := d.GetOk(fmt.Sprintf("rules.0.%s", ruleName)) |
| 92 | + if !exists { |
| 93 | + continue |
| 94 | + } |
| 95 | + switch ruleValue := ruleValue.(type) { |
| 96 | + case []any: |
| 97 | + if len(ruleValue) == 0 { |
| 98 | + continue |
| 99 | + } |
| 100 | + case map[string]any: |
| 101 | + if len(ruleValue) == 0 { |
| 102 | + continue |
| 103 | + } |
| 104 | + case any: |
| 105 | + if ruleValue == nil { |
| 106 | + continue |
| 107 | + } |
| 108 | + } |
| 109 | + if slices.Contains(allowedRules, ruleName) { |
| 110 | + continue |
| 111 | + } else { |
| 112 | + tflog.Debug(ctx, fmt.Sprintf("Invalid rule for %s target", target), map[string]any{"rule": ruleName, "value": ruleValue}) |
| 113 | + return fmt.Errorf("rule %q is not valid for %[2]s target; %[2]s targets only support: %v", ruleName, target, allowedRules) |
| 114 | + } |
| 115 | + } |
| 116 | + tflog.Debug(ctx, fmt.Sprintf("Rules validation passed for %s target", target)) |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func validateRepositoryRulesetConditionsFieldForBranchAndTagTargets(ctx context.Context, target string, conditions map[string]any) error { |
| 121 | + tflog.Debug(ctx, fmt.Sprintf("Validating conditions field for %s target", target), map[string]any{"target": target, "conditions": conditions}) |
| 122 | + |
| 123 | + if conditions["ref_name"] == nil || len(conditions["ref_name"].([]any)) == 0 { |
| 124 | + tflog.Debug(ctx, fmt.Sprintf("Missing ref_name for %s target", target), map[string]any{"target": target}) |
| 125 | + return fmt.Errorf("ref_name must be set for %s target", target) |
| 126 | + } |
| 127 | + |
| 128 | + tflog.Debug(ctx, fmt.Sprintf("Conditions validation passed for %s target", target)) |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func validateConditionsFieldForBranchAndTagTargets(ctx context.Context, target string, conditions map[string]any) error { |
| 133 | + tflog.Debug(ctx, fmt.Sprintf("Validating conditions field for %s target", target), map[string]any{"target": target, "conditions": conditions}) |
| 134 | + |
| 135 | + if conditions["ref_name"] == nil || len(conditions["ref_name"].([]any)) == 0 { |
| 136 | + tflog.Debug(ctx, fmt.Sprintf("Missing ref_name for %s target", target), map[string]any{"target": target}) |
| 137 | + return fmt.Errorf("ref_name must be set for %s target", target) |
| 138 | + } |
| 139 | + |
| 140 | + if (conditions["repository_name"] == nil || len(conditions["repository_name"].([]any)) == 0) && (conditions["repository_id"] == nil || len(conditions["repository_id"].([]any)) == 0) { |
| 141 | + tflog.Debug(ctx, fmt.Sprintf("Missing repository_name or repository_id for %s target", target), map[string]any{"target": target}) |
| 142 | + return fmt.Errorf("either repository_name or repository_id must be set for %s target", target) |
| 143 | + } |
| 144 | + tflog.Debug(ctx, fmt.Sprintf("Conditions validation passed for %s target", target)) |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func validateConditionsFieldForPushTarget(ctx context.Context, conditions map[string]any) error { |
| 149 | + tflog.Debug(ctx, "Validating conditions field for push target", map[string]any{"target": "push", "conditions": conditions}) |
| 150 | + |
| 151 | + if conditions["ref_name"] != nil && len(conditions["ref_name"].([]any)) > 0 { |
| 152 | + tflog.Debug(ctx, "Invalid ref_name for push target", map[string]any{"ref_name": conditions["ref_name"]}) |
| 153 | + return fmt.Errorf("ref_name must not be set for push target") |
| 154 | + } |
| 155 | + tflog.Debug(ctx, "Conditions validation passed for push target") |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func validateConditionsFieldForRepositoryTarget(ctx context.Context, conditions map[string]any) error { |
| 160 | + tflog.Debug(ctx, "Validating conditions field for repository target", map[string]any{"target": "repository", "conditions": conditions}) |
| 161 | + |
| 162 | + if conditions["ref_name"] != nil && len(conditions["ref_name"].([]any)) > 0 { |
| 163 | + tflog.Debug(ctx, "Invalid ref_name for repository target", map[string]any{"ref_name": conditions["ref_name"]}) |
| 164 | + return fmt.Errorf("ref_name must not be set for repository target") |
| 165 | + } |
| 166 | + if conditions["repository_name"] == nil || len(conditions["repository_name"].([]any)) == 0 || conditions["repository_id"] == nil || len(conditions["repository_id"].([]any)) == 0 { |
| 167 | + return fmt.Errorf("one of repository_name or repository_id must be set for repository target") |
| 168 | + } |
| 169 | + tflog.Debug(ctx, "Conditions validation passed for repository target") |
| 170 | + return nil |
| 171 | +} |
0 commit comments