Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

The generateYAML() function in compiler_yaml.go was a 136-line monolith handling job building, validation, header generation, and body generation. This made it difficult to test components in isolation and harder to maintain.

Changes

Split into three focused helpers:

  • buildJobsAndValidate() - Job building and dependency validation
  • generateWorkflowHeader() - Header comments with ANSI stripping
  • generateWorkflowBody() - Workflow structure (name, triggers, permissions, jobs)

The main generateYAML() now orchestrates these helpers in 30 lines (78% reduction):

func (c *Compiler) generateYAML(data *WorkflowData, markdownPath string) (string, error) {
    // Build all jobs and validate dependencies
    if err := c.buildJobsAndValidate(data, markdownPath); err != nil {
        return "", err
    }

    var yaml strings.Builder
    yaml.Grow(256 * 1024)

    // Generate workflow header comments
    c.generateWorkflowHeader(&yaml, data)

    // Generate workflow body structure
    c.generateWorkflowBody(&yaml, data)

    yamlContent := yaml.String()

    // Post-processing for trial mode
    if c.trialMode && c.hasIssueTrigger(data.On) {
        yamlContent = c.replaceIssueNumberReferences(yamlContent)
    }

    return yamlContent, nil
}

Testing

Added unit tests for each helper function with table-driven test cases. All existing integration tests pass without modification, confirming identical YAML output.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Code Quality] Split generateYAML() function in compiler_yaml.go</issue_title>
<issue_description>## Description

The generateYAML() function in compiler_yaml.go is likely >100 lines and handles multiple concerns including job building, validation, header generation, and body generation. Splitting this function will improve testability, readability, and maintainability.

Current Issues

  • Single large function handling multiple responsibilities
  • Difficult to test individual components in isolation
  • Complex logic flow makes maintenance challenging
  • Violates single responsibility principle

Suggested Changes

Extract the main YAML generation function into smaller, focused helper methods:

  1. buildJobsAndValidate(data, markdownPath) error

    • Handle job building and validation logic
    • Return early if validation fails
    • Can be unit tested independently
  2. generateWorkflowHeader(yaml, data) error

    • Generate workflow metadata and header section
    • Isolated header generation logic
    • Easy to test with different data structures
  3. generateWorkflowBody(yaml, data) error

    • Generate workflow body content
    • Focused on body structure only
    • Testable separately from header

Files Affected

  • pkg/workflow/compiler_yaml.go (currently 510 lines)
  • pkg/workflow/compiler_yaml_test.go (add new test cases for extracted functions)

Success Criteria

  • generateYAML() function is split into 3-4 smaller functions
  • Each extracted function has a single, clear responsibility
  • All existing tests pass without modification
  • New unit tests added for each extracted function
  • Code complexity is reduced (easier to understand each piece)
  • No change in compiled YAML output (identical functionality)

Benefits

  • Testability: Each component can be tested in isolation
  • Readability: Smaller functions are easier to understand
  • Maintainability: Changes to one concern don't affect others
  • Reusability: Helper functions can be reused if needed
  • Debugging: Easier to identify issues in specific areas

Impact

  • Estimated Effort: 2-3 hours
  • Risk: Low (preserve existing behavior, add tests)
  • Value: High (significantly improves code quality)

Source

Extracted from Daily Compiler Code Quality Report discussion githubnext/gh-aw#12164

Priority

Medium - Improves code organization and testability, but not blocking functionality.

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 11, 2026, 5:10 AM UTC

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


💡 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.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor generateYAML function for improved readability Refactor generateYAML() into focused helper functions Jan 28, 2026
Copilot AI requested a review from pelikhan January 28, 2026 07:28
@pelikhan pelikhan marked this pull request as ready for review January 28, 2026 14:02
@pelikhan pelikhan merged commit 72f8665 into main Jan 28, 2026
157 of 158 checks passed
@pelikhan pelikhan deleted the copilot/refactor-generate-yaml-function branch January 28, 2026 14:16
github-actions bot pushed a commit that referenced this pull request Jan 28, 2026
Remove self-documenting comments that stated 'Strip ANSI escape codes'
before StripANSIEscapeCodes() function calls. The function name itself
is sufficiently clear about the operation being performed.

The high-level function comment already documents that all ANSI codes
are stripped, so inline comments are redundant.

Changes based on:
- PR #12210 - Refactor generateYAML() into focused helper functions
- Commit 72f8665 - Recent refactoring work

No functional changes - comments only.
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.

[Code Quality] Split generateYAML() function in compiler_yaml.go

2 participants