GitHub 是做什麼的?
GitHub 操作使用 gh。本地端的 commit/branch/push/pull 則使用 git。深度審查請使用程式碼閱讀工具。
認證
gh auth status
gh auth login
Gateway HOME 可能不同於 operator HOME。如果 gh 的認證資訊存在於其他位置,請在 gateway 服務環境中設定 GH_CONFIG_DIR 並重新啟動。
PR
gh pr list --repo owner/repo --json number,title,state,author,url
gh pr view 55 --repo owner/repo --json title,body,author,files,commits,reviews,reviewDecision
gh pr checks 55 --repo owner/repo
gh pr diff 55 --repo owner/repo
gh pr create --repo owner/repo --title "feat: title" --body-file /tmp/pr.md
在建立或更新 PR 說明時,僅當 Runtime 行提供 sessionUrl=<確切網址> 時,才附加此最終頁尾。將 <sessionUrl> 替換為該網址的原文;不得自行建構或修改。sessionUrl 僅在明確為公開工作階段時提供。若無此資訊則省略頁尾;絕不可用私人聊天網址替代或啟用分享功能來獲取。保留任何發佈標記(最多一個頁尾),並保持頁尾在最後:
---
[檢視 OpenClaw 團隊工作階段](https://raw.githubusercontent.com/clawdbot/clawdbot/main/<sessionUrl>)
直接使用網址:gh pr view https://github.com/owner/repo/pull/55。
非同步合併
若儲存庫提供原生合併工作流程,請優先使用。對於其他地方需要授權的程式化合併,請透過 gh api 使用 GitHub 的非同步合併 API;gh pr merge 不會選擇此 API。請提供已審查的 head SHA、保留必要的審查/檢查,並先檢查堆疊成員資格:提交一個堆疊 PR 也會合併或排隊其開放的下游 PR,這些 PR 需要各自的審查和授權。
gh api -H 'X-Octopool-Require: merge-async-v1' \
--method PUT repos/owner/repo/pulls/55/merge-async \
-H 'X-GitHub-Api-Version: 2026-03-10' \
-f sha=REVIEWED_HEAD_SHA -f merge_method=squash -f merge_action=direct_merge
gh api -H 'X-Octopool-Require: merge-async-v1' \
repos/owner/repo/pulls/55/merge-async/REQUEST_UUID \
-H 'X-GitHub-Api-Version: 2026-03-10' -H 'Cache-Control: max-age=0'
在 api 之後,始終保持 Octopool 防護標頭為第一個。受保護的舊版包裝器會在其有機會改寫合併權限之前拒絕此標頭;如果被拒絕,請使用包含非同步防護 的 Octopool 建置版本。受支援的包裝器會保留 SHA/method/action/bypass,僅改寫 commit 文字。原生 gh 可以傳遞該標頭。請勿移除它、停用原則或選擇原始二進位檔案來繞過拒絕。
在輪詢之前,從 202 回應中保留 details.uuid。pending 表示等待;merged 包含 details.sha,但仍需最終的 PR/head 驗證。enqueued 表示已排入佇列,並非已合併:請監控 PR 本身。若要遵循已設定的佇列,請使用 merge_action=default(或明確使用 merge_queue 來排入佇列),並省略僅限直接合併的 method/title/message 欄位。未經授權,絕不啟用 bypass_rules。
若為 failed,請回報 details.message。409 表示存在先前的請求;檢查其 UUID 和選項,不要重新提交。遺失的回應、輪詢錯誤以及 24 小時後的結果過期需要重新協調,絕不能盲目重試或使用同步後備方案。使用有界限的等待和新鮮的讀取來進行輪詢。非同步處理不會為未滿足的檢查啟用自動合併;當該行為獲得授權時,請使用原生工作流程或 gh pr merge --auto。
合併所有權
當使用者要求 landing 或合併 PR 時,最終結果是 PR 在 GitHub 上已驗證的狀態,而不是審查、worker 輪次或 CI 觀察的結束。
- 保持工作活躍,直到
gh pr view ... --json state,mergedAt,mergeCommit證明state是MERGED。 - 將審查發現、合併衝突、失敗的檢查和要求變更視為範圍內的後續工作。修補它們,重新執行必要的關卡,並重新評估確切的更新後 head。
- 待處理的檢查是等待狀態,而非完成狀態。使用儲存庫支援的等待或合併工作流程;不要因為部分綠燈檢查就宣稱成功。
- 如果工作已委派給持久工作階段且該工作階段在合併前停止,請繼續同一個工作階段,而不是將其報告視為最終結果。
- 僅在繼續工作需要新的權限、無法取得的憑證或無法安全推斷的產品決策時,才將狀態標記為受阻。報告確切的阻礙因素並將 PR 保持未合併狀態。
Issue
gh issue list --repo owner/repo --state open --json number,title,labels,url
gh issue view 42 --repo owner/repo --json title,body,comments,labels,state
gh issue create --repo owner/repo --title "Bug: ..." --body-file /tmp/issue.md
gh issue comment 42 --repo owner/repo --body-file /tmp/comment.md
gh issue close 42 --repo owner/repo --comment "Fixed in ..."
CI/執行
gh run list --repo owner/repo --limit 10
gh run view <run-id> --repo owner/repo --json status,conclusion,headSha,url
gh run view <run-id> --repo owner/repo --log-failed
gh run rerun <run-id> --repo owner/repo --failed
等待 CI
將下面的等待程式作為單一的後台 exec 執行(background: true,timeoutSeconds 高於預期 CI 時間)。當其執行結果表示退出會喚醒此對話時,結束此回合:喚醒會攜帶退出碼和輸出尾部。否則,使用 process poll 收集結果。不要為相同的檢查啟動第二個等待程式。輪詢迴圈和一次性自動化每個檢查會消耗一個模型回合(#199122)。
固定您推送的 commit。一個讀取 PR head 即時狀態的等待程式可能會在推送後不久將舊 head 視為已穩定並立即退出;gh pr checks --watch 存在此競爭條件(#199663)。
# Exit 0: every check passed. 1: a check failed. 11: the PR head moved. 12: no check ran.
# 2: GitHub API reads failed 5 times in a row.
pr=55 repo=owner/repo sha=$(git rev-parse HEAD) # the exact commit you pushed
seen=0 n=0 e=0
while :; do
head=$(gh pr view "$pr" --repo "$repo" --json headRefOid --jq .headRefOid)
if [ "$head" = "$sha" ]; then
seen=1
if ! runs=$(gh api --paginate "repos/$repo/commits/$sha/check-runs" \
--jq '.check_runs[] | "\(.status) \(.conclusion) \(.name)"') ||
! st=$(gh api "repos/$repo/commits/$sha/status" --jq 'select(.total_count > 0) | .state'); then
e=$((e + 1)); [ "$e" -ge 5 ] && { echo "API-ERROR $sha"; exit 2; }
n=$((n + 1)); sleep 30; continue
fi
e=0
bad=$(printf '%s\n' "$runs" | grep -E '^completed (failure|timed_out|action_required|startup_failure) ')
[ -n "$bad" ] && { echo "FAIL $sha"; echo "$bad"; exit 1; }
case "$st" in failure | error) echo "FAIL $sha commit status: $st"; exit 1 ;; esac
if [ "$st" != pending ] && { [ -z "$runs" ] ||
! printf '%s\n' "$runs" | grep -Eqv '^completed (success|skipped|neutral|cancelled) '; }; then
{ [ "$st" = success ] || printf '%s\n' "$runs" | grep -q '^completed success '; } &&
{ echo "PASS $sha"; exit 0; }
[ "$n" -ge 4 ] && { echo "NO-RUN $sha"; exit 12; }
fi
elif [ -n "$head" ] && { [ "$seen" = 1 ] || [ "$n" -ge 4 ]; }; then
echo "HEAD-MOVED expected=$sha actual=$head"; exit 11
fi
n=$((n + 1)); sleep 30
done
此迴圈會給 GitHub 大約 2 分鐘的時間來顯示新的 head。它會等待已排隊、執行中以及 stale 的執行,並忽略已取消的執行,因為並發群組會取消被取代的執行。它也會讀取 commit 狀態,某些聚合閘道會使用這些狀態。當沒有任何執行成功時(例如所有執行都被跳過或根本沒有啟動),它會以狀態碼 12 退出,因為草稿 PR 會跳過 CI。對於已知的單一執行,gh run watch <run-id> --repo owner/repo --exit-status 會在執行失敗時以非零狀態碼退出。
API
gh api repos/owner/repo/pulls/55 --jq '.title, .state, .user.login'
gh api repos/owner/repo/labels --jq '.[].name'
gh api --cache 1h repos/owner/repo --jq '{stars: .stargazers_count, forks: .forks_count}'
對結構化輸出使用 --json 和 --jq。對包含反引號、shell 程式碼片段、環境變數名稱或使用者文字的評論/內容,請使用 --body-file。