अगर आपके पास सबसे सही तरीकों या नियमों का कोई कस्टम सेट है और आपको GitHub पर Gemini Code Assist से उसकी जांच करानी है, तो अपनी रिपॉज़िटरी के .gemini/ रूट फ़ोल्डर में styleguide.md फ़ाइल जोड़ी जा सकती है. GitHub पर Gemini Code Assist के एंटरप्राइज़ वर्शन का इस्तेमाल करने वाले लोग, Google Cloud Console का इस्तेमाल करके स्टाइल गाइड की जानकारी जोड़ सकते हैं. इससे वे इस जानकारी का इस्तेमाल कई रिपॉज़िटरी में कर पाएंगे.
दोनों ही मामलों में, स्टाइल गाइड को सामान्य मार्कडाउन फ़ाइल के तौर पर माना जाता है. साथ ही, यह GitHub पर Gemini Code Assist के इस्तेमाल किए जाने वाले स्टैंडर्ड प्रॉम्प्ट को बड़ा करता है. स्टाइल गाइड जोड़ने के बारे में निर्देश जानने के लिए, कॉन्फ़िगरेशन फ़ाइलें जोड़ना लेख पढ़ें.
कोड की समीक्षा करने के स्टैंडर्ड पैटर्न
जब कस्टम स्टाइल गाइड तय नहीं की जाती हैं, तो Gemini Code Assist इन मुख्य कैटगरी के कोड की समीक्षा करता है:
सही होना: यह पक्का करता है कि कोड, उम्मीद के मुताबिक काम करे और मुश्किल मामलों को हैंडल करे. साथ ही, लॉजिक से जुड़ी गड़बड़ियों, रेस कंडीशन या एपीआई के गलत इस्तेमाल की जांच करता है.
परफ़ॉर्मेंस: इससे परफ़ॉर्मेंस से जुड़ी संभावित समस्याओं या ऑप्टिमाइज़ेशन के लिए उपलब्ध जगहों का पता चलता है. जैसे, बहुत ज़्यादा लूप, मेमोरी लीक, डेटा स्ट्रक्चर का सही तरीके से इस्तेमाल न होना, बार-बार की जाने वाली कैलकुलेशन, बहुत ज़्यादा लॉगिंग, और स्ट्रिंग में बदलाव करने का सही तरीका इस्तेमाल न करना.
बदलाव करने में आसानी: इससे कोड को पढ़ने में आसानी होती है. साथ ही, यह कोड के मॉड्यूलर होने और भाषा के मुहावरों और सबसे सही तरीकों का पालन करने का आकलन करता है. यह वैरिएबल, फ़ंक्शन, और क्लास के लिए खराब नामकरण, टिप्पणियों या दस्तावेज़ों की कमी, जटिल कोड, कोड डुप्लीकेशन, फ़ॉर्मैटिंग में अंतर, और मैजिक नंबर को टारगेट करता है.
सुरक्षा: यह डेटा हैंडलिंग या इनपुट की पुष्टि करने से जुड़ी संभावित कमज़ोरियों की पहचान करता है. जैसे, संवेदनशील डेटा को असुरक्षित तरीके से सेव करना, इंजेक्शन अटैक, ऐक्सेस कंट्रोल का सही तरीके से इस्तेमाल न करना, क्रॉस-साइट रिक्वेस्ट फ़ोर्जरी (सीएसआरएफ़), और असुरक्षित डायरेक्ट ऑब्जेक्ट रेफ़रंस (आईडीओआर).
अन्य: पुल अनुरोध की समीक्षा करते समय, अन्य विषयों पर भी विचार किया जाता है. जैसे, टेस्टिंग, परफ़ॉर्मेंस, स्केलेबिलिटी, मॉड्युलैरिटी और दोबारा इस्तेमाल करने की क्षमता, और गड़बड़ी लॉगिंग और मॉनिटरिंग.
कॉन्फ़िगरेशन फ़ाइलें जोड़ना
Gemini Code Assist के काम करने के तरीके में बदलाव किया जा सकता है. इसके लिए, आपको अपनी रिपॉज़िटरी के रूट में मौजूद .gemini/ फ़ोल्डर में, कॉन्फ़िगरेशन फ़ाइलें जोड़नी होंगी. अगर आपने इन फ़ाइलों को .gemini/ फ़ोल्डर में जोड़ा है, तो Gemini Code Assist इनका इस्तेमाल करेगा:
config.yaml: यह एक ऐसी फ़ाइल होती है जिसमें कॉन्फ़िगर की जा सकने वाली कई सुविधाएँ होती हैं. इन्हें चालू या बंद किया जा सकता है. इसमें ग्लोब पैटर्न का इस्तेमाल करके, अनदेखी की जाने वाली फ़ाइलें तय करने की सुविधा भी शामिल है.styleguide.md: यह एक मार्कडाउन फ़ाइल है. इसमें Gemini Code Assist को कुछ खास नियमों के बारे में बताया जाता है. ये वे नियम होते हैं जिनका पालन आपको कोड की समीक्षा करते समय करना होता है.
उदाहरण: config.yaml
यहां दिए गए कोड स्निपेट में, config.yaml फ़ाइल का उदाहरण दिया गया है. इस उदाहरण में, हर प्रॉपर्टी को Gemini Code Assist की ओर से इस्तेमाल की जाने वाली डिफ़ॉल्ट वैल्यू पर सेट किया गया है. इस स्निपेट का इस्तेमाल, अपनी config.yaml फ़ाइल बनाने के लिए टेंप्लेट के तौर पर किया जा सकता है:
have_fun: false
code_review:
disable: false
comment_severity_threshold: MEDIUM
max_review_comments: -1
pull_request_opened:
help: false
summary: true
code_review: true
include_drafts: true
ignore_patterns: []
config.yaml स्कीमा
नीचे दिया गया कोड स्निपेट, config.yaml फ़ाइल का स्कीमा है. इससे कॉन्फ़िगरेशन के सभी संभावित विकल्पों और उनकी मान्य वैल्यू के बारे में पता चलता है:
$schema: "http://json-schema.org/draft-07/schema#" title: RepoConfig description: Configuration for Gemini Code Assist on a repository. All fields are optional and have default values. type: object properties: have_fun: type: boolean description: Enables fun features such as a poem in the initial pull request summary. Default: false. ignore_patterns: type: array items: type: string description: A list of glob patterns for files and directories that Gemini Code Assist should ignore. Files matching any pattern in this list will be skipped during interactions. Default: []. code_review: type: object description: Configuration for code reviews. All fields are optional and have default values. properties: disable: type: boolean description: Disables Gemini from acting on pull requests. Default: false. comment_severity_threshold: type: string enum: - LOW - MEDIUM - HIGH - CRITICAL description: The minimum severity of review comments to consider. Default: MEDIUM. max_review_comments: type: integer format: int64 description: The maximum number of review comments to consider. Use -1 for unlimited. Default: -1. pull_request_opened: type: object description: Configuration for pull request opened events. All fields are optional and have default values. properties: help: type: boolean description: Posts a help message on pull request open. Default: false. summary: type: boolean description: Posts a pull request summary on the pull request open. Default: true. code_review: type: boolean description: Posts a code review on pull request open. Default: true. include_drafts: type: boolean description: Enables agent functionality on draft pull requests. Default: true.
styleguide.md
styleguide.md फ़ाइल में, स्कीमा तय नहीं किया गया है. इसके बजाय, यह एक सामान्य भाषा में बताया गया है कि आपको Gemini Code Assist से कोड की समीक्षा किस तरह करानी है. यहां दिए गए कोड स्निपेट में, styleguide.md फ़ाइल का उदाहरण दिया गया है:
# Company X Python Style Guide
# Introduction
This style guide outlines the coding conventions for Python code developed at Company X.
It's based on PEP 8, but with some modifications to address specific needs and
preferences within our organization.
# Key Principles
* **Readability:** Code should be easy to understand for all team members.
* **Maintainability:** Code should be easy to modify and extend.
* **Consistency:** Adhering to a consistent style across all projects improves
collaboration and reduces errors.
* **Performance:** While readability is paramount, code should be efficient.
# Deviations from PEP 8
## Line Length
* **Maximum line length:** 100 characters (instead of PEP 8's 79).
* Modern screens allow for wider lines, improving code readability in many cases.
* Many common patterns in our codebase, like long strings or URLs, often exceed 79 characters.
## Indentation
* **Use 4 spaces per indentation level.** (PEP 8 recommendation)
## Imports
* **Group imports:**
* Standard library imports
* Related third party imports
* Local application/library specific imports
* **Absolute imports:** Always use absolute imports for clarity.
* **Import order within groups:** Sort alphabetically.
## Naming Conventions
* **Variables:** Use lowercase with underscores (snake_case): `user_name`, `total_count`
* **Constants:** Use uppercase with underscores: `MAX_VALUE`, `DATABASE_NAME`
* **Functions:** Use lowercase with underscores (snake_case): `calculate_total()`, `process_data()`
* **Classes:** Use CapWords (CamelCase): `UserManager`, `PaymentProcessor`
* **Modules:** Use lowercase with underscores (snake_case): `user_utils`, `payment_gateway`
## Docstrings
* **Use triple double quotes (`"""Docstring goes here."""`) for all docstrings.**
* **First line:** Concise summary of the object's purpose.
* **For complex functions/classes:** Include detailed descriptions of parameters, return values,
attributes, and exceptions.
* **Use Google style docstrings:** This helps with automated documentation generation.
```python
def my_function(param1, param2):
"""Single-line summary.
More detailed description, if necessary.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
Raises:
ValueError: If `param2` is invalid.
"""
# function body here
```
## Type Hints
* **Use type hints:** Type hints improve code readability and help catch errors early.
* **Follow PEP 484:** Use the standard type hinting syntax.
## Comments
* **Write clear and concise comments:** Explain the "why" behind the code, not just the "what".
* **Comment sparingly:** Well-written code should be self-documenting where possible.
* **Use complete sentences:** Start comments with a capital letter and use proper punctuation.
## Logging
* **Use a standard logging framework:** Company X uses the built-in `logging` module.
* **Log at appropriate levels:** DEBUG, INFO, WARNING, ERROR, CRITICAL
* **Provide context:** Include relevant information in log messages to aid debugging.
## Error Handling
* **Use specific exceptions:** Avoid using broad exceptions like `Exception`.
* **Handle exceptions gracefully:** Provide informative error messages and avoid crashing the program.
* **Use `try...except` blocks:** Isolate code that might raise exceptions.
# Tooling
* **Code formatter:** [Specify formatter, e.g., Black] - Enforces consistent formatting automatically.
* **Linter:** [Specify linter, e.g., Flake8, Pylint] - Identifies potential issues and style violations.
# Example
```python
"""Module for user authentication."""
import hashlib
import logging
import os
from companyx.db import user_database
LOGGER = logging.getLogger(__name__)
def hash_password(password: str) -> str:
"""Hashes a password using SHA-256.
Args:
password (str): The password to hash.
Returns:
str: The hashed password.
"""
salt = os.urandom(16)
salted_password = salt + password.encode('utf-8')
hashed_password = hashlib.sha256(salted_password).hexdigest()
return f"{salt.hex()}:{hashed_password}"
def authenticate_user(username: str, password: str) -> bool:
"""Authenticates a user against the database.
Args:
username (str): The user's username.
password (str): The user's password.
Returns:
bool: True if the user is authenticated, False otherwise.
"""
try:
user = user_database.get_user(username)
if user is None:
LOGGER.warning("Authentication failed: User not found - %s", username)
return False
stored_hash = user.password_hash
salt, hashed_password = stored_hash.split(':')
salted_password = bytes.fromhex(salt) + password.encode('utf-8')
calculated_hash = hashlib.sha256(salted_password).hexdigest()
if calculated_hash == hashed_password:
LOGGER.info("User authenticated successfully - %s", username)
return True
else:
LOGGER.warning("Authentication failed: Incorrect password - %s", username)
return False
except Exception as e:
LOGGER.error("An error occurred during authentication: %s", e)
return False
```
एक से ज़्यादा रिपॉज़िटरी में कॉन्फ़िगरेशन फ़ाइलें मैनेज करना
अगर आपके पास GitHub पर Gemini Code Assist का एंटरप्राइज़ वर्शन है, तो Google Cloud Console का इस्तेमाल करके, कॉन्फ़िगरेशन का एक सेट और एक स्टाइल गाइड को उन सभी रिपॉज़िटरी पर लागू किया जा सकता है जो Developer Connect कनेक्शन में लिंक हैं.
ध्यान दें कि अगर इस तरह से मैनेज की गई किसी रिपॉज़िटरी में अपना config.yaml या styleguide.md भी है, तो ऐसा होगा:
रिपॉज़िटरी की
config.yamlसेटिंग, Google Cloud Console की सेटिंग को बदल देती हैं.इस रिपॉज़िटरी के
styleguide.mdको Google Cloud Console की स्टाइल गाइड के साथ जोड़ दिया गया है.
यहां दिए गए चरणों में बताया गया है कि एक से ज़्यादा रिपॉज़िटरी में, कॉन्फ़िगरेशन के एक सेट और एक स्टाइल गाइड को कैसे कंट्रोल किया जाता है. इन चरणों में यह माना गया है कि आपने पहले Enterprise वर्शन सेट अप किया है.
Google Cloud Console में, Gemini Code Assist एजेंट और टूल पेज पर जाएं.
एजेंट सेक्शन में, कोड असिस्ट सोर्स कोड मैनेजमेंट कार्ड ढूंढें. इसके बाद, ऐडवांस पर क्लिक करें.
Code Assist की सोर्स कोड मैनेजमेंट सुविधा में बदलाव करें पैनल खुलता है.
कनेक्शन टेबल में, उस कनेक्शन के नाम पर क्लिक करें जिस पर आपको कॉन्फ़िगरेशन या स्टाइल गाइड लागू करनी है.
कनेक्शन की जानकारी वाला पेज खुलता है.
सेटिंग टैब में जाकर, उन सेटिंग को अपडेट करें जिनमें आपको बदलाव करना है.
स्टाइल गाइड टैब में, वह स्टाइल गाइड जोड़ें जिसका इस्तेमाल इस कनेक्शन से जुड़ी रिपॉज़िटरी को करना है.
सेव करें पर क्लिक करें.