-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor: Eliminate 447 lines of duplicate safe output config handling #9257
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
Refactor: Eliminate 447 lines of duplicate safe output config handling #9257
Conversation
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
- Created safe_output_config_builder.go with reflection-based approach - buildHandlerConfig() automatically extracts fields from structs - buildSafeOutputConfigs() processes all safe output types - Handles embedded structs (BaseSafeOutputConfig, SafeOutputTargetConfig, etc.) - Reduces compiler_safe_outputs_config.go from 476 to 17 lines - All existing tests pass without modifications Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
The presence of a non-nil config pointer indicates the handler should be enabled,
even if all fields are zero-values. This matches the original behavior where an
empty config like `create-issue: {}` would still enable the handler.
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
pelikhan
left a comment
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.
@copilot can we define a struct and marshal?
Good suggestion! I explored using JSON marshaling + struct tags instead of manual reflection. However, the config structs currently only have Two options:
Which would you prefer? The reflection approach is already working and well-tested. Adding JSON tags would be cleaner long-term but requires changes to many struct definitions. |
|
@copilot format, lint |
Applied Go formatter (gofmt) to align field mappings and struct literals for better readability. No functional changes. Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Refactor Safe Output Configuration - Phase 1 Complete ✅
Summary
Successfully eliminated ~450 lines of duplicate code in
compiler_safe_outputs_config.goby implementing a generic, reflection-based configuration builder. This addresses the first major refactoring opportunity identified in the issue.Implementation Statistics
Files Created
pkg/workflow/safe_output_config_builder.go(256 lines)buildHandlerConfig()using reflectionpkg/workflow/safe_output_config_builder_test.go(314 lines)Key Features
✅ Automatic field extraction via reflection
✅ Handles embedded structs recursively
✅ Boolean pointer support for allow_* fields
✅ Customizers for special cases (base_branch, defaults)
✅ Type-safe with compile-time checking
✅ Zero regressions - all tests pass
✅ Properly formatted and linted
Adding New Safe Output Types
Before (required ~25 lines of duplicate code):
After (requires 1 line):
{fieldName: "NewType", handlerName: "new_type"},Benefits
Test Coverage
All existing tests pass:
Code Quality
gofmtgolangci-lintwith zero issuesImpact: This refactoring eliminates a major source of technical debt and establishes a pattern for future consolidation.
Original prompt
This section details on the original issue you should resolve
<issue_title>[refactor] Semantic Function Clustering Analysis - Major Refactoring Opportunities Identified</issue_title>
<issue_description>## Executive Summary
Analysis of 220 non-test Go files in the
pkg/directory has identified significant opportunities for code consolidation and improved organization. The most critical finding is ~2,000 lines of duplicated code in safe output configuration handling, with repetitive patterns across 60+ nearly identical functions.Full Report
Analysis Overview
1. Critical Issues - High Priority
🔴 Issue #1: Massive Safe Output Config Duplication
Scope: 19 files with 60+ nearly identical functions
Lines of duplicate code: ~2,000
Impact: High - makes maintenance difficult and error-prone
Problem Details
The safe output configuration system has severe code duplication:
Single 476-line mega-function in
pkg/workflow/compiler_safe_outputs_config.go:17-476addHandlerManagerConfigEnvVar()60+ duplicate
parse*Config()functions across 30+ filescreate_issue.go,create_discussion.go,create_pull_request.go, etc.37+ duplicate
build*Job()functionsBuildListSafeOutputJob()exists insafe_output_builder.gobut is underutilizedExample: Repetitive Code in
compiler_safe_outputs_config.goExample: Duplicate Parse Pattern
Found in
create_issue.go:23-50,create_discussion.go:23-50,create_pull_request.go:23-50, and 10+ other files:This pattern is repeated 30+ times with only the type name changing!
Recommendation
Refactor using generics and registry pattern:
Create generic config parser in
pkg/workflow/config_helpers.go:Replace 476-line function with handler registry:
Consolidate into subdirectory structure:
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.