# 团队中的 gh 和 git

> git 和 GitHub CLI 如何为智能体工作：通过团队拥有者的 AirBuild GitHub App 获得 GitHub 访问权限、gh access、提示行，以及提交中的共同作者行。

Source: https://air.build/cn/docs/cli/gh-git/

智能体使用普通的 `git` 和 `gh`（GitHub 的命令行工具）。两者都已登录 GitHub，无需登录或配置。AirBuild 在此之上加了三样东西：`gh access` 命令、命令失败时的几行 `hint:` 提示，以及提交中的 AirBuild 共同作者行。

## 在哪里运行

云端团队和 Mac 团队中的智能体都可以使用这两个命令。在云端团队中，环境自带 `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.`（环境中未安装 git，请让用户安装后重试）。见[选择智能体的运行位置](https://air.build/cn/docs/get-started/where-agents-run/)。

所有参数都原样传给真正的 `git` 或 `gh`，输出和退出码也都是它们自己的。AirBuild 的提示行只写到 stderr，因此读取 stdout 的程序看不到它们。

## GitHub 访问权限从哪里来

登录用的是团队拥有者的 GitHub 账户，通过 设置 › [GitHub](https://air.build/cn/docs/settings/github/) 中的 **AirBuild GitHub App** 连接。已登录不等于能访问某个仓库。GitHub 依次检查：

1. 团队已连接 GitHub。
2. AirBuild GitHub App 已安装在拥有该仓库的账户或组织上。
3. 该仓库在与该安装共享的仓库之列。
4. 对于任何写入操作，App 和已连接的账户都有写入权限。

所以 `gh auth status` 可能显示 “Logged in”，公开仓库也能读取，但推送仍会被一个简单的 403 拒绝。`gh auth status` 之后，`gh` 会附上一条说明，讲的正是这一点。

缺少访问权限时，智能体会在聊天中发送一张 `request_github_repo` 卡片。卡片会请对方授权一个仓库、整个账户，或让对方自己选择仓库；如果 GitHub 尚未连接或连接已失效，还会先引导对方连接 GitHub。

## gh access

```text
gh access [OWNER/REPO | OWNER]
gh access --help
```

`gh access` 是 AirBuild 自己的命令。它向 AirBuild 查询：对这个团队而言，仓库卡在上述哪一层，以及 `request_github_repo` 卡片能否解决。

| 参数 | 检查内容 |
| --- | --- |
| 无 | 当前文件夹 `origin` 远程对应的仓库（必须在 github.com 上）。 |
| `OWNER/REPO` | 该仓库。也可以用 github.com 地址或 `github.com/OWNER/REPO`。 |
| `OWNER` | AirBuild GitHub App 是否安装在该账户上，以及它共享的是全部仓库还是选定的仓库。 |

回答是一到两行纯文本。可能的回答如下：

| 回答 | 卡片能解决吗？ |
| --- | --- |
| 访问权限已具备：App 覆盖该仓库并可以推送，拥有 App 对内容、拉取请求、议题、工作流和 Actions 的权限。 | 不能；什么都不缺。回答会列出其他可能拒绝推送的原因：受保护的分支或规则集、修改了 `.github/workflows` 但 App 没有 `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 检出目录中不带参数运行时，它会提示 `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` 回答：

```text
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`，后者以同样的方式登录。

## 提交中的共同作者行

智能体运行的每个 `git commit` 都会在提交信息中加上这一行尾注：

```text
Co-authored-by: AirBuild <315705251+airbuild[bot]@users.noreply.github.com>
```

这样，该提交在 GitHub 上会显示 AirBuild 为共同作者。

- 所有形式的 `git commit` 都会加上，包括 `--amend`、`--fixup`、`-m`、`-F` 以及在编辑器中写的提交信息。
- 不会重复添加：已有完全相同尾注的提交信息保持不变。
- `git merge`、`git cherry-pick` 和 `git revert` 不会添加。
- 它通过 git 自带的 `--trailer` 选项添加，而不是钩子，因此不会改动仓库的钩子和配置。

要去掉它：

| 方式 | 范围 |
| --- | --- |
| 在 设置 › [GitHub](https://air.build/cn/docs/settings/github/) 中关闭 **提交署名 AirBuild**（“Agent 做的提交会在 GitHub 上显示 AirBuild 为共同作者。”）。只有团队拥有者可以更改；默认开启。 | 团队的所有智能体。 |
| 运行 git 时设置 `AIRBUILD_GIT_COAUTHOR=0`（也可以是 `false`、`off` 或 `no`）。 | 仅这一条命令。 |

如果 AirBuild 无法读取团队的设置（例如尚未连接 GitHub），仍会添加尾注。

## 示例

推送前检查对某个仓库的访问权限：

```bash
gh access example-org/example-app
```

检查 AirBuild GitHub App 是否安装在某个组织上：

```bash
gh access example-org
```

单次提交不加共同作者行：

```bash
AIRBUILD_GIT_COAUTHOR=0 git commit -m "Update README"
```

照常创建拉取请求：

```bash
git push -u origin feature/login
gh pr create --fill
```
