|
| 1 | +""" |
| 2 | +Full Repository Workflow - Process files with 100% test coverage from JaCoCo results |
| 3 | +""" |
| 4 | +from colorama import Fore, Style |
| 5 | +from pathlib import Path |
| 6 | +import os |
| 7 | + |
| 8 | +# Import workflow utilities |
| 9 | +from workflow.workflow_utils import parse_antipattern_results, get_repository_paths_from_files |
| 10 | +from workflow.backup_manager import create_repository_backup |
| 11 | +from workflow.results_manager import save_intermediate_results, create_processing_summary |
| 12 | +from workflow.file_operations import read_java_file, save_refactored_code |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +def read_jacoco_results(jacoco_results_dir: str = "../jacoco_results") -> list: |
| 17 | + """Read the list of files with 100% coverage from JaCoCo results.""" |
| 18 | + results_path = Path(jacoco_results_dir) |
| 19 | + |
| 20 | + if not results_path.exists(): |
| 21 | + print(Fore.RED + f"JaCoCo results directory not found: {results_path}" + Style.RESET_ALL) |
| 22 | + return [] |
| 23 | + |
| 24 | + # Find all result files |
| 25 | + all_files = list(results_path.glob("*.txt")) |
| 26 | + if not all_files: |
| 27 | + print(Fore.RED + f"No JaCoCo result files found in: {results_path}" + Style.RESET_ALL) |
| 28 | + return [] |
| 29 | + |
| 30 | + # Separate combined file from individual repo files |
| 31 | + combined_file = results_path / "all_100_percent_coverage_files.txt" |
| 32 | + repo_files = [f for f in all_files if f.name != "all_100_percent_coverage_files.txt"] |
| 33 | + |
| 34 | + print(Fore.CYAN + "\nAvailable JaCoCo result files:" + Style.RESET_ALL) |
| 35 | + print("0) All repositories (combined)") |
| 36 | + |
| 37 | + # Show individual repository options |
| 38 | + for i, repo_file in enumerate(repo_files, 1): |
| 39 | + # Extract repo name from filename (remove _100_percent_coverage.txt suffix) |
| 40 | + repo_name = repo_file.stem.replace("_100_percent_coverage", "") |
| 41 | + print(f"{i}) {repo_name}") |
| 42 | + |
| 43 | + # Get user choice |
| 44 | + while True: |
| 45 | + try: |
| 46 | + choice = input(f"\nSelect repository to process (0-{len(repo_files)}): ").strip() |
| 47 | + choice_num = int(choice) |
| 48 | + |
| 49 | + if choice_num == 0: |
| 50 | + # Use combined file |
| 51 | + selected_file = combined_file |
| 52 | + repo_name = "All repositories" |
| 53 | + break |
| 54 | + elif 1 <= choice_num <= len(repo_files): |
| 55 | + # Use specific repo file |
| 56 | + selected_file = repo_files[choice_num - 1] |
| 57 | + repo_name = selected_file.stem.replace("_100_percent_coverage", "") |
| 58 | + break |
| 59 | + else: |
| 60 | + print(Fore.RED + f"Invalid choice. Please enter a number between 0 and {len(repo_files)}" + Style.RESET_ALL) |
| 61 | + except ValueError: |
| 62 | + print(Fore.RED + "Invalid input. Please enter a number." + Style.RESET_ALL) |
| 63 | + |
| 64 | + # Read the selected file |
| 65 | + if not selected_file.exists(): |
| 66 | + print(Fore.RED + f"Selected file not found: {selected_file}" + Style.RESET_ALL) |
| 67 | + return [] |
| 68 | + |
| 69 | + try: |
| 70 | + with open(selected_file, 'r') as f: |
| 71 | + file_paths = [line.strip() for line in f if line.strip()] |
| 72 | + |
| 73 | + print(Fore.GREEN + f"Selected: {repo_name}" + Style.RESET_ALL) |
| 74 | + print(Fore.GREEN + f"Found {len(file_paths)} files with 100% test coverage" + Style.RESET_ALL) |
| 75 | + return file_paths |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + print(Fore.RED + f"Error reading file {selected_file}: {e}" + Style.RESET_ALL) |
| 79 | + return [] |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +def process_java_files_with_workflow(file_paths: list, settings, db_manager, prompt_manager, langgraph): |
| 84 | + """Process each Java file through the agentic workflow.""" |
| 85 | + processed_files = [] |
| 86 | + failed_files = [] |
| 87 | + |
| 88 | + for i, file_path in enumerate(file_paths, 1): |
| 89 | + print(Fore.BLUE + f"\n{'='*60}" + Style.RESET_ALL) |
| 90 | + print(Fore.BLUE + f"Processing file {i}/{len(file_paths)}: {file_path}" + Style.RESET_ALL) |
| 91 | + print(Fore.BLUE + f"{'='*60}" + Style.RESET_ALL) |
| 92 | + |
| 93 | + # Read the Java file content |
| 94 | + java_code = read_java_file(file_path) |
| 95 | + if java_code is None: |
| 96 | + failed_files.append(file_path) |
| 97 | + continue |
| 98 | + |
| 99 | + # Create initial state for this file |
| 100 | + initial_state = { |
| 101 | + "code": java_code, |
| 102 | + "context": None, |
| 103 | + "trove_context": None, |
| 104 | + "antipatterns_scanner_results": None, |
| 105 | + "refactoring_strategy_results": None, |
| 106 | + "refactored_code": None, |
| 107 | + "code_review_results": None, |
| 108 | + "code_review_times": 0, |
| 109 | + "msgs": [], |
| 110 | + "answer": None, |
| 111 | + "current_file_path": file_path # Track current file being processed |
| 112 | + } |
| 113 | + |
| 114 | + try: |
| 115 | + # Run the agentic workflow |
| 116 | + print(Fore.CYAN + "Running agentic workflow..." + Style.RESET_ALL) |
| 117 | + final_state = langgraph.invoke(initial_state) |
| 118 | + |
| 119 | + # Save intermediate results for analysis |
| 120 | + save_intermediate_results(file_path, final_state, settings) |
| 121 | + |
| 122 | + # Check if refactoring was successful |
| 123 | + if final_state.get('refactored_code'): |
| 124 | + # Parse anti-pattern results |
| 125 | + antipatterns_found, antipatterns_count = parse_antipattern_results(final_state.get('antipatterns_scanner_results')) |
| 126 | + |
| 127 | + # Save the refactored code back to the file |
| 128 | + if save_refactored_code(file_path, final_state['refactored_code']): |
| 129 | + processed_files.append({ |
| 130 | + 'file_path': file_path, |
| 131 | + 'status': 'success', |
| 132 | + 'antipatterns_found': antipatterns_found, |
| 133 | + 'antipatterns_count': antipatterns_count, |
| 134 | + 'code_review_times': final_state.get('code_review_times', 0), |
| 135 | + 'has_intermediate_results': True |
| 136 | + }) |
| 137 | + print(Fore.GREEN + f"Successfully processed: {file_path}" + Style.RESET_ALL) |
| 138 | + else: |
| 139 | + failed_files.append(file_path) |
| 140 | + else: |
| 141 | + # Parse anti-pattern results |
| 142 | + antipatterns_found, antipatterns_count = parse_antipattern_results(final_state.get('antipatterns_scanner_results')) |
| 143 | + |
| 144 | + print(Fore.YELLOW + f"No refactored code generated for: {file_path}" + Style.RESET_ALL) |
| 145 | + processed_files.append({ |
| 146 | + 'file_path': file_path, |
| 147 | + 'status': 'no_refactoring', |
| 148 | + 'antipatterns_found': antipatterns_found, |
| 149 | + 'antipatterns_count': antipatterns_count, |
| 150 | + 'code_review_times': final_state.get('code_review_times', 0), |
| 151 | + 'has_intermediate_results': True |
| 152 | + }) |
| 153 | + |
| 154 | + except Exception as e: |
| 155 | + print(Fore.RED + f"Error processing {file_path}: {e}" + Style.RESET_ALL) |
| 156 | + failed_files.append(file_path) |
| 157 | + |
| 158 | + return processed_files, failed_files |
| 159 | + |
| 160 | + |
| 161 | +def run_full_repo_workflow(settings, db_manager, prompt_manager, langgraph): |
| 162 | + """Run the full repository workflow for files with 100% test coverage.""" |
| 163 | + print(Fore.BLUE + "\n=== Full Repository Workflow ===" + Style.RESET_ALL) |
| 164 | + print("Process Java files with 100% test coverage from JaCoCo results...") |
| 165 | + |
| 166 | + # Read JaCoCo results to get files with 100% test coverage |
| 167 | + print(Fore.CYAN + "\nReading JaCoCo results..." + Style.RESET_ALL) |
| 168 | + file_paths = read_jacoco_results() |
| 169 | + |
| 170 | + if not file_paths: |
| 171 | + print(Fore.RED + "No files found in JaCoCo results. Please run JaCoCo analysis first." + Style.RESET_ALL) |
| 172 | + print("Run: python jacoco_tool/jacoco_analysis.py") |
| 173 | + return False |
| 174 | + |
| 175 | + # Extract repository paths from file paths |
| 176 | + print(Fore.CYAN + "\nIdentifying repositories to backup..." + Style.RESET_ALL) |
| 177 | + repo_paths = get_repository_paths_from_files(file_paths) |
| 178 | + |
| 179 | + if not repo_paths: |
| 180 | + print(Fore.RED + "No repository paths could be identified from the file paths." + Style.RESET_ALL) |
| 181 | + return False |
| 182 | + |
| 183 | + print(f"Found {len(repo_paths)} repositories to backup:") |
| 184 | + for repo_path in sorted(repo_paths): |
| 185 | + repo_name = Path(repo_path).name |
| 186 | + print(f" • {repo_name} ({repo_path})") |
| 187 | + |
| 188 | + # Ask user for confirmation to proceed with backup and processing |
| 189 | + print(f"\nFiles to process ({len(file_paths)} total):") |
| 190 | + for i, path in enumerate(file_paths[:5], 1): # Show first 5 files |
| 191 | + print(f" {i}. {path}") |
| 192 | + if len(file_paths) > 5: |
| 193 | + print(f" ... and {len(file_paths) - 5} more files") |
| 194 | + |
| 195 | + proceed = input(f"\nProceed with backing up {len(repo_paths)} repositories and processing {len(file_paths)} files? (Y/N): ").strip().lower() |
| 196 | + if proceed != 'y': |
| 197 | + print("Operation cancelled.") |
| 198 | + return False |
| 199 | + |
| 200 | + # Create repository backups |
| 201 | + print(Fore.BLUE + f"\n{'='*60}" + Style.RESET_ALL) |
| 202 | + print(Fore.BLUE + "CREATING REPOSITORY BACKUPS" + Style.RESET_ALL) |
| 203 | + print(Fore.BLUE + f"{'='*60}" + Style.RESET_ALL) |
| 204 | + |
| 205 | + backup_info = create_repository_backup(repo_paths) |
| 206 | + |
| 207 | + if backup_info['failed_backups']: |
| 208 | + print(Fore.RED + f"\nWarning: {len(backup_info['failed_backups'])} repositories failed to backup:" + Style.RESET_ALL) |
| 209 | + for failed in backup_info['failed_backups']: |
| 210 | + print(Fore.RED + f" {failed['repo_path']}: {failed['error']}" + Style.RESET_ALL) |
| 211 | + |
| 212 | + continue_anyway = input("\nContinue processing despite backup failures? (Y/N): ").strip().lower() |
| 213 | + if continue_anyway != 'y': |
| 214 | + print("Operation cancelled due to backup failures.") |
| 215 | + return False |
| 216 | + |
| 217 | + print(Fore.GREEN + f"\nSuccessfully backed up {len(backup_info['backed_up_repos'])} repositories" + Style.RESET_ALL) |
| 218 | + print(Fore.GREEN + f"Backup location: {backup_info['backup_dir']}" + Style.RESET_ALL) |
| 219 | + |
| 220 | + # Process each file through the agentic workflow |
| 221 | + print(Fore.BLUE + f"\n{'='*60}" + Style.RESET_ALL) |
| 222 | + print(Fore.BLUE + "STARTING FILE PROCESSING" + Style.RESET_ALL) |
| 223 | + print(Fore.BLUE + f"{'='*60}" + Style.RESET_ALL) |
| 224 | + |
| 225 | + processed_files, failed_files = process_java_files_with_workflow( |
| 226 | + file_paths, settings, db_manager, prompt_manager, langgraph |
| 227 | + ) |
| 228 | + |
| 229 | + # Create comprehensive processing summary |
| 230 | + summary_file = create_processing_summary(processed_files, backup_info) |
| 231 | + |
| 232 | + # Generate summary report |
| 233 | + print(Fore.BLUE + "\n" + "="*80 + Style.RESET_ALL) |
| 234 | + print(Fore.BLUE + "BATCH PROCESSING SUMMARY" + Style.RESET_ALL) |
| 235 | + print(Fore.BLUE + "="*80 + Style.RESET_ALL) |
| 236 | + |
| 237 | + # Backup summary |
| 238 | + print(Fore.CYAN + "Repository Backup Summary:" + Style.RESET_ALL) |
| 239 | + print(f" Backup timestamp: {backup_info['timestamp']}") |
| 240 | + print(f" Backup location: {backup_info['backup_dir']}") |
| 241 | + print(f" Repositories backed up: {len(backup_info['backed_up_repos'])}") |
| 242 | + if backup_info['failed_backups']: |
| 243 | + print(f" Failed backups: {len(backup_info['failed_backups'])}") |
| 244 | + |
| 245 | + # Processing summary |
| 246 | + print(Fore.CYAN + "\nFile Processing Summary:" + Style.RESET_ALL) |
| 247 | + print(f" Total files processed: {len(processed_files)}") |
| 248 | + print(f" Failed files: {len(failed_files)}") |
| 249 | + |
| 250 | + # Categorize results |
| 251 | + successful_refactoring = [f for f in processed_files if f['status'] == 'success'] |
| 252 | + no_refactoring_needed = [f for f in processed_files if f['status'] == 'no_refactoring'] |
| 253 | + files_with_antipatterns = [f for f in processed_files if f.get('antipatterns_found', False)] |
| 254 | + total_antipatterns = sum(f.get('antipatterns_count', 0) for f in processed_files) |
| 255 | + |
| 256 | + print(Fore.GREEN + f" Successfully refactored: {len(successful_refactoring)}" + Style.RESET_ALL) |
| 257 | + print(Fore.YELLOW + f" No refactoring needed: {len(no_refactoring_needed)}" + Style.RESET_ALL) |
| 258 | + print(Fore.RED + f" Failed: {len(failed_files)}" + Style.RESET_ALL) |
| 259 | + print(Fore.MAGENTA + f" Files with anti-patterns: {len(files_with_antipatterns)}" + Style.RESET_ALL) |
| 260 | + print(Fore.MAGENTA + f" Total anti-patterns found: {total_antipatterns}" + Style.RESET_ALL) |
| 261 | + |
| 262 | + # Statistics |
| 263 | + if processed_files: |
| 264 | + refactor_rate = len(successful_refactoring) / len(processed_files) * 100 |
| 265 | + antipattern_rate = len(files_with_antipatterns) / len(processed_files) * 100 |
| 266 | + |
| 267 | + |
| 268 | + print(Fore.CYAN + "\nProcessing Statistics:" + Style.RESET_ALL) |
| 269 | + print(f" Refactoring success rate: {refactor_rate:.1f}%") |
| 270 | + print(f" Anti-pattern detection rate: {antipattern_rate:.1f}%") |
| 271 | + print(f" Total anti-patterns found: {total_antipatterns}") |
| 272 | + |
| 273 | + # Show detailed results |
| 274 | + if successful_refactoring: |
| 275 | + print(Fore.GREEN + "\nSuccessfully refactored files:" + Style.RESET_ALL) |
| 276 | + for file_info in successful_refactoring: |
| 277 | + antipatterns_info = f" (antipatterns: {file_info.get('antipatterns_count', 0)})" if file_info.get('antipatterns_count', 0) > 0 else "" |
| 278 | + print(f"{file_info['file_path']} (reviews: {file_info['code_review_times']}){antipatterns_info}") |
| 279 | + |
| 280 | + if failed_files: |
| 281 | + print(Fore.RED + "\nFailed files:" + Style.RESET_ALL) |
| 282 | + for file_path in failed_files: |
| 283 | + print(f"{file_path}") |
| 284 | + |
| 285 | + print(Fore.GREEN + f"\nBatch processing complete!" + Style.RESET_ALL) |
| 286 | + print(Fore.CYAN + f"Repository backups available at: {backup_info['backup_dir']}" + Style.RESET_ALL) |
| 287 | + print(f"To restore a repository, copy from backup directory back to original location.") |
| 288 | + |
| 289 | + # Intermediate results information |
| 290 | + print(Fore.MAGENTA + f"\nIntermediate Results:" + Style.RESET_ALL) |
| 291 | + print(f" Individual file analysis results saved in: ../processing_results/") |
| 292 | + if summary_file: |
| 293 | + print(f" Comprehensive summary saved: {Path(summary_file).name}") |
0 commit comments