gh-pr-create
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerate 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: equals , or is empty, or shows an existing PR.
$HEAD$BASE$COMMITSgh pr view --json url 2>&1Dirty working tree: If is non-empty, warn: "You have uncommitted changes that won't be included in this PR."
git status --porcelainPush 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)若出现以下情况则终止操作: 与相同,或为空,或检测到已存在的PR。
$HEAD$BASE$COMMITSgh pr view --json url 2>&1工作区未提交变更: 若返回非空结果,需发出警告:"你存在未提交的变更,这些变更不会被包含在本次PR中。"
git status --porcelain若未设置上游分支或本地分支领先于远程,则执行推送:
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 first. If none, use references/pr-template.md.
gh repo view --json pullRequestTemplatesGenerate title — conventional-commit format:
type(scope): subjectTypes: , , , , , , , . Imperative, lowercase, no period, under 50 chars. Single commit: reuse if already conventional. Multiple: synthesize dominant type.
featfixdocsrefactortestchoreciperfFill 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 , , or . If found, include them. If none found, ask briefly: "Any issues to link?"
Closes #XFixes #XResolves #XIrrelevant 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模板: 首先通过检查仓库是否有PR模板。若无模板,则使用references/pr-template.md。
gh repo view --json pullRequestTemplates生成标题——符合conventional-commit格式:
type(scope): subject类型包括:(新功能)、(修复)、(文档)、(重构)、(测试)、(杂项)、(持续集成)、(性能优化)。标题需使用祈使语气、小写、无句号、长度不超过50字符。若只有单个提交且已符合规范,则直接复用;若有多个提交,则整合出占主导的类型。
featfixdocsrefactortestchoreciperf填充PR正文:
- 单个提交:使用该提交的完整信息填充模板。
- 多个提交:整合提炼出摘要,按逻辑区域分组展示变更内容。
- 低质量提交(如"fix""wip"):询问用户具体变更内容及原因。
关联议题: 扫描提交信息中的、或关键字。若找到则包含这些关联;若未找到,需简要询问:"是否有需要关联的议题?"
Closes #XFixes #XResolves #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合并时自动关闭。