|
|
Gemini API for Code Review and Documentation
Author: Venkata Sudhakar
Gemini can review source code and produce actionable findings covering security, performance, error handling, and style. ShopMax India uses this in their CI/CD pipeline to flag issues before human review. The same capability generates Google-style docstrings from existing undocumented functions. Pass code as a text block with clear instructions about what to check. For security, list vulnerability categories explicitly. For docstrings, specify the format so output can be pasted directly into the codebase. The below example shows how ShopMax India automates code review and docstring generation for their Python microservices.
It gives the following output,
[HIGH] SQL Injection: String concatenation builds SQL query.
Fix: use parameterised query: WHERE customer_id = %s
[HIGH] N+1 Query: get_product() called in loop for each order.
Fix: collect all product_ids then fetch with one IN query.
[MEDIUM] No error handling: db.connect() can raise exceptions.
Fix: wrap in try/finally to ensure connection is closed.
[LOW] Missing type hints and docstring.
For docstring generation, pass undocumented functions and ask Gemini to add Google-style docstrings without changing logic.
It gives the following output,
def calculate_discount(price: float, tier: str) -> float:
"""Calculate discounted price for ShopMax India loyalty tiers.
Args:
price: Original product price in Rs.
tier: Customer tier - gold, silver, or standard.
Returns:
Discounted price. Gold 15% off, Silver 10% off.
"""
if tier == "gold":
return price * 0.85
Integrate into ShopMax India CI/CD: a Cloud Build step calls the review function on changed Python files in each pull request and posts findings as GitHub comments. HIGH severity issues block the merge automatically.
|
|