Skip to content

[file-diet] Refactor Large Go File: pkg/workflow/compiler_safe_outputs_consolidated.go #7250

@agentic-workflows-dev

Description

@agentic-workflows-dev

Overview

The file pkg/workflow/compiler_safe_outputs_consolidated.go has grown to 1,368 lines, making it difficult to maintain and test effectively. This file orchestrates the consolidated safe outputs job compilation and contains 30 functions, primarily consisting of step configuration builders for different safe output operations (issues, PRs, discussions, etc.).

Current State

  • File: pkg/workflow/compiler_safe_outputs_consolidated.go
  • Size: 1,368 lines (71% over the 800-line healthy threshold)
  • Functions: 30 total functions
  • Test Coverage: No dedicated test file (compiler_safe_outputs_consolidated_test.go does not exist)
  • Related Tests: 18 test files cover various safe outputs aspects (total: ~5,500 LOC)
  • Complexity: High - manages 20+ different safe output types with similar patterns

Architecture Analysis

The file follows a clear but repetitive pattern:

  1. Lines 1-100: Package setup, SafeOutputStepConfig struct definition
  2. Lines 38-537: buildConsolidatedSafeOutputsJob - main orchestrator (499 lines)
  3. Lines 537-669: Core helper functions (132 lines)
  4. Lines 679-1,368: 25 individual step config builders (689 lines)

Current Groupings Identified

GitHub Issues Operations (5 functions):

  • buildCreateIssueStepConfig
  • buildUpdateIssueStepConfig
  • buildCloseIssueStepConfig
  • buildLinkSubIssueStepConfig
  • Related helper functions

Pull Request Operations (6 functions):

  • buildCreatePullRequestStepConfig
  • buildUpdatePullRequestStepConfig
  • buildClosePullRequestStepConfig
  • buildCreatePRReviewCommentStepConfig
  • buildPushToPullRequestBranchStepConfig
  • buildAddReviewerStepConfig

Discussion Operations (3 functions):

  • buildCreateDiscussionStepConfig
  • buildUpdateDiscussionStepConfig
  • buildCloseDiscussionStepConfig

Shared Operations (4 functions):

  • buildAddCommentStepConfig (works with issues, PRs, discussions)
  • buildAddLabelsStepConfig
  • buildHideCommentStepConfig
  • buildUploadAssetsStepConfig

Specialized Operations (7 functions):

  • buildCreateCodeScanningAlertStepConfig
  • buildAssignMilestoneStepConfig
  • buildAssignToAgentStepConfig
  • buildAssignToUserStepConfig
  • buildUpdateReleaseStepConfig
  • buildCreateAgentTaskStepConfig
  • buildUpdateProjectStepConfig

Refactoring Strategy

Proposed File Splits

Split the monolithic file into 6 focused modules based on functional domains:

1. compiler_safe_outputs_core.go (~200 lines)

Responsibility: Core orchestration and shared utilities

Contents:

  • SafeOutputStepConfig struct definition
  • buildConsolidatedSafeOutputsJob main orchestrator
  • buildConsolidatedSafeOutputStep helper
  • buildJobLevelSafeOutputEnvVars helper
  • buildDetectionSuccessCondition helper

Rationale: Central coordination logic used by all step builders


2. compiler_safe_outputs_issues.go (~200 lines)

Responsibility: Issue-related safe output operations

Contents:

  • buildCreateIssueStepConfig
  • buildUpdateIssueStepConfig
  • buildCloseIssueStepConfig
  • buildLinkSubIssueStepConfig
  • Any issue-specific pre-step builders

Estimated LOC: ~200 lines


3. compiler_safe_outputs_prs.go (~300 lines)

Responsibility: Pull request operations

Contents:

  • buildCreatePullRequestStepConfig
  • buildUpdatePullRequestStepConfig
  • buildClosePullRequestStepConfig
  • buildCreatePRReviewCommentStepConfig
  • buildPushToPullRequestBranchStepConfig
  • buildAddReviewerStepConfig
  • buildCreatePullRequestPreStepsConsolidated
  • buildPushToPullRequestBranchPreStepsConsolidated

Estimated LOC: ~300 lines (includes pre-steps logic)


4. compiler_safe_outputs_discussions.go (~150 lines)

Responsibility: Discussion operations

Contents:

  • buildCreateDiscussionStepConfig
  • buildUpdateDiscussionStepConfig
  • buildCloseDiscussionStepConfig

Estimated LOC: ~150 lines


5. compiler_safe_outputs_shared.go (~200 lines)

Responsibility: Operations that work across multiple entity types

Contents:

  • buildAddCommentStepConfig (issues, PRs, discussions)
  • buildAddLabelsStepConfig
  • buildHideCommentStepConfig
  • buildUploadAssetsStepConfig

Estimated LOC: ~200 lines


6. compiler_safe_outputs_specialized.go (~318 lines)

Responsibility: Specialized/less common operations

Contents:

  • buildCreateCodeScanningAlertStepConfig
  • buildAssignMilestoneStepConfig
  • buildAssignToAgentStepConfig
  • buildAssignToUserStepConfig
  • buildUpdateReleaseStepConfig
  • buildCreateAgentTaskStepConfig
  • buildUpdateProjectStepConfig

Estimated LOC: ~318 lines


Benefits of This Split

Improved Navigation: Developers can quickly locate issue, PR, or discussion logic
Parallel Development: Multiple developers can work on different entity types simultaneously
Targeted Testing: Each module can have focused test coverage
Reduced Merge Conflicts: Changes to one entity type won't conflict with others
Clearer Dependencies: Explicit imports show which modules depend on core utilities
Better Code Review: Smaller files are easier to review and understand

Test Coverage Plan

Create 6 new test files matching the split structure:

1. compiler_safe_outputs_core_test.go

Test Cases:

  • Main orchestrator builds correct job structure
  • Script setup step is generated correctly
  • Permission aggregation works across all step types
  • Condition chaining with threat detection
  • Output mapping and dependencies
  • Target Coverage: >80%

2. compiler_safe_outputs_issues_test.go

Test Cases:

  • Create issue step config generation
  • Update issue with title/body/state
  • Close issue with comment
  • Link sub-issues with dependencies
  • Condition handling for each operation
  • Target Coverage: >80%

3. compiler_safe_outputs_prs_test.go

Test Cases:

  • Create PR with branch setup pre-steps
  • Update PR title/body/base
  • Close PR with comment
  • Review comment creation on specific files/lines
  • Push to PR branch with git operations
  • Add reviewer assignment
  • Target Coverage: >80%

4. compiler_safe_outputs_discussions_test.go

Test Cases:

  • Create discussion in category
  • Update discussion title/body
  • Close discussion with reason
  • Target Coverage: >80%

5. compiler_safe_outputs_shared_test.go

Test Cases:

  • Add comment to issue/PR/discussion (context detection)
  • Add labels with validation
  • Hide comment (spam/abuse)
  • Upload assets with path handling
  • Target Coverage: >80%

6. compiler_safe_outputs_specialized_test.go

Test Cases:

  • Code scanning alert creation with SARIF
  • Milestone assignment
  • Agent task creation
  • User assignment
  • Release updates
  • Project updates
  • Target Coverage: >80%

Implementation Guidelines

Phase 1: Preparation (No Code Changes)

  1. Review existing test coverage in related test files
  2. Document current behavior of all 30 functions
  3. Identify shared dependencies between functions
  4. Create test plan for each new module

Phase 2: Test-First Approach

  1. Create test files first with comprehensive test cases
  2. Import from original file temporarily to validate tests pass
  3. Run make test-unit to ensure baseline coverage
  4. Document any gaps in test coverage

Phase 3: Incremental Refactoring

  1. Start with smallest module: compiler_safe_outputs_discussions.go

    • Extract 3 discussion functions
    • Update imports in test files
    • Run make test-unit and make lint
    • Commit with message: refactor: extract discussion safe outputs to dedicated file
  2. Continue with each module in order of independence:

    • Specialized → Shared → Issues → PRs → Core
    • Core should be last since it's imported by all others
  3. After each split:

    • ✅ All tests pass (make test-unit)
    • ✅ Linting succeeds (make lint)
    • ✅ Build works (make build)
    • ✅ No breaking changes to public API

Phase 4: Cleanup

  1. Delete original file once all functions are migrated
  2. Update documentation if needed
  3. Run full validation: make agent-finish
  4. Final commit: refactor: complete safe outputs consolidation split

Acceptance Criteria

  • Original 1,368-line file is split into 6 focused files
  • Each new file is under 500 lines
  • All 30 functions are correctly migrated
  • 6 new test files with >80% coverage each
  • All tests pass (make test-unit)
  • Code passes linting (make lint)
  • Build succeeds (make build)
  • No breaking changes to public API
  • No regression in existing test coverage
  • Import paths are correct and minimal

Additional Context

  • Repository Guidelines: Follow patterns in .github/instructions/developer.instructions.md
  • Code Organization: Prefer many small files grouped by functionality (documented pattern)
  • Existing Patterns: Similar organization exists in engine separation (copilot_engine.go, claude_engine.go, etc.)
  • Testing Reference: Match patterns in existing pkg/workflow/*_test.go files

Campaign Metrics

Go File Size Reduction Campaign

  • Analysis Date: 2025-12-22
  • Largest File: 1,368 lines
  • Files Over 800 LOC: 20 files
  • Reduction Target: Split into 6 files averaging ~228 lines each

Priority: Medium
Effort: Large (estimated 8-12 hours across multiple PRs)
Expected Impact:

  • ✅ Significantly improved maintainability
  • ✅ Easier onboarding for new contributors
  • ✅ Reduced cognitive load during code review
  • ✅ Better test isolation and coverage
  • ✅ Fewer merge conflicts in active development areas

AI generated by Daily File Diet

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions