|
| 1 | +--- |
| 2 | +name: Duplicate Code Detector |
| 3 | +description: Identifies duplicate code patterns across the codebase and suggests refactoring opportunities |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + schedule: daily |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + issues: read |
| 10 | + pull-requests: read |
| 11 | +engine: codex |
| 12 | +safe-outputs: |
| 13 | + create-issue: |
| 14 | + expires: 2d |
| 15 | + title-prefix: "[duplicate-code] " |
| 16 | + labels: [code-quality, automated-analysis] |
| 17 | + assignees: copilot |
| 18 | + group: true |
| 19 | + max: 3 |
| 20 | +timeout-minutes: 15 |
| 21 | +strict: true |
| 22 | +--- |
| 23 | + |
| 24 | +# Duplicate Code Detection |
| 25 | + |
| 26 | +Analyze code to identify duplicated patterns using semantic analysis. Report significant findings that require refactoring. |
| 27 | + |
| 28 | +## Task |
| 29 | + |
| 30 | +Detect and report code duplication by: |
| 31 | + |
| 32 | +1. **Analyzing Recent Commits**: Review changes in the latest commits |
| 33 | +2. **Detecting Duplicated Code**: Identify similar or duplicated code patterns using semantic analysis |
| 34 | +3. **Reporting Findings**: Create a detailed issue if significant duplication is detected (threshold: >10 lines or 3+ similar patterns) |
| 35 | + |
| 36 | +## Context |
| 37 | + |
| 38 | +- **Repository**: ${{ github.repository }} |
| 39 | +- **Commit ID**: ${{ github.event.head_commit.id }} |
| 40 | +- **Triggered by**: @${{ github.actor }} |
| 41 | + |
| 42 | +## Analysis Workflow |
| 43 | + |
| 44 | +### 1. Changed Files Analysis |
| 45 | + |
| 46 | +Identify and analyze modified files: |
| 47 | +- Determine files changed in the recent commits using `git log` and `git diff` |
| 48 | +- Focus on source code files (programming language files) |
| 49 | +- **Exclude test files** from analysis (files matching patterns: `*_test.*`, `*.test.*`, `*.spec.*`, `test_*.*`, or located in directories named `test`, `tests`, `__tests__`, or `spec`) |
| 50 | +- **Exclude generated files** and build artifacts |
| 51 | +- **Exclude workflow files** from analysis (files under `.github/workflows/*`) |
| 52 | +- Use code exploration tools to understand file structure |
| 53 | +- Read modified file contents to examine changes |
| 54 | + |
| 55 | +### 2. Duplicate Detection |
| 56 | + |
| 57 | +Apply analysis to find duplicates: |
| 58 | + |
| 59 | +**Pattern Search**: |
| 60 | +- Search for duplication indicators using grep and code search: |
| 61 | + - Similar function signatures |
| 62 | + - Repeated logic blocks |
| 63 | + - Similar variable naming patterns |
| 64 | + - Near-identical code blocks |
| 65 | +- Look for functions with similar names across different files |
| 66 | +- Identify structural similarities in code organization |
| 67 | + |
| 68 | +**Semantic Analysis**: |
| 69 | +- Compare code blocks for logical similarity beyond textual matching |
| 70 | +- Identify different implementations of the same functionality |
| 71 | +- Look for copy-paste patterns with minor variations |
| 72 | + |
| 73 | +### 3. Duplication Evaluation |
| 74 | + |
| 75 | +Assess findings to identify true code duplication: |
| 76 | + |
| 77 | +**Duplication Types**: |
| 78 | +- **Exact Duplication**: Identical code blocks in multiple locations |
| 79 | +- **Structural Duplication**: Same logic with minor variations (different variable names, etc.) |
| 80 | +- **Functional Duplication**: Different implementations of the same functionality |
| 81 | +- **Copy-Paste Programming**: Similar code blocks that could be extracted into shared utilities |
| 82 | + |
| 83 | +**Assessment Criteria**: |
| 84 | +- **Severity**: Amount of duplicated code (lines of code, number of occurrences) |
| 85 | +- **Impact**: Where duplication occurs (critical paths, frequently called code) |
| 86 | +- **Maintainability**: How duplication affects code maintainability |
| 87 | +- **Refactoring Opportunity**: Whether duplication can be easily refactored |
| 88 | + |
| 89 | +### 4. Issue Reporting |
| 90 | + |
| 91 | +Create separate issues for each distinct duplication pattern found (maximum 3 patterns per run). Each pattern should get its own issue to enable focused remediation. |
| 92 | + |
| 93 | +**When to Create Issues**: |
| 94 | +- Only create issues if significant duplication is found (threshold: >10 lines of duplicated code OR 3+ instances of similar patterns) |
| 95 | +- **Create one issue per distinct duplication pattern** - do NOT bundle multiple patterns in a single issue |
| 96 | +- Limit to the top 3 most significant patterns if more are found |
| 97 | +- Use the `create_issue` tool from safe-outputs MCP **once for each pattern** |
| 98 | + |
| 99 | +**Issue Contents for Each Pattern**: |
| 100 | +- **Executive Summary**: Brief description of this specific duplication pattern |
| 101 | +- **Duplication Details**: Specific locations and code blocks for this pattern only |
| 102 | +- **Severity Assessment**: Impact and maintainability concerns for this pattern |
| 103 | +- **Refactoring Recommendations**: Suggested approaches to eliminate this pattern |
| 104 | +- **Code Examples**: Concrete examples with file paths and line numbers for this pattern |
| 105 | + |
| 106 | +## Detection Scope |
| 107 | + |
| 108 | +### Report These Issues |
| 109 | + |
| 110 | +- Identical or nearly identical functions in different files |
| 111 | +- Repeated code blocks that could be extracted to utilities |
| 112 | +- Similar classes or modules with overlapping functionality |
| 113 | +- Copy-pasted code with minor modifications |
| 114 | +- Duplicated business logic across components |
| 115 | + |
| 116 | +### Skip These Patterns |
| 117 | + |
| 118 | +- Standard boilerplate code (imports, exports, package declarations) |
| 119 | +- Test setup/teardown code (acceptable duplication in tests) |
| 120 | +- **All test files** (files matching: `*_test.*`, `*.test.*`, `*.spec.*`, `test_*.*`, or in `test/`, `tests/`, `__tests__/`, `spec/` directories) |
| 121 | +- **All workflow files** (files under `.github/workflows/*`) |
| 122 | +- Configuration files with similar structure |
| 123 | +- Language-specific patterns (constructors, getters/setters) |
| 124 | +- Small code snippets (<5 lines) unless highly repetitive |
| 125 | +- Generated code or vendored dependencies |
| 126 | + |
| 127 | +### Analysis Depth |
| 128 | + |
| 129 | +- **Primary Focus**: Files changed in recent commits (excluding test files and workflow files) |
| 130 | +- **Secondary Analysis**: Check for duplication with existing codebase |
| 131 | +- **Cross-Reference**: Look for patterns across the repository |
| 132 | +- **Historical Context**: Consider if duplication is new or existing |
| 133 | + |
| 134 | +## Issue Template |
| 135 | + |
| 136 | +For each distinct duplication pattern found, create a separate issue using this structure: |
| 137 | + |
| 138 | +````markdown |
| 139 | +# 🔍 Duplicate Code Detected: [Pattern Name] |
| 140 | + |
| 141 | +*Analysis of commit ${{ github.event.head_commit.id }}* |
| 142 | + |
| 143 | +**Assignee**: @copilot |
| 144 | + |
| 145 | +## Summary |
| 146 | + |
| 147 | +[Brief overview of this specific duplication pattern] |
| 148 | + |
| 149 | +## Duplication Details |
| 150 | + |
| 151 | +### Pattern: [Description] |
| 152 | +- **Severity**: High/Medium/Low |
| 153 | +- **Occurrences**: [Number of instances] |
| 154 | +- **Locations**: |
| 155 | + - `path/to/file1.ext` (lines X-Y) |
| 156 | + - `path/to/file2.ext` (lines A-B) |
| 157 | +- **Code Sample**: |
| 158 | + ````[language] |
| 159 | + [Example of duplicated code] |
| 160 | + ```` |
| 161 | + |
| 162 | +## Impact Analysis |
| 163 | + |
| 164 | +- **Maintainability**: [How this affects code maintenance] |
| 165 | +- **Bug Risk**: [Potential for inconsistent fixes] |
| 166 | +- **Code Bloat**: [Impact on codebase size] |
| 167 | + |
| 168 | +## Refactoring Recommendations |
| 169 | + |
| 170 | +1. **[Recommendation 1]** |
| 171 | + - Extract common functionality to: `suggested/path/utility.ext` |
| 172 | + - Estimated effort: [hours/complexity] |
| 173 | + - Benefits: [specific improvements] |
| 174 | + |
| 175 | +2. **[Recommendation 2]** |
| 176 | + [... additional recommendations ...] |
| 177 | + |
| 178 | +## Implementation Checklist |
| 179 | + |
| 180 | +- [ ] Review duplication findings |
| 181 | +- [ ] Prioritize refactoring tasks |
| 182 | +- [ ] Create refactoring plan |
| 183 | +- [ ] Implement changes |
| 184 | +- [ ] Update tests |
| 185 | +- [ ] Verify no functionality broken |
| 186 | + |
| 187 | +## Analysis Metadata |
| 188 | + |
| 189 | +- **Analyzed Files**: [count] |
| 190 | +- **Detection Method**: Semantic code analysis |
| 191 | +- **Commit**: ${{ github.event.head_commit.id }} |
| 192 | +- **Analysis Date**: [timestamp] |
| 193 | +```` |
| 194 | + |
| 195 | +## Operational Guidelines |
| 196 | + |
| 197 | +### Security |
| 198 | +- Never execute untrusted code or commands |
| 199 | +- Only use read-only analysis tools |
| 200 | +- Do not modify files during analysis |
| 201 | + |
| 202 | +### Efficiency |
| 203 | +- Focus on recently changed files first |
| 204 | +- Use semantic analysis for meaningful duplication, not superficial matches |
| 205 | +- Stay within timeout limits (balance thoroughness with execution time) |
| 206 | + |
| 207 | +### Accuracy |
| 208 | +- Verify findings before reporting |
| 209 | +- Distinguish between acceptable patterns and true duplication |
| 210 | +- Consider language-specific idioms and best practices |
| 211 | +- Provide specific, actionable recommendations |
| 212 | + |
| 213 | +### Issue Creation |
| 214 | +- Create **one issue per distinct duplication pattern** - do NOT bundle multiple patterns in a single issue |
| 215 | +- Limit to the top 3 most significant patterns if more are found |
| 216 | +- Only create issues if significant duplication is found |
| 217 | +- Include sufficient detail for coding agents to understand and act on findings |
| 218 | +- Provide concrete examples with file paths and line numbers |
| 219 | +- Suggest practical refactoring approaches |
| 220 | +- Assign issue to @copilot for automated remediation |
| 221 | +- Use descriptive titles that clearly identify the specific pattern (e.g., "Duplicate Code: Error Handling Pattern in Parser Module") |
| 222 | + |
| 223 | +**Objective**: Improve code quality by identifying and reporting meaningful code duplication that impacts maintainability. Focus on actionable findings that enable automated or manual refactoring. |
0 commit comments