|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/github/gh-aw/pkg/logger" |
| 9 | +) |
| 10 | + |
| 11 | +// preprocessBoolFieldAsString converts the value of a boolean config field |
| 12 | +// to a string before YAML unmarshaling. This lets struct fields typed as |
| 13 | +// *string accept both literal boolean values (true/false) and GitHub Actions |
| 14 | +// expression strings (e.g. "${{ inputs.draft-prs }}"). |
| 15 | +// |
| 16 | +// If the value is a bool it is converted to "true" or "false". |
| 17 | +// If the value is a string it must be a GitHub Actions expression (starts |
| 18 | +// with "${{" and ends with "}}"); any other free-form string is rejected |
| 19 | +// and an error is returned. |
| 20 | +func preprocessBoolFieldAsString(configData map[string]any, fieldName string, debugLog *logger.Logger) error { |
| 21 | + if configData == nil { |
| 22 | + return nil |
| 23 | + } |
| 24 | + if val, exists := configData[fieldName]; exists { |
| 25 | + switch v := val.(type) { |
| 26 | + case bool: |
| 27 | + if v { |
| 28 | + configData[fieldName] = "true" |
| 29 | + } else { |
| 30 | + configData[fieldName] = "false" |
| 31 | + } |
| 32 | + if debugLog != nil { |
| 33 | + debugLog.Printf("Converted %s bool to string before unmarshaling", fieldName) |
| 34 | + } |
| 35 | + case string: |
| 36 | + if !isExpression(v) { |
| 37 | + return fmt.Errorf("field %q must be a boolean or a GitHub Actions expression (e.g. '${{ inputs.flag }}'), got string %q", fieldName, v) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// preprocessIntFieldAsString converts the value of an integer config field |
| 45 | +// to a string before YAML unmarshaling. This lets struct fields typed as |
| 46 | +// *string accept both literal integer values and GitHub Actions expression |
| 47 | +// strings (e.g. "${{ inputs.max-issues }}"). |
| 48 | +// |
| 49 | +// If the value is an int, int64, float64, or uint64 it is converted to its |
| 50 | +// decimal string representation. |
| 51 | +// If the value is a string it must be a GitHub Actions expression (starts |
| 52 | +// with "${{" and ends with "}}"); any other free-form string is rejected |
| 53 | +// and an error is returned. |
| 54 | +func preprocessIntFieldAsString(configData map[string]any, fieldName string, debugLog *logger.Logger) error { |
| 55 | + if configData == nil { |
| 56 | + return nil |
| 57 | + } |
| 58 | + if val, exists := configData[fieldName]; exists { |
| 59 | + switch v := val.(type) { |
| 60 | + case int: |
| 61 | + configData[fieldName] = strconv.Itoa(v) |
| 62 | + if debugLog != nil { |
| 63 | + debugLog.Printf("Converted %s int to string before unmarshaling", fieldName) |
| 64 | + } |
| 65 | + case int64: |
| 66 | + configData[fieldName] = strconv.FormatInt(v, 10) |
| 67 | + if debugLog != nil { |
| 68 | + debugLog.Printf("Converted %s int64 to string before unmarshaling", fieldName) |
| 69 | + } |
| 70 | + case float64: |
| 71 | + configData[fieldName] = strconv.Itoa(int(v)) |
| 72 | + if debugLog != nil { |
| 73 | + debugLog.Printf("Converted %s float64 to string before unmarshaling", fieldName) |
| 74 | + } |
| 75 | + case uint64: |
| 76 | + configData[fieldName] = strconv.FormatUint(v, 10) |
| 77 | + if debugLog != nil { |
| 78 | + debugLog.Printf("Converted %s uint64 to string before unmarshaling", fieldName) |
| 79 | + } |
| 80 | + case string: |
| 81 | + if !isExpression(v) { |
| 82 | + return fmt.Errorf("field %q must be an integer or a GitHub Actions expression (e.g. '${{ inputs.max }}'), got string %q", fieldName, v) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// preprocessStringArrayFieldAsTemplatable handles a string-array config field that also |
| 90 | +// accepts a GitHub Actions expression string (e.g. "${{ inputs.labels }}"). |
| 91 | +// |
| 92 | +// When the field value is an expression string it is wrapped in a single-element []string |
| 93 | +// so that existing YAML struct-unmarshal code (which expects []string) continues to work |
| 94 | +// unchanged. The handler config builder then detects this single-element expression slice |
| 95 | +// and stores it as a JSON string rather than a JSON array, allowing GitHub Actions to |
| 96 | +// evaluate the expression at runtime before the config.json file is written. |
| 97 | +// |
| 98 | +// Free-form strings that are not GitHub Actions expressions are rejected with an error. |
| 99 | +// Array values ([]string, []any) are left untouched for the normal YAML unmarshal path. |
| 100 | +func preprocessStringArrayFieldAsTemplatable(configData map[string]any, fieldName string, debugLog *logger.Logger) error { |
| 101 | + if configData == nil { |
| 102 | + return nil |
| 103 | + } |
| 104 | + if val, exists := configData[fieldName]; exists { |
| 105 | + if s, ok := val.(string); ok { |
| 106 | + if !isExpression(s) { |
| 107 | + var exampleExpr string |
| 108 | + if strings.Contains(fieldName, "-") { |
| 109 | + exampleExpr = fmt.Sprintf("${{ inputs['%s'] }}", fieldName) |
| 110 | + } else { |
| 111 | + exampleExpr = fmt.Sprintf("${{ inputs.%s }}", fieldName) |
| 112 | + } |
| 113 | + return fmt.Errorf("field %q must be an array of strings or a GitHub Actions expression (e.g. '%s'), got string %q", fieldName, exampleExpr, s) |
| 114 | + } |
| 115 | + configData[fieldName] = []string{s} |
| 116 | + if debugLog != nil { |
| 117 | + debugLog.Printf("Wrapped %s expression string in single-element array before unmarshaling", fieldName) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// preprocessProtectedFilesField preprocesses the "protected-files" field in configData, |
| 125 | +// handling both the legacy string-enum form and the new object form. |
| 126 | +// |
| 127 | +// String form (unchanged): "blocked", "allowed", or "fallback-to-issue". |
| 128 | +// Object form: { policy: "blocked", exclude: ["AGENTS.md"] } |
| 129 | +// - policy is optional; when missing or empty, this preprocessing step treats it as absent |
| 130 | +// and leaves downstream default handling to apply (the "protected-files" key is deleted) |
| 131 | +// - exclude is a list of filenames/path-prefixes to remove from the default protected set |
| 132 | +// |
| 133 | +// When the object form is encountered the field is normalised in-place: |
| 134 | +// - "protected-files" is replaced with the extracted policy string, or deleted when policy is absent/empty |
| 135 | +// - The extracted exclude slice is returned so callers can store it in the config struct |
| 136 | +// |
| 137 | +// When the string form is encountered the field is left unchanged and nil is returned. |
| 138 | +// The debugLog parameter is optional; pass nil to suppress debug output. |
| 139 | +func preprocessProtectedFilesField(configData map[string]any, debugLog *logger.Logger) []string { |
| 140 | + if configData == nil { |
| 141 | + return nil |
| 142 | + } |
| 143 | + raw, exists := configData["protected-files"] |
| 144 | + if !exists || raw == nil { |
| 145 | + return nil |
| 146 | + } |
| 147 | + pfMap, ok := raw.(map[string]any) |
| 148 | + if !ok { |
| 149 | + return nil |
| 150 | + } |
| 151 | + if policy, ok := pfMap["policy"].(string); ok && policy != "" { |
| 152 | + configData["protected-files"] = policy |
| 153 | + if debugLog != nil { |
| 154 | + debugLog.Printf("protected-files object form: policy=%s", policy) |
| 155 | + } |
| 156 | + } else { |
| 157 | + delete(configData, "protected-files") |
| 158 | + if debugLog != nil { |
| 159 | + debugLog.Print("protected-files object form: no policy, using default") |
| 160 | + } |
| 161 | + } |
| 162 | + return parseStringSliceAny(pfMap["exclude"], debugLog) |
| 163 | +} |
| 164 | + |
| 165 | +// preprocessExpiresField handles the common expires field preprocessing pattern. |
| 166 | +// This function: |
| 167 | +// 1. Parses the expires value through parseExpiresFromConfig (handles integers, strings, and boolean false) |
| 168 | +// 2. Handles explicit disablement when expires=false (returns -1) |
| 169 | +// 3. Normalizes the value to hours and updates configData["expires"] in place |
| 170 | +// 4. Logs the parsed value with the provided logger |
| 171 | +// |
| 172 | +// Returns true if expires was explicitly disabled with false, false otherwise. |
| 173 | +// This helper consolidates duplicate preprocessing logic used in parseCreateIssuesConfig and parseCreateDiscussionsConfig. |
| 174 | +func preprocessExpiresField(configData map[string]any, debugLog *logger.Logger) bool { |
| 175 | + expiresDisabled := false |
| 176 | + if configData != nil { |
| 177 | + if expires, exists := configData["expires"]; exists { |
| 178 | + expiresInt := parseExpiresFromConfig(configData) |
| 179 | + if expiresInt == -1 { |
| 180 | + expiresDisabled = true |
| 181 | + configData["expires"] = 0 |
| 182 | + } else if expiresInt > 0 { |
| 183 | + configData["expires"] = expiresInt |
| 184 | + } else { |
| 185 | + configData["expires"] = 0 |
| 186 | + } |
| 187 | + if debugLog != nil { |
| 188 | + debugLog.Printf("Parsed expires value %v to %d hours (disabled=%t)", expires, expiresInt, expiresDisabled) |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + return expiresDisabled |
| 193 | +} |
0 commit comments