|
| 1 | +import logging |
| 2 | + |
| 3 | +import modal |
| 4 | +from codegen import CodegenApp, Codebase |
| 5 | +from codegen.extensions.github.types.events.pull_request import PullRequestLabeledEvent |
| 6 | +from codegen.extensions.tools.github.create_pr_comment import create_pr_comment |
| 7 | +from dotenv import load_dotenv |
| 8 | +import networkx as nx |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +cg = CodegenApp(name="codegen-github-checks") |
| 16 | + |
| 17 | + |
| 18 | +def create_graph_from_codebase(repo_path): |
| 19 | + """Create a directed graph representing import relationships in a codebase.""" |
| 20 | + codebase = Codebase.from_repo(repo_path) |
| 21 | + G = nx.MultiDiGraph() |
| 22 | + |
| 23 | + for imp in codebase.imports: |
| 24 | + if imp.from_file and imp.to_file: |
| 25 | + G.add_edge( |
| 26 | + imp.to_file.filepath, |
| 27 | + imp.from_file.filepath, |
| 28 | + color="red" if getattr(imp, "is_dynamic", False) else "black", |
| 29 | + label="dynamic" if getattr(imp, "is_dynamic", False) else "static", |
| 30 | + is_dynamic=getattr(imp, "is_dynamic", False), |
| 31 | + ) |
| 32 | + return G |
| 33 | + |
| 34 | + |
| 35 | +def convert_all_calls_to_kwargs(codebase): |
| 36 | + for file in codebase.files: |
| 37 | + for function_call in file.function_calls: |
| 38 | + function_call.convert_args_to_kwargs() |
| 39 | + |
| 40 | + print("All function calls have been converted to kwargs") |
| 41 | + |
| 42 | + |
| 43 | +def find_import_cycles(G): |
| 44 | + """Identify strongly connected components (cycles) in the import graph.""" |
| 45 | + cycles = [scc for scc in nx.strongly_connected_components(G) if len(scc) > 1] |
| 46 | + print(f"🔄 Found {len(cycles)} import cycles.") |
| 47 | + |
| 48 | + for i, cycle in enumerate(cycles, 1): |
| 49 | + print(f"\nCycle #{i}: Size {len(cycle)} files") |
| 50 | + print(f"Total number of imports in cycle: {G.subgraph(cycle).number_of_edges()}") |
| 51 | + |
| 52 | + print("\nFiles in this cycle:") |
| 53 | + for file in cycle: |
| 54 | + print(f" - {file}") |
| 55 | + |
| 56 | + return cycles |
| 57 | + |
| 58 | + |
| 59 | +def find_problematic_import_loops(G, cycles): |
| 60 | + """Identify cycles with both static and dynamic imports between files.""" |
| 61 | + problematic_cycles = [] |
| 62 | + |
| 63 | + for i, scc in enumerate(cycles): |
| 64 | + if i == 2: |
| 65 | + continue |
| 66 | + |
| 67 | + mixed_imports = {} |
| 68 | + for from_file in scc: |
| 69 | + for to_file in scc: |
| 70 | + if G.has_edge(from_file, to_file): |
| 71 | + edges = G.get_edge_data(from_file, to_file) |
| 72 | + dynamic_count = sum(1 for e in edges.values() if e["color"] == "red") |
| 73 | + static_count = sum(1 for e in edges.values() if e["color"] == "black") |
| 74 | + |
| 75 | + if dynamic_count > 0 and static_count > 0: |
| 76 | + mixed_imports[(from_file, to_file)] = { |
| 77 | + "dynamic": dynamic_count, |
| 78 | + "static": static_count, |
| 79 | + "edges": edges, |
| 80 | + } |
| 81 | + |
| 82 | + if mixed_imports: |
| 83 | + problematic_cycles.append({"files": scc, "mixed_imports": mixed_imports, "index": i}) |
| 84 | + |
| 85 | + print(f"Found {len(problematic_cycles)} cycles with potentially problematic imports.") |
| 86 | + |
| 87 | + for i, cycle in enumerate(problematic_cycles): |
| 88 | + print(f"\n⚠️ Problematic Cycle #{i + 1} (Index {cycle['index']}): Size {len(cycle['files'])} files") |
| 89 | + print("\nFiles in cycle:") |
| 90 | + for file in cycle["files"]: |
| 91 | + print(f" - {file}") |
| 92 | + print("\nMixed imports:") |
| 93 | + for (from_file, to_file), imports in cycle["mixed_imports"].items(): |
| 94 | + print(f"\n From: {from_file}") |
| 95 | + print(f" To: {to_file}") |
| 96 | + print(f" Static imports: {imports['static']}") |
| 97 | + print(f" Dynamic imports: {imports['dynamic']}") |
| 98 | + |
| 99 | + return problematic_cycles |
| 100 | + |
| 101 | + |
| 102 | +@cg.github.event("pull_request:labeled") |
| 103 | +def handle_pr(event: PullRequestLabeledEvent): |
| 104 | + codebase = Codebase.from_repo(event.repository.get("full_name"), commit=event.pull_request.head.sha) |
| 105 | + |
| 106 | + G = create_graph_from_codebase(event.repository.get("full_name")) |
| 107 | + cycles = find_import_cycles(G) |
| 108 | + problematic_loops = find_problematic_import_loops(G, cycles) |
| 109 | + |
| 110 | + # Build comment message |
| 111 | + message = ["### Import Cycle Analysis - GitHub Check\n"] |
| 112 | + |
| 113 | + if problematic_loops: |
| 114 | + message.append("\n### ⚠️ Potentially Problematic Import Cycles") |
| 115 | + message.append("Cycles with mixed static and dynamic imports, which might recquire attention.") |
| 116 | + for i, cycle in enumerate(problematic_loops, 1): |
| 117 | + message.append(f"\n#### Problematic Cycle {i}") |
| 118 | + for (from_file, to_file), imports in cycle["mixed_imports"].items(): |
| 119 | + message.append(f"\nFrom: `{from_file}`") |
| 120 | + message.append(f"To: `{to_file}`") |
| 121 | + message.append(f"- Static imports: {imports['static']}") |
| 122 | + message.append(f"- Dynamic imports: {imports['dynamic']}") |
| 123 | + else: |
| 124 | + message.append("\nNo problematic import cycles found! 🎉") |
| 125 | + |
| 126 | + create_pr_comment( |
| 127 | + codebase, |
| 128 | + event.pull_request.number, |
| 129 | + "\n".join(message), |
| 130 | + ) |
| 131 | + |
| 132 | + return { |
| 133 | + "message": "PR event handled", |
| 134 | + "num_files": len(codebase.files), |
| 135 | + "num_functions": len(codebase.functions), |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | +base_image = ( |
| 140 | + modal.Image.debian_slim(python_version="3.13") |
| 141 | + .apt_install("git") |
| 142 | + .pip_install( |
| 143 | + "codegen", |
| 144 | + ) |
| 145 | +) |
| 146 | + |
| 147 | +app = modal.App("codegen-test") |
| 148 | + |
| 149 | + |
| 150 | +@app.function(image=base_image, secrets=[modal.Secret.from_dotenv()]) |
| 151 | +@modal.asgi_app() |
| 152 | +def fastapi_app(): |
| 153 | + print("Starting codegen fastapi app") |
| 154 | + return cg.app |
0 commit comments