Skip to content

Refactor: Extract scattered helper functions into dedicated files#3601

Merged
pelikhan merged 2 commits intomainfrom
copilot/organize-helper-functions
Nov 11, 2025
Merged

Refactor: Extract scattered helper functions into dedicated files#3601
pelikhan merged 2 commits intomainfrom
copilot/organize-helper-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 11, 2025

Generic helper functions were scattered across config.go and frontmatter_extraction.go without clear organizational pattern, making them difficult to discover and maintain.

Changes

Created helper files:

  • pkg/workflow/config_helpers.go - Configuration parsing helpers (parseLabelsFromConfig, parseTitlePrefixFromConfig, parseTargetRepoFromConfig, parseStringFromConfig)
  • pkg/workflow/frontmatter_helpers.go - Frontmatter utilities (extractStringValue, parseIntValue, filterMapKeys)

Cleaned up original files:

  • pkg/workflow/config.go - Removed helper functions (58→4 lines)
  • pkg/workflow/frontmatter_extraction.go - Removed generic utilities, retained compiler-specific methods

All functions remain in the workflow package with unchanged signatures. Existing test coverage maintained without modification.

Example

// Before: helpers mixed with other logic in config.go
func parseLabelsFromConfig(configMap map[string]any) []string { ... }

// After: organized in dedicated config_helpers.go
// Usage unchanged - same package, same signature
labels := parseLabelsFromConfig(configMap)

Related to #3435

Original prompt

This section details on the original issue you should resolve

<issue_title>[task] Organize scattered helper functions into dedicated helper files</issue_title>
<issue_description>## Objective
Create dedicated helper files and move generic helper functions from various files to improve code organization and reduce duplication.

Context

Generic helper functions are distributed across multiple files without clear organizational pattern. Examples include configuration parsing helpers in config.go and generic utilities in frontmatter_extraction.go.

Part of issue #3435.

Approach

1. Create helper files

  • pkg/workflow/config_helpers.go - Configuration parsing helpers
  • pkg/workflow/frontmatter_helpers.go - Frontmatter extraction utilities

2. Move configuration parsing helpers from config.go

Move these functions to config_helpers.go:

  • parseLabelsFromConfig(configMap map[string]any) []string
  • parseTitlePrefixFromConfig(configMap map[string]any) string
  • parseTargetRepoFromConfig(configMap map[string]any) string
  • Any other similar config parsing helper functions

3. Move frontmatter utilities from frontmatter_extraction.go

Move these functions to frontmatter_helpers.go:

  • extractStringValue(frontmatter map[string]any, key string) string
  • parseIntValue(value any) (int, bool)
  • filterMapKeys(m map[string]any, keysToKeep []string) map[string]any
  • Any other similar generic utility functions

4. Update imports and references

  • Update all files that call these helper functions
  • Ensure no circular dependencies
  • Verify helper functions are exported (capitalized) if needed across packages

Files to Modify

Create:

  • pkg/workflow/config_helpers.go
  • pkg/workflow/frontmatter_helpers.go

Update:

  • pkg/workflow/config.go (remove moved functions)
  • pkg/workflow/frontmatter_extraction.go (remove moved functions)
  • Any files that import these helper functions
  • Update test files as needed

Acceptance Criteria

AI generated by Plan Command for #3435</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- 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>
Copilot AI changed the title [WIP] Organize helper functions into dedicated files Refactor: Extract scattered helper functions into dedicated files Nov 11, 2025
Copilot AI requested a review from pelikhan November 11, 2025 05:25
@pelikhan pelikhan marked this pull request as ready for review November 11, 2025 05:28
Copilot AI review requested due to automatic review settings November 11, 2025 05:28
@pelikhan pelikhan merged commit 37d3fe7 into main Nov 11, 2025
97 checks passed
@pelikhan pelikhan deleted the copilot/organize-helper-functions branch November 11, 2025 05:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the codebase to improve organization by extracting scattered helper functions from config.go and frontmatter_extraction.go into dedicated helper files. The refactoring maintains all existing functionality without changing function signatures or test coverage.

  • Created two new helper files with clear, focused purposes for configuration and frontmatter parsing utilities
  • Moved generic helper functions from their scattered locations to dedicated files
  • All functions remain in the same workflow package with unchanged signatures, ensuring no breaking changes

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
pkg/workflow/config_helpers.go New file containing configuration parsing helpers (parseLabelsFromConfig, parseTitlePrefixFromConfig, parseTargetRepoFromConfig, parseStringFromConfig)
pkg/workflow/frontmatter_helpers.go New file containing frontmatter utility functions (extractStringValue, parseIntValue, filterMapKeys)
pkg/workflow/config.go Reduced to placeholder file with comment indicating functions moved to config_helpers.go
pkg/workflow/frontmatter_extraction.go Removed three helper functions with note about their relocation to frontmatter_helpers.go

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1 to +7
package workflow

import (
"github.com/githubnext/gh-aw/pkg/logger"
)

var configHelpersLog = logger.New("workflow:config_helpers")
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a package-level documentation comment to explain the purpose of this file. For example:

package workflow

import (
	"github.com/githubnext/gh-aw/pkg/logger"
)

// This file contains helper functions for parsing and extracting values from
// safe output configuration maps used in workflow compilation.

var configHelpersLog = logger.New("workflow:config_helpers")

This would improve discoverability and help developers understand the file's purpose at a glance.

Copilot uses AI. Check for mistakes.
Comment on lines 1 to +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
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be removed rather than kept as an empty placeholder. According to the project's coding guidelines on file organization, empty placeholder files are considered an anti-pattern. Since all the configuration parsing helpers have been moved to config_helpers.go, this file no longer serves a purpose and keeping it adds unnecessary clutter to the codebase.

If configuration-related functionality needs to be added in the future, a new file can be created at that time with actual content.

See below for a potential fix:


Copilot uses AI. Check for mistakes.
Comment on lines +1 to +2
package workflow

Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a package-level documentation comment to explain the purpose of this file, similar to how other helper files in the codebase are documented. For example:

package workflow

// This file contains generic utility functions for extracting and parsing
// values from frontmatter maps used throughout the workflow compilation process.

This would improve discoverability and help developers understand the file's purpose at a glance.

See below for a potential fix:

@ -1,3 +1,5 @@
// This file contains generic utility functions for extracting and parsing
// values from frontmatter maps used throughout the workflow compilation process.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[task] Organize scattered helper functions into dedicated helper files

3 participants