This application is a GitHub integration that analyzes import cycles in codebases. It automatically runs when a pull request is labeled and checks for potentially problematic import patterns in the modified codebase.
- Analyzes import relationships in codebases
- Detects circular import dependencies
- Identifies problematic cycles with mixed static and dynamic imports
- Automatically comments on pull requests with detailed analysis
-
The app creates a directed graph representing import relationships in the codebase
for imp in codebase.imports: if imp.from_file and imp.to_file: G.add_edge( imp.to_file.filepath, imp.from_file.filepath, color="red" if getattr(imp, "is_dynamic", False) else "black", label="dynamic" if getattr(imp, "is_dynamic", False) else "static", is_dynamic=getattr(imp, "is_dynamic", False), )
-
It identifies strongly connected components (cycles) in the import graph
cycles = [scc for scc in nx.strongly_connected_components(G) if len(scc) > 1]
-
It specifically flags cycles that contain both static and dynamic imports
dynamic_count = sum(1 for e in edges.values() if e["color"] == "red") static_count = sum(1 for e in edges.values() if e["color"] == "black") if dynamic_count > 0 and static_count > 0: mixed_imports[(from_file, to_file)] = { "dynamic": dynamic_count, "static": static_count, "edges": edges, }
-
Results are posted as a comment on the pull request
message = ["### Import Cycle Analysis - GitHub Check\n"] if problematic_loops: message.append("\n### ⚠️ Potentially Problematic Import Cycles") message.append("Cycles with mixed static and dynamic imports, which might recquire attention.") for i, cycle in enumerate(problematic_loops, 1): message.append(f"\n#### Problematic Cycle {i}") for (from_file, to_file), imports in cycle["mixed_imports"].items(): message.append(f"\nFrom: `{from_file}`") message.append(f"To: `{to_file}`") message.append(f"- Static imports: {imports['static']}") message.append(f"- Dynamic imports: {imports['dynamic']}") else: message.append("\nNo problematic import cycles found! 🎉")
-
Ensure you have the following dependencies:
- Python 3.13
- Modal
- Codegen
- NetworkX
- python-dotenv
-
Set up your environment variables in a
.envfileGITHUB_TOKEN: Your GitHub token, configured withrepoandworkflowscopes.
-
Deploy the app using Modal:
modal deploy app.py
- After deployment, configure your GitHub App's webhook URL in its developer settings to point to your Modal endpoint with the endpoint
/github/events - The app will analyze imports via the Modal deployment whenever a pull request receives a
Codegenlabel
- After deployment, configure your GitHub App's webhook URL in its developer settings to point to your Modal endpoint with the endpoint
The application uses Codegen to parse the codebase and a combination of NetworkX and Codegen to analyze the import relationships. The app is structured as a Modal App with a FastAPI server.
The analysis runs when a pull request is labeled (pull_request:labeled event).
The analysis results are posted as a markdown-formatted comment on the pull request, including:
- Summary statistics
- Detailed cycle information
- Warning indicators for problematic import patterns
Feel free to submit issues and enhancement requests!