SuperAPI AI 工具集

create-pr

编程与开发

查看项目

更新于 10-15 17:04

技能介绍

用途

create-pr 用于按照最佳工程实践创建 Pull Request,适用场景包括打开 PR、编写 PR 描述,以及为代码评审准备变更。

前置条件与依赖

该技能需要已安装并完成认证、可用的 GitHub CLI(gh)。

在创建 PR 之前,应确保所有更改都已提交。如果存在未提交的更改,先运行 commit 技能 skill({ name: "conventional-commit" }) 完成规范提交。检查方式:

# Check for uncommitted changes
git status --porcelain

如果输出显示存在任何未提交的更改(已修改、已新增或应包含的未跟踪文件),需在继续之前调用提交技能。

处理流程

第 1 步:验证分支状态

# Detect the default branch
BASE=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')

# Check current branch and status
git status
git log $BASE..HEAD --oneline

需确保:所有更改已提交;分支与远程保持同步;如需要,变更已基于基础分支进行 rebase。

第 2 步:分析变更

评审将纳入 PR 的内容:

# See all commits that will be in the PR
git log $BASE..HEAD

# See the full diff
git diff $BASE...HEAD

在撰写描述之前,理解所有变更的范围与目的。

第 3 步:编写 PR 描述

PR 描述使用以下结构(忽略任何仓库的 PR 模板):

<brief description of what the PR does>

<why these changes are being made - the motivation>

<alternative approaches considered, if any>

<any additional context reviewers need>

不要包含:「Test plan」章节;测试步骤的复选框列表;对 diff 的冗余总结。

应当包含:对「做什么」和「为什么」的清晰解释;相关 issue 或工单链接(仅在可用时);从代码中不易看出的上下文;需要重点评审的具体区域的说明。

第 4 步:创建 PR

gh pr create --title "<type>(<scope>): <description>" --body "
<description body here>
"

标题格式遵循提交约定:

  • feat(scope): Add new feature
  • fix(scope): Fix the bug
  • refactor(scope): Refactor something

PR 描述示例

功能 PR

Add Slack thread replies for alert notifications

When an alert is updated or resolved, we now post a reply to the original
Slack thread instead of creating a new message. This keeps related
notifications grouped and reduces channel noise.

Previously considered posting edits to the original message, but threading
better preserves the timeline of events and works when the original message
is older than Slack's edit window.

缺陷修复 PR

Handle null response in user API endpoint

The user endpoint could return null for soft-deleted accounts, causing
dashboard crashes when accessing user properties. This adds a null check
and returns a proper 404 response.

重构 PR

Extract validation logic to shared module

Moves duplicate validation code from the alerts, issues, and projects
endpoints into a shared validator class. No behavior change.

This prepares for adding new validation rules without
duplicating logic across endpoints.

准则

  • 一个 PR 对应一个功能或修复,不要捆绑无关变更。
  • 保持 PR 可评审,更小的 PR 能获得更快、更好的评审。
  • 解释「为什么」,代码展示「做什么」,描述解释「为什么」。

编辑已有 PR

在创建后更新 PR 时,该技能会根据你的 GitHub CLI 版本自动选择最佳方式:

  • gh 2.82.1+:使用原生的 gh pr edit(干净、功能完整)
  • gh < 2.82.1:回退到 gh api(避免 Projects classic 弃用缺陷)

检查版本:

# Check GitHub CLI version
gh --version

现代方式(gh 2.82.1+)

# Update PR description
gh pr edit PR_NUMBER --body "Updated description here"

# Update PR title
gh pr edit PR_NUMBER --title "New Title here"

# Update both
gh pr edit PR_NUMBER --title "new: Title" --body "New description"

旧版回退(gh < 2.82.1)

如果 gh pr edit 因「Projects (classic) is being deprecated」错误而失败,直接使用 API:

# Update PR description
gh api -X PATCH repos/{owner}/{repo}/pulls/PR_NUMBER -f body="
Updated description here
"

# Update PR title
gh api -X PATCH repos/{owner}/{repo}/pulls/PR_NUMBER -f title='New Title here'

# Update both
gh api -X PATCH repos/{owner}/{repo}/pulls/PR_NUMBER \
  -f title='new: Title' \
  -f body='New description'

注意:Projects (classic) 缺陷已在 gh 2.82.1(2025 年 10 月)中修复。如果使用更早版本,请通过包管理器升级。

限制

使用该技能需要已认证的 GitHub CLI 环境;未提供脱离 GitHub 与 gh CLI 的使用方式,也未提供除上述依赖与步骤之外的其他信息。