#!/usr/bin/env python3 """ Prepare PR from AI response Converts response format to PR format """ import json from pathlib import Path from datetime import datetime import sys def convert_response_to_pr(): """Convert AI response to PR format""" # Find latest response response_dir = Path('/shared/ai-gitops/responses') response_files = list(response_dir.glob('*_response.json')) if not response_files: print("No response files found") return False latest = max(response_files, key=lambda p: p.stat().st_mtime) print(f"Converting response: {latest.name}") with open(latest, 'r') as f: response = json.load(f) # Extract suggestions and build config suggestions = response.get('suggestions', []) config_lines = [] for suggestion in suggestions: if 'config' in suggestion: config_lines.append(suggestion['config']) if not config_lines: print("No configuration in response") return False # Create pending PR directory and file pr_dir = Path('/shared/ai-gitops/pending_prs') pr_dir.mkdir(parents=True, exist_ok=True) pr_file = pr_dir / f"pr_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" pr_data = { "title": f"AI Network Optimization - {response.get('focus_area', 'general').title()}", "suggestions": '\n'.join(config_lines), "model": "llama2:13b", "feedback_aware": response.get('feedback_aware', True), "feedback_count": 6, "timestamp": datetime.now().isoformat(), "focus_area": response.get('focus_area', 'security') } with open(pr_file, 'w') as f: json.dump(pr_data, f, indent=2) print(f"✅ Created PR file: {pr_file.name}") return True if __name__ == "__main__": if convert_response_to_pr(): sys.exit(0) else: sys.exit(1)