{"meta":{"title":"使用 Copilot 生成优化的评审过程","intro":"使用 Copilot 自动化评审以优化和改进评审过程。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/enterprise-cloud@latest/copilot","title":"GitHub Copilot"},{"href":"/zh/enterprise-cloud@latest/copilot/tutorials","title":"教程"},{"href":"/zh/enterprise-cloud@latest/copilot/tutorials/optimize-code-reviews","title":"优化代码评审"}],"documentType":"article"},"body":"# 使用 Copilot 生成优化的评审过程\n\n使用 Copilot 自动化评审以优化和改进评审过程。\n\n## 介绍\n\n在减少对次要实现细节（如命名和样式约定）花费的时间时，代码评审更高效，而是将精力集中在满足用户需求的更高级别设计、解决问题和功能上。\n\n在本文中，我们将展示如何通过自动评审 Copilot 优化你的审阅过程，这样你可以减少在细微更改上花费的时间，更多地专注于对问题解决的细致探讨和对实现过程的深入理解，从而不仅仅是充分，而是巧妙地满足用户需求。\n\n## 1. 提升来自 Copilot 的审查质量\n\n```\n          Copilot 代码评审 可以为存储库中的所有拉取请求提供自动评审，并通过捕获代码中不需要的更改提高审阅效率。 与自定义说明配对时，会更有效， Copilot 代码评审 因为它可以提供根据团队工作方式、使用的工具或项目细节而定制的响应。 \n```\n\n编写自定义说明的最佳做法包括：\n\n* 不同的标题\n* 要点\n* 简短的直接说明\n\n我们来看一个示例。 如果要使用 Python 生成订单处理系统，自定义说明可能包括特定于Python的格式、性能和安全编码做法，以及与项目直接相关的指南。 以下示例显示了自定义指令的几行可能是什么样子。\n\n```markdown\n## Repository context\n- This repository implements an order processing system (order intake, payment, fulfillment) where correctness, security, and auditability are critical. \n\n## Style and conventions\n- Follow the PEP 8 and PEP 257 style guide for Python.\n- Use clear, domain-relevant names (orders, payments, inventory, customers, shipments).\n- Prefer small, focused functions and methods with clearly defined responsibilities.\n\n## Secure coding \n- Verify proper input validation and sanitization.\n- Review authentication and authorization logic.\n\n## Error handling guidelines\n- Handle timeouts and network errors gracefully.\n- Ensure failures are logged with enough detail for debugging.\n\n## Order processing context\n- Ensure order creation, payment handling, and updates are idempotent to avoid duplicate orders or duplicate charges.\n- Validate and normalize all order, payment, and customer data before persisting or acting on it.\n- Do not log or persist sensitive data (passwords, raw payment details, full identifiers) without hashing, encryption, or redaction.\n- Call out obvious performance issues in core order workflows (e.g., N+1 queries, per-order synchronous network calls) and suggest simpler, more efficient alternatives.\n\n## Review style\n- Be concise, specific and actionable.\n- Explain the \"why\" behind recommendations using bullet points.\n```\n\n在下一部分，我们将演示自定义指令如何正向影响 Copilot 在 PR 中给出的审查评论。\n\n## 2. 自动审核以改进样式规范和性能\n\n当自动 Copilot 代码评审 启用时，Copilot 会在拉取请求从草稿状态移出后自动进行审查。\n\n假设你提交一个拉取请求，其中包含以下函数来更新订单处理系统中的库存。\n\n```python\ndef notify_inventory(product_id, quantity):\n    requests.post(\"http://inventory-service/update\", json={\"product_id\": product_id, \"quantity\": quantity})\n```\n\n虽然这可以正常运行，但它缺乏清晰性和稳健性。 类型的意图是隐式的，没有文档字符串，它不包括错误处理或日志记录，这使得它不清楚通知是否实际工作。\n\n```\n          Copilot 将查看代码，并可能提出以下注释和代码建议。\n```\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\n```copilot\nMissing error handling for network calls\n\nThe notify_inventory function makes a synchronous network call without any error handling for timeouts, connection failures, or non-200 responses.\n\nWhy this matters:\n\n- Network failures will cause unhandled exceptions and crash the order processing flow\n- Orders could be marked as CONFIRMED but inventory never gets updated\n- This violates the requirement to \"handle timeouts and network errors gracefully\"\n- Failures won't be logged with enough detail for debugging\n\nRecommendation: Add proper error handling with timeout and logging:\n```\n\n```python\ndef notify_inventory(product_id: str, quantity: int) -> bool:\n    \"\"\"Notify the inventory service of order placement.\n\n    Args:\n        product_id: The product identifier.\n        quantity: The quantity ordered.\n\n    Returns:\n        True if the notification succeeded; False otherwise.\n    \"\"\"\n    try:\n        response = requests.post(\n            \"http://inventory-service/update\",\n            json={\"product_id\": product_id, \"quantity\": quantity},\n            timeout=5,\n        )\n        response.raise_for_status()\n        logger.info(\n            \"Inventory notified for product %s, quantity %d (status %d)\",\n            product_id,\n            quantity,\n            response.status_code,\n        )\n        return True\n    except requests.exceptions.Timeout:\n        logger.error(\n            \"Timeout notifying inventory for product %s, quantity %d\",\n            product_id,\n            quantity,\n            exc_info=True,\n        )\n    except requests.exceptions.RequestException:\n        logger.error(\n            \"Failed to notify inventory for product %s, quantity %d\",\n            product_id,\n            quantity,\n            exc_info=True,\n        )\n    return False\n```\n\nCopilot 的响应遵循你在自定义指令中提供的指导原则。 在此示例中，它遵循适用于Python的 PEP 8 和 257 样式指南，并建议改进以更好地处理超时和错误日志记录，同时简明地解释建议背后的推理。\n\n> \\[!NOTE] 在接受和提交之前，请始终仔细查看 Copilot 的建议。\n\n在编码时，自动审查评论可以帮助你更好地理解；在审阅时，它们能帮助你专注于对他人的反馈并缩小其范围。\n\n## 3. 标记安全漏洞并修复它们\n\n接下来，假设你的任务是改进密码存储在订单处理系统中的方式。 可使用认为足以对用户密码进行哈希处理以便保护用户密码的代码提交拉取请求。\n\n```python\ndef get_password_hash(password: str, salt: str) -> str:\n    \"\"\"Hash a password with the given salt using SHA-256.\n\n    Returns the hexadecimal representation of the hashed password.\n    \"\"\"\n    return hashlib.sha256((password + salt).encode()).hexdigest()\n\nclass User:\n    \"\"\"Represents a user in the order processing system.\"\"\"\n\n    def __init__(self, username: str, password: str, salt: str):\n        \"\"\"Initialize a User with username, password, and salt.\n\n        The password is hashed and stored for authentication.\n        \"\"\"\n        self.username = username\n        self.salt = salt\n        self.password_hash = get_password_hash(password, self.salt)\n\n    def verify_password(self, password: str) -> bool:\n        \"\"\"Verify a plain-text password against the stored hash.\"\"\"\n        return get_password_hash(password, self.salt) == self.password_hash\n```\n\n但是，在此示例中，使用 SHA-256 是不可接受的，因为它的计算成本不足以保护用户密码。\n\n虽然 Copilot 代码评审 可以提出安全最佳实践建议，但针对 Copilot自动修复 的 code scanning 方案更进一步。 通过利用code scanning的能力结合CodeQL分析来分析GitHub存储库中的代码并查找安全漏洞和编码错误，Copilot自动修复然后可以对扫描结果提出修复建议，从而更有效地预防和减少漏洞。\n\n例如， Copilot自动修复 可以对代码进行以下注释。\n\n```copilot\nUsing SHA-256 for password hashing is insecure for authentication systems. SHA-256 is designed to be fast, making it vulnerable to brute-force attacks. \n\nTo fix the problem, use a password-specific hashing algorithm like bcrypt, scrypt, or argon2 (e.g., `argon2-cffi` from the PyPI package) which are designed to be slow and include built-in salting mechanisms.\n```\n\n```\n          Copilot自动修复 还将针对漏洞进行潜在修复的代码建议供你查看。 在这种情况下，它可能会提出代码建议（如下所示）导入包并更新与哈希密码相关的代码。 \n```\n\n```python\nfrom argon2 import PasswordHasher\n```\n\n```python\ndef get_initial_hash(password: str):\n    ph = PasswordHasher()\n    return ph.hash(password)\n\ndef check_password(password: str, known_hash):\n    ph = PasswordHasher()\n    return ph.verify(known_hash, password)\n```\n\n> \\[!NOTE]\n>\n> * 在接受 Copilot 提议的任何更改之前，请始终核实并确认。\n> * 在此示例中， Copilot 代码评审 还可以突出显示生成唯一盐的必要性。\n\n如你所看到的，自动识别漏洞以及修复漏洞的建议有助于确保安全成为优先事项。\nCopilot自动修复 使你能够专注于了解安全编码以及最适合代码库和项目的修补程序。\n\n## 使用 Copilot 优化了评论\n\n自动审阅注释可帮助你优化评论并更有效地保护代码，而不管你的体验级别如何。\n\n* 自定义说明帮助优化Copilot 代码评审的响应，使其更符合我们的项目和用户需求。我们还了解到如何定制Copilot在反馈中提供的解释程度。\n* Copilot 代码评审  帮助我们快速改进错误日志记录，并了解为什么很重要。\n* ```\n            针对 Copilot自动修复 的 code scanning 方案帮助我们避免使用不充分的密码哈希方式，并保护用户数据。   \n  ```\n\n## 后续步骤\n\n若要使用 Copilot 的评审功能提高评论的效率和有效性，请按照以下步骤开始操作。\n\n1. 自定义说明的创建应结合项目和代码库的具体要求。 自行编写，或从我们的示例库中获取灵感。 请参阅“[自定义说明](/zh/enterprise-cloud@latest/copilot/tutorials/customization-library/custom-instructions)”。\n2. 若要为存储库启用自动 Copilot 代码评审 ，请参阅 [通过GitHub Copilot配置自动代码评审](/zh/enterprise-cloud@latest/copilot/how-tos/copilot-on-github/set-up-copilot/configure-automatic-review)。\n3. 若要为存储库配置 Copilot自动修复 ，需要启用 code scanning。 一旦code scanning启用CodeQL分析，Copilot自动修复就默认启用。 有关最简单的设置，请参阅 [配置代码扫描的默认设置](/zh/enterprise-cloud@latest/code-security/code-scanning/enabling-code-scanning/configuring-default-setup-for-code-scanning)。\n\n## 延伸阅读\n\n若要更深入地查看 AI 生成的代码，请参阅 [查看 AI 生成的代码](/zh/enterprise-cloud@latest/copilot/tutorials/review-ai-generated-code)。"}