Assist users in creating or modifying GitHub Pull Requests, automatically analyze branch commits, and generate Traditional Chinese PR titles and descriptions.
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 summarize 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 already 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-checks
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 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 detailed commit information
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
The following format must be strictly followed, ensuring the output contains real Markdown line breaks, rather than a string containing \n :
For the complete template, refer to
references/pr-template.md
### 摘要
[一句話總結此 PR 的主要目的]
### 修改內容
- 變更項目 1:描述具體的修改內容
- 變更項目 2:描述具體的修改內容
- 變更項目 3:描述具體的修改內容
### ⚠️ 風險評估
[評估此 PR 是否有破壞性變更、需要特別注意的地方]
常見風險類型:
- 資料庫變更(migration、schema 修改)
- API 變更(endpoint 修改、參數變更)
- 設定檔變更(環境變數、設定參數)
- 相依性更新(套件版本升級)
若無風險,可註明:「無破壞性變更」
### 備註
[其他需要審查者注意的地方,如測試方式、部署注意事項、相關 Issue 連結等]
Step 5: Create the Pull Request
Use GitHub CLI to create the PR.
⚠️ It is absolutely forbidden to use --body parameter directly. You must first write the content to a temporary file, then use --body-file to read the content, to avoid newline characters being escaped as \n strings.
# 正確做法:
# 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
Display to the user:
- PR number and title
- Current status (OPEN/DRAFT)
- Source branch → Target branch
- PR link
Step 3: Confirm the changes to make
Perform the corresponding action based on the user's needs:
Edit title
gh pr edit <PR編號或URL> --title "<新標題>"
Edit 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 process committed changes: Ignore unstaged changes
- Language requirement: PR title and description useTraditional Chinese (zh-TW)
- Default target branch: If the user does not specify, defaults to
main - Requires GitHub CLI: Ensure it is installed and logged in to
ghtool
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 creation flow
- Pre-check: Confirm the branch exists on the remote
- Get commits:
git --no-pager log --oneline develop..HEAD - Analyze changes to generate title and description
- Execute:
gh pr create --base develop --head feature/login --title "feat: 實作使用者登入功能" --body "..."
Example 2: PR already exists - Automatically enter edit 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 edit flow
- Report: "This branch already has PR #42 'feat: Add login feature'. What changes 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 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 template and writing guidereferences/gh-pr-commands.md- Complete reference for GitHub CLI PR-related commands