-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor: Extract common template sync logic to eliminate 80 lines of duplication #4256
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
Conversation
- Create ensureFileMatchesTemplate helper function - Refactor ensureAgentFromTemplate to use helper - Refactor ensureCopilotInstructions to use helper - Add comprehensive unit tests for the helper - Reduce file size from 151 to 125 lines (-26 lines) Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
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.
Pull Request Overview
This PR refactors duplicate template synchronization logic by extracting a common helper function. The refactoring reduces code duplication by ~80 lines, consolidating identical file-sync workflows that only differed in path constants and template variables.
Key Changes
- Extracted
ensureFileMatchesTemplatehelper function that parameterizes the common workflow (git root lookup, directory creation, content comparison, file writing, verbose logging) - Refactored
ensureAgentFromTemplatefrom 48 lines to 11 lines as a thin wrapper - Refactored
ensureCopilotInstructionsfrom 46 lines to 11 lines as a thin wrapper - Added comprehensive test coverage in
copilot_agents_test.gowith 7 test scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/cli/copilot-agents.go | Extracted shared helper function and refactored two functions to use it, eliminating 80 lines of duplication |
| pkg/cli/copilot_agents_test.go | Added new test file with comprehensive coverage of the helper function including creation, updates, skip logic, and verbose output scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| expectedLog string | ||
| }{ | ||
| { | ||
| name: "logs creation", | ||
| existingContent: "", | ||
| expectedLog: "Created", | ||
| }, | ||
| { | ||
| name: "logs update", | ||
| existingContent: "# Old Content", | ||
| expectedLog: "Updated", | ||
| }, | ||
| { | ||
| name: "logs up-to-date", | ||
| existingContent: "# Test Template", | ||
| expectedLog: "up-to-date", |
Copilot
AI
Nov 18, 2025
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.
The expectedLog field is defined in the test struct but never used for validation. The test comment on line 198 acknowledges that stdout is not captured. Either implement stdout capture and validation using this field, or remove it to avoid confusion.
| expectedLog string | |
| }{ | |
| { | |
| name: "logs creation", | |
| existingContent: "", | |
| expectedLog: "Created", | |
| }, | |
| { | |
| name: "logs update", | |
| existingContent: "# Old Content", | |
| expectedLog: "Updated", | |
| }, | |
| { | |
| name: "logs up-to-date", | |
| existingContent: "# Test Template", | |
| expectedLog: "up-to-date", | |
| }{ | |
| { | |
| name: "logs creation", | |
| existingContent: "", | |
| }, | |
| { | |
| name: "logs update", | |
| existingContent: "# Old Content", | |
| }, | |
| { | |
| name: "logs up-to-date", | |
| existingContent: "# Test Template", |
ensureAgentFromTemplateandensureCopilotInstructionsimplemented identical file-synchronization workflows (~40 lines each), differing only in path constants and template variables.Changes
ensureFileMatchesTemplatehelper - Parameterized common workflow (git root lookup, directory creation, content comparison, file writing, verbose logging)copilot_agents_test.gowith 7 scenarios validating helper behaviorBefore/After
Impact
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.