11package settings
22
33import (
4+ "encoding/json"
45 "fmt"
56 "net/url"
67 "regexp"
@@ -64,11 +65,11 @@ type Settings struct {
6465 SecretGHAppRepoScoped bool `default:"true" json:"secret-github-app-token-scoped"`
6566 SecretGhAppTokenScopedExtraRepos string `json:"secret-github-app-scope-extra-repos"`
6667
67- ErrorLogSnippet bool `default:"true" json:"error-log-snippet"`
68- ErrorLogSnippetNumberOfLines int `default:"3" json:"error-log-snippet-number-of-lines"`
69- ErrorDetection bool `default:"true" json:"error-detection-from-container-logs"`
70- ErrorDetectionNumberOfLines int `default:"50" json:"error-detection-max-number-of-lines"`
71- ErrorDetectionSimpleRegexp string `default:"^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)" json:"error-detection-simple-regexp"`
68+ ErrorLogSnippet bool `default:"true" json:"error-log-snippet"`
69+ ErrorLogSnippetNumberOfLines int `default:"3" json:"error-log-snippet-number-of-lines"`
70+ ErrorDetection bool `default:"true" json:"error-detection-from-container-logs"`
71+ ErrorDetectionNumberOfLines int `default:"50" json:"error-detection-max-number-of-lines"`
72+ ErrorDetectionSimpleRegexp [] string `default:"^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)"` // Note: no json tag, handled specially
7273
7374 EnableCancelInProgressOnPullRequests bool `json:"enable-cancel-in-progress-on-pull-requests"`
7475 EnableCancelInProgressOnPush bool `json:"enable-cancel-in-progress-on-push"`
@@ -105,11 +106,10 @@ func DefaultSettings() Settings {
105106
106107func DefaultValidators () map [string ]func (string ) error {
107108 return map [string ]func (string ) error {
108- "ErrorDetectionSimpleRegexp" : isValidRegex ,
109- "TektonDashboardURL" : isValidURL ,
110- "CustomConsoleURL" : isValidURL ,
111- "CustomConsolePRTaskLog" : startWithHTTPorHTTPS ,
112- "CustomConsolePRDetail" : startWithHTTPorHTTPS ,
109+ "TektonDashboardURL" : isValidURL ,
110+ "CustomConsoleURL" : isValidURL ,
111+ "CustomConsolePRTaskLog" : startWithHTTPorHTTPS ,
112+ "CustomConsolePRDetail" : startWithHTTPorHTTPS ,
113113 }
114114}
115115
@@ -121,6 +121,13 @@ func SyncConfig(logger *zap.SugaredLogger, setting *Settings, config map[string]
121121 return fmt .Errorf ("failed to validate and assign values: %w" , err )
122122 }
123123
124+ // Parse error detection patterns (supports backward compatibility)
125+ // Handling error detection specially as we want to keep backward compatibility while
126+ // allowing []string as a valid input value.
127+ if err := parseErrorDetectionPatterns (setting , config ); err != nil {
128+ return fmt .Errorf ("failed to parse error detection patterns: %w" , err )
129+ }
130+
124131 value , _ := setting .HubCatalogs .Load ("default" )
125132 catalogDefault , ok := value .(HubCatalog )
126133 if ok {
@@ -159,3 +166,61 @@ func startWithHTTPorHTTPS(url string) error {
159166 }
160167 return nil
161168}
169+
170+ // parseErrorDetectionPatterns parses the error-detection-simple-regexp from ConfigMap
171+ // and populates ErrorDetectionSimpleRegexp as []string. It supports:
172+ // 1. Single string pattern (backward compatible)
173+ // 2. JSON array format: ["pattern1", "pattern2"]
174+ // 3. Newline-separated patterns (YAML multi-line).
175+ func parseErrorDetectionPatterns (setting * Settings , config map [string ]string ) error {
176+ regexpValue := config ["error-detection-simple-regexp" ]
177+
178+ // If not in config, use default
179+ if regexpValue == "" {
180+ regexpValue = "^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)"
181+ }
182+
183+ regexpValue = strings .TrimSpace (regexpValue )
184+
185+ // Try to parse as JSON array first
186+ if strings .HasPrefix (regexpValue , "[" ) && strings .HasSuffix (regexpValue , "]" ) {
187+ var patterns []string
188+ if err := json .Unmarshal ([]byte (regexpValue ), & patterns ); err == nil {
189+ // Validate each pattern
190+ for _ , pattern := range patterns {
191+ if err := isValidRegex (pattern ); err != nil {
192+ return fmt .Errorf ("invalid regex in array: %w" , err )
193+ }
194+ }
195+ setting .ErrorDetectionSimpleRegexp = patterns
196+ return nil
197+ }
198+ }
199+
200+ // Check if it contains newlines (multi-line YAML format)
201+ if strings .Contains (regexpValue , "\n " ) {
202+ lines := strings .Split (regexpValue , "\n " )
203+ var patterns []string
204+ for _ , line := range lines {
205+ line = strings .TrimSpace (line )
206+ if line == "" {
207+ continue
208+ }
209+ if err := isValidRegex (line ); err != nil {
210+ return fmt .Errorf ("invalid regex in multi-line format: %w" , err )
211+ }
212+ patterns = append (patterns , line )
213+ }
214+ if len (patterns ) > 0 {
215+ setting .ErrorDetectionSimpleRegexp = patterns
216+ return nil
217+ }
218+ }
219+
220+ // Single pattern (backward compatible)
221+ if err := isValidRegex (regexpValue ); err != nil {
222+ return err
223+ }
224+ setting .ErrorDetectionSimpleRegexp = []string {regexpValue }
225+ return nil
226+ }
0 commit comments