Help users create or modify a GitHub Pull Request, automatically analyze branch commits and generate a Traditional Chinese PR title and description.
Features
- Create PR: Create a Pull Request from the current branch to the target branch
- Modify PR: Update the title or description of an existing PR
- Analyze commits: Automatically aggregate branch changes to generate PR content
Decision Flow
使用者請求 PR 相關操作
│
├─ 步驟 1:檢查當前分支是否有對應的 PR
│ │
│ │ gh pr view --json number,title,state 2>&1
│ │
│ ├─ 【有輸出且 state="OPEN"】→ PR 已存在
│ │ │
│ │ └─ 進入【修改 PR 流程】
│ │ - 顯示現有 PR 資訊
│ │ - 詢問使用者要修改的內容
│ │ - 執行 gh pr edit 指令
│ │
│ └─ 【無輸出或錯誤】→ PR 不存在
│ │
│ └─ 進入【建立 PR 流程】
│ - 執行前置檢查
│ - 分析 commits
│ - 產生標題與描述
│ - 執行 gh pr create 指令
│
└─ 步驟 2:執行對應流程並回報結果
Check whether the PR exists
# 檢查當前分支是否已有 PR
gh pr view --json number,title,state,url
Decision logic:
| Output result | Decision | Follow-up action |
|---|---|---|
Return JSON and state: "OPEN" |
PR already exists and is open | Enter the modification flow |
Return JSON and state: "MERGED" |
PR already merged | Ask whether to create a new PR |
Return JSON and state: "CLOSED" |
PR closed | Ask whether to reopen or create a new PR |
Error message no pull requests found |
PR does not exist | Enter the creation flow |
Create PR Flow
Step 0: Pre-check
Confirm that the current branch has been pushed to the remote:
# 取得當前分支名稱
git rev-parse --abbrev-ref HEAD
# 檢查分支是否存在於遠端
git ls-remote --heads origin $(git rev-parse --abbrev-ref HEAD)
If there is no output: The branch has not been pushed to the remote yet; run the following first:
git push -u origin <當前分支>
Step 1: Get the commit list
Get the commits of the current branch relative to the target branch (default main):
git --no-pager log --oneline <目標分支>..<當前分支>
Step 2: Get commit details
Get change statistics for each commit:
git --no-pager show <commit-hash> --stat
Step 3: Generate the PR title
Determine the title strategy based on the number of commits:
| Case | Title strategy |
|---|---|
| Single commit | Use that commit message directly |
| Multiple commits | A descriptive title summarizing all changes |
Step 4: Generate the PR description
For the full template, refer to
references/pr-template.md
The PR content should include the following sections:
### 摘要
[一句話總結此 PR 的主要目的]
### 修改內容
- 變更項目 1:描述具體的修改內容
- 變更項目 2:描述具體的修改內容
- 變更項目 3:描述具體的修改內容
### ⚠️ 風險評估
[評估此 PR 是否有破壞性變更、需要特別注意的地方]
常見風險類型:
- 資料庫變更(migration、schema 修改)
- API 變更(endpoint 修改、參數變更)
- 設定檔變更(環境變數、設定參數)
- 相依性更新(套件版本升級)
若無風險,可註明:「無破壞性變更」
### 備註
[其他需要審查者注意的地方,如測試方式、部署注意事項、相關 Issue 連結等]
Step 5: Create the Pull Request
Use GitHub CLI to create the PR.
⚠️ Important: Use --body-file to avoid escaping issues
Using --body directly on the command line will cause the newline character \n and other special characters to have escaping issues.It is recommended to use the file method:
# 方法 1(推薦):將內容寫入暫存檔案
# 1. 建立暫存檔案 pr-body.md,寫入 PR 描述內容
# 2. 使用 --body-file 讀取檔案
gh pr create --base <目標分支> --head <當前分支> --title "<標題>" --body-file pr-body.md
# 3. 建立完成後刪除暫存檔案
PowerShell example:
# 將 PR 描述寫入檔案
@"
### 摘要
實作使用者登入功能
### 修改內容
- 新增登入 API 端點
- 實作 JWT 驗證機制
### ⚠️ 風險評估
無破壞性變更
"@ | Out-File -FilePath pr-body.md -Encoding UTF8
# 建立 PR
gh pr create --base main --title "feat: 實作登入功能" --body-file pr-body.md
# 清理暫存檔案
Remove-Item pr-body.md
Bash example:
# 將 PR 描述寫入檔案
cat << 'EOF' > pr-body.md
### 摘要
實作使用者登入功能
### 修改內容
- 新增登入 API 端點
- 實作 JWT 驗證機制
### ⚠️ 風險評估
無破壞性變更
EOF
# 建立 PR
gh pr create --base main --title "feat: 實作登入功能" --body-file pr-body.md
# 清理暫存檔案
rm pr-body.md
Common options:
| Option | Description |
|---|---|
--body-file <file> |
Read the PR description from a file (recommended) |
--draft |
Create a draft PR |
--reviewer <users> |
Specify reviewers (comma-separated) |
--assignee <users> |
Specify assignees |
--label <labels> |
Add labels |
Modify PR Flow
When the PR already exists, enter this flow.
Step 1: Get existing PR information
gh pr view --json number,title,body,state,url,headRefName,baseRefName
Step 2: Display PR summary
Show the user:
- PR number and title
- Current status (OPEN/DRAFT)
- Source branch → Target branch
- PR link
Step 3: Confirm the modification content
Perform the corresponding operation based on the user's needs:
Modify title
gh pr edit <PR編號或URL> --title "<新標題>"
Modify description
gh pr edit <PR編號或URL> --body "<新描述>"
Add reviewers/labels
gh pr edit <PR編號或URL> --add-reviewer <users>
gh pr edit <PR編號或URL> --add-label <labels>
View existing PR
# 列出當前 repo 的 PR
gh pr list
# 查看特定 PR 詳情
gh pr view <PR編號>
Notes
- Only handle committed changes: ignore unstaged changes
- Language requirement: PR titles and descriptions useTraditional Chinese (zh-TW)
- Default target branch: if the user does not specify, the default is
main - Requires GitHub CLI: ensure that
ghis installed and logged in
Examples
Example 1: PR does not exist - create a new PR
User request: "Help me create a PR to develop"
Execution flow:
- Check PR:
gh pr view→ error "no pull requests found" - Determine: PR does not exist, enter the creation flow
- Pre-check: confirm the branch is on the remote
- Get commits:
git --no-pager log --oneline develop..HEAD - Analyze the changes to generate a title and description
- Execute:
gh pr create --base develop --head feature/login --title "feat: 實作使用者登入功能" --body "..."
Example 2: PR already exists - automatically enter modification mode
User request: "Help me open a PR"
Execution flow:
- Check PR:
gh pr view --json number,title,state{"number": 42, "title": "feat: 新增登入功能", "state": "OPEN"} - Determine: PR already exists, enter the modification flow
- Report: "This branch already has PR #42 'feat: Add login feature'. What modification would you like to make?"
- Wait for user instructions
Example 3: Explicitly modify a PR
User request: "Change the title of PR #42 to 'Fix shopping cart calculation error'"
Execute:
gh pr edit 42 --title "修正購物車計算錯誤"
Example 4: PR already merged
User request: "Help me create a PR"
Execution flow:
- Check PR:
gh pr view --json number,title,state{"number": 38, "title": "feat: 舊功能", "state": "MERGED"} - Determine: PR already merged
- Report: "PR #38 for this branch has already been merged. If there are new changes that require a PR, please confirm that there are new commits."
References
references/pr-template.md- PR description templates and writing guidelinesreferences/gh-pr-commands.md- Complete reference for GitHub CLI PR-related commands