-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor: Extract shared helpers for safe output config parsing #3995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Added parseTargetRepoWithValidation() helper to consolidate target-repo parsing and wildcard validation logic - Updated parseBaseSafeOutputConfig() to accept defaultMax parameter, eliminating need to set Max before calling - Refactored 10 parsing functions to use new helpers: * create_issue.go * create_discussion.go * add_comment.go * create_pr_review_comment.go * create_agent_task.go * create_pull_request.go * create_code_scanning_alert.go * missing_tool.go * publish_assets.go * push_to_pull_request_branch.go * update_issue.go - Added comprehensive test coverage for parseTargetRepoWithValidation() - All existing tests pass, functionality preserved Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this 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 successfully refactors safe output configuration parsing by extracting shared helper functions, eliminating approximately 65 lines of duplicate code across 11 parser files. The refactoring introduces two key helpers: parseTargetRepoWithValidation() which combines target-repo extraction with wildcard validation, and an enhanced parseBaseSafeOutputConfig() that accepts a defaultMax parameter to standardize default value initialization.
Key changes:
- Centralized target-repo wildcard validation logic into a single reusable function
- Standardized default max value initialization through function parameter
- Improved consistency in configuration parsing patterns across all safe output types
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/workflow/config_helpers.go |
Added parseTargetRepoWithValidation() helper that combines extraction and wildcard validation |
pkg/workflow/safe_output_config.go |
Enhanced parseBaseSafeOutputConfig() with defaultMax parameter for consistent default initialization |
pkg/workflow/create_issue.go |
Refactored to use new helpers; added else branch for nil config handling |
pkg/workflow/create_discussion.go |
Refactored to use new helpers; missing else branch for nil config case |
pkg/workflow/create_agent_task.go |
Refactored to use new helpers; missing else branch for nil config case |
pkg/workflow/add_comment.go |
Refactored to use new helpers; added else branch for nil config handling |
pkg/workflow/create_pr_review_comment.go |
Refactored to use new helpers; added else branch for nil config handling |
pkg/workflow/create_pull_request.go |
Refactored to use new helpers; contains redundant github-token parsing and unnecessary Max override |
pkg/workflow/update_issue.go |
Refactored to use new helpers; added else branch for nil config handling |
pkg/workflow/create_code_scanning_alert.go |
Refactored to use parseBaseSafeOutputConfig with default max; missing else branch for consistency |
pkg/workflow/missing_tool.go |
Updated to use parseBaseSafeOutputConfig with default max parameter |
pkg/workflow/publish_assets.go |
Updated to use parseBaseSafeOutputConfig with default max parameter |
pkg/workflow/push_to_pull_request_branch.go |
Updated to use parseBaseSafeOutputConfig with default max parameter |
pkg/workflow/config_parsing_helpers_test.go |
Added comprehensive unit tests for parseTargetRepoWithValidation() covering valid, invalid, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/workflow/create_pull_request.go
Outdated
| // Parse common base fields with default max of 1 (always max 1 for pull requests) | ||
| c.parseBaseSafeOutputConfig(configMap, &pullRequestsConfig.BaseSafeOutputConfig, 1) | ||
|
|
||
| // Note: max parameter is not supported for pull requests (always limited to 1) | ||
| // If max is specified, it will be ignored as pull requests are singular only | ||
| // If max is specified, it will be overridden to 1 | ||
| pullRequestsConfig.Max = 1 |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary forced overwrite of Max to 1 after calling parseBaseSafeOutputConfig.
The function parseBaseSafeOutputConfig is called on line 212 with defaultMax parameter set to 1, which already sets pullRequestsConfig.Max = 1. Immediately overwriting it again on line 216 is redundant.
If the intent is to enforce that Max is always 1 for pull requests regardless of user configuration, this should be done before calling parseBaseSafeOutputConfig so that user-provided values are ignored. The current implementation allows users to specify a different max value (which would be set by parseBaseSafeOutputConfig), only to silently override it.
Suggested fix: Either:
- Remove line 216 if user configuration should be respected, OR
- Move line 216 before line 212 and don't pass a defaultMax parameter (pass -1 to skip default initialization), then only parse github-token in parseBaseSafeOutputConfig
See below for a potential fix:
// Parse github-token (Note: max is always 1 for pull requests and user-specified max is ignored)
if githubToken, exists := configMap["github-token"]; exists {
if githubTokenStr, ok := githubToken.(string); ok {
pullRequestsConfig.GitHubToken = githubTokenStr
}
}
// Always enforce max = 1 for pull requests, ignoring user-specified max
pullRequestsConfig.Max = 1
// Parse common base fields, but skip max (pass -1 to avoid setting default)
c.parseBaseSafeOutputConfig(configMap, &pullRequestsConfig.BaseSafeOutputConfig, -1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8e36630. The code now:
- Calls
parseBaseSafeOutputConfig()with-1to parse github-token without setting a default max - Unconditionally sets
Max = 1after parsing to enforce the PR limit
This makes the intent clearer: pull requests always have Max=1 regardless of user input, and the override happens after all parsing is complete.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot apply comments, format lint run tests |
- Remove redundant setting of Max before parseBaseSafeOutputConfig - Parse github-token and user-specified max first with parseBaseSafeOutputConfig(-1) - Then unconditionally override Max to 1 to enforce PR limit - Makes intent clearer: PRs always have Max=1 regardless of user input Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Done! Applied all review comments and verified:
All safe output parsers now have consistent handling of nil/empty configs with proper default max values. |
✅ Refactoring Complete: Duplicate Safe Output Config Parsers
Summary
Successfully eliminated duplicate code across 10+ safe output configuration parsers by extracting shared helper functions. The refactoring reduces code duplication by ~65 lines while maintaining 100% test coverage and functionality.
Changes Made
1. New Helper Functions
parseTargetRepoWithValidation(configMap) (string, bool)pkg/workflow/config_helpers.goUpdated
parseBaseSafeOutputConfig(configMap, config, defaultMax)pkg/workflow/safe_output_config.goconfig.Max = Xassignments2. Refactored Files (11 total)
3. Test Coverage
TestParseTargetRepoWithValidationwith 5 comprehensive test casesQuality Metrics
✅ Code reduction: ~65 lines of duplicated code eliminated
✅ Consistency: All parsers use identical validation logic
✅ Maintainability: Validation changes only need updating 2 functions
✅ Safety: Comprehensive test coverage ensures no regressions
✅ Clean: All tests pass (make test-unit successful)
✅ Format: Code properly formatted (make fmt)
Code Review Feedback Addressed
All review comments have been addressed:
Files Changed
Production code: Net reduction of ~51 lines (65 removed, 14 added)
Test code: +60 lines (comprehensive coverage)
All implementation checklist items from the original issue are complete! ✨
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.