{"meta":{"title":"从GitHub问题实现功能","intro":"向GitHub提出一个问题GitHub Copilot CLI，然后引导对话朝着实现该功能的方向进行。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot 指南"},{"href":"/zh/copilot/tutorials/copilot-cookbook/generate-code","title":"生成代码"},{"href":"/zh/copilot/tutorials/copilot-cookbook/generate-code/implement-a-feature","title":"实现功能"}],"documentType":"article"},"body":"# 从GitHub问题实现功能\n\n向GitHub提出一个问题GitHub Copilot CLI，然后引导对话朝着实现该功能的方向进行。\n\nGitHub Copilot CLI 可以阅读 GitHub 问题、提出实施计划并起草满足要求的代码更改。 由于该工作流是以对话方式进行的，因此你可以纠正错误假设、指出相关文件，并在 GitHub Copilot CLI 工作的过程中逐步完善处理方式。\n\n## 示例方案\n\n设想你在一个平台团队工作，该团队负责维护一个为客户订阅提供支持的内部 Node.js 计费 API。 产品经理将打开一个问题，请求一个新 `GET /subscriptions/{id}/plan` 终结点，该终结点返回附加到给定订阅的计划，以及单元测试和 OpenAPI 规范的更新。 此问题包括所需的响应结构、订阅缺失或已取消时的错误情况，以及指向现有 `/subscriptions/{id}` 端点的链接，可作为参考模式。 可以将问题 GitHub Copilot CLI 交给并使其搭建路由、控制器和测试的基架，然后打开拉取请求。\n\n## 示例工作流\n\nGitHub Copilot CLI 可以直接读取 GitHub问题，规划工作，编写代码，运行测试，并打开拉取请求。\n\n### 将问题拉入上下文\n\n从存储库的根目录开始，并按 URL 或 `owner/repo#number`引用问题。\nGitHub Copilot CLI 使用 GitHub MCP 服务器获取问题正文、评论和关联项，然后在该会话的剩余时间内保留这些上下文以供后续使用。\n\n```text copy\nRead octo-org/billing-api#482 and any issues or PRs it links to. \nSummarize what needs to be implemented before we plan the work.\n```\n\n### 起草一份实施计划 `/plan`\n\n切换到计划模式，以便在 GitHub Copilot CLI 触摸任何文件之前思考更改。\n\n```text copy\n/plan Implement the subscription lookup endpoint described in the issue. \nFollow the same conventions as the existing GET /subscriptions/{id} endpoint.\n```\n\n> \\[!NOTE] 以下响应是示例。 Copilot Chat 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nGitHub Copilot CLI 返回类似于以下内容的计划：\n\n```text\n1. Add a `getPlanForSubscription` method in `src/services/subscriptionService.ts` that reuses the existing billing client, throws `NotFoundError` when the subscription or plan does not exist, and throws `CancelledSubscriptionError` when the subscription has been cancelled.\n1. Add a `GET /subscriptions/:id/plan` route in `src/routes/subscriptions.ts`, mirroring the error handling in the sibling `GET /subscriptions/:id` route and mapping `CancelledSubscriptionError` to a `410` response.\n1. Update `openapi/subscriptions.yaml` with the new path, response schema, and `404` and `410` responses.\n1. Add unit tests in `test/services/subscriptionService.test.ts` and `test/routes/subscriptions.test.ts` covering the success case, missing subscription, cancelled subscription, and missing plan.\n1. Run the test suite and update the changelog entry.\n```\n\n审查该计划，对任何看起来有问题的地方提出异议，并在批准之前要求 GitHub Copilot CLI 对其进行修改。 例如，你可能会回复 `Use the existing error classes in src/errors—NotFoundError for missing subscriptions or plans and CancelledSubscriptionError for cancelled subscriptions rather than introducing new ones` 并让其更新受影响的步骤。\n\n### 启动实施工作，并根据需要进行引导调整\n\n批准计划以开始编码。\nGitHub Copilot CLI 会逐步执行，并在应用每项更改之前先显示出来。 如果它以错误的方向前进，请中断和重定向。\n\n例如，假设 GitHub Copilot CLI 在你的 `getPlanForSubscription` 方法中添加了一个新的数据库查询，而不是复用你们团队标准化采用的计费客户端。 你可以让它停下并控制方向：\n\n```text copy\nDon't add a new query here. The billing client in \nsrc/clients/billingClient.ts already exposes a getPlan method. \nUse that and update the service to handle its NotFound response.\n```\n\nGitHub Copilot CLI 修改代码并继续执行剩余的计划步骤。\n\n### 生成并运行单元测试\n\n当 GitHub Copilot CLI 进入测试步骤时，它会生成与现有测试文件中的模式相匹配的测试框架。 对于计划终结点，它可能会添加如下情况：\n\n* 返回有效订阅的订阅计划记录。\n* 当订阅不存在时返回 `404` 。\n* 取消订阅后返回 `410` 。\n* 当订阅未附加计划时返回 `404` 。\n\n编写测试后， GitHub Copilot CLI 在终端中运行它们，以便立即看到结果。\n\n```text copy\nRun the test suite for the new endpoint and fix any failures.\n```\n\n如果测试失败， GitHub Copilot CLI 读取失败输出，更新实现，然后重新运行，直到测试套件为绿色。 审查每项修复措施，确保它解决的是根本原因，而不是掩盖问题。\n\n### 使用 `/diff` 查看更改\n\n使用 `/diff` 查看整个会话期间所做更改的汇总视图。\n\n```text copy\n/diff\n```\n\n如果出现错误，请在提交之前要求 GitHub Copilot CLI 对其进行修改。 例如，你可以回复 `Revert the formatting changes in src/routes/subscriptions.ts to only keep the new route handler`，将差异范围限定为预期的更改。\n\n### 打开拉取请求\n\n实现、测试和查看该功能后，要求 GitHub Copilot CLI 打开拉取请求。 它使用 GitHub MCP 服务器推送分支并创建拉取请求\n\n```text copy\nCommit the changes on a new branch, push it, and open a pull request \nagainst main. Link it to octo-org/billing-api#482 and summarize the \nimplementation, the tests added, and any follow-up work.\n```\n\nGitHub Copilot CLI 会返回拉取请求的 URL，以便你可以从该链接继续后续操作。\n\n## 延伸阅读\n\n* [GitHub Copilot CLI](/zh/copilot/how-tos/copilot-cli)"}