From fbdb6880058e2db75147ba460ca7da4f258130cb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 11 Nov 2025 05:11:41 +0000 Subject: [PATCH 1/2] Initial plan From 86866b021bdecf5d819719ab2062be8fe154c4dd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 11 Nov 2025 05:21:18 +0000 Subject: [PATCH 2/2] Create helper files and move scattered helper functions - Created pkg/workflow/config_helpers.go with config parsing functions - Created pkg/workflow/frontmatter_helpers.go with frontmatter utilities - Moved parseLabelsFromConfig, parseTitlePrefixFromConfig, parseTargetRepoFromConfig, parseStringFromConfig from config.go - Moved extractStringValue, parseIntValue, filterMapKeys from frontmatter_extraction.go - All tests passing (make test-unit) - Build successful (make build) - Code formatted and linted (make fmt, make lint) Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --- pkg/workflow/config.go | 57 +------------------------- pkg/workflow/config_helpers.go | 57 ++++++++++++++++++++++++++ pkg/workflow/frontmatter_extraction.go | 47 +-------------------- pkg/workflow/frontmatter_helpers.go | 48 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 101 deletions(-) create mode 100644 pkg/workflow/config_helpers.go create mode 100644 pkg/workflow/frontmatter_helpers.go diff --git a/pkg/workflow/config.go b/pkg/workflow/config.go index 40cd9dd8dd2..4e775849723 100644 --- a/pkg/workflow/config.go +++ b/pkg/workflow/config.go @@ -1,57 +1,4 @@ package workflow -import ( - "github.com/githubnext/gh-aw/pkg/logger" -) - -var configLog = logger.New("workflow:config") - -// parseLabelsFromConfig extracts and validates labels from a config map -// Returns a slice of label strings, or nil if labels is not present or invalid -func parseLabelsFromConfig(configMap map[string]any) []string { - if labels, exists := configMap["labels"]; exists { - configLog.Print("Parsing labels from config") - if labelsArray, ok := labels.([]any); ok { - var labelStrings []string - for _, label := range labelsArray { - if labelStr, ok := label.(string); ok { - labelStrings = append(labelStrings, labelStr) - } - } - // Return the slice even if empty (to distinguish from not provided) - if labelStrings == nil { - configLog.Print("No valid label strings found, returning empty array") - return []string{} - } - configLog.Printf("Parsed %d labels from config", len(labelStrings)) - return labelStrings - } - } - return nil -} - -// parseStringFromConfig is a generic helper that extracts and validates a string value from a config map -// Returns the string value, or empty string if not present or invalid -func parseStringFromConfig(configMap map[string]any, key string) string { - if value, exists := configMap[key]; exists { - if valueStr, ok := value.(string); ok { - configLog.Printf("Parsed %s from config: %s", key, valueStr) - return valueStr - } - } - return "" -} - -// parseTitlePrefixFromConfig extracts and validates title-prefix from a config map -// Returns the title prefix string, or empty string if not present or invalid -func parseTitlePrefixFromConfig(configMap map[string]any) string { - return parseStringFromConfig(configMap, "title-prefix") -} - -// parseTargetRepoFromConfig extracts the target-repo value from a config map. -// Returns the target repository slug as a string, or empty string if not present or invalid. -// This function does not perform any special handling or validation for wildcard values ("*"); -// callers are responsible for validating the returned value as needed. -func parseTargetRepoFromConfig(configMap map[string]any) string { - return parseStringFromConfig(configMap, "target-repo") -} +// Configuration parsing helpers have been moved to config_helpers.go +// This file is kept as a placeholder for any future config-related functionality diff --git a/pkg/workflow/config_helpers.go b/pkg/workflow/config_helpers.go new file mode 100644 index 00000000000..b0531db272d --- /dev/null +++ b/pkg/workflow/config_helpers.go @@ -0,0 +1,57 @@ +package workflow + +import ( + "github.com/githubnext/gh-aw/pkg/logger" +) + +var configHelpersLog = logger.New("workflow:config_helpers") + +// parseLabelsFromConfig extracts and validates labels from a config map +// Returns a slice of label strings, or nil if labels is not present or invalid +func parseLabelsFromConfig(configMap map[string]any) []string { + if labels, exists := configMap["labels"]; exists { + configHelpersLog.Print("Parsing labels from config") + if labelsArray, ok := labels.([]any); ok { + var labelStrings []string + for _, label := range labelsArray { + if labelStr, ok := label.(string); ok { + labelStrings = append(labelStrings, labelStr) + } + } + // Return the slice even if empty (to distinguish from not provided) + if labelStrings == nil { + configHelpersLog.Print("No valid label strings found, returning empty array") + return []string{} + } + configHelpersLog.Printf("Parsed %d labels from config", len(labelStrings)) + return labelStrings + } + } + return nil +} + +// parseStringFromConfig is a generic helper that extracts and validates a string value from a config map +// Returns the string value, or empty string if not present or invalid +func parseStringFromConfig(configMap map[string]any, key string) string { + if value, exists := configMap[key]; exists { + if valueStr, ok := value.(string); ok { + configHelpersLog.Printf("Parsed %s from config: %s", key, valueStr) + return valueStr + } + } + return "" +} + +// parseTitlePrefixFromConfig extracts and validates title-prefix from a config map +// Returns the title prefix string, or empty string if not present or invalid +func parseTitlePrefixFromConfig(configMap map[string]any) string { + return parseStringFromConfig(configMap, "title-prefix") +} + +// parseTargetRepoFromConfig extracts the target-repo value from a config map. +// Returns the target repository slug as a string, or empty string if not present or invalid. +// This function does not perform any special handling or validation for wildcard values ("*"); +// callers are responsible for validating the returned value as needed. +func parseTargetRepoFromConfig(configMap map[string]any) string { + return parseStringFromConfig(configMap, "target-repo") +} diff --git a/pkg/workflow/frontmatter_extraction.go b/pkg/workflow/frontmatter_extraction.go index f34ca0d41ce..0e6faa7ad55 100644 --- a/pkg/workflow/frontmatter_extraction.go +++ b/pkg/workflow/frontmatter_extraction.go @@ -11,36 +11,7 @@ import ( var frontmatterLog = logger.New("workflow:frontmatter_extraction") -// extractStringValue extracts a string value from the frontmatter map -func extractStringValue(frontmatter map[string]any, key string) string { - value, exists := frontmatter[key] - if !exists { - return "" - } - - if strValue, ok := value.(string); ok { - return strValue - } - - return "" -} - -// parseIntValue safely parses various numeric types to int -// This is a common utility used across multiple parsing functions -func parseIntValue(value any) (int, bool) { - switch v := value.(type) { - case int: - return v, true - case int64: - return int(v), true - case uint64: - return int(v), true - case float64: - return int(v), true - default: - return 0, false - } -} +// Note: extractStringValue, parseIntValue, and filterMapKeys have been moved to frontmatter_helpers.go // addCustomSafeOutputEnvVars adds custom environment variables to safe output job steps func (c *Compiler) addCustomSafeOutputEnvVars(steps *[]string, data *WorkflowData) { @@ -86,22 +57,6 @@ func (c *Compiler) addSafeOutputCopilotGitHubTokenForConfig(steps *[]string, dat *steps = append(*steps, fmt.Sprintf(" github-token: %s\n", effectiveToken)) } -// filterMapKeys creates a new map excluding the specified keys -func filterMapKeys(original map[string]any, excludeKeys ...string) map[string]any { - excludeSet := make(map[string]bool) - for _, key := range excludeKeys { - excludeSet[key] = true - } - - result := make(map[string]any) - for key, value := range original { - if !excludeSet[key] { - result[key] = value - } - } - return result -} - // extractYAMLValue extracts a scalar value from the frontmatter map func (c *Compiler) extractYAMLValue(frontmatter map[string]any, key string) string { if value, exists := frontmatter[key]; exists { diff --git a/pkg/workflow/frontmatter_helpers.go b/pkg/workflow/frontmatter_helpers.go new file mode 100644 index 00000000000..00fe24298c1 --- /dev/null +++ b/pkg/workflow/frontmatter_helpers.go @@ -0,0 +1,48 @@ +package workflow + +// extractStringValue extracts a string value from the frontmatter map +func extractStringValue(frontmatter map[string]any, key string) string { + value, exists := frontmatter[key] + if !exists { + return "" + } + + if strValue, ok := value.(string); ok { + return strValue + } + + return "" +} + +// parseIntValue safely parses various numeric types to int +// This is a common utility used across multiple parsing functions +func parseIntValue(value any) (int, bool) { + switch v := value.(type) { + case int: + return v, true + case int64: + return int(v), true + case uint64: + return int(v), true + case float64: + return int(v), true + default: + return 0, false + } +} + +// filterMapKeys creates a new map excluding the specified keys +func filterMapKeys(original map[string]any, excludeKeys ...string) map[string]any { + excludeSet := make(map[string]bool) + for _, key := range excludeKeys { + excludeSet[key] = true + } + + result := make(map[string]any) + for key, value := range original { + if !excludeSet[key] { + result[key] = value + } + } + return result +}