# 从GitHub问题实现功能

向GitHub提出一个问题GitHub Copilot CLI，然后引导对话朝着实现该功能的方向进行。

GitHub Copilot CLI 可以阅读 GitHub 问题、提出实施计划并起草满足要求的代码更改。 由于该工作流是以对话方式进行的，因此你可以纠正错误假设、指出相关文件，并在 GitHub Copilot CLI 工作的过程中逐步完善处理方式。

## 示例方案

设想你在一个平台团队工作，该团队负责维护一个为客户订阅提供支持的内部 Node.js 计费 API。 产品经理将打开一个问题，请求一个新 `GET /subscriptions/{id}/plan` 终结点，该终结点返回附加到给定订阅的计划，以及单元测试和 OpenAPI 规范的更新。 此问题包括所需的响应结构、订阅缺失或已取消时的错误情况，以及指向现有 `/subscriptions/{id}` 端点的链接，可作为参考模式。 可以将问题 GitHub Copilot CLI 交给并使其搭建路由、控制器和测试的基架，然后打开拉取请求。

## 示例工作流

GitHub Copilot CLI 可以直接读取 GitHub问题，规划工作，编写代码，运行测试，并打开拉取请求。

### 将问题拉入上下文

从存储库的根目录开始，并按 URL 或 `owner/repo#number`引用问题。
GitHub Copilot CLI 使用 GitHub MCP 服务器获取问题正文、评论和关联项，然后在该会话的剩余时间内保留这些上下文以供后续使用。

```text copy
Read octo-org/billing-api#482 and any issues or PRs it links to. 
Summarize what needs to be implemented before we plan the work.
```

### 起草一份实施计划 `/plan`

切换到计划模式，以便在 GitHub Copilot CLI 触摸任何文件之前思考更改。

```text copy
/plan Implement the subscription lookup endpoint described in the issue. 
Follow the same conventions as the existing GET /subscriptions/{id} endpoint.
```

> \[!NOTE] 以下响应是示例。 Copilot Chat 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。

GitHub Copilot CLI 返回类似于以下内容的计划：

```text
1. 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.
1. 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.
1. Update `openapi/subscriptions.yaml` with the new path, response schema, and `404` and `410` responses.
1. 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.
1. Run the test suite and update the changelog entry.
```

审查该计划，对任何看起来有问题的地方提出异议，并在批准之前要求 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` 并让其更新受影响的步骤。

### 启动实施工作，并根据需要进行引导调整

批准计划以开始编码。
GitHub Copilot CLI 会逐步执行，并在应用每项更改之前先显示出来。 如果它以错误的方向前进，请中断和重定向。

例如，假设 GitHub Copilot CLI 在你的 `getPlanForSubscription` 方法中添加了一个新的数据库查询，而不是复用你们团队标准化采用的计费客户端。 你可以让它停下并控制方向：

```text copy
Don't add a new query here. The billing client in 
src/clients/billingClient.ts already exposes a getPlan method. 
Use that and update the service to handle its NotFound response.
```

GitHub Copilot CLI 修改代码并继续执行剩余的计划步骤。

### 生成并运行单元测试

当 GitHub Copilot CLI 进入测试步骤时，它会生成与现有测试文件中的模式相匹配的测试框架。 对于计划终结点，它可能会添加如下情况：

* 返回有效订阅的订阅计划记录。
* 当订阅不存在时返回 `404` 。
* 取消订阅后返回 `410` 。
* 当订阅未附加计划时返回 `404` 。

编写测试后， GitHub Copilot CLI 在终端中运行它们，以便立即看到结果。

```text copy
Run the test suite for the new endpoint and fix any failures.
```

如果测试失败， GitHub Copilot CLI 读取失败输出，更新实现，然后重新运行，直到测试套件为绿色。 审查每项修复措施，确保它解决的是根本原因，而不是掩盖问题。

### 使用 `/diff` 查看更改

使用 `/diff` 查看整个会话期间所做更改的汇总视图。

```text copy
/diff
```

如果出现错误，请在提交之前要求 GitHub Copilot CLI 对其进行修改。 例如，你可以回复 `Revert the formatting changes in src/routes/subscriptions.ts to only keep the new route handler`，将差异范围限定为预期的更改。

### 打开拉取请求

实现、测试和查看该功能后，要求 GitHub Copilot CLI 打开拉取请求。 它使用 GitHub MCP 服务器推送分支并创建拉取请求

```text copy
Commit the changes on a new branch, push it, and open a pull request 
against main. Link it to octo-org/billing-api#482 and summarize the 
implementation, the tests added, and any follow-up work.
```

GitHub Copilot CLI 会返回拉取请求的 URL，以便你可以从该链接继续后续操作。

## 延伸阅读

* [GitHub Copilot CLI](/zh/enterprise-cloud@latest/copilot/how-tos/copilot-cli)