Skip to content

Refactor add_interactive.go into 6 domain-focused modules#12545

Merged
pelikhan merged 3 commits intomainfrom
copilot/refactor-add-interactive-module
Jan 29, 2026
Merged

Refactor add_interactive.go into 6 domain-focused modules#12545
pelikhan merged 3 commits intomainfrom
copilot/refactor-add-interactive-module

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Split 1025-line add_interactive.go into smaller, focused modules organized by functional domain. Each module under 300 lines.

Module Structure

add_interactive_orchestrator.go  243 lines  Main flow coordination, config struct
add_interactive_engine.go        253 lines  AI engine selection, API key collection
add_interactive_auth.go           180 lines  GitHub CLI auth, repo validation, permissions
add_interactive_workflow.go      193 lines  Status polling, workflow execution
add_interactive_git.go            127 lines  PR creation, merging, branch updates
add_interactive_secrets.go         82 lines  Secret management, GitHub Secrets API

Test Coverage

Added unit tests for orchestration flow, secret management, and workflow status checking (321 lines total).

Changes

  • Orchestrator: Owns AddInteractiveConfig and main flow through RunAddInteractive()
  • Engine: Handles Copilot PAT creation flow separately from generic API key collection
  • Auth: Groups all GitHub/repo validation checks (auth status, Actions enabled, write permissions)
  • Workflow: Isolated status polling retry logic and workflow dispatch handling
  • Git: Encapsulates PR merge flow and local branch sync
  • Secrets: Minimal interface for checking/adding repository secrets

Public API unchanged - RunAddInteractive() signature and behavior identical.

Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor add_interactive.go into focused modules (1025 lines)</issue_title>
<issue_description>### Overview

The file pkg/cli/add_interactive.go has grown to 1025 lines, making it difficult to maintain and test. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/cli/add_interactive.go
  • Size: 1025 lines
  • Test Coverage: No test file found
  • Complexity: Multiple distinct functional domains with 23 functions handling Git operations, GitHub API interactions, user prompts, and workflow orchestration
Full File Analysis

Detailed Breakdown

The file contains 23 functions organized into these functional areas:

1. Repository & Authentication Checks (5 functions, ~170 lines)

  • checkGHAuthStatus() - Verify GitHub CLI authentication
  • checkGitRepository() - Verify git repo and get org/repo info
  • checkRepoVisibility() - Check if repo is public/private
  • checkActionsEnabled() - Verify GitHub Actions enabled
  • checkUserPermissions() - Check user has write access

2. Secret Management (2 functions, ~80 lines)

  • checkExistingSecrets() - Fetch existing repository secrets
  • addRepositorySecret() - Add a new repository secret

3. AI Engine Selection & API Key Collection (4 functions, ~240 lines)

  • selectAIEngineAndKey() - Prompt user to select engine and provide key (110 lines)
  • collectAPIKey() - Collect API key for selected engine
  • collectCopilotPAT() - Special handling for Copilot PAT creation (60 lines)
  • collectGenericAPIKey() - Generic API key collection (60 lines)

4. Workflow Resolution & Display (3 functions, ~50 lines)

  • resolveWorkflows() - Resolve workflow specifications early
  • showWorkflowDescriptions() - Display workflow descriptions
  • determineFilesToAdd() - Determine which files will be added

5. Change Application & Git Operations (4 functions, ~120 lines)

  • getSecretInfo() - Get secret name and value for engine
  • confirmChanges() - Ask user to confirm changes
  • applyChanges() - Create PR, merge it, and add secret (70 lines)
  • updateLocalBranch() - Update local branch after merge (40 lines)

6. Workflow Execution & Status (4 functions, ~160 lines)

  • checkStatusAndOfferRun() - Check workflow status and offer to run (135 lines)
  • getWorkflowStatuses() - Helper to get workflow statuses (45 lines)
  • mergePullRequest() - Merge a pull request (10 lines)
  • showFinalInstructions() - Display final instructions (20 lines)

7. Main Orchestration (1 function, ~95 lines)

  • RunAddInteractive() - Main entry point that coordinates all steps

Complexity Hotspots

  • selectAIEngineAndKey() (lines 372-478): 110 lines handling engine selection logic with multiple priority checks (workflow frontmatter, existing secrets, environment variables)
  • checkStatusAndOfferRun() (lines 825-956): 135 lines managing workflow status polling, spinner UI, and conditional run triggering
  • collectCopilotPAT() (lines 499-557): 60 lines with extensive GitHub PAT creation instructions
  • applyChanges() (lines 701-769): 70 lines coordinating workflow addition, PR creation, and merging

Code Patterns

  • Heavy use of console formatting for user-facing output
  • Interactive prompts using charmbracelet/huh library
  • GitHub CLI (gh) command execution via workflow.RunGH()
  • State stored in AddInteractiveConfig struct passed through methods
  • No test coverage for any interactive flows

Refactoring Strategy

Proposed File Splits

Based on semantic analysis, split the file into the following modules:

  1. add_interactive_orchestrator.go

    • Functions: RunAddInteractive(), resolveWorkflows(), showWorkflowDescriptions(), determineFilesToAdd(), confirmChanges(), showFinalInstructions()
    • Responsibility: Main orchestration and workflow coordination
    • Estimated LOC: ~250 lines
  2. add_interactive_auth.go

    • Functions: checkGHAuthStatus(), checkGitRepository(), checkRepoVisibility(), checkActionsEnabled(), checkUserPermissions()
    • Responsibility: Authentication and repository validation
    • Estimated LOC: ~200 lines
  3. add_interactive_secrets.go

    • Functions: checkExistingSecrets(), addRepositorySecret(), getSecretInfo()
    • Responsibility: Secret management and GitHub Secrets API interaction
    • Estimated LOC: ~120 lines
  4. add_interactive_engine.go

    • Functions: selectAIEngineAndKey(), collectAPIKey(), collectCopilotPAT(), collectGenericAPIKey()
    • Responsibility: AI engine selection and API key collection
    • Estimated LOC: ~240 lines
  5. add_interactive_git.go

    • Functions: applyChanges(), updateLocalBranch(), mergePullRequest()
    • Responsibility: Git operations, PR creation, and merging
    • Estima...

💡 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 January 29, 2026 14:22
- Split 1025-line file into 6 modules:
  - add_interactive_orchestrator.go (243 lines) - main orchestration
  - add_interactive_auth.go (180 lines) - authentication/validation
  - add_interactive_secrets.go (82 lines) - secret management
  - add_interactive_engine.go (253 lines) - AI engine selection
  - add_interactive_git.go (127 lines) - Git operations
  - add_interactive_workflow.go (193 lines) - workflow status
- Add comprehensive tests for all modules
- All modules under 300 lines
- Build, tests, and linting pass

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Use _ = os.Setenv() to explicitly ignore errors
- Reduces gosec warnings from 75 to 73

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor add_interactive.go into focused modules Refactor add_interactive.go into 6 domain-focused modules Jan 29, 2026
Copilot AI requested a review from pelikhan January 29, 2026 14:32
@pelikhan pelikhan marked this pull request as ready for review January 29, 2026 14:34
@pelikhan pelikhan merged commit a20e37d into main Jan 29, 2026
161 checks passed
@pelikhan pelikhan deleted the copilot/refactor-add-interactive-module branch January 29, 2026 15:17
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 add_interactive.go into focused modules (1025 lines)

2 participants