实战案例

三个由浅入深的任务,覆盖本教程:注释驱动补全、Chat 摸清仓库、以及 GitHub 上的 Issue → Agent PR。把提示词贴进 Copilot;命令按你的仓库改。不要在生产仓库关着审查跑这些练习。


案例 1:注释生成函数并自己验收

目标: 用幽灵文本写出带边界行为的纯函数,而不是「看起来能跑」。

准备: 空文件夹即可。

Windows:

mkdir $HOME\copilot-lab-1
cd $HOME\copilot-lab-1
code .

macOS:

mkdir -p ~/copilot-lab-1
cd ~/copilot-lab-1
code .

新建 slug.js,只输入注释和函数头,等灰色建议:

// Convert a string to a URL slug: lowercase, trim, spaces to '-',
// strip characters that are not letters, digits, or '-'.
// Collapse repeated '-'. Empty input → "".
function slugify(input) {

Tab 接受或 Esc 丢掉再改注释。然后新建 slug.test.js(或你习惯的测试文件),用注释再要一组断言:

// Tiny tests without a framework: throw if slugify("Hello World") !== "hello-world"
// Also cover "", "  a--b  ", and punctuation.

若补全塞进了你没装的测试库,Esc 掉,注释里写 Do not add Jest。Windows 与 macOS 都可用 node --test slug.test.js;没有 --test 就改用几行 console.assert

完成标准:

检查通过
实现来自 Copilot,但你读过每一行是 / 否
空字符串与重复连字符有测试是 / 否
测试是绿的是 / 否

对应章节:快速上手行内补全


案例 2:用 Chat 上手陌生仓库并只改一处

目标: 两小时内指出入口、测试命令,并合并一处你理解的小改动。

克隆一个你不熟的中小型开源库,用 VS Code 打开仓库根

git clone <repo-url> unfamiliar-repo
cd unfamiliar-repo
code .

Ctrl+Alt+I(Windows)或 Control+Command+I(macOS),先发:

@workspace Explain this codebase. Point me to main entry points and
modules I must read before changing behavior. Cite file paths.
@workspace Quote the exact commands this repo uses for install, test,
lint, and dev server. Prefer package.json / Makefile / pyproject.toml.
Do not invent scripts.

在本机跑它引用的命令(示例,以 Chat 引用的为准):

npm ci
npm test
npm ci
npm test

再选一个无鉴权、无迁移的小改动(文案、README 错字、缺失的 JSDoc)。Chat 用 Ask 或 Edit,不要一上来 Agent 扫全库:

Add JSDoc to the exported function I have selected.
Do not change runtime behavior. Do not touch lockfiles.

审查 diff,再跑同一组测试。只提交这一处。

完成标准:

检查通过
入口路径来自仓库文件,不是编造是 / 否
测试命令来自配置文件是 / 否
只合并一处小 diff,测试仍绿(或你理解失败原因)是 / 否

对应:Chat。若你更想用 Cursor 做同一练习,走 Cursor 实战,不要两个 Agent 同时写。


案例 3:Issue 交给 Copilot,人审 PR

目标: 走官方 cloud agent 闭环——不是在 IDE 里「感觉它做完了」。

准备:

  • 你有推送权限的练习仓库(不要用生产)
  • 套餐与仓库政策允许 Copilot cloud agent(见 Coding Agent
  • 一条写清楚的 Issue

Issue 正文示例:

## Goal
Add GET /health returning JSON {"ok": true} using the existing HTTP stack.
Do not change authentication.

## Success
- One new test in the same style as the repo
- README mentions the endpoint in one sentence

## Out of scope
- New dependencies unless the repo already uses them
- Refactors of unrelated files

在 GitHub 上:Assignees → Copilot。可选 prompt 里再贴一遍「不要动鉴权」。等 PR 出现后:

  1. 用 Copilot 面板问 Summarize this pull request(见 GitHub 集成)。
  2. 自己点 Files changed,核对有没有多余重构。
  3. 本机检出 PR 分支跑测试(Windows / macOS 相同):
git fetch origin
git checkout <pr-branch>

然后跑案例 2 里那条测试命令。

  1. 需要修改时,在 PR 评论里跟进,不要只在已指派的 Issue 下留言。
  2. 只有测试绿、diff 符合 Issue 时才合并。

若组织禁用 Agent:改用 CLI 在本机完成同样的 /health,再自己开 PR——仍然要人审。

完成标准:

检查通过
Issue 有成功标准与 Out of scope是 / 否
PR 来自 Copilot 或你明确改用的 CLI 路径是 / 否
你在本机跑过测试,而不是只看网页绿色是 / 否

练习之后

把约定写进 .github/copilot-instructions.md。是否并用 Cursor 见 Copilot 与 Cursor


下一步

评论