Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 31, 2025

✅ Refactoring Complete: compile_orchestrator.go (1200 → 262 lines)

Successfully refactored the large compile_orchestrator.go file into focused modules following the repository's established patterns.

Refactoring Summary

Before:

  • compile_orchestrator.go: 1,200 lines (monolithic file with 195+ control structures)

After:

  • compile_orchestrator.go: 262 lines (78% reduction) ✅
  • compile_orchestration.go: 480 lines (main compilation logic)
  • compile_output_formatter.go: 65 lines (output formatting wrappers)
  • compile_compiler_setup.go: 140 lines (compiler setup & configuration)
  • compile_workflow_processor.go: 221 lines (workflow file processing)
  • compile_batch_operations.go: 274 lines (batch operations & file cleanup)
  • compile_post_processing.go: 177 lines (post-compilation processing)

Total: 1,619 lines across 7 focused files

Recent Changes (Linting Fixes)

Fixed all linting issues identified by golangci-lint:

  • errcheck: Added blank identifiers for non-fatal error returns (purge operations, gitattributes, action cache)
  • revive: Removed empty error handling blocks
  • unused: Removed unused file and functions

All tests pass ✅
All linting checks pass ✅
Build succeeds ✅

Module Responsibilities

1. compile_orchestrator.go (262 lines)

  • Main CompileWorkflows() function (simplified to ~70 lines)
  • getRepositoryRelativePath() helper
  • renderGeneratedCampaignOrchestratorMarkdown() for campaign rendering
  • generateAndCompileCampaignOrchestrator() for campaign generation

2. compile_orchestration.go (480 lines)

  • compileSpecificFiles() - orchestrates specific file compilation
  • compileAllFilesInDirectory() - orchestrates directory-wide compilation
  • Post-processing coordination
  • Output formatting coordination
  • Purge operation coordination

3. compile_output_formatter.go (65 lines)

  • formatCompilationSummary() - format compilation statistics
  • formatValidationOutput() - format validation results as JSON
  • formatActionlintOutput() - display actionlint summary
  • formatStatsTable() - display workflow statistics table

4. compile_compiler_setup.go (140 lines)

  • createAndConfigureCompiler() - create compiler with full config
  • configureCompilerFlags() - set validation, strict mode, trial mode
  • setupActionMode() - configure action script inlining mode
  • setupRepositoryContext() - set repository slug for schedule scattering
  • validateActionModeConfig() - validate action mode configuration

5. compile_workflow_processor.go (221 lines)

  • compileWorkflowFile() - compile a single workflow markdown file
  • processCampaignSpec() - process a campaign spec file

6. compile_batch_operations.go (274 lines)

  • runBatchActionlint() - run actionlint on multiple lock files
  • purgeOrphanedLockFiles() - remove orphaned .lock.yml files
  • purgeInvalidFiles() - remove .invalid.yml files
  • purgeOrphanedCampaignOrchestrators() - remove orphaned .campaign.g.md files
  • purgeOrphanedCampaignOrchestratorLockFiles() - remove orphaned .campaign.g.lock.yml files

7. compile_post_processing.go (177 lines)

  • generateDependabotManifestsWrapper() - generate Dependabot manifests
  • generateMaintenanceWorkflowWrapper() - generate maintenance workflow
  • validateCampaignsWrapper() - validate campaign specs
  • collectWorkflowStatisticsWrapper() - collect workflow statistics
  • updateGitAttributes() - update .gitattributes file
  • saveActionCache() - save action cache

Testing Results

  • All new files compile successfully ✅
  • All unit tests pass (including TestGetRepositoryRelativePath*) ✅
  • Build succeeds (make build) ✅
  • Linting passes (make lint) ✅
  • No breaking changes to public API ✅
  • compile_orchestrator_stability_test.go passes ✅

Benefits Achieved

78% reduction in main orchestrator file size (1200 → 262 lines)
7 focused files with clear single responsibilities
Improved maintainability - easier to understand and modify
Better testability - focused unit tests for each module
No breaking changes - public API remains unchanged
Follows repository patterns - consistent with other CLI files
Reduced complexity - smaller functions with fewer control structures
Clean linting - all golangci-lint checks pass

Code Organization Patterns Followed

  • File naming: compile_*.go for consistency with existing files
  • Logger naming: cli:filename pattern (e.g., cli:compile_orchestration)
  • Function visibility: Internal helpers are unexported
  • Documentation: Each file has package-level documentation explaining organization rationale
  • Error handling: Consistent error propagation and formatting
  • Linting compliance: All code passes golangci-lint checks

Campaign Status Update

Files Over 800-Line Threshold: Now 16 files (reduced from 17)

This refactor successfully reduces the count of large files and improves overall codebase health.


Campaign: Go File Size Reduction
Metrics Date: 2025-12-31
Status: ✅ Complete

Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor Large Go File: pkg/cli/compile_orchestrator.go (1200 lines)</issue_title>
<issue_description>## Overview

The file pkg/cli/compile_orchestrator.go has grown to 1200 lines, making it difficult to maintain and test. The main CompileWorkflows function alone contains over 1000 lines with 195+ control structures, indicating high complexity and the need for refactoring into smaller, focused files with improved test coverage.

Current State

  • File: pkg/cli/compile_orchestrator.go
  • Size: 1200 lines
  • Test Coverage: 1200 lines across multiple test files (compile_command_test.go: 1060 lines, compile_orchestrator_stability_test.go: 140 lines)
  • Test-to-Source Ratio: 1:1 (adequate coverage exists)
  • Complexity: Very High
    • Only 4 functions, but CompileWorkflows is monolithic (1000+ lines)
    • 195+ control structures (if/for statements)
    • Handles 15+ distinct responsibilities in a single function

Campaign Status

Files Over 800-Line Threshold: 17 files currently exceed healthy limits

This refactor will reduce the count and improve overall codebase health.

Refactoring Strategy

The CompileWorkflows function is doing too much. Based on semantic analysis, split into focused modules:

Proposed File Splits

1. compile_config_validator.go

  • Functions:
    • validateCompileConfig() - Configuration validation
    • validateWorkflowDirectory() - Workflow directory setup
    • resolveWorkflowFile() - File resolution logic
  • Responsibility: Pre-compilation configuration validation
  • Estimated LOC: ~150 lines

2. compile_compiler_setup.go

  • Functions:
    • createCompiler() - Compiler initialization
    • configureCompilerFlags() - Set validation, strict mode, trial mode
    • setupActionMode() - Action mode detection and configuration
    • setupRepositoryContext() - Repository slug and git root setup
  • Responsibility: Compiler instance creation and configuration
  • Estimated LOC: ~200 lines

3. compile_workflow_processor.go

  • Functions:
    • processWorkflowFile() - Single workflow compilation logic
    • processCampaignSpec() - Campaign spec handling
    • collectLockFilesForLinting() - Lock file collection
  • Responsibility: Per-file processing logic
  • Estimated LOC: ~250 lines

4. compile_batch_operations.go

  • Functions:
    • runBatchActionlint() - Batch actionlint execution
    • runBatchZizmor() - Batch security linting
    • runBatchPoutine() - Batch supply chain analysis
    • purgeOrphanedFiles() - Clean up orphaned .lock.yml and .invalid.yml files
  • Responsibility: Batch operations on compiled workflows
  • Estimated LOC: ~200 lines

5. compile_output_formatter.go

  • Functions:
    • printCompilationSummary() - Already exists, move here
    • displayStatsTable() - Already exists, move here
    • displayActionlintSummary() - Already exists, move here
    • formatValidationResults() - JSON output formatting
    • sanitizeValidationResults() - Security sanitization
  • Responsibility: Output formatting and display
  • Estimated LOC: ~150 lines

6. compile_post_processing.go

  • Functions:
    • generateDependabotManifests() - Dependabot generation wrapper
    • generateMaintenanceWorkflow() - Maintenance workflow generation
    • validateCampaignSpecs() - Campaign validation wrapper
    • collectWorkflowStatistics() - Stats collection
  • Responsibility: Post-compilation generation and validation
  • Estimated LOC: ~150 lines

7. compile_orchestrator.go (Refactored)

  • Functions:
    • CompileWorkflows() - Main orchestrator (dramatically simplified)
    • getRepositoryRelativePath() - Keep helper
    • renderGeneratedCampaignOrchestratorMarkdown() - Keep campaign rendering
    • generateAndCompileCampaignOrchestrator() - Keep campaign generation
  • Responsibility: High-level orchestration and campaign support
  • Estimated LOC: ~250 lines (reduction from 1200 to 250)

Shared Utilities

Extract common functionality into existing files:

  • git_helpers.go: Git root finding, repository slug extraction (may already exist)
  • file_helpers.go: File path resolution, glob operations

Interface Abstractions

Consider introducing interfaces to reduce coupling:

  • WorkflowProcessor interface: For processing different file types (workflows vs campaigns)
  • LinterRunner interface: For running different linters (actionlint, zizmor, poutine)
  • OutputFormatter interface: For different output formats (text, JSON, stats)

Test Coverage Plan

Add comprehensive tests for each new file:

1. compile_config_validator_test.go

  • Test cases:
    • Valid configuration passes
    • Invalid workflow directory rejected
    • File resolut...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 31, 2025 02:39
- compile_output_formatter.go: Output formatting wrappers
- compile_config_validator.go: Configuration validation
- compile_compiler_setup.go: Compiler setup and configuration
- compile_workflow_processor.go: Workflow file processing
- compile_batch_operations.go: Batch operations (linting, purging)
- compile_post_processing.go: Post-compilation processing

All files compile successfully and follow the repository's patterns.

Co-authored-by: mnkiefer <[email protected]>
- Reduced compile_orchestrator.go from 1200 to 262 lines (78% reduction)
- Created compile_orchestration.go with main compilation logic (488 lines)
- CompileWorkflows now delegates to compileSpecificFiles and compileAllFilesInDirectory
- All unit tests pass successfully
- Maintains exact same behavior and public API

Co-authored-by: mnkiefer <[email protected]>
Copilot AI changed the title [WIP] Refactor large Go file compile_orchestrator.go for maintainability Refactor compile_orchestrator.go: split 1200-line file into focused modules Dec 31, 2025
Copilot AI requested a review from mnkiefer December 31, 2025 02:54
@pelikhan
Copy link
Contributor

@copilot lint go

- Fix errcheck: Add blank identifier for non-fatal errors in purge operations
- Fix errcheck: Add blank identifier for non-fatal updateGitAttributes calls
- Fix errcheck: Add blank identifier for non-fatal saveActionCache calls
- Fix revive: Remove empty error handling blocks
- Fix unused: Remove unused compile_config_validator.go file
- Fix unused: Remove unused collectWorkflowStatisticsFromDir function
- Fix unused: Remove unused collectLockFilesForLinting function

All linting issues resolved. All tests pass.

Co-authored-by: pelikhan <[email protected]>
Copy link
Contributor Author

Copilot AI commented Dec 31, 2025

@copilot lint go

Fixed all linting issues in commit adfb4f1:

  • errcheck (5 instances): Added blank identifiers for non-fatal error returns
  • revive (2 instances): Removed empty error handling blocks
  • unused (5 instances): Removed unused file and functions

All make lint checks now pass ✅

Copilot AI requested a review from pelikhan December 31, 2025 03:23
@pelikhan pelikhan marked this pull request as ready for review December 31, 2025 03:28
@pelikhan pelikhan merged commit 2dd6883 into main Dec 31, 2025
57 of 58 checks passed
@pelikhan pelikhan deleted the copilot/refactor-compile-orchestrator-file branch December 31, 2025 03:28
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.

[file-diet] Refactor Large Go File: pkg/cli/compile_orchestrator.go (1200 lines)

3 participants