התאמה אישית של ההתנהגות של Gemini Code Assist ב-GitHub

אם יש לכם קבוצה מותאמת אישית של שיטות מומלצות או מוסכמות שאתם רוצים ש-Gemini Code Assist ב-GitHub יבדוק, אתם יכולים להוסיף קובץ styleguide.md לתיקיית הבסיס .gemini/ של המאגר. משתמשים בגרסה לארגונים של Gemini Code Assist ב-GitHub יכולים להשתמש במסוף Google Cloud כדי להוסיף מידע על מדריך סגנון לשימוש במספר מאגרים. בשני המקרים, מדריך הסגנון נחשב לקובץ Markdown רגיל, והוא מרחיב את ההנחיה הרגילה שבה משתמש Gemini Code Assist ב-GitHub. הוראות להוספת מדריך סגנון מופיעות במאמר בנושא הוספת קובצי הגדרה.

דפוסים סטנדרטיים של בקרת קוד

אם לא מציינים מדריכי סגנון מותאמים אישית, אלה הקטגוריות העיקריות של תחומים שבהם Gemini Code Assist מתמקד בבדיקת הקוד:

  • נכונות: מוודא שהקוד פועל כמצופה ומטפל במקרים חריגים, בודק שגיאות לוגיות, תנאי מירוץ או שימוש לא נכון ב-API.

  • יעילות: זיהוי צווארי בקבוק פוטנציאליים בביצועים או תחומים לאופטימיזציה, כמו לולאות מוגזמות, דליפות זיכרון, מבני נתונים לא יעילים, חישובים מיותרים, רישום מוגזם ביומן ומניפולציה לא יעילה של מחרוזות.

  • יכולת תחזוקה: הערכה של קריאות הקוד, המודולריות וההקפדה על ניבים של השפה ושיטות מומלצות. הכלי מזהה שמות לא טובים של משתנים, פונקציות ומחלקות, חוסר הערות או תיעוד, קוד מורכב, כפילויות בקוד, פורמט לא עקבי ומספרים קסומים.

  • אבטחה: מזהה נקודות חולשה פוטנציאליות בטיפול בנתונים או באימות קלט, כמו אחסון לא מאובטח של מידע אישי רגיש, מתקפות הזרקה, אמצעי בקרה לא מספיקים לגישה, זיוף בקשות באתרים שונים (CSRF) והפניות לא מאובטחות לאובייקטים ישירים (IDOR).

  • שונות: נושאים אחרים נלקחים בחשבון כשבודקים את בקשת המיזוג, כמו בדיקות, ביצועים, יכולת הרחבה, מודולריות ושימוש חוזר, ורישום ביומן ומעקב אחר שגיאות.

הוספת קובצי תצורה

אפשר לשנות את ההתנהגות של Gemini Code Assist על ידי הוספה של קובצי תצורה נתמכים לתיקייה .gemini/ שנמצאת בבסיס המאגר. ‫Gemini Code Assist משתמש בקבצים הבאים, אם הוספתם אותם לתיקייה .gemini/:

  • config.yaml: קובץ שמכיל תכונות שונות שאפשר להגדיר, להפעיל או להשבית, כולל ציון קבצים להתעלמות באמצעות דפוסי glob.

  • styleguide.md: קובץ Markdown שמכיל הוראות ל-Gemini Code Assist עם כללים ספציפיים שרוצים שהוא יפעל לפיהם במהלך בדיקת הקוד.

config.yaml דוגמה

קטע הקוד הבא הוא דוגמה לקובץ config.yaml. בדוגמה הזו, כל מאפיין מוגדר לערך ברירת המחדל שבו משתמש Gemini Code Assist. אפשר להשתמש בקטע הקוד הזה כתבנית ליצירת קובץ config.yaml משלכם:

have_fun: false
memory_config:
  disabled: 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: [].
  memory_config:
    type: object
    description: Configuration for persistent memory, which is used to improve responses.
    properties:
      disabled:
        type: boolean
        description: Whether to disable persistent memory for this specific repository, if Improve response quality has been enabled for multiple repositories. Default: false.
  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.

ניהול תצורות בכמה מאגרים

אפשר לנהל כמה היבטים של Gemini Code Assist ב-GitHub בכמה מאגרים:

  • אם יש לכם את הגרסה לצרכנים, אתם יכולים להחליף את המצב של הגדרות מסוימות לכל המאגרים שמשויכים לחשבון.

  • אם יש לכם את גרסת Enterprise, אתם יכולים להפעיל או להשבית הגדרות מסוימות לכמה מאגרי קוד, ואתם יכולים להחיל מדריך סגנון אחד על כמה מאגרי קוד. מאגרי הנתונים מקובצים לפי חיבור Developer Connect, וניהול ההגדרות המשותפות ומדריך הסגנון שלהם מתבצע דרך מסוף Google Cloud.

אם מאגר מנוהל כחלק מקבוצה אבל יש לו גם config.yaml או styleguide.md משלו, מתרחשת ההתנהגות הבאה:

  • ההגדרות של מאגר config.yaml מבטלות את ההגדרות של הקבוצה.

  • ה-styleguide.md של המאגר משולב עם מדריך הסגנון של הקבוצה.

בשלבים הבאים מוסבר איך לשלוט בקבוצה אחת של הגדרות, ובמקרה של משתמשים בגרסת Enterprise, במדריך סגנון אחד, בכמה מאגרי מידע. אנחנו יוצאים מנקודת הנחה שכבר הגדרתם את Gemini Code Assist ב-GitHub.

צרכנות

  1. ב-Gemini Code Assist, עוברים לדף ההגדרות.

    כניסה להגדרות

  2. אם תתבקשו לעשות זאת, לחצו על Log in with GitHub (כניסה באמצעות GitHub) ופועלים לפי ההנחיות כדי להיכנס ל-GitHub מתוך Gemini Code Assist.

  3. בדף Gemini, בוחרים את החשבון שרוצים לעבוד איתו, מעיינים בתנאים ובהגבלות ולוחצים על המשך.

  4. בדף Free agent, מעדכנים את ההגדרות לפי הצורך.

  5. לוחצים על שמירה.

Enterprise

  1. במסוף Google Cloud, עוברים לדף Agents & Tools של Gemini Code Assist.

    מעבר אל Agents & Tools

  2. בקטע סוכנים, מאתרים את הכרטיס ניהול קוד מקור של Code Assist ולוחצים על מתקדם.

    החלונית עריכת ניהול קוד מקור של Code Assist נפתחת.

  3. בטבלה Connections, לוחצים על שם החיבור שרוצים להחיל עליו הגדרה או מדריך סגנון.

    ייפתח דף הפרטים של החיבור.

  4. בכרטיסייה הגדרות, מעדכנים את ההגדרות שרוצים לשנות.

  5. בכרטיסייה Style guide (הנחיות סגנון), מוסיפים את הנחיות הסגנון שרוצים שהמאגרים שמשויכים לחיבור הזה ישתמשו בהן.

  6. לוחצים על שמירה.