{/* Trang này được tạo tự động từ SKILL.md của kỹ năng bởi website/scripts/generate-skill-docs.py. Chỉnh sửa nguồn SKILL.md, không phải trang này. */}
Quy trình làm việc của GitHub Pr
Vòng đời PR của GitHub: phân nhánh, cam kết, mở, CI, hợp nhất.
Siêu dữ liệu kỹ năng
| Nguồn | Đi kèm (được cài đặt theo mặc định) |
| Đường dẫn |
skills/GitHub/GitHub-pr-workflow ` | | Phiên bản |
1.1.0 ` | | Tác giả | Đại lý Hermes | | Giấy phép | MIT | | Nền tảng | Linux, macOS, Windows | | Thẻ |
GitHub
, `Pull-Requests
, `CI/CD
, `Git
, `Automation
,
Merge |
| Kỹ năng liên quan | XPROTECTX26XPROTECTX, XPROTECTX27XPROTECTX |
Tham khảo: đầy đủ SKILL.md
Sau đây là định nghĩa kỹ năng đầy đủ mà Hermes tải khi kỹ năng này được kích hoạt. Đây là những gì tác nhân coi là hướng dẫn khi kỹ năng được kích hoạt.
Quy trình yêu cầu kéo GitHub
Hướng dẫn đầy đủ để quản lý vòng đời PR. Mỗi phần hiển thị cách
gh trước tiên, sau đó là cách dự phòng
`git
curl cho các máy không có
`gh
.
Điều kiện tiên quyết
- Đã xác thực bằng GitHub (xem kỹ năng `GitHub-auth
)
- Bên trong kho git có điều khiển từ xa GitHub
Phát hiện xác thực nhanh
# Determine which method to use throughout this workflow
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
AUTH="gh"
else
AUTH="git"
# Ensure we have a token for API calls
if [ -z "$GitHub_TOKEN" ]; then
if [ -f ~/.Hermes/.env ] && grep -q "^GitHub_TOKEN=" ~/.Hermes/.env; then
GitHub_TOKEN=$(grep "^GitHub_TOKEN=" ~/.Hermes/.env | head -1 | cut -d= -f2 | tr -d '\n\r')
elif grep -q "GitHub.com" ~/.git-credentials 2>/dev/null; then
GitHub_TOKEN=$(grep "GitHub.com" ~/.git-credentials 2>/dev/null | head -1 | sed 's|https://[^:]*:\([^@]*\)@.*|\1|')
fi
fi
fi
echo "Using: $AUTH"
`
### Trích xuất Chủ sở hữu/Repo từ Git Remote
Nhiều lệnh
`curl
` cần
`owner/repo
. Giải nén nó từ điều khiển git:
``` bash
# Works for both HTTPS and SSH remote URLs
REMOTE_URL=$(git remote get-url origin)
OWNER_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*GitHub\.com[:/]||; s|\.git$||')
OWNER=$(echo "$OWNER_REPO" | cut -d/ -f1)
REPO=$(echo "$OWNER_REPO" | cut -d/ -f2)
echo "Owner: $OWNER, Repo: $REPO"
`
---
##1. Tạo chi nhánh
Phần này hoàn toàn là
`git
- giống hệt nhau:
``` bash
# Make sure you're up to date
git fetch origin
git checkout main && git pull origin main
# Create and switch to a new branch
git checkout -b feat/add-user-authentication
`
``Quy ước đặt tên chi nhánh:
-
`feat/description
- tính năng mới
-
`fix/description
- sửa lỗi
-
`refactor/description
` — tái cấu trúc mã
-
`docs/description
` — tài liệu
-
`ci/description
` — Thay đổi CI/CD
## 2. Thực hiện cam kết
Sử dụng các công cụ tệp của tác nhân (
`write_file
,
`patch
) để thực hiện thay đổi, sau đó cam kết:
``` bash
# Stage specific files
git add src/auth.py src/models/user.py tests/test_auth.py
# Commit with a conventional commit message
git commit -m "feat: add JWT-based user authentication
- Add login/register endpoints
- Add User model with password hashing
- Add auth middleware for protected routes
- Add unit tests for auth flow"
`
``Định dạng thông báo cam kết (Cam kết thông thường):
`
type(scope): short description
Longer explanation if needed. Wrap at 72 characters.
`
``Các loại:
`feat
,
`fix
,
`refactor
,
`docs
,
`test
,
`ci
,
`chore
,
`perf
## 3. Đẩy và tạo PR
### Đẩy nhánh (theo cách tương tự)
``` bash
git push -u origin HEAD
`
### Tạo PR`**Với gh:**
`bash
gh pr create \
--title "feat: add JWT-based user authentication" \
--body "## Summary
- Adds login and register API endpoints
- JWT token generation and validation
## Test Plan
- [ ] Unit tests pass
Closes #42"
`
``Tùy chọn:
--draft
,
--reviewer user1,user2
,
--label "enhancement"
,
--base develop
``**Với git + cuộn tròn:**
``` bash
BRANCH=$(git branch --show-current)
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
-H "Accept: application/vnd.GitHub.v3+JSON" \
https://API.GitHub.com/repos/$OWNER/$REPO/pulls \
-d "{
\"title\": \"feat: add JWT-based user authentication\",
\"body\": \"## Summary\nAdds login and register API endpoints.\n\nCloses #42\",
\"head\": \"$BRANCH\",
\"base\": \"main\"
}"
`
``JSON phản hồi bao gồm PR
`number
- lưu nó cho các lệnh sau.
Để tạo dưới dạng bản nháp, hãy thêm
"draft": true
` vào phần nội dung JSON.
## 4. Theo dõi trạng thái CI
### Kiểm tra trạng thái CI`**Với gh:**
``` bash
# One-shot check
gh pr checks
# Watch until all checks finish (polls every 10s)
gh pr checks --watch
`
``**Với git + cuộn tròn:**
``` bash
# Get the latest commit SHA on the current branch
SHA=$(git rev-parse HEAD)
# Query the combined status
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/commits/$SHA/status \
| Python3 -c "
import sys, JSON
data = JSON.load(sys.stdin)
print(f\"Overall: \{data['state']}\")
for s in data.get('statuses', []):
print(f\" \{s['context']}: \{s['state']} - \{s.get('description', '')}\")"
# Also check GitHub Actions check runs (separate endpoint)
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/commits/$SHA/check-runs \
| Python3 -c "
import sys, JSON
data = JSON.load(sys.stdin)
for cr in data.get('check_runs', []):
print(f\" \{cr['name']}: \{cr['status']} / \{cr['conclusion'] or 'pending'}\")"
`
### Thăm dò ý kiến cho đến khi hoàn thành (git + Curl)
``` bash
# Simple polling loop — check every 30 seconds, up to 10 minutes
SHA=$(git rev-parse HEAD)
for i in $(seq 1 20); do
STATUS=$(curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/commits/$SHA/status \
| Python3 -c "import sys,JSON; print(JSON.load(sys.stdin)['state'])")
echo "Check $i: $STATUS"
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failure" ] || [ "$STATUS" = "error" ]; then
break
fi
sleep 30
done
`
## 5. Tự động sửa lỗi CI
Khi CI bị lỗi, chẩn đoán và sửa chữa. Vòng lặp này hoạt động với một trong hai phương thức xác thực.
### Bước 1: Nhận thông tin chi tiết về lỗi`**Với gh:**
``` bash
# List recent workflow runs on this branch
gh run list --branch $(git branch --show-current) --limit 5
# View failed logs
gh run view <RUN_ID --log-failed
`
``**Với git + cuộn tròn:**
``` bash
BRANCH=$(git branch --show-current)
# List workflow runs on this branch
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
"https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs?branch=$BRANCH&per_page=5" \
| Python3 -c "
import sys, JSON
runs = JSON.load(sys.stdin)['workflow_runs']
for r in runs:
print(f\"Run \{r['id']}: \{r['name']} - \{r['conclusion'] or r['status']}\")"
# Get failed job logs (download as zip, extract, read)
RUN_ID=<run_id
curl -s -L \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID/logs \
-o /tmp/ci-logs.zip
cd /tmp && unzip -o ci-logs.zip -d ci-logs && cat ci-logs/*.txt
`
### Bước 2: Sửa và Đẩy
Sau khi xác định được sự cố, hãy sử dụng các công cụ tệp (
`patch
,
`write_file
) để khắc phục:
``` bash
git add <fixed_files
git commit -m "fix: resolve CI failure in <check_name"
git push
`
### Bước 3: Xác minh
Kiểm tra lại trạng thái CI bằng các lệnh từ Phần 4 ở trên.
### Tự động sửa mẫu vòng lặp
Khi được yêu cầu tự động sửa CI, hãy làm theo vòng lặp sau:
1. Kiểm tra trạng thái CI → xác định lỗi
2. Đọc nhật ký lỗi → hiểu lỗi
3. Sử dụng
`read_file
+
`patch
/
`write_file
` → sửa mã
4.
`git add . && git commit -m "fix: ..." && git push
5. Đợi CI → kiểm tra lại trạng thái
6. Lặp lại nếu vẫn thất bại (tối đa 3 lần thử, sau đó hỏi người dùng)
## 6. Sáp nhập`**Với gh:**
``` bash
# Squash merge + delete branch (cleanest for feature branches)
gh pr merge --squash --delete-branch
# Enable auto-merge (merges when all checks pass)
gh pr merge --auto --squash --delete-branch
`
``**Với git + cuộn tròn:**
``` bash
PR_NUMBER=<number
# Merge the PR via API (squash)
curl -s -X PUT \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/merge \
-d "{
\"merge_method\": \"squash\",
\"commit_title\": \"feat: add user authentication (#$PR_NUMBER)\"
}"
# Delete the remote branch after merge
BRANCH=$(git branch --show-current)
git push origin --delete $BRANCH
# Switch back to main locally
git checkout main && git pull origin main
git branch -d $BRANCH
`
``Các phương thức hợp nhất:
"merge"
` (cam kết hợp nhất),
"squash"
,
"rebase"
### Bật Tự động hợp nhất (curl)
``` bash
# Auto-merge requires the repo to have it enabled in settings.
# This uses the GraphQL API since REST doesn't support auto-merge.
PR_NODE_ID=$(curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER \
| Python3 -c "import sys,JSON; print(JSON.load(sys.stdin)['node_id'])")
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/graphql \
-d "\{\"query\": \"mutation { enablePullRequestAutoMerge(input: \{pullRequestId: \\\"$PR_NODE_ID\\\", mergeMethod: SQUASH}) { CLIentMutationId } }\"}"
`
## 7. Ví dụ hoàn chỉnh về quy trình làm việc
``` bash
# 1. Start from clean main
git checkout main && git pull origin main
# 2. Branch
git checkout -b fix/login-redirect-bug
# 3. (Agent makes code changes with file tools)
# 4. Commit
git add src/auth/login.py tests/test_login.py
git commit -m "fix: correct redirect URL after login
Preserves the ?next= parameter instead of always redirecting to /dashboard."
# 5. Push
git push -u origin HEAD
# 6. Create PR (picks gh or curl based on what's available)
# ... (see Section 3)
# 7. Monitor CI (see Section 4)
# 8. Merge when green (see Section 6)
`
## Tham khảo các lệnh PR hữu ích
| Hành động | gh | git + cuộn tròn |
|--------|------|----------|
| Liệt kê PR của tôi |
gh pr list --author @me
` |
`curl -s -H "Authorization: token $GitHub_TOKEN" "https://API.GitHub.com/repos/$OWNER/$REPO/pulls?state=open"
` |
| Xem khác biệt PR |
gh pr diff
` |
`git diff main...HEAD
` (cục bộ) hoặc
`curl -H "Accept: application/vnd.GitHub.diff" ...
` |
| Thêm bình luận |
gh pr comment N --body "..."
` |
`curl -X POST .../issues/N/comments -d '\{"body":"..."}'
` |
| Yêu cầu xem xét |
gh pr edit N --add-reviewer user
` |
`curl -X POST .../pulls/N/requested_reviewers -d '\{"reviewers":["user"]}'
` |
| Đóng PR |
gh pr close N
` |
`curl -X PATCH .../pulls/N -d '\{"state":"closed"}'
` |
| Kiểm tra PR của ai đó |
gh pr checkout N
` |
`git fetch origin pull/N/head:pr-N && git checkout pr-N
` |