團隊中的 gh 與 git
git 與 GitHub CLI 如何為 Agent 運作:透過團隊擁有者的 AirBuild GitHub App 取得 GitHub 存取權、gh access、提示行,以及提交上的共同作者行。
Agent 使用一般的 git 與 gh(GitHub 的命令列工具)。兩者都已登入 GitHub,因此不需要登入或設定任何東西。AirBuild 在此之上加了三樣東西:gh access 指令、指令失敗時的幾行 hint:,以及提交上的 AirBuild 共同作者行。
執行位置
雲端團隊與 Mac 團隊上的 Agent 都能使用這兩個指令。在雲端團隊上,環境自帶 git 與 gh。在 Mac 團隊上,gh 隨 AirBuild 提供(不會使用你自行安裝的 gh),git 則是 Mac 本身的,例如 Xcode 安裝的版本;若沒有安裝,git 會停止並顯示 Error: git is not installed in this environment. Ask your user to install it and retry.。請參閱選擇 Agent 的執行位置。
所有引數都會原封不動地傳給真正的 git 或 gh,輸出與結束代碼也都是它們自己的。AirBuild 的訊息只寫到 stderr,因此讀取 stdout 的程式看不到它們。
GitHub 存取權從何而來
登入身分是團隊擁有者的 GitHub 帳號,透過設定 › GitHub 中的 AirBuild GitHub App 連結。已登入不等於擁有儲存庫的存取權。GitHub 會依序檢查:
- 團隊已連結 GitHub。
- AirBuild GitHub App 已安裝在擁有該儲存庫的帳號或組織上。
- 該儲存庫在與此安裝共用的儲存庫之中。
- 若是寫入操作,App 與已連結的帳號都必須具備寫入權限。
因此,gh auth status 可能顯示 “Logged in”,公開儲存庫也能讀取,但推送時仍會被直接以 403 拒絕。執行 gh auth status 後,gh 會加上一段說明,正是在講這件事。
缺少存取權時,Agent 會在聊天中傳送 request_github_repo 卡片。卡片會向對方請求某個儲存庫或整個帳號,或讓對方自行挑選儲存庫;若尚未連結 GitHub 或連結已失效,也會先引導對方完成連結。
gh access
gh access [OWNER/REPO | OWNER]
gh access --helpgh access 是 AirBuild 自己的指令。它會向 AirBuild 查詢,對這個團隊而言,某個儲存庫卡在上述哪一層,以及 request_github_repo 卡片能否解決。
| 引數 | 檢查內容 |
|---|---|
| 無 | 目前資料夾 origin 遠端所指的儲存庫(必須位於 github.com)。 |
OWNER/REPO |
該儲存庫。也可使用 github.com 網址或 github.com/OWNER/REPO。 |
OWNER |
AirBuild GitHub App 是否安裝在該帳號上,以及共用的是所有儲存庫還是選定的儲存庫。 |
回答為一到兩行純文字。可能的回答如下:
| 回答 | 卡片能解決嗎? |
|---|---|
| 存取權已就緒:App 涵蓋該儲存庫且可以推送,並具備 App 在內容、拉取請求、議題、工作流程與 Actions 方面的權限。 | 不能;沒有缺少任何東西。回答會列出其他可能拒絕推送的原因:受保護的分支或規則集、App 沒有 workflows 權限卻變更了 .github/workflows 下的檔案、非快轉推送遭拒,或名稱錯誤。若 gh api 回傳 403,請加上 -i 再執行一次;X-Accepted-GitHub-Permissions 標頭會指出該端點需要的權限。 |
| App 涵蓋該儲存庫,但它在擁有者上的 Contents 權限不是寫入。 | 不能。若 GitHub 顯示該帳號有來自 AirBuild 的權限請求待處理,接受後即可推送。 |
| App 涵蓋該儲存庫,但已連結的帳號本身無法推送。 | 不能。該帳號需要儲存庫擁有者授予寫入權限,否則就改到它能推送的儲存庫進行。 |
| App 已安裝在擁有者上,但這個儲存庫未與它共用。公開儲存庫仍可讀取。 | 能。對方在卡片開啟的頁面中加入該儲存庫即可。 |
| App 未安裝在擁有者的帳號上。公開儲存庫仍可讀取;私人儲存庫及所有寫入操作則不行。 | 能,前提是該帳號是對方自己的,或是對方管理的組織。若是其他人的帳號,請在 fork 或對方擁有的儲存庫中進行。 |
| App 的安裝在 GitHub 上已被停權。 | 能。可在卡片的頁面上解除停權。 |
| 該組織強制使用 SAML 單一登入,而團隊的 GitHub 登入尚未完成此步驟。 | 能。卡片會讓對方重新登入 GitHub,屆時會要求單一登入。 |
| GitHub 上沒有該名稱的使用者或組織。 | 不能。請檢查拼字,例如使用 git remote -v。 |
| 擁有者底下沒有該帳號能看到的同名儲存庫。 | 不能。請檢查名稱;它可能已被重新命名、移動或刪除。 |
| 這個團隊尚未連結 GitHub。 | 能。卡片會引導對方連結 GitHub。 |
| GitHub 連結已無法更新。 | 能。卡片會引導對方重新連結 GitHub。 |
若 AirBuild 無法回答,gh access 會停止並顯示 Error: could not check GitHub access for OWNER/REPO. Name it as OWNER/REPO (or a bare OWNER), or retry shortly.,結束代碼為 2。若未提供引數且不在 GitHub 的 checkout 中,則會顯示 Error: gh access needs OWNER/REPO: this directory has no github.com origin remote.
即使沒有 gh,gh access 也能運作。
提示行
指令失敗時,AirBuild 會在 GitHub 自己的錯誤訊息之後,於 stderr 加上以 hint: 開頭的幾行:
gh:任何以錯誤結束的指令之後。git:只在與遠端通訊的指令之後:clone、fetch、pull、push、ls-remote、remote與submodule。以非零值結束的本機指令(例如找不到符合項目的git grep)不會有提示。
提示內容就是針對該指令所通訊之儲存庫的 gh access 回答:
hint: GitHub access to `example-org/example-app` for this Team: the AirBuild GitHub App is installed on `example-org`, but this repository is not among the ones shared with it. Reading it still works if it is public; pushing, opening pull requests and every other write do not.
hint: A card repairs this: send a request_github_repo card for `example-org/example-app` with bot_send_card and stop until the user resolves it. The user adds the repository on the page it opens.儲存庫的判定方式:
| 指令 | 儲存庫 |
|---|---|
git |
命令列上的 github.com 網址;否則為指令指定的遠端,或其預設使用的遠端(push 時為推送遠端),再不然則為 origin。 |
gh |
-R / --repo、GH_REPO 變數、傳給 gh api 的 repos/OWNER/REPO/… 路徑,或 gh repo <verb> 的引數。否則為目前資料夾的 origin,並多加一行提示說明:hint: (that is the repository of this directory; if the command addressed another one, run gh access OWNER/REPO for it.) |
若無法判定儲存庫,或 AirBuild 沒有回答,則會給出一般性的提示:執行 gh access OWNER/REPO 查看缺少什麼,若卡片能解決,就傳送 request_github_repo 卡片。
會被拒絕的登入指令
登入與登出由 AirBuild 負責,因此下列 gh auth 指令會以結束代碼 2 停止:
| 指令 | 結果 |
|---|---|
gh auth login, gh auth logout, gh auth refresh, gh auth setup-git |
Error: gh auth <verb> is not used here.,並附上一行說明:GitHub 憑證會自動提供,缺少或失效的存取權請用 request_github_repo 卡片修復。 |
gh auth status |
可正常執行,之後會加上:note: this is the Team owner's sign-in through the AirBuild GitHub App. Being signed in is not access to a repository: gh access OWNER/REPO shows what this token can do there. |
即使尚未連結 GitHub,gh --help 與 gh --version 也能運作。GitHub 尚未就緒時,其他 gh 指令會以結束代碼 2 停止:
| 訊息 | 處理方式 |
|---|---|
Error: GitHub is not configured for this Team. |
傳送 request_github_repo 卡片,待對方連結 GitHub 後再重試。 |
Error: the GitHub connection for this Team can no longer be refreshed. |
傳送 request_github_repo 卡片讓對方重新連結 GitHub,然後重試。 |
Error: Airbuild could not provide a GitHub token (HTTP …). Retry shortly. |
稍後再試。 |
若主機上沒有 gh,會輸出 Error: the GitHub CLI is not available in this environment.,並建議改用以相同方式登入的 git。
提交上的共同作者行
Agent 執行的每個 git commit 都會在提交訊息中加上這個 trailer:
Co-authored-by: AirBuild <315705251+airbuild[bot]@users.noreply.github.com>如此一來,GitHub 上的提交就會顯示 AirBuild 為共同作者。
- 它會加到所有形式的
git commit,包括--amend、--fixup、-m、-F,以及在編輯器中撰寫的訊息。 - 不會重複加入:已含有完全相同 trailer 的訊息會維持原樣。
git merge、git cherry-pick與git revert不會加入它。- 它是透過 git 本身的
--trailer選項加入,而非 hook,因此不會動到儲存庫的 hook 與設定。
若不想加入:
| 方式 | 範圍 |
|---|---|
| 在設定 › GitHub 中關閉提交署名 AirBuild(「Agent 做的提交會在 GitHub 上顯示 AirBuild 為共同作者。」)。只有團隊擁有者能變更;預設為開啟。 | 團隊的所有 Agent。 |
以 AIRBUILD_GIT_COAUTHOR=0(也可用 false、off 或 no)執行 git。 |
僅限該次指令。 |
若 AirBuild 無法讀取團隊的設定(例如尚未連結 GitHub),則會加入 trailer。
範例
推送到儲存庫前先檢查存取權:
gh access example-org/example-app檢查 AirBuild GitHub App 是否已安裝在某個組織上:
gh access example-org單次提交時不加共同作者行:
AIRBUILD_GIT_COAUTHOR=0 git commit -m "Update README"照常開啟拉取請求:
git push -u origin feature/login
gh pr create --fill