gh-pr-create

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Generate and submit a GitHub pull request from the current feature branch.
Iron laws: (1) No PR without user preview. (2) No raw commit dumps — always synthesize.
从当前功能分支生成并提交GitHub拉取请求(PR)。
核心准则: (1) 必须经过用户预览才能创建PR;(2) 不得直接转存原始提交记录——必须进行整合提炼。

The Process

操作流程

Step 1: Validate & Push

步骤1:验证与推送

Gather state and fail fast:
bash
BASE=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')
HEAD=$(git branch --show-current)
COMMITS=$(git log "$BASE".."$HEAD" --oneline)
Stop if:
$HEAD
equals
$BASE
, or
$COMMITS
is empty, or
gh pr view --json url 2>&1
shows an existing PR.
Dirty working tree: If
git status --porcelain
is non-empty, warn: "You have uncommitted changes that won't be included in this PR."
Push if upstream is not set or local is ahead:
git push -u origin "$HEAD"
收集状态信息并快速终止(若不符合条件):
bash
BASE=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')
HEAD=$(git branch --show-current)
COMMITS=$(git log "$BASE".."$HEAD" --oneline)
若出现以下情况则终止操作:
$HEAD
$BASE
相同,或
$COMMITS
为空,或
gh pr view --json url 2>&1
检测到已存在的PR。
工作区未提交变更:
git status --porcelain
返回非空结果,需发出警告:"你存在未提交的变更,这些变更不会被包含在本次PR中。"
若未设置上游分支或本地分支领先于远程,则执行推送:
git push -u origin "$HEAD"

Step 2: Gather Context & Generate Content

步骤2:收集上下文信息并生成PR内容

bash
git log "$BASE".."$HEAD" --pretty=format:'%h %s' --reverse
git diff --stat "$BASE".."$HEAD"
Detect PR template: Check
gh repo view --json pullRequestTemplates
first. If none, use references/pr-template.md.
Generate title — conventional-commit format:
type(scope): subject
Types:
feat
,
fix
,
docs
,
refactor
,
test
,
chore
,
ci
,
perf
. Imperative, lowercase, no period, under 50 chars. Single commit: reuse if already conventional. Multiple: synthesize dominant type.
Fill the body:
  • Single commit: Use the commit's full message to fill the template.
  • Multiple commits: Synthesize a summary. Group changes by logical area.
  • Low-quality commits (e.g., "fix", "wip"): Ask the user what changed and why.
Linked issues: Scan commit messages for
Closes #X
,
Fixes #X
, or
Resolves #X
. If found, include them. If none found, ask briefly: "Any issues to link?"
Irrelevant template sections: Write "N/A" — don't delete them.
bash
git log "$BASE".."$HEAD" --pretty=format:'%h %s' --reverse
git diff --stat "$BASE".."$HEAD"
检测PR模板: 首先通过
gh repo view --json pullRequestTemplates
检查仓库是否有PR模板。若无模板,则使用references/pr-template.md
生成标题——符合conventional-commit格式:
type(scope): subject
类型包括:
feat
(新功能)、
fix
(修复)、
docs
(文档)、
refactor
(重构)、
test
(测试)、
chore
(杂项)、
ci
(持续集成)、
perf
(性能优化)。标题需使用祈使语气、小写、无句号、长度不超过50字符。若只有单个提交且已符合规范,则直接复用;若有多个提交,则整合出占主导的类型。
填充PR正文:
  • 单个提交:使用该提交的完整信息填充模板。
  • 多个提交:整合提炼出摘要,按逻辑区域分组展示变更内容。
  • 低质量提交(如"fix""wip"):询问用户具体变更内容及原因。
关联议题: 扫描提交信息中的
Closes #X
Fixes #X
Resolves #X
关键字。若找到则包含这些关联;若未找到,需简要询问:"是否有需要关联的议题?"
与本次PR无关的模板章节: 填写"N/A"——不得删除这些章节。

Step 3: Preview

步骤3:预览

Show the preview:
═══ PR PREVIEW ═══════════════════════
Title: type(scope): subject
Base: main ← Head: feature-branch
Body:
──────────────────────────────────────
[full body content]
──────────────────────────────────────
Closes: #123, #456 (or "None")
══════════════════════════════════════
Ask: "Create this PR? (yes / edit / cancel)"
<HARD-GATE>USER APPROVAL — preview must be approved before creation</HARD-GATE>
展示预览内容:
═══ PR PREVIEW ═══════════════════════
Title: type(scope): subject
Base: main ← Head: feature-branch
Body:
──────────────────────────────────────
[full body content]
──────────────────────────────────────
Closes: #123, #456 (or "None")
══════════════════════════════════════
询问用户:"是否创建此PR?(是/编辑/取消)"
<HARD-GATE>用户确认——必须经过预览确认后才能创建PR</HARD-GATE>

Step 4: Create

步骤4:创建PR

bash
gh pr create --base "$BASE" --title "$TITLE" --body "$BODY"
Print PR URL. If issues linked, note they'll auto-close on merge.
bash
gh pr create --base "$BASE" --title "$TITLE" --body "$BODY"
打印PR的URL。若有关联议题,需提示这些议题会在PR合并时自动关闭。